[rkward/work/split_views] rkward/windows: Keep unnamed split script windows in sync, too.

Thomas Friedrichsmeier null at kde.org
Thu Jun 22 19:07:32 UTC 2017


Git commit 54be00f92ba7be285a13443d369db271bc1910c9 by Thomas Friedrichsmeier.
Committed on 22/06/2017 at 19:07.
Pushed by tfry into branch 'work/split_views'.

Keep unnamed split script windows in sync, too.

M  +39   -8    rkward/windows/rkcommandeditorwindow.cpp
M  +5    -3    rkward/windows/rkcommandeditorwindow.h
M  +1    -0    rkward/windows/rkworkplace.cpp

https://commits.kde.org/rkward/54be00f92ba7be285a13443d369db271bc1910c9

diff --git a/rkward/windows/rkcommandeditorwindow.cpp b/rkward/windows/rkcommandeditorwindow.cpp
index 43b450b9..70c392bd 100644
--- a/rkward/windows/rkcommandeditorwindow.cpp
+++ b/rkward/windows/rkcommandeditorwindow.cpp
@@ -45,6 +45,7 @@
 #include <kio/deletejob.h>
 #include <kio/job.h>
 #include <kconfiggroup.h>
+#include <krandom.h>
 
 #include "../misc/rkcommonfunctions.h"
 #include "../misc/rkstandardicons.h"
