[education/rkward] rkward: Port some more cases away from RCommandReceiver

Thomas Friedrichsmeier null at kde.org
Sat May 21 20:27:32 BST 2022


Git commit 2abf6710d5ef984e7927dfd6196d2e59db65b345 by Thomas Friedrichsmeier.
Committed on 21/05/2022 at 19:27.
Pushed by tfry into branch 'master'.

Port some more cases away from RCommandReceiver

M  +20   -27   rkward/agents/rkloadagent.cpp
M  +1    -4    rkward/agents/rkloadagent.h
M  +4    -0    rkward/rbackend/rcommand.h
M  +8    -16   rkward/settings/rksettingsmoduler.cpp
M  +1    -5    rkward/settings/rksettingsmoduler.h
M  +26   -33   rkward/windows/rkhelpsearchwindow.cpp
M  +2    -4    rkward/windows/rkhelpsearchwindow.h

https://invent.kde.org/education/rkward/commit/2abf6710d5ef984e7927dfd6196d2e59db65b345

diff --git a/rkward/agents/rkloadagent.cpp b/rkward/agents/rkloadagent.cpp
index 7e62d5ce..62b56b68 100644
--- a/rkward/agents/rkloadagent.cpp
+++ b/rkward/agents/rkloadagent.cpp
@@ -56,23 +56,11 @@ RKLoadAgent::RKLoadAgent (const QUrl &url, bool merge) {
 		RInterface::issueCommand (command);
 	}
 
