[education/rkward] rkward: Port RObjectViewer away from RCommandReceiver, fix quirks in RKInlineProgressControl along the way.

Thomas Friedrichsmeier null at kde.org
Thu Jun 9 15:30:34 BST 2022


Git commit 9f3c942c555ad0308507f9112eb5b393ab79c041 by Thomas Friedrichsmeier.
Committed on 09/06/2022 at 14:29.
Pushed by tfry into branch 'master'.

Port RObjectViewer away from RCommandReceiver, fix quirks in RKInlineProgressControl along the way.

M  +10   -4    rkward/misc/rkprogresscontrol.cpp
M  +51   -104  rkward/robjectviewer.cpp
M  +8    -11   rkward/robjectviewer.h

https://invent.kde.org/education/rkward/commit/9f3c942c555ad0308507f9112eb5b393ab79c041

diff --git a/rkward/misc/rkprogresscontrol.cpp b/rkward/misc/rkprogresscontrol.cpp
index abae9e48..1f2b9bef 100644
--- a/rkward/misc/rkprogresscontrol.cpp
+++ b/rkward/misc/rkprogresscontrol.cpp
@@ -384,6 +384,7 @@ void RKProgressControlDialog::closeEvent (QCloseEvent *e) {
 #include <KMessageBox>
 #include <KStandardAction>
 
+#include "../windows/rkmdiwindow.h"
 #include "rkstandardicons.h"
 
 RKInlineProgressControl::RKInlineProgressControl(QWidget *display_area, bool allow_cancel) : QObject(display_area),
@@ -396,7 +397,12 @@ RKInlineProgressControl::RKInlineProgressControl(QWidget *display_area, bool all
 						close_action(nullptr) {
 	RK_TRACE(MISC);
 
-	display_area->window()->installEventFilter(this);
+	QWidget* logical_parent = display_area;
+	while(logical_parent->parentWidget() && !(logical_parent->isWindow() || qobject_cast<RKMDIWindow*>(logical_parent))) {
+		logical_parent = logical_parent->parentWidget();
+	}
+	logical_parent->installEventFilter(this);
+	//display_area->installEventFilter(this);
 
 	wrapper = new QWidget(display_area);
 	wrapper->setAutoFillBackground(true);
@@ -510,9 +516,9 @@ void RKInlineProgressControl::show(int delay_ms) {
 	t->start();
 }
 
-bool RKInlineProgressControl::eventFilter(QObject *, QEvent *e) {
+bool RKInlineProgressControl::eventFilter(QObject *w, QEvent *e) {
 	RK_TRACE(MISC);
-	if (e->type() == QEvent::Resize) {
+	if (e->type() == QEvent::Resize || e->type() == QEvent::Show) {
 		QTimer::singleShot(0, this, [this]() {
 			wrapper->resize(display_area->size());
 		});
@@ -527,7 +533,7 @@ bool RKInlineProgressControl::eventFilter(QObject *, QEvent *e) {
 
 			autoclose = autoclose_save;
 			if (ignore) {
-				e->accept();
+				e->ignore();
 				return true;
 			}
 			cancelAndClose();
diff --git a/rkward/robjectviewer.cpp b/rkward/robjectviewer.cpp
index 996264a4..48c3f489 100644
--- a/rkward/robjectviewer.cpp
+++ b/rkward/robjectviewer.cpp
@@ -1,6 +1,6 @@
 /*
 robjectviewer - This file is part of RKWard (https://rkward.kde.org). Created: Tue Aug 24 2004
-SPDX-FileCopyrightText: 2004-2009 by Thomas Friedrichsmeier <thomas.friedrichsmeier at kdemail.net>
+SPDX-FileCopyrightText: 2004-2022 by Thomas Friedrichsmeier <thomas.friedrichsmeier at kdemail.net>
 SPDX-FileContributor: The RKWard Team <rkward-devel at kde.org>
 SPDX-License-Identifier: GPL-2.0-or-later
 */
@@ -22,6 +22,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #include "core/robject.h"
 #include "misc/rkdummypart.h"
 #include "../misc/rkcommonfunctions.h"
+#include "misc/rkprogresscontrol.h"
 
 #include "debug.h"
 
@@ -145,32 +146,28 @@ void RObjectViewer::currentTabChanged (int new_current) {
 
 ///////////////// RObjectViewerWidget /////////////////////
 
-RObjectViewerWidget::RObjectViewerWidget (QWidget* parent, RObject* object) : QWidget (parent), RCommandReceiver () {
+RObjectViewerWidget::RObjectViewerWidget(QWidget* parent, RObject* object) : QWidget(parent) {
 	RK_TRACE (APP);
 
 	_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);
+	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_label = new QLabel();
+	status_layout->addWidget(status_label);
 
 	status_layout->addStretch ();
 
-	update_button = new QPushButton (i18n ("Update"), this);
-	connect (update_button, &QPushButton::clicked, this, &RObjectViewerWidget::update);
-	status_layout->addWidget (update_button);
-
-	cancel_button = new QPushButton (i18n ("Cancel"), this);
-	connect (cancel_button, &QPushButton::clicked, this, &RObjectViewerWidget::cancel);
-	status_layout->addWidget (cancel_button);
+	update_button = new QPushButton(i18n("Update"));
+	connect(update_button, &QPushButton::clicked, this, &RObjectViewerWidget::update);
+	status_layout->addWidget(update_button);
 
-	area = new QTextEdit (this);
-	area->setReadOnly (true);
-	area->setLineWrapMode (QTextEdit::NoWrap);
-	main_layout->addWidget (area);
+	area = new QTextEdit(this);
+	area->setReadOnly(true);
+	area->setLineWrapMode(QTextEdit::NoWrap);
+	main_layout->addWidget(area);
 
 	initialized = false;
 }
@@ -182,15 +179,12 @@ RObjectViewerWidget::~RObjectViewerWidget () {
 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);
+	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);
-	}
+	status_label->setText (reason);
+	update_button->setEnabled (_object != 0);
 }
 
 void RObjectViewerWidget::initialize () {
@@ -201,25 +195,26 @@ void RObjectViewerWidget::initialize () {
 	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);
-}
-
-void RObjectViewerWidget::cancel () {
-	RK_TRACE (APP);
+void RObjectViewerWidget::update() {
+	RK_TRACE(APP);
 
-	cancelOutstandingCommands ();
-	setText (i18n ("Click \"Update\" to fetch information"));
-	cancel_button->setEnabled (false);
-	update_button->setEnabled (_object != 0);
+	if (!_object) {
+		RK_ASSERT(_object);
+		return;
+	}
+	update_button->setEnabled(false);
+
+	auto command = makeCommand();
+	auto control = new RKInlineProgressControl(this, true);
+	control->addRCommand(command);
+	control->setAutoCloseWhenCommandsDone(true);
+	control->setText(i18n("Fetching information"));
+	control->show(100);
+	command->whenFinished(this, [this](RCommand *command) {
+		setText(command->fullOutput());
+		update_button->setEnabled (_object != 0);
+	});
+	RInterface::issueCommand(command);
 }
 
 void RObjectViewerWidget::setText (const QString& text) {
@@ -232,79 +227,31 @@ void RObjectViewerWidget::setText (const QString& text) {
 	area->insertPlainText (text);
 }
 
-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 RObjectViewerWidget::rCommandDone (RCommand* command) {
-	RK_TRACE (APP);
-
-	if (command->wasCanceled ()) {
-		cancel ();
-	} else {
-		setText (command->fullOutput ());
-		ready ();
-	}
-}
-
 ////////////////// summary widget /////////////////
 
-void RObjectSummaryWidget::update () {
-	RK_TRACE (APP);
-
-	if (!_object) {
-		RK_ASSERT (false);
-		return;
-	}
-
-	RObjectViewerWidget::update ();
-
-	RCommand *command = new RCommand ("print(summary(" + _object->getFullName () + "))", RCommand::App, QString (), this);
-	RInterface::issueCommand (command, 0);
+RCommand* RObjectSummaryWidget::makeCommand() {
+	RK_TRACE(APP);
+	return new RCommand("print(summary(" + _object->getFullName() + "))", RCommand::App);
 }
 
 ////////////////// print widget /////////////////
 
-void RObjectPrintWidget::update () {
-	RK_TRACE (APP);
-
-	if (!_object) {
-		RK_ASSERT (false);
-		return;
-	}
-
-	RObjectViewerWidget::update ();
+RCommand* RObjectPrintWidget::makeCommand() {
+	RK_TRACE(APP);
 
 	// make sure to print as wide as possible
-	RCommand* command = new RCommand ("local({\n"
+	return 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);
-	RInterface::issueCommand (command, 0);
+	                                  "\tprint(" + _object->getFullName() + ")\n"
+	                                  "})", RCommand::App);
 }
 
 ////////////////// structure widget /////////////////
 
-void RObjectStructureWidget::update () {
-	RK_TRACE (APP);
-
-	if (!_object) {
-		RK_ASSERT (false);
-		return;
-	}
-
-	RObjectViewerWidget::update ();
-
-	RCommand *command = new RCommand ("str(" + _object->getFullName () + ')', RCommand::App, QString (), this);
-	RInterface::issueCommand (command, 0);
+RCommand* RObjectStructureWidget::makeCommand() {
+	RK_TRACE(APP);
+	return new RCommand("str(" + _object->getFullName() + ')', RCommand::App);
 }
 
diff --git a/rkward/robjectviewer.h b/rkward/robjectviewer.h
index e540be61..020abc05 100644
--- a/rkward/robjectviewer.h
+++ b/rkward/robjectviewer.h
@@ -1,6 +1,6 @@
 /*
 robjectviewer - This file is part of the RKWard project. Created: Tue Aug 24 2004
-SPDX-FileCopyrightText: 2004-2007 by Thomas Friedrichsmeier <thomas.friedrichsmeier at kdemail.net>
+SPDX-FileCopyrightText: 2004-2022 by Thomas Friedrichsmeier <thomas.friedrichsmeier at kdemail.net>
 SPDX-FileContributor: The RKWard Team <rkward-devel at kde.org>
 SPDX-License-Identifier: GPL-2.0-or-later
 */
@@ -10,10 +10,10 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #include <qwidget.h>
 #include <qstring.h>
 
-#include "rbackend/rcommandreceiver.h"
 #include "core/rkmodificationtracker.h"
 #include "windows/rkmdiwindow.h"
 
+class RCommand;
 class RObject;
 class QTextEdit;
 class QTabWidget;
@@ -26,7 +26,7 @@ A simple object viewer. You pass it an object in the constructor. It will extrac
 
 @author Thomas Friedrichsmeier
 */
-class RObjectViewer : public RKMDIWindow, public RCommandReceiver, public RObjectListener {
+class RObjectViewer : public RKMDIWindow, public RObjectListener {
 Q_OBJECT
 public:
 	~RObjectViewer ();
@@ -63,7 +63,7 @@ private:
 /** Since the two widgets in the RObjectViewer are largely similar, this is the common base for them. The base class itself is *not* useful.
 
 @author Thomas Friedrichsmeier */
-class RObjectViewerWidget : public QWidget, public RCommandReceiver {
+class RObjectViewerWidget : public QWidget {
 Q_OBJECT
 protected:
 	RObjectViewerWidget (QWidget* parent, RObject* object);
@@ -75,16 +75,13 @@ public:
 	void initialize ();
 	void setText (const QString& text);
 public slots:
-	void cancel ();
 	virtual void update ();
 protected:
-	void rCommandDone (RCommand *command) override;
-	void ready ();
+	virtual RCommand* makeCommand() = 0;
 
 	QLabel* status_label;
 
 	QPushButton *update_button;
-	QPushButton *cancel_button;
 	QTextEdit *area;
 
 	bool initialized;
@@ -99,7 +96,7 @@ public:
 	~RObjectSummaryWidget () {};
 
 	/** reimplemented from RObjectViewerWidget to call "summary" */
-	void update () override;
+	RCommand* makeCommand() override;
 };
 
 /** Represents the "print" area in an RObjectViewer */
@@ -109,7 +106,7 @@ public:
 	~RObjectPrintWidget () {};
 
 	/** reimplemented from RObjectViewerWidget to call "print" */
-	void update () override;
+	RCommand* makeCommand() override;
 };
 
 /** Represents the "str" area in an RObjectViewer */
@@ -119,7 +116,7 @@ public:
 	~RObjectStructureWidget () {};
 
 	/** reimplemented from RObjectViewerWidget to call "str" */
-	void update () override;
+	RCommand* makeCommand() override;
 };
 
 #endif



More information about the rkward-tracker mailing list