[rkward-cvs] SF.net SVN: rkward: [2263] branches/KDE4_port

tfry at users.sourceforge.net tfry at users.sourceforge.net
Thu Nov 22 13:42:56 UTC 2007


Revision: 2263
          http://rkward.svn.sourceforge.net/rkward/?rev=2263&view=rev
Author:   tfry
Date:     2007-11-22 05:42:56 -0800 (Thu, 22 Nov 2007)

Log Message:
-----------
RObjectViewer redesign

Modified Paths:
--------------
    branches/KDE4_port/ChangeLog
    branches/KDE4_port/rkward/rbackend/rcommand.h
    branches/KDE4_port/rkward/robjectviewer.cpp
    branches/KDE4_port/rkward/robjectviewer.h

Modified: branches/KDE4_port/ChangeLog
===================================================================
--- branches/KDE4_port/ChangeLog	2007-11-22 11:16:54 UTC (rev 2262)
+++ branches/KDE4_port/ChangeLog	2007-11-22 13:42:56 UTC (rev 2263)
@@ -1,3 +1,4 @@
+- object viewer was redesigned to use space better, and only fetch the print-representation when needed
 - more icons added
 - data.frame editor can be set to read-only mode
 - code completion in script editor shows icons for the different object types

Modified: branches/KDE4_port/rkward/rbackend/rcommand.h
===================================================================
--- branches/KDE4_port/rkward/rbackend/rcommand.h	2007-11-22 11:16:54 UTC (rev 2262)
+++ branches/KDE4_port/rkward/rbackend/rcommand.h	2007-11-22 13:42:56 UTC (rev 2263)
@@ -188,6 +188,8 @@
 	bool wasTried () { return (status & WasTried); };
 /** the command failed */
 	bool failed () { return (status & Failed); };
+/** the command was cancelled before it was executed */
+	bool wasCanceled () { return (wasTried () && failed () && (status & Canceled)); }
 /** the command succeeded (wasTried () && (!failed ()) */
 	bool succeeded () { return ((status & WasTried) && !(status & Failed)); };
 /** command has a string output retrievable via RCommand::output () */

Modified: branches/KDE4_port/rkward/robjectviewer.cpp
===================================================================
--- branches/KDE4_port/rkward/robjectviewer.cpp	2007-11-22 11:16:54 UTC (rev 2262)
+++ branches/KDE4_port/rkward/robjectviewer.cpp	2007-11-22 13:42:56 UTC (rev 2263)
@@ -16,14 +16,13 @@
  ***************************************************************************/
 #include "robjectviewer.h"
 
-#include <qlayout.h>
-#include <QScrollArea>
 #include <qlabel.h>
 #include <QTextEdit>
 #include <qfont.h>
 #include <QPushButton>
-#include <QCloseEvent>
 #include <QVBoxLayout>
+#include <QHBoxLayout>
+#include <QTabWidget>
 
 #include <klocale.h>
 #include <kvbox.h>
@@ -39,10 +38,7 @@
 
 #include "debug.h"
 