@@ -78,14 +79,37 @@ RKCommandEditorWindowPart::~RKCommandEditorWindowPart () {
 #define GET_HELP_URL 1
 #define NUM_BLOCK_RECORDS 6
 
-RKCommandEditorWindow::RKCommandEditorWindow (QWidget *parent, const QUrl url, const QString& encoding, bool use_r_highlighting, bool use_codehinting, bool read_only, bool delete_on_close) : RKMDIWindow (parent, RKMDIWindow::CommandEditorWindow) {
+//static
+QMap<QString, KTextEditor::Document*> RKCommandEditorWindow::unnamed_documents;
+
+RKCommandEditorWindow::RKCommandEditorWindow (QWidget *parent, const QUrl _url, const QString& encoding, bool use_r_highlighting, bool use_codehinting, bool read_only, bool delete_on_close) : RKMDIWindow (parent, RKMDIWindow::CommandEditorWindow) {
 	RK_TRACE (COMMANDEDITOR);
 
+	QString id_header = QStringLiteral ("unnamedscript://");
+
 	KTextEditor::Editor* editor = KTextEditor::Editor::instance ();
 	RK_ASSERT (editor);
 
+	QUrl url = _url;
 	m_doc = 0;
-	if (!url.isEmpty ()) {  // TODO: what about delete_on_close
+
+	// Lookup of existing text editor documents: First, if no url is given at all, create a new document, and register an id, in case this window will get split, later
+	if (url.isEmpty ()) {
+		m_doc = editor->createDocument (RKWardMainWindow::getMain ());
+		_id = id_header + KRandom::randomString (16).toLower ();
+		RK_ASSERT (!unnamed_documents.contains (_id));
+		unnamed_documents.insert (_id, m_doc);
+	} else if (url.url ().startsWith (id_header)) { // Next, handle the case that a pseudo-url is passed in
+		_id = url.url ();
+		m_doc = unnamed_documents.value (_id);
+		url.clear ();
+		if (!m_doc) {  // can happen while restoring saved workplace.
+			m_doc = editor->createDocument (RKWardMainWindow::getMain ());
+			unnamed_documents.insert (_id, m_doc);
+		}
+	} else {   // regular url given. Try to find an existing document for that url
+		       // NOTE: we cannot simply use the same map as above, for this purpose, as document urls may change.
+		       // instead we iterate over the document list.
 		QList<KTextEditor::Document*> docs = editor->documents ();
 		for (int i = 0; i < docs.count (); ++i) {
 			if (docs[i]->url ().matches (url, QUrl::NormalizePathSegments | QUrl::StripTrailingSlash | QUrl::PreferLocalFile)) {
@@ -93,11 +117,17 @@ RKCommandEditorWindow::RKCommandEditorWindow (QWidget *parent, const QUrl url, c
 				break;
 			}
 		}
+	}
+
+	// if an existing document is re-used, try to honor decoding.
+	if (m_doc) {
 		if (!encoding.isEmpty () && (m_doc->encoding () != encoding)) {
 			m_doc->setEncoding (encoding);
 			m_doc->documentReload ();
 		}
 	}
+
+	// no existing document was found, so create one and load the url
 	if (!m_doc) {
 		m_doc = editor->createDocument (RKWardMainWindow::getMain ()); // The document may have to outlive this window
 
@@ -210,10 +240,6 @@ RKCommandEditorWindow::RKCommandEditorWindow (QWidget *parent, const QUrl url, c
 RKCommandEditorWindow::~RKCommandEditorWindow () {
 	RK_TRACE (COMMANDEDITOR);
 
-	// NOTE: TODO: Ideally we'd only write out a changed config, but how to detect config changes?
-	// 	Alternatively, only for the last closed script window
-	// KF5 TODO: verify that the code below is no longer needed
-	//m_doc->editor ()->writeConfig ();
 	if (!url ().isEmpty ()) {
 		QString p_url = RKWorkplace::mainWorkplace ()->portableUrl (m_doc->url ());
 		KConfigGroup conf (RKWorkplace::mainWorkplace ()->workspaceConfig (), QString ("SkriptDocumentSettings %1").arg (p_url));
@@ -228,6 +254,11 @@ RKCommandEditorWindow::~RKCommandEditorWindow () {
 	if (views.isEmpty ()) {
 		delete m_doc;
 		if (!delete_on_close.isEmpty ()) KIO::del (delete_on_close)->start ();
+		unnamed_documents.remove (_id);
+	}
+	// NOTE, under rather unlikely circumstances, the above may leave stale ids->stale pointers in the map: Create unnamed window, split it, save to a url, split again, close the first two windows, close the last. This situation should be caught by the following, however:
+	if (!url ().isEmpty () && !_id.isEmpty ()) {
+		unnamed_documents.remove (_id);
 	}
 }
 
@@ -516,12 +547,12 @@ void RKCommandEditorWindow::autoSaveHandlerJobFinished (RKJobSequence* seq) {
 	}
 }
 
-QUrl RKCommandEditorWindow::url () {
+QUrl RKCommandEditorWindow::url () const {
 //	RK_TRACE (COMMANDEDITOR);
 	return (m_doc->url ());
 }
 
-bool RKCommandEditorWindow::isModified() {
+bool RKCommandEditorWindow::isModified () {
 	RK_TRACE (COMMANDEDITOR);
 	return m_doc->isModified();
 }
diff --git a/rkward/windows/rkcommandeditorwindow.h b/rkward/windows/rkcommandeditorwindow.h
index 6f464c2c..187075bc 100644
--- a/rkward/windows/rkcommandeditorwindow.h
+++ b/rkward/windows/rkcommandeditorwindow.h
@@ -166,7 +166,9 @@ public:
 	void setReadOnly (bool ro);
 
 /** Return current url */
-	QUrl url ();
+	QUrl url () const;
+/** Returns an id string for this document. Meaningful, only when url is empty. For keeping track of split views on unnamed/unsaved windows */
+	QString id () const { return _id; };
 
 	QString provideContext (int line_rev) override;
 	void currentHelpContext (QString* symbol, QString* package) override;
@@ -264,8 +266,8 @@ private:
 
 	QUrl delete_on_close;
 
-/** open given URL. See RKCommandEditorWindow () */
-	bool openURL (const QUrl url, const QString& encoding=QString (), bool use_r_highlighting=true, bool read_only=false, bool delete_on_close=false);
+	QString _id;
+	static QMap<QString, KTextEditor::Document*> unnamed_documents;
 };
 
 /** Simple class to provide HTML highlighting for arbitrary R code. */
diff --git a/rkward/windows/rkworkplace.cpp b/rkward/windows/rkworkplace.cpp
index 733a8902..4040fb7e 100644
--- a/rkward/windows/rkworkplace.cpp
+++ b/rkward/windows/rkworkplace.cpp
@@ -759,6 +759,7 @@ QString RKWorkplace::makeItemDescription (RKMDIWindow *win) const {
 	} else if (win->isType (RKMDIWindow::CommandEditorWindow)) {
 		type = "script";
 		specification = static_cast<RKCommandEditorWindow*> (win)->url ().url ();
+		if (specification.isEmpty ()) specification = static_cast<RKCommandEditorWindow*> (win)->id ();
 	} else if (win->isType (RKMDIWindow::OutputWindow)) {
 		type = "output";
 		specification = static_cast<RKHTMLWindow*> (win)->url ().url ();



More information about the rkward-tracker mailing list