[education/rkward] rkward: Implement change notification for recent urls

Thomas Friedrichsmeier null at kde.org
Wed Apr 20 14:02:40 BST 2022


Git commit 73850e68e375624823719709217945148e760d07 by Thomas Friedrichsmeier.
Committed on 19/04/2022 at 09:56.
Pushed by tfry into branch 'master'.

Implement change notification for recent urls

M  +23   -1    rkward/settings/rkrecenturls.cpp
M  +10   -3    rkward/settings/rkrecenturls.h
M  +19   -8    rkward/windows/rkhtmlwindow.cpp
M  +3    -2    rkward/windows/rkhtmlwindow.h

https://invent.kde.org/education/rkward/commit/73850e68e375624823719709217945148e760d07

diff --git a/rkward/settings/rkrecenturls.cpp b/rkward/settings/rkrecenturls.cpp
index 57425207..cf054597 100644
--- a/rkward/settings/rkrecenturls.cpp
+++ b/rkward/settings/rkrecenturls.cpp
@@ -15,6 +15,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #include <kconfigwidgets_version.h>
 
 #include "rksettingsmodulecommandeditor.h"
+#include "../rkward.h"
 
 #include "../debug.h"
 
@@ -26,10 +27,12 @@ static QString _output_id("rkoutput");
 QString RKRecentUrls::outputId() { return _output_id; }
 
 QHash<QString, KRecentFilesAction*> RKRecentUrls::actions;