-	command = new RCommand ("load (\"" + filename + "\")", RCommand::App | RCommand::ObjectListUpdate, QString (), this, WORKSPACE_LOAD_COMMAND);
-	RInterface::issueCommand (command);
-
-	RKWorkplace::mainWorkplace ()->setWorkspaceURL (url);
-}
-
-RKLoadAgent::~RKLoadAgent () {
-	RK_TRACE (APP);
-}
-
-void RKLoadAgent::rCommandDone (RCommand *command) {
-	RK_TRACE (APP);
-	
-	if (command->getFlags () == WORKSPACE_LOAD_COMMAND) {
-		if (command->failed ()) {
-			KMessageBox::error (0, i18n ("There has been an error opening file '%1':\n%2", RKWorkplace::mainWorkplace ()->workspaceURL ().path (), command->error ()), i18n ("Error loading workspace"));
-			RKWorkplace::mainWorkplace ()->setWorkspaceURL (QUrl());
+	command = new RCommand ("load (\"" + filename + "\")", RCommand::App | RCommand::ObjectListUpdate);
+	command->whenFinished(this, [this](RCommand* command){
+		if (command->failed()) {
+			KMessageBox::error(0, i18n("There has been an error opening file '%1':\n%2", RKWorkplace::mainWorkplace()->workspaceURL().path(), command->warnings() + command->error()), i18n("Error loading workspace"));
+			RKWorkplace::mainWorkplace()->setWorkspaceURL(QUrl());
 		} else {
 			RKWorkplace::mainWorkplace ()->restoreWorkplace (0, _merge);
 			if (RKSettingsModuleGeneral::cdToWorkspaceOnLoad ()) {
@@ -81,17 +69,22 @@ void RKLoadAgent::rCommandDone (RCommand *command) {
 				}
 			}
 		}
-		RInterface::issueCommand(QString(), RCommand::EmptyCommand | RCommand::App, QString(), this, WORKSPACE_LOAD_COMPLETE_COMMAND);
+		RCommand *c = new RCommand(QString(), RCommand::EmptyCommand | RCommand::App);
+		c->whenFinished(this, [this]() {
+			RKWardMainWindow::getMain()->slotSetStatusReady();
+			RKWardMainWindow::getMain()->setWorkspaceMightBeModified(false);
+			RKOutputDirectory::getCurrentOutput();  // make sure some output file exists
+
+			deleteLater();
+		});
+		RInterface::issueCommand(c);
 		RKWardMainWindow::getMain ()->setCaption (QString ());	// trigger update of caption
-	} else if (command->getFlags () == WORKSPACE_LOAD_COMPLETE_COMMAND) {
-		RKWardMainWindow::getMain ()->slotSetStatusReady ();
-		RKWardMainWindow::getMain ()->setWorkspaceMightBeModified (false);
-		RKOutputDirectory::getCurrentOutput();  // make sure some output file exists
+	});
+	RInterface::issueCommand (command);
 
-		delete this;
-		return;
-	} else {
-		RK_ASSERT (false);
-	}
+	RKWorkplace::mainWorkplace ()->setWorkspaceURL (url);
 }
 
+RKLoadAgent::~RKLoadAgent () {
+	RK_TRACE (APP);
+}
diff --git a/rkward/agents/rkloadagent.h b/rkward/agents/rkloadagent.h
index b6d3471b..079d743b 100644
--- a/rkward/agents/rkloadagent.h
+++ b/rkward/agents/rkloadagent.h
@@ -8,7 +8,6 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #define RKLOADAGENT_H
 
 #include <qobject.h>
-#include "../rbackend/rcommandreceiver.h"
 
 #include <qstring.h>
 #include <QUrl>
@@ -19,14 +18,12 @@ class QTemporaryFile;
 agents, the RKLoadAgent self-destructs when done.
 @author Thomas Friedrichsmeier
 */
-class RKLoadAgent : public QObject, public RCommandReceiver {
+class RKLoadAgent : public QObject {
 	Q_OBJECT
 public:
 	explicit RKLoadAgent (const QUrl &url, bool merge=false);
 
 	~RKLoadAgent ();
-protected:
-	void rCommandDone (RCommand *command) override;
 private:
 /// needed if file to be loaded is remote
 	QTemporaryFile* tmpfile;
diff --git a/rkward/rbackend/rcommand.h b/rkward/rbackend/rcommand.h
index fa6d29e9..acbea540 100644
--- a/rkward/rbackend/rcommand.h
+++ b/rkward/rbackend/rcommand.h
@@ -202,6 +202,10 @@ public:
 	RCommandNotifier* notifier ();
 /** same as RObject::rQuote */
 	static QString rQuote (const QString &quoted);
+
+	template<typename T> void whenFinished(const QObject* receiver, const T func) {
+		QObject::connect(notifier(), &RCommandNotifier::commandFinished, receiver, func);
+	};
 private:
 friend class RInterface;
 friend class RCommandStack;
diff --git a/rkward/settings/rksettingsmoduler.cpp b/rkward/settings/rksettingsmoduler.cpp
index e152d9f8..eaa9fec7 100755
--- a/rkward/settings/rksettingsmoduler.cpp
+++ b/rkward/settings/rksettingsmoduler.cpp
@@ -310,7 +310,7 @@ RKConfigValue<QString> RKSettingsModuleRPackages::cran_mirror_url {"CRAN mirror
 QStringList RKSettingsModuleRPackages::defaultliblocs;
 QString RKSettingsModuleRPackages::r_libs_user;
 
-RKSettingsModuleRPackages::RKSettingsModuleRPackages (RKSettings *gui, QWidget *parent) : RKSettingsModule(gui, parent), RCommandReceiver () {
+RKSettingsModuleRPackages::RKSettingsModuleRPackages (RKSettings *gui, QWidget *parent) : RKSettingsModule(gui, parent) {
 	RK_TRACE (SETTINGS);
 
 	QVBoxLayout *main_vbox = new QVBoxLayout (this);
@@ -451,12 +451,17 @@ QIcon RKSettingsModuleRPackages::icon() const {
 	return RKStandardIcons::getIcon(RKStandardIcons::ObjectPackageEnvironment);
 }
 
-#define SELECT_CRAN_MIRROR_COMMAND 123
 void RKSettingsModuleRPackages::selectCRANMirror () {
 	RK_TRACE (SETTINGS);
 	QString title = i18n ("Select CRAN mirror");
 	
-	RCommand* command = new RCommand ("rk.select.CRAN.mirror()\n", RCommand::App | RCommand::GetStringVector, title, this, SELECT_CRAN_MIRROR_COMMAND);
+	RCommand* command = new RCommand ("rk.select.CRAN.mirror()\n", RCommand::App | RCommand::GetStringVector, title);
+	connect(command->notifier(), &RCommandNotifier::commandFinished, this, [this](RCommand *command) {
+		if (command->succeeded()) {
+			RK_ASSERT(command->getDataLength() >= 1);
+			cran_mirror_input->setText(command->stringVector().value(0));
+		}
+	});
 
 	RKProgressControl* control = new RKProgressControl (this, title, title, RKProgressControl::CancellableProgress);
 	control->addRCommand (command, true);
@@ -464,19 +469,6 @@ void RKSettingsModuleRPackages::selectCRANMirror () {
 	control->doModal (true);
 }
 
-void RKSettingsModuleRPackages::rCommandDone (RCommand *command) {
-	RK_TRACE (SETTINGS);
-
-	if (command->getFlags () == SELECT_CRAN_MIRROR_COMMAND) {
-		if (command->succeeded ()) {
-			RK_ASSERT (command->getDataLength () >= 1);
-			cran_mirror_input->setText (command->stringVector ().value (0));
-		}
-	} else {
-		RK_ASSERT (false);
-	}
-}
-
 QString RKSettingsModuleRPackages::libLocsCommand () {
 	RK_TRACE (SETTINGS);
 
diff --git a/rkward/settings/rksettingsmoduler.h b/rkward/settings/rksettingsmoduler.h
index 15daf547..3f5ea7f5 100644
--- a/rkward/settings/rksettingsmoduler.h
+++ b/rkward/settings/rksettingsmoduler.h
@@ -78,14 +78,12 @@ private:
 	static QString help_base_url;
 };
 
-#include "../rbackend/rcommandreceiver.h"
-
 /**
 Configure packages and library paths
 
 @author Thomas Friedrichsmeier
 */
-class RKSettingsModuleRPackages : public RKSettingsModule, public RCommandReceiver {
+class RKSettingsModuleRPackages : public RKSettingsModule {
 	Q_OBJECT
 public:
 	RKSettingsModuleRPackages (RKSettings *gui, QWidget *parent);
@@ -117,8 +115,6 @@ public slots:
 	void addLibLoc (QStringList *string_list);
 	void addRepository (QStringList *string_list);
 	void selectCRANMirror ();
-protected:
-	void rCommandDone (RCommand *command) override;
 private:
 friend class RKLoadLibsDialog;
 	static QString libLocsCommand ();
diff --git a/rkward/windows/rkhelpsearchwindow.cpp b/rkward/windows/rkhelpsearchwindow.cpp
index 16882837..8e105def 100644
--- a/rkward/windows/rkhelpsearchwindow.cpp
+++ b/rkward/windows/rkhelpsearchwindow.cpp
@@ -1,6 +1,6 @@
 /*
 rkhelpsearchwindow - This file is part of RKWard (https://rkward.kde.org). Created: Fri Feb 25 2005
-SPDX-FileCopyrightText: 2005-2011 by Thomas Friedrichsmeier <thomas.friedrichsmeier at kdemail.net>
+SPDX-FileCopyrightText: 2005-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
 */
@@ -24,7 +24,6 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #include <QSortFilterProxyModel>
 
 #include "../rbackend/rkrinterface.h"
-#include "../rbackend/rcommandreceiver.h"
 #include "../rbackend/rksessionvars.h"
 #include "../debug.h"
 
@@ -34,9 +33,6 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #include "../misc/rkdummypart.h"
 #include "../misc/rkstandardicons.h"
 
-#define GET_HELP 1
-#define HELP_SEARCH 2
-
 // result columns
 #define COL_TYPE 0
 #define COL_TOPIC 1
@@ -158,7 +154,13 @@ void RKHelpSearchWindow::getFunctionHelp (const QString &function_name, const QS
 	command.append (")");
 	if (type == "vignette") command.append (")");
 
-	RInterface::issueCommand (command, RCommand::App | RCommand::GetStringVector, i18n ("Find HTML help for %1", function_name), this, GET_HELP);
+	auto c = new RCommand(command, RCommand::App | RCommand::GetStringVector, i18n("Find HTML help for %1", function_name));
+	c->whenFinished(this, [this](RCommand* command) {
+		if (command->failed ()) {
+			KMessageBox::sorry (this, i18n ("No help found on '%1'. Maybe the corresponding package is not installed/loaded, or maybe you mistyped the command. Try using Help->Search R Help for more options.", command->command ().section ('\"', 1, 1)), i18n ("No help found"));
+		}
+	});
+	RInterface::issueCommand(c);
 }
 
 void RKHelpSearchWindow::slotFindButtonClicked () {
@@ -190,10 +192,24 @@ void RKHelpSearchWindow::slotFindButtonClicked () {
 	QString fields = fieldsList->itemData (fieldsList->currentIndex ()).toString ();
 
 	QString s = ".rk.get.search.results (" + RObject::rQuote (field->currentText ()) + ", agrep=" + agrep + ", ignore.case=" + ignoreCase + package + ", fields=" + fields + ')';
-	
-	RInterface::issueCommand (s, RCommand::App | RCommand::Sync | RCommand::GetStringVector, QString (), this, HELP_SEARCH, 0);
-	setEnabled (false);
-	field->addItem (field->currentText ());
+
+	auto c = new RCommand(s, RCommand::App | RCommand::Sync | RCommand::GetStringVector);
+	c->whenFinished(this, [this](RCommand *command) {
+		QStringList res;
+		if (command->failed ()) {
+			RK_ASSERT (false);
+		} else {
+			RK_ASSERT (command->getDataType () == RData::StringVector);
+			res = command->stringVector ();
+		}
+		results->setResults (res);
+
+		for (int i = 0; i < COL_COUNT; ++i) results_view->resizeColumnToContents (i);
+		setEnabled(true);
+	});
+	RInterface::issueCommand(c);
+	setEnabled(false);
+	field->addItem(field->currentText());
 }
 
 void RKHelpSearchWindow::resultDoubleClicked (const QModelIndex& index) {
@@ -227,29 +243,6 @@ void RKHelpSearchWindow::updateInstalledPackages () {
 	packagesList->setCurrentIndex (index);
 }
 
-void RKHelpSearchWindow::rCommandDone (RCommand *command) {
-	RK_TRACE (APP);
-	if (command->getFlags () == HELP_SEARCH) {
-		QStringList res;
-		if (command->failed ()) {
-			RK_ASSERT (false);
-		} else {
-			RK_ASSERT (command->getDataType () == RData::StringVector);
-			res = command->stringVector ();
-		}
-		results->setResults (res);
-
-		for (int i = 0; i < COL_COUNT; ++i) results_view->resizeColumnToContents (i);
-		setEnabled(true);
-	} else if (command->getFlags () == GET_HELP) {
-		if (command->failed ()) {
-			KMessageBox::sorry (this, i18n ("No help found on '%1'. Maybe the corresponding package is not installed/loaded, or maybe you mistyped the command. Try using Help->Search R Help for more options.", command->command ().section ('\"', 1, 1)), i18n ("No help found"));
-		}
-	} else {
-		RK_ASSERT (false);
-	}
-}
-
 //////////////// RKHelpResultsModel ////////////////
 
 RKHelpSearchResultsModel::RKHelpSearchResultsModel (QObject *parent) : QAbstractTableModel (parent) {
diff --git a/rkward/windows/rkhelpsearchwindow.h b/rkward/windows/rkhelpsearchwindow.h
index 4c690f90..d49b3d93 100644
--- a/rkward/windows/rkhelpsearchwindow.h
+++ b/rkward/windows/rkhelpsearchwindow.h
@@ -1,6 +1,6 @@
 /*
 rkhelpsearchwindow - This file is part of the RKWard project. Created: Fri Feb 25 2005
-SPDX-FileCopyrightText: 2005-2011 by Thomas Friedrichsmeier <thomas.friedrichsmeier at kdemail.net>
+SPDX-FileCopyrightText: 2005-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
 */
@@ -11,7 +11,6 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #include <qwidget.h>
 #include <QAbstractTableModel>
 
-#include "../rbackend/rcommandreceiver.h"
 #include "rkmdiwindow.h"
 
 class QFocusEvent;
@@ -26,12 +25,11 @@ class RCommandChain;
 /** Provides a UI interface for help-search.
 
 @author Pierre Ecochard */
-class RKHelpSearchWindow : public RKMDIWindow, public RCommandReceiver {
+class RKHelpSearchWindow : public RKMDIWindow {
 	Q_OBJECT
 public:
 	RKHelpSearchWindow (QWidget *parent, bool tool_window, const char *name=0);
 	~RKHelpSearchWindow ();
-	void rCommandDone (RCommand *command) override;
 /** small convenience function to get context help for RKCommandEditorWindow and RKConsole.
 @param context_line The current line
 @param cursor_pos cursor position in the current line


More information about the rkward-tracker mailing list