-#define SUMMARY_COMMAND 1
-#define PRINT_COMMAND 2
-
-RObjectViewer::RObjectViewer (QWidget *parent, RObject *object) : RKMDIWindow (parent, RKMDIWindow::ObjectWindow, false), RObjectListener (RObjectListener::ObjectView) {
+RObjectViewer::RObjectViewer (QWidget *parent, RObject *object, ViewerPage initial_page) : RKMDIWindow (parent, RKMDIWindow::ObjectWindow, false), RObjectListener (RObjectListener::ObjectView) {
 // KDE 4: TODO might listen for object meta / data changes as well
 	RK_TRACE (APP);
 	RK_ASSERT (object);
@@ -53,50 +49,29 @@
 
 	QVBoxLayout *layout = new QVBoxLayout (this);
 	layout->setContentsMargins (0, 0, 0, 0);
-	QScrollArea *wrapper = new QScrollArea (this);
-	wrapper->setWidgetResizable (true);
-	KVBox *box = new KVBox (wrapper);
-	wrapper->setWidget (box);
-	layout->addWidget (wrapper);
 
-	wrapper->setFocusPolicy (Qt::StrongFocus);
-	setPart (new RKDummyPart (this, wrapper));
-	initializeActivationSignals ();
+	// TODO: what an ugly hack... This should be the job of RObject::getObjectDescription().
+	description_label = new QLabel ("<nobr>" + _object->getObjectDescription ().replace ("<br>", "  -</nobr>  <nobr>") + "</nobr>", this);
+	description_label->setWordWrap (true);
+	layout->addWidget (description_label);
+	status_label = new QLabel (this);
+	status_label->hide ();
+	layout->addWidget (status_label);
 
-	KHBox *toprow = new KHBox (box);
-	description_label = new QLabel (toprow);
-	KVBox *statusbox = new KVBox (toprow);
-	statusbox->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
-	status_label = new QLabel (statusbox);
-	update_button = new QPushButton (i18n ("Update"), statusbox);
-	cancel_button = new QPushButton (i18n ("Cancel"), statusbox);
-	connect (update_button, SIGNAL (clicked ()), this, SLOT (update ()));
-	connect (cancel_button, SIGNAL (clicked ()), this, SLOT (cancel ()));
+	tabs = new QTabWidget (this);
+	tabs->insertTab (SummaryPage, summary_widget = new RObjectSummaryWidget (tabs, object), i18n ("summary (x)"));
+	tabs->insertTab (PrintPage, print_widget = new RObjectPrintWidget (tabs, object), i18n ("print (x)"));
+	layout->addWidget (tabs);
 
-	KHBox *row = new KHBox (box);
-	QLabel *label = new QLabel (i18n("\nResult of 'summary (%1)':\n", object->getFullName ()), row);
-	row->setStretchFactor (label, 10);
-	toggle_summary_button = new QPushButton (i18n ("Hide"), row);
-	connect (toggle_summary_button, SIGNAL (clicked ()), this, SLOT (toggleSummary ()));
+	tabs->setFocusPolicy (Qt::StrongFocus);
+	setPart (new RKDummyPart (this, tabs));
+	initializeActivationSignals ();
 
-	summary_area = new QTextEdit (box);
-	summary_area->setReadOnly (true);
-	summary_area->setLineWrapMode (QTextEdit::NoWrap);
+	tabs->setCurrentIndex (initial_page);
+	currentTabChanged (initial_page);
+	connect (tabs, SIGNAL (currentChanged(int)), this, SLOT (currentTabChanged (int)));
 
-	row = new KHBox (box);
-	label = new QLabel (i18n("\nResult of 'print (%1)':\n", object->getFullName ()), row);
-	row->setStretchFactor (label, 10);
-	toggle_print_button = new QPushButton (i18n ("Hide"), row);
-	connect (toggle_print_button, SIGNAL (clicked ()), this, SLOT (togglePrint ()));
-
-	print_area = new QTextEdit (box);
-	print_area->setReadOnly (true);
-	print_area->setLineWrapMode (QTextEdit::NoWrap);
-
 	setCaption (i18n("Object Viewer: ") + object->getShortName ());
-	//resize (minimumSizeHint ().expandedTo (QSize (640, 480)));
-	update ();
-	show ();
 }
 
 RObjectViewer::~RObjectViewer () {
@@ -105,98 +80,189 @@
 	if (_object) stopListenForObject (_object);
 }
 
-void RObjectViewer::toggleSummary () {
+void RObjectViewer::objectRemoved (RObject *object) {
 	RK_TRACE (APP);
 
-	summary_area->setShown (summary_area->isHidden ());
-	if (summary_area->isHidden ()) {
-		toggle_summary_button->setText (i18n ("Show"));
+	if (object == _object) {
+		summary_widget->objectKilled ();
+		print_widget->objectKilled ();
+
+		QString reason = i18n ("<b>Object was deleted!</b>");
+		summary_widget->invalidate (reason);
+		print_widget->invalidate (reason);
+
+		QPalette palette = status_label->palette ();
+		palette.setColor (status_label->foregroundRole (), Qt::red);
+		status_label->setPalette (palette);
+		status_label->setText (reason);
+		status_label->show ();
+
+		stopListenForObject (_object);
+		_object = 0;
 	} else {
-		toggle_summary_button->setText (i18n ("Hide"));
+		RK_ASSERT (false);
 	}
 }
 
-void RObjectViewer::togglePrint () {
+void RObjectViewer::currentTabChanged (int new_current) {
 	RK_TRACE (APP);
 
-	print_area->setShown (print_area->isHidden ());
-	if (print_area->isHidden ()) {
-		toggle_print_button->setText (i18n ("Show"));
+	if (new_current == SummaryPage) {
+		summary_widget->initialize ();
+	} else if (new_current == PrintPage) {
+		print_widget->initialize ();
 	} else {
-		toggle_print_button->setText (i18n ("Hide"));
+		RK_ASSERT (false);
 	}
 }
 
-void RObjectViewer::cancel () {
+///////////////// RObjectViewerWidget /////////////////////
+
+RObjectViewerWidget::RObjectViewerWidget (QWidget* parent, RObject* object) : QWidget (parent), RCommandReceiver () {
 	RK_TRACE (APP);
 
-	cancel_button->setEnabled (false);
-	update_button->setEnabled (true);
-	cancelOutstandingCommands ();
+	_object = object;
+	QVBoxLayout* main_layout = new QVBoxLayout (this);
+	main_layout->setContentsMargins (0, 0, 0, 0);
+	QHBoxLayout* status_layout = new QHBoxLayout ();
+	main_layout->addLayout (status_layout);
+
+	status_label = new QLabel (this);
+	status_layout->addWidget (status_label);
+
+	status_layout->addStretch ();
+
+	update_button = new QPushButton (i18n ("Update"), this);
+	connect (update_button, SIGNAL (clicked ()), this, SLOT (update ()));
+	status_layout->addWidget (update_button);
+
+	cancel_button = new QPushButton (i18n ("Cancel"), this);
+	connect (cancel_button, SIGNAL (clicked ()), this, SLOT (cancel ()));
+	status_layout->addWidget (cancel_button);
+
+	area = new QTextEdit (this);
+	area->setReadOnly (true);
+	area->setLineWrapMode (QTextEdit::NoWrap);
+	main_layout->addWidget (area);
+
+	initialized = false;
 }
 
-void RObjectViewer::update () {
+RObjectViewerWidget::~RObjectViewerWidget () {
 	RK_TRACE (APP);
-	if (!_object) return;
+}
 
-	status_label->setText (i18n ("Fetching information"));
+void RObjectViewerWidget::invalidate (const QString& reason) {
+	RK_TRACE (APP);
+
+	if (outstanding_commands.isEmpty ()) {
+		QPalette palette = status_label->palette ();
+		palette.setColor (status_label->foregroundRole (), Qt::red);
+		status_label->setPalette (palette);
+
+		status_label->setText (reason);
+		update_button->setEnabled (_object != 0);
+		cancel_button->setEnabled (false);
+	}
+}
+
+void RObjectViewerWidget::initialize () {
+	RK_TRACE (APP);
+
+	if (initialized) return;
+	update ();
+	initialized = true;
+}
+
+void RObjectViewerWidget::update () {
+	RK_TRACE (APP);
+
+	RK_ASSERT (outstanding_commands.isEmpty ());
+	RK_ASSERT (_object);
+
+	setText (i18n ("Fetching information. Please wait."));
+
+	update_button->setEnabled (false);
 	cancel_button->setEnabled (true);
-	update_button->setEnabled (false);
-	description_label->setText (_object->getObjectDescription ());
+}
 
-	summary_area->setPlainText (QString::null);
-	print_area->setPlainText (QString::null);
+void RObjectViewerWidget::cancel () {
+	RK_TRACE (APP);
+
+	cancelOutstandingCommands ();
+	setText (i18n ("Click \"Update\" to fetch information"));
+	cancel_button->setEnabled (false);
+	update_button->setEnabled (_object != 0);
+}
+
+void RObjectViewerWidget::setText (const QString& text) {
+	RK_TRACE (APP);
+
+	area->setPlainText (QString ());
 	QFont font = KGlobalSettings::fixedFont ();
-	summary_area->setCurrentFont (font);
-	print_area->setCurrentFont (font);
+	area->setCurrentFont (font);
 
-	RCommand *command = new RCommand ("print(summary(" + _object->getFullName () + "))", RCommand::App, QString::null, this, SUMMARY_COMMAND);
-	RKGlobals::rInterface ()->issueCommand (command, 0);
+	area->insertPlainText (text);
+}
 
-	// make sure to print as wide as possible
-	command = new RCommand ("local({\n"
-	                                  "\trk.temp.width.save <- getOption(\"width\")\n"
-	                                  "\toptions(width=10000)\n"
-	                                  "\ton.exit(options(width=rk.temp.width.save))\n"
-	                                  "\tprint(" + _object->getFullName () + ")\n"
-	                                  "})", RCommand::App, QString::null, this, PRINT_COMMAND);
-	RKGlobals::rInterface ()->issueCommand (command, 0);
+void RObjectViewerWidget::ready () {
+	RK_TRACE (APP);
+
+	QPalette palette = status_label->palette ();
+	palette.setColor (status_label->foregroundRole (), Qt::black);
+	status_label->setPalette (palette);
+	status_label->setText (i18n ("Ready"));
+	cancel_button->setEnabled (false);
+	update_button->setEnabled (_object != 0);
 }
 
-void RObjectViewer::objectRemoved (RObject *object) {
+void RObjectViewerWidget::rCommandDone (RCommand* command) {
 	RK_TRACE (APP);
 
-	if (object == _object) {
-		status_label->setText (i18n ("<b>Object was deleted!</b>"));
-		update_button->setEnabled (false);
-		stopListenForObject (_object);
-		_object = 0;
+	if (command->wasCanceled ()) {
+		cancel ();
 	} else {
-		RK_ASSERT (false);
+		setText (command->fullOutput ());
+		ready ();
 	}
 }
 
-void RObjectViewer::rCommandDone (RCommand *command) {
+////////////////// summary widget /////////////////
+
+void RObjectSummaryWidget::update () {
 	RK_TRACE (APP);
-	RK_ASSERT (command);
 
-	if (command->getFlags () == SUMMARY_COMMAND) {
-		summary_area->insertPlainText (command->fullOutput ());
-	} else if (command->getFlags () == PRINT_COMMAND) {
-		print_area->insertPlainText (command->fullOutput ());
-		status_label->setText (i18n ("Ready"));
-		update_button->setEnabled (true);
-		cancel_button->setEnabled (false);
-	} else {
+	if (!_object) {
 		RK_ASSERT (false);
+		return;
 	}
 
+	RObjectViewerWidget::update ();
+
+	RCommand *command = new RCommand ("print(summary(" + _object->getFullName () + "))", RCommand::App, QString (), this);
+	RKGlobals::rInterface ()->issueCommand (command, 0);
 }
 
-void RObjectViewer::closeEvent (QCloseEvent *e) {
-	hide ();
-	e->accept ();
-	deleteLater ();
+////////////////// print widget /////////////////
+
+void RObjectPrintWidget::update () {
+	RK_TRACE (APP);
+
+	if (!_object) {
+		RK_ASSERT (false);
+		return;
+	}
+
+	RObjectViewerWidget::update ();
+
+	// make sure to print as wide as possible
+	RCommand* command = new RCommand ("local({\n"
+	                                  "\trk.temp.width.save <- getOption(\"width\")\n"
+	                                  "\toptions(width=10000)\n"
+	                                  "\ton.exit(options(width=rk.temp.width.save))\n"
+	                                  "\tprint(" + _object->getFullName () + ")\n"
+	                                  "})", RCommand::App, QString (), this);
+	RKGlobals::rInterface ()->issueCommand (command, 0);
 }
 
 #include "robjectviewer.moc"

Modified: branches/KDE4_port/rkward/robjectviewer.h
===================================================================
--- branches/KDE4_port/rkward/robjectviewer.h	2007-11-22 11:16:54 UTC (rev 2262)
+++ branches/KDE4_port/rkward/robjectviewer.h	2007-11-22 13:42:56 UTC (rev 2263)
@@ -26,12 +26,13 @@
 
 class RObject;
 class QTextEdit;
-class QCloseEvent;
+class QTabWidget;
 class QLabel;
 class QPushButton;
+class RObjectViewerWidget;
 
 /**
-An extremely simple object viewer. You pass it an object in the constructor. It will extract some information and display that as text, then call R "print (object)" and display the result. You don't need to store a pointer. The window will self-destruct when closed.
+A simple object viewer. You pass it an object in the constructor. It will extract some information and display that as text.
 
 @author Thomas Friedrichsmeier
 */
@@ -41,30 +42,78 @@
 	~RObjectViewer ();
 
 	RObject *object () { return _object; };
+
+	enum ViewerPage {
+		SummaryPage = 0,
+		PrintPage = 1
+	};
 public slots:
-	void cancel ();
-	void update ();
-	void toggleSummary ();
-	void togglePrint ();
+	void currentTabChanged (int new_current);
 protected:
 	friend class RKWorkplace;
-	RObjectViewer (QWidget *parent, RObject *object);
+	RObjectViewer (QWidget *parent, RObject *object, ViewerPage initial_page = SummaryPage);
 
-	void rCommandDone (RCommand *command);
-	void closeEvent (QCloseEvent *e);
 	void objectRemoved (RObject *object);
 private:
 	QLabel *status_label;
 	QLabel *description_label;
+	QTabWidget* tabs;
+	RObjectViewerWidget* summary_widget;
+	RObjectViewerWidget* print_widget;
+
+	RObject *_object;
+};
+
+/** Since the two widgets in the RObjectViewer are largely similar, this is the common base for them. The base class itself is *not* useful.
+
+ at author Thomas Friedrichsmeier */
+class RObjectViewerWidget : public QWidget, public RCommandReceiver {
+Q_OBJECT
+protected:
+	RObjectViewerWidget (QWidget* parent, RObject* object);
+	virtual ~RObjectViewerWidget ();
+public:
+	void objectKilled () { _object = 0; };
+
+	void invalidate (const QString& reason);
+	void initialize ();
+	void setText (const QString& text);
+public slots:
+	void cancel ();
+	virtual void update ();
+protected:
+	void rCommandDone (RCommand *command);
+	void ready ();
+
+	QLabel* status_label;
+
 	QPushButton *update_button;
 	QPushButton *cancel_button;
-	QPushButton *toggle_summary_button;
-	QPushButton *toggle_print_button;
-	QTextEdit *print_area;
-	QTextEdit *summary_area;
-	QString caption;
+	QTextEdit *area;
 
-	RObject *_object;
+	bool initialized;
+
+	RObject* _object;
 };
 
+/** Represents the "summary" area in an RObjectViewer */
+class RObjectSummaryWidget : public RObjectViewerWidget {
+public:
+	RObjectSummaryWidget (QWidget* parent, RObject* object) : RObjectViewerWidget (parent, object) {};
+	~RObjectSummaryWidget () {};
+
+	/** reimplemented from RObjectViewerWidget to call "summary" */
+	void update ();
+};
+
+/** Represents the "print" area in an RObjectViewer */
+class RObjectPrintWidget : public RObjectViewerWidget {
+public:
+	RObjectPrintWidget (QWidget* parent, RObject* object) : RObjectViewerWidget (parent, object) {}
+	~RObjectPrintWidget () {};
+
+	/** reimplemented from RObjectViewerWidget to call "print" */
+	void update ();
+};
+
 #endif


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list