+RKRecentUrls* RKRecentUrls::_notifier = nullptr;
 
 void RKRecentUrls::addRecentUrl(const QString& id, const QUrl& url) {
 	RK_TRACE(SETTINGS);
 	action(id)->addUrl(url);
+	notifyChangeProxy();
 }
 
 QUrl RKRecentUrls::mostRecentUrl(const QString& id) {
@@ -62,7 +65,23 @@ QList<QUrl> RKRecentUrls::allRecentUrls(const QString& id) {
 #endif
 }
 
-RKRecentUrls::RKRecentUrls() {
+RKRecentUrls* RKRecentUrls::notifier() {
+	RK_TRACE(SETTINGS);
+	if (!_notifier) {
+		_notifier = new RKRecentUrls(RKWardMainWindow::getMain());
+	}
+	return _notifier;
+}
+
+void RKRecentUrls::notifyChangeProxy() {
+	if (_notifier) _notifier->notifyChange();
+}
+
+void RKRecentUrls::notifyChange() {
+	emit recentUrlsChanged();
+}
+
+RKRecentUrls::RKRecentUrls(QObject* parent) : QObject(parent) {
 	RK_TRACE(SETTINGS);
 	// Not currrently used
 }
@@ -100,6 +119,7 @@ KRecentFilesAction * RKRecentUrls::action(const QString& id) {
 		if (!id.isEmpty()) act->loadEntries(cg.group(id));
 		act->setMaxItems(RKSettingsModuleCommandEditor::maxNumRecentFiles());  // TODO: Move setting somewhere else
 		QObject::connect(act, &QObject::destroyed, [id]() { RKRecentUrls::actions.remove(id); });
+		QObject::connect(act, &KRecentFilesAction::recentListCleared, &RKRecentUrls::notifyChangeProxy);
 		actions.insert(id, act);
 	}
 	return actions[id];
@@ -111,3 +131,5 @@ void RKRecentUrls::cleanup() {
 	}
 	actions.clear();
 }
+
+#include "rkrecenturls.moc"
diff --git a/rkward/settings/rkrecenturls.h b/rkward/settings/rkrecenturls.h
index 04d8a772..e0c36d22 100644
--- a/rkward/settings/rkrecenturls.h
+++ b/rkward/settings/rkrecenturls.h
@@ -10,12 +10,13 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #include <QString>
 #include <QHash>
 #include <QUrl>
-#include <QPointer>
+#include <QObject>
 
 class KRecentFilesAction;
 
 /** A wrapper around KRecentFilesAction to have a uniform handling across all the places where we remember the last used urls. */
-class RKRecentUrls {
+class RKRecentUrls : public QObject {
+	Q_OBJECT
 public:
 /** @see mostRecentUrl() */
 	static void addRecentUrl(const QString &id, const QUrl &url);
@@ -30,11 +31,17 @@ public:
 	static QString scriptsId();
 	static QString workspaceId();
 	static QString outputId();
+	static RKRecentUrls* notifier();
+signals:
+	void recentUrlsChanged();
 private:
-	RKRecentUrls();
+	RKRecentUrls(QObject* parent);
 	~RKRecentUrls();
+	void notifyChange();
+	static void notifyChangeProxy();
 	static QHash<QString, KRecentFilesAction*> actions;
 	static KRecentFilesAction* action(const QString &id);
+	static RKRecentUrls* _notifier;
 };
 
 #endif
diff --git a/rkward/windows/rkhtmlwindow.cpp b/rkward/windows/rkhtmlwindow.cpp
index 05238f9f..30fde668 100644
--- a/rkward/windows/rkhtmlwindow.cpp
+++ b/rkward/windows/rkhtmlwindow.cpp
@@ -520,18 +520,22 @@ void RKHTMLWindow::slotBack () {
 	openLocationFromHistory (url_history[current_history_position]);
 }
 
+static bool isRKWardUrl(const QUrl &url) {
+	return url.scheme() == QStringLiteral("rkward");
+}
+
 void RKHTMLWindow::openRKHPage (const QUrl &url) {
 	RK_TRACE (APP);
 
-	RK_ASSERT (url.scheme () == "rkward");
-	changeURL (url);
+	RK_ASSERT(isRKWardUrl(url));
+	if (url != this->url()) changeURL(url);  // see ::refresh()
 	bool ok = false;
 	if ((url.host () == "component") || (url.host () == "page")) {
 		useMode (HTMLHelpWindow);
 
 		startNewCacheFile ();
 		RKHelpRenderer render (current_cache_file);
-		ok = render.renderRKHelp (url);
+		ok = render.renderRKHelp(url, this);
 		current_cache_file->close ();
 
 		QUrl cache_url = QUrl::fromLocalFile (current_cache_file->fileName ());
@@ -553,7 +557,7 @@ void RKHTMLWindow::openRKHPage (const QUrl &url) {
 bool RKHTMLWindow::handleRKWardURL (const QUrl &url, RKHTMLWindow *window) {
 	RK_TRACE (APP);
 
-	if (url.scheme () == "rkward") {
+	if (isRKWardUrl(url)) {
 		if (url.host () == "runplugin") {
 			QString path = url.path ();
 			if (path.startsWith ('/')) path = path.mid (1);
@@ -777,8 +781,14 @@ void RKHTMLWindow::updateCaption (const QUrl &url) {
 
 void RKHTMLWindow::refresh () {
 	RK_TRACE (APP);
+	RK_DEBUG(APP, DL_DEBUG, "reload %s", qPrintable(url().url()));
 
-	view->reload ();
+	if (isRKWardUrl(url())) {
+		// need to re-render the page
+		openRKHPage(url());
+	} else {
+		view->reload();
+	}
 }
 
 void RKHTMLWindow::scrollToBottom () {
@@ -995,10 +1005,10 @@ void RKHTMLWindowPart::setHelpWindowSkin() {
 //////////////////////////////////////////
 //////////////////////////////////////////
 
-bool RKHelpRenderer::renderRKHelp (const QUrl &url) {
+bool RKHelpRenderer::renderRKHelp (const QUrl &url, RKHTMLWindow* container) {
 	RK_TRACE (APP);
 
-	if (url.scheme () != "rkward") {
+	if (!isRKWardUrl(url)) {
 		RK_ASSERT (false);
 		return (false);
 	}
@@ -1105,6 +1115,7 @@ bool RKHelpRenderer::renderRKHelp (const QUrl &url) {
 				}
 				writeHTML(QString("<li><<a href=\"rkward://open/%1/\">%2</a>></li>\n").arg(category, i18n("Choose another file")));
 				writeHTML("</ul>\n");
+				if (container) QObject::connect(RKRecentUrls::notifier(), &RKRecentUrls::recentUrlsChanged, container, &RKHTMLWindow::refresh);
 			}
 		}
 		writeHTML (renderHelpFragment (*it));
@@ -1264,7 +1275,7 @@ QString RKHelpRenderer::prepareHelpLink (const QString &href, const QString &tex
 	} else {
 		QString ltext;
 		QUrl url (href);
-		if (url.scheme () == "rkward") {
+		if (isRKWardUrl(url)) {
 			if (url.host () == "component") {
 				RKComponentHandle *chandle = componentPathToHandle (url.path ());
 				if (chandle) ltext = chandle->getLabel ();
diff --git a/rkward/windows/rkhtmlwindow.h b/rkward/windows/rkhtmlwindow.h
index 4b462b0a..58d65424 100644
--- a/rkward/windows/rkhtmlwindow.h
+++ b/rkward/windows/rkhtmlwindow.h
@@ -176,8 +176,9 @@ public:
 	explicit RKHelpRenderer (QIODevice *_device) { device = _device; help_xml = 0; component_xml = 0; };
 /** destructor */
 	~RKHelpRenderer () {};
-// for dealing with rkward://[page|component]-pages
-	bool renderRKHelp (const QUrl &url);
+/** render an rkward://[page|component]-page to the device given in the ctor.
+ * @param container : Should be page contain dynamic elements, connections will be set up to call refresh(), on the container, as appropriate. May be nullptr. */
+	bool renderRKHelp(const QUrl &url, RKHTMLWindow* container);
 private:
 	XMLHelper *help_xml;
 	XMLHelper *component_xml;



More information about the rkward-tracker mailing list