[education/rkward] rkward: Port more code away from RCommandReceiver

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


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

Port more code away from RCommandReceiver

M  +16   -11   rkward/misc/rkprogresscontrol.cpp
M  +6    -6    rkward/misc/rkprogresscontrol.h
M  +2    -1    rkward/rbackend/rcommand.cpp
M  +0    -10   rkward/rbackend/rcommandreceiver.cpp
M  +0    -2    rkward/rbackend/rcommandreceiver.h
M  +5    -6    rkward/windows/rkcommandlog.h
M  +24   -19   rkward/windows/rkwindowcatcher.cpp

https://invent.kde.org/education/rkward/commit/b6eead2042daee8df2f82b290a15979a674db250

diff --git a/rkward/misc/rkprogresscontrol.cpp b/rkward/misc/rkprogresscontrol.cpp
index f95322cd..abae9e48 100644
--- a/rkward/misc/rkprogresscontrol.cpp
+++ b/rkward/misc/rkprogresscontrol.cpp
@@ -67,7 +67,6 @@ RKProgressControl::RKProgressControl (QObject *parent, const QString &text, cons
 	is_done = false;
 	modal = false;
 	autodelete = false;
-	done_command = 0;
 }
 
 RKProgressControl::~RKProgressControl () {
@@ -76,6 +75,12 @@ RKProgressControl::~RKProgressControl () {
 	if (!is_done) done ();
 }
 
+void RKProgressControl::autoDeleteWhenDone(){
+	RK_TRACE (MISC);
+	autodelete = true;
+	if (outstanding_commands.isEmpty()) deleteLater();
+}
+
 bool RKProgressControl::doModal (bool autodelete) {
 	RK_TRACE (MISC);
 	RK_ASSERT (!dialog);
@@ -143,8 +148,14 @@ void RKProgressControl::addRCommand (RCommand *command, bool done_when_finished)
 	RK_TRACE (MISC);
 	RK_ASSERT (command);
 
-	command->addReceiver (this);
-	if (done_when_finished) done_command = command;
+	outstanding_commands.append(command);
+	connect(command->notifier(), &RCommandNotifier::commandOutput, this, QOverload<RCommand*, const ROutput*>::of(&RKProgressControl::newOutput));
+	if (done_when_finished) {
+		command->whenFinished(this, [this, done_when_finished](RCommand* command) {
+			outstanding_commands.removeAll(command);
+			if (done_when_finished) done();
+		});
+	}
 }
 
 void RKProgressControl::dialogDestroyed () {
@@ -154,7 +165,7 @@ void RKProgressControl::dialogDestroyed () {
 	if ((!is_done) && (mode & AllowCancel)) {
 		is_done = true;
 		if (mode & AutoCancelCommands) {
-			for (RCommandList::const_iterator it = outstanding_commands.cbegin (); it != outstanding_commands.cend (); ++it) {
+			for (auto it = outstanding_commands.cbegin (); it != outstanding_commands.cend (); ++it) {
 				RInterface::instance()->cancelCommand(*it);
 			}
 		}
@@ -187,7 +198,7 @@ void RKProgressControl::createDialog () {
 	}
 }
 
-void RKProgressControl::newOutput (RCommand *, ROutput *output) {
+void RKProgressControl::newOutput (RCommand *, const ROutput *output) {
 	RK_TRACE (MISC);
 	RK_ASSERT (output);
 
@@ -198,12 +209,6 @@ void RKProgressControl::newOutput (RCommand *, ROutput *output) {
 	}
 }
 
-void RKProgressControl::rCommandDone (RCommand * command) {
-	RK_TRACE (MISC);
-
-	if (command == done_command) done ();
-}
-
 QString RKProgressControl::fullCommandOutput() {
 	RK_TRACE (MISC);
 
diff --git a/rkward/misc/rkprogresscontrol.h b/rkward/misc/rkprogresscontrol.h
index 2f5deae3..874e58b9 100644
--- a/rkward/misc/rkprogresscontrol.h
+++ b/rkward/misc/rkprogresscontrol.h
@@ -11,7 +11,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #include <QList>
 #include <qstring.h>
 
-#include "../rbackend/rcommandreceiver.h"
+#include "../rbackend/rcommand.h"
 
 class QCloseEvent;
 class QDialog;
@@ -28,7 +28,7 @@ TODO: Remove in favor of RKInlineProgressControl ?
 
 @author Thomas Friedrichsmeier
 */
-class RKProgressControl : public QObject, public RCommandReceiver {
+class RKProgressControl : public QObject {
 	Q_OBJECT
 public:
 /** create an RKProgressContol dialog
@@ -68,6 +68,8 @@ public:
 /** add a command to listen to. Warning: You will always first call addRCommand, then submit the command to RInterface, never the other way around. Else there could be a race condition!
 @param done_when_finished If set to true, the done () -slot is auto-called when the given command has completed */
 	void addRCommand (RCommand *command, bool done_when_finished=false);
+/** Causes the dialog to delete itself, once all outstanding commands have finished. Usually set from doModel()/doNonModal(), instead. */
+	void autoDeleteWhenDone ();
 	QString fullCommandOutput ();
 signals:
 	void cancelled ();
@@ -84,8 +86,7 @@ private:
 
 	RKProgressControlDialog *dialog;
 	QList<ROutput> output_log;
-
-	RCommand *done_command;
+	QList<RCommand*> outstanding_commands;
 
 	bool autodelete;
 	bool modal;
@@ -94,8 +95,7 @@ private:
 	QString text;
 	QString caption;
 protected:
-	void newOutput (RCommand *, ROutput *output) override;
-	void rCommandDone (RCommand *command) override;
+	void newOutput (RCommand *, const ROutput *output);
 };
 
 class KMessageWidget;
diff --git a/rkward/rbackend/rcommand.cpp b/rkward/rbackend/rcommand.cpp
index fc275039..0c3eb1eb 100644
--- a/rkward/rbackend/rcommand.cpp
+++ b/rkward/rbackend/rcommand.cpp
@@ -49,7 +49,6 @@ RCommand::RCommand(const QString &command, int type, const QString &rk_equiv, RC
 	for (int i = 0; i < MAX_RECEIVERS_PER_RCOMMAND; ++i) receivers[i] = 0;
 	if (!(type & Internal)) {
 		addReceiver (receiver);
-		addReceiver (RKCommandLog::getLog ());
 	}
 }
 
@@ -112,6 +111,7 @@ void RCommand::finished () {
 		receivers[i]->delCommand (this);
 		receivers[i]->rCommandDone (this);
 	}
+	RKCommandLog::getLog()->rCommandDone(this);
 	if (_notifier) _notifier->emitFinished (this);
 }
 
@@ -122,6 +122,7 @@ void RCommand::newOutput (ROutput *output) {
 		if (receivers[i] == 0) continue;
 		receivers[i]->newOutput (this, output);
 	}
+	RKCommandLog::getLog()->newOutput(this, output);
 	if (_notifier) _notifier->emitOutput(this, output);
 }
 
diff --git a/rkward/rbackend/rcommandreceiver.cpp b/rkward/rbackend/rcommandreceiver.cpp
index 949a1f0a..3d90e34b 100644
--- a/rkward/rbackend/rcommandreceiver.cpp
+++ b/rkward/rbackend/rcommandreceiver.cpp
@@ -43,13 +43,3 @@ void RCommandReceiver::delCommand (RCommand *command) {
 
 	if (delete_when_done && outstanding_commands.isEmpty ()) delete this;
 }
-
-void RCommandReceiver::autoDeleteWhenDone () {
-	RK_TRACE (RBACKEND);
-
-	if (outstanding_commands.isEmpty ()) {
-		delete this;
-		return;
-	}
-	delete_when_done = true;
-}
diff --git a/rkward/rbackend/rcommandreceiver.h b/rkward/rbackend/rcommandreceiver.h
index 5c2a5fd2..0db15c09 100644
--- a/rkward/rbackend/rcommandreceiver.h
+++ b/rkward/rbackend/rcommandreceiver.h
@@ -34,8 +34,6 @@ public:
 	RCommandReceiver ();
 /** destructor */
 	virtual ~RCommandReceiver ();
-/** Causes the receiver to wait until all outstanding_commands (if any) are finished, then deletes itself */
-	void autoDeleteWhenDone ();
 protected:
 	friend class RCommand;
 	friend class RInterface;
diff --git a/rkward/windows/rkcommandlog.h b/rkward/windows/rkcommandlog.h
index 4c6424eb..32e415ef 100644
--- a/rkward/windows/rkcommandlog.h
+++ b/rkward/windows/rkcommandlog.h
@@ -13,10 +13,8 @@ SPDX-License-Identifier: GPL-2.0-or-later
 
 #include "rkmdiwindow.h"
 #include "../settings/rksettings.h"
-#include "../rbackend/rcommandreceiver.h"
+#include "../rbackend/rcommand.h"
 
-class RCommand;
-struct ROutput;
 class RKCommandLogView;
 class RKCommandLogPart;
 
@@ -25,20 +23,21 @@ class RKCommandLogPart;
 @author Thomas Friedrichsmeier
 */
 
-class RKCommandLog : public RKMDIWindow, public RCommandReceiver {
+class RKCommandLog : public RKMDIWindow {
 	Q_OBJECT
 public: 
 /** Adds input to the log_view-window (i.e. commands issued) */
 	void addInput (RCommand *command);
 /** Adds output to the log_view-window (i.e. replies received) */
-	void newOutput (RCommand *command, ROutput *output_fragment) override;
+	void newOutput (RCommand *command, ROutput *output_fragment);
 
 	static RKCommandLog *getLog () { return rkcommand_log; };
 
 	RKCommandLogView *getView () { return log_view; };
 protected:
+friend class RCommand;
 /** Command has finished. If the command has failed, it may be necessary to print some more information */
-	void rCommandDone (RCommand *command) override;
+	void rCommandDone (RCommand *command);
 	RKCommandLog (QWidget *parent, bool tool_window, const char *name=0);
 	~RKCommandLog ();
 public slots:
diff --git a/rkward/windows/rkwindowcatcher.cpp b/rkward/windows/rkwindowcatcher.cpp
index fbc73819..79d99155 100644
--- a/rkward/windows/rkwindowcatcher.cpp
+++ b/rkward/windows/rkwindowcatcher.cpp
@@ -550,16 +550,21 @@ void RKCaughtX11Window::setFixedSizeManual () {
 	delete dialog;
 }
 
+static void issueCommand(RCommand* command, RKProgressControl* control) {
+	control->addRCommand(command);
+	RInterface::issueCommand(command);
+}
+
 void RKCaughtX11Window::activateDevice () {
 	RK_TRACE (MISC);
 
-	RInterface::issueCommand ("dev.set (" + QString::number (device_number) + ')', RCommand::App, i18n ("Activate graphics device number %1", device_number), error_dialog);
+	issueCommand(new RCommand("dev.set (" + QString::number(device_number) + ')', RCommand::App, i18n("Activate graphics device number %1", device_number)), error_dialog);
 }
 
 void RKCaughtX11Window::copyDeviceToOutput () {
 	RK_TRACE (MISC);
 
-	RInterface::issueCommand ("dev.set (" + QString::number (device_number) + ")\ndev.copy (device=rk.graph.on)\nrk.graph.off ()", RCommand::App | RCommand::CCOutput, i18n ("Copy contents of graphics device number %1 to output", device_number), error_dialog);
+	issueCommand(new RCommand("dev.set (" + QString::number(device_number) + ")\ndev.copy (device=rk.graph.on)\nrk.graph.off ()", RCommand::App | RCommand::CCOutput, i18n("Copy contents of graphics device number %1 to output", device_number)), error_dialog);
 }
 
 void RKCaughtX11Window::printDevice () {
@@ -567,7 +572,7 @@ void RKCaughtX11Window::printDevice () {
 
 	QString printer_device;
 	if (RKSettingsModuleGraphics::kdePrintingEnabled ()) printer_device = "rk.printer.device";
-	RInterface::issueCommand ("dev.set (" + QString::number (device_number) + ")\ndev.print (" + printer_device + ')', RCommand::App, i18n ("Print contents of graphics device number %1", device_number), error_dialog);
+	issueCommand(new RCommand("dev.set (" + QString::number(device_number) + ")\ndev.print (" + printer_device + ')', RCommand::App, i18n("Print contents of graphics device number %1", device_number)), error_dialog);
 }
 
 void RKCaughtX11Window::copyDeviceToRObject () {
@@ -599,7 +604,7 @@ void RKCaughtX11Window::copyDeviceToRObject () {
 
 		QString name = chooser->currentFullName ();
 
-		RInterface::issueCommand ("dev.set (" + QString::number (device_number) + ")\n" + name + " <- recordPlot ()", RCommand::App | RCommand::ObjectListUpdate, i18n ("Save contents of graphics device number %1 to object '%2'", device_number, name), error_dialog);
+		issueCommand(new RCommand("dev.set (" + QString::number(device_number) + ")\n" + name + " <- recordPlot ()", RCommand::App | RCommand::ObjectListUpdate, i18n("Save contents of graphics device number %1 to object '%2'", device_number, name)), error_dialog);
 	}
 
 	delete dialog;
@@ -608,59 +613,59 @@ void RKCaughtX11Window::copyDeviceToRObject () {
 void RKCaughtX11Window::duplicateDevice () {
 	RK_TRACE (MISC);
 
-	RInterface::issueCommand ("rk.duplicate.device (" + QString::number (device_number) + ')', RCommand::App, i18n ("Duplicate graphics device number %1", device_number), error_dialog);
+	issueCommand(new RCommand("rk.duplicate.device (" + QString::number(device_number) + ')', RCommand::App, i18n("Duplicate graphics device number %1", device_number)), error_dialog);
 }
 
 void RKCaughtX11Window::nextPlot () {
 	RK_TRACE (MISC);
 
-	RCommand* c = new RCommand ("rk.next.plot (" + QString::number (device_number) + ')', RCommand::App, i18n ("Load next plot in device number %1", device_number), error_dialog);
+	RCommand* c = new RCommand("rk.next.plot (" + QString::number(device_number) + ')', RCommand::App, i18n("Load next plot in device number %1", device_number));
 	setStatusMessage (i18n ("Loading plot from history"), c);
-	RInterface::issueCommand (c);
+	issueCommand(c, error_dialog);
 }
 
 void RKCaughtX11Window::previousPlot () {
 	RK_TRACE (MISC);
 
-	RCommand* c = new RCommand ("rk.previous.plot (" + QString::number (device_number) + ')', RCommand::App, i18n ("Load previous plot in device number %1", device_number), error_dialog);
+	RCommand* c = new RCommand("rk.previous.plot (" + QString::number(device_number) + ')', RCommand::App, i18n("Load previous plot in device number %1", device_number));
 	setStatusMessage (i18n ("Loading plot from history"), c);
-	RInterface::issueCommand (c);
+	issueCommand(c, error_dialog);
 }
 
 void RKCaughtX11Window::firstPlot () {
 	RK_TRACE (MISC);
 
-	RCommand* c = new RCommand ("rk.first.plot (" + QString::number (device_number) + ')', RCommand::App, i18n ("Load first plot in device number %1", device_number), error_dialog);
+	RCommand* c = new RCommand("rk.first.plot (" + QString::number(device_number) + ')', RCommand::App, i18n("Load first plot in device number %1", device_number));
 	setStatusMessage (i18n ("Loading plot from history"), c);
-	RInterface::issueCommand (c);
+	issueCommand(c, error_dialog);
 }
 
 void RKCaughtX11Window::lastPlot () {
 	RK_TRACE (MISC);
 
-	RCommand* c = new RCommand ("rk.last.plot (" + QString::number (device_number) + ')', RCommand::App, i18n ("Load last plot in device number %1", device_number), error_dialog);
+	RCommand* c = new RCommand("rk.last.plot (" + QString::number(device_number) + ')', RCommand::App, i18n("Load last plot in device number %1", device_number));
 	setStatusMessage (i18n ("Loading plot from history"), c);
-	RInterface::issueCommand (c);
+	issueCommand(c, error_dialog);
 }
 
 void RKCaughtX11Window::gotoPlot (int index) {
 	RK_TRACE (MISC);
 
-	RCommand* c = new RCommand ("rk.goto.plot (" + QString::number (device_number) + ", " + QString::number (index+1) + ')', RCommand::App, i18n ("Load plot %1 in device number %2", index, device_number), error_dialog);
+	RCommand* c = new RCommand("rk.goto.plot (" + QString::number(device_number) + ", " + QString::number(index+1) + ')', RCommand::App, i18n("Load plot %1 in device number %2", index, device_number));
 	setStatusMessage (i18n ("Loading plot from history"), c);
-	RInterface::issueCommand (c);
+	issueCommand(c, error_dialog);
 }
 
 void RKCaughtX11Window::forceAppendCurrentPlot () {
 	RK_TRACE (MISC);
 
-	RInterface::issueCommand ("rk.force.append.plot (" + QString::number (device_number) + ')', RCommand::App, i18n ("Append this plot to history (device number %1)", device_number), error_dialog);
+	issueCommand(new RCommand("rk.force.append.plot (" + QString::number(device_number) + ')', RCommand::App, i18n("Append this plot to history (device number %1)", device_number)), error_dialog);
 }
 
 void RKCaughtX11Window::removeCurrentPlot () {
 	RK_TRACE (MISC);
 
-	RInterface::issueCommand ("rk.removethis.plot (" + QString::number (device_number) + ')', RCommand::App, i18n ("Remove current plot from history (device number %1)", device_number), error_dialog);
+	issueCommand(new RCommand("rk.removethis.plot (" + QString::number(device_number) + ')', RCommand::App, i18n("Remove current plot from history (device number %1)", device_number)), error_dialog);
 }
 
 void RKCaughtX11Window::clearHistory () {
@@ -668,13 +673,13 @@ void RKCaughtX11Window::clearHistory () {
 
 	if (KMessageBox::warningContinueCancel (this, i18n ("This will clear the plot history for all device windows, not just this one. If this is not your intent, press cancel, below.")) != KMessageBox::Continue) return;
 
-	RInterface::issueCommand ("rk.clear.plot.history ()", RCommand::App, i18n ("Clear plot history"), error_dialog);
+	issueCommand(new RCommand("rk.clear.plot.history ()", RCommand::App, i18n("Clear plot history")), error_dialog);
 }
 
 void RKCaughtX11Window::showPlotInfo () {
 	RK_TRACE (MISC);
 
-	RInterface::issueCommand ("rk.show.plot.info (" + QString::number (device_number) + ')', RCommand::App, i18n ("Plot properties (device number %1)", device_number), error_dialog);
+	issueCommand(new RCommand("rk.show.plot.info (" + QString::number(device_number) + ')', RCommand::App, i18n("Plot properties (device number %1)", device_number)), error_dialog);
 }
 
 void RKCaughtX11Window::updateHistoryActions (int history_length, int position, const QStringList &labels) {



More information about the rkward-tracker mailing list