[rkward/work/render_rmd] rkward: Fix assorted script preview bugs

Thomas Friedrichsmeier null at kde.org
Fri Sep 28 19:41:22 BST 2018


Git commit cf14b94c12ab9a0b7c383c3c1c08698c6db19f7d by Thomas Friedrichsmeier.
Committed on 28/09/2018 at 18:39.
Pushed by tfry into branch 'work/render_rmd'.

Fix assorted script preview bugs

M  +11   -1    rkward/misc/rkxmlguipreviewarea.cpp
M  +3    -0    rkward/misc/rkxmlguipreviewarea.h
M  +1    -1    rkward/rbackend/rpackages/rkward/R/rk.plugin-functions.R
M  +12   -10   rkward/windows/rkcommandeditorwindow.cpp

https://commits.kde.org/rkward/cf14b94c12ab9a0b7c383c3c1c08698c6db19f7d

diff --git a/rkward/misc/rkxmlguipreviewarea.cpp b/rkward/misc/rkxmlguipreviewarea.cpp
index 9384f9a7..1ff23f2f 100644
--- a/rkward/misc/rkxmlguipreviewarea.cpp
+++ b/rkward/misc/rkxmlguipreviewarea.cpp
@@ -57,6 +57,16 @@ RKXMLGUIPreviewArea::~RKXMLGUIPreviewArea () {
 	}
 }
 
+void RKXMLGUIPreviewArea::setLabel (const QString& label) {
+	RK_TRACE (PLUGIN);
+
+	if (label == _label) return;
+	_label = label;
+	if (wrapper_widget) {
+		lab->setText (label);
+	}
+}
+
 QWidget* RKXMLGUIPreviewArea::wrapperWidget () {
 	if (wrapper_widget) return wrapper_widget;
 
@@ -69,7 +79,7 @@ QWidget* RKXMLGUIPreviewArea::wrapperWidget () {
 	vl->addWidget (line);
 	QHBoxLayout *hl = new QHBoxLayout ();
 	vl->addLayout (hl);
-	QLabel *lab = new QLabel (_label, wrapper_widget);
+	lab = new QLabel (_label, wrapper_widget);
 	QFont fnt (lab->font ());
 	fnt.setBold (true);
 	lab->setFont (fnt);
diff --git a/rkward/misc/rkxmlguipreviewarea.h b/rkward/misc/rkxmlguipreviewarea.h
index e46d2891..f1d05ddf 100644
--- a/rkward/misc/rkxmlguipreviewarea.h
+++ b/rkward/misc/rkxmlguipreviewarea.h
@@ -25,6 +25,7 @@
 
 class QMenu;
 class QToolButton;
+class QLabel;
 
 class RKXMLGUIPreviewArea : public KXmlGuiWindow {
 	Q_OBJECT
@@ -34,6 +35,7 @@ public:
 	/** Returns a wrapper widget (created on first call of this function) that contains this widget along with a caption (see setLabel()), menu button, and close button. */
 	QWidget *wrapperWidget ();
 	QString label () const { return _label; };
+	void setLabel (const QString &label);
 protected:
 	/** build / destroy menu, when child is added removed. Note that we are in the fortunate situation that RKMDIWindow-children only ever get to the
 	 *  preview area via reparenting, i.e. contrary to usual QEvent::ChildAdded semnatics, they are always fully constructed, when added. */
@@ -45,6 +47,7 @@ signals:
 private:
 	QWidget *wrapper_widget;
 	QString _label;
+	QLabel *lab;
 	QMenu *menu;
 	QPointer<KParts::Part> current;
 };
diff --git a/rkward/rbackend/rpackages/rkward/R/rk.plugin-functions.R b/rkward/rbackend/rpackages/rkward/R/rk.plugin-functions.R
index eecd9653..23578c79 100644
--- a/rkward/rbackend/rpackages/rkward/R/rk.plugin-functions.R
+++ b/rkward/rbackend/rpackages/rkward/R/rk.plugin-functions.R
@@ -247,7 +247,7 @@ assign(".rk.preview.data", list (), envir=.rk.variables)
 #' @rdname rk.assign.preview.data
 "rk.discard.preview.data" <- function (id) {
 	pdata <- .rk.variables$.rk.preview.data
-	if (!is.null (pdata[[id]]) && !is.null (pdata[[id]]$on.delete)) pdata[[id]]$on.delete (id)
+	if (is.list (pdata[[id]]) && !is.null (pdata[[id]]$on.delete)) pdata[[id]]$on.delete (id)
 	pdata[[id]] <- NULL
 	assign (".rk.preview.data", pdata, envir=.rk.variables)
 	rk.sync (.rk.variables$.rk.preview.data)
diff --git a/rkward/windows/rkcommandeditorwindow.cpp b/rkward/windows/rkcommandeditorwindow.cpp
index e46f593e..cab5439c 100644
--- a/rkward/windows/rkcommandeditorwindow.cpp
+++ b/rkward/windows/rkcommandeditorwindow.cpp
@@ -177,7 +177,7 @@ RKCommandEditorWindow::RKCommandEditorWindow (QWidget *parent, const QUrl _url,
 	KTextEditor::ModificationInterface* em_iface = qobject_cast<KTextEditor::ModificationInterface*> (m_doc);
 	if (em_iface) em_iface->setModifiedOnDiskWarning (true);
 	else RK_ASSERT (false);
-	preview = new RKXMLGUIPreviewArea (i18n ("Preview of rendered R Markdown"), this);
+	preview = new RKXMLGUIPreviewArea (QString(), this);
 	preview_manager = new RKPreviewManager (this);
 	connect (preview_manager, &RKPreviewManager::statusChanged, [this]() { preview_timer.start (500); });
 	m_view = m_doc->createView (this);
@@ -820,6 +820,7 @@ void RKCommandEditorWindow::doRenderPreview () {
 
 	if (!preview_dir) preview_dir = new QTemporaryDir ();
 	QFile save (preview_dir->filePath ("script.R"));
+	if (actionmenu_preview->currentItem () == RMarkdownPreview) save.setFileName (preview_dir->filePath ("markdownscript.Rmd"));
 	RK_ASSERT (save.open (QIODevice::WriteOnly));
 	QTextStream out (&save);
 	out.setCodec ("UTF-8");     // make sure that all characters can be saved, without nagging the user
@@ -828,7 +829,7 @@ void RKCommandEditorWindow::doRenderPreview () {
 
 	QString command;
 	if (actionmenu_preview->currentItem () == RMarkdownPreview) {
-		save.rename (preview_dir->filePath ("markdownscript.Rmd"));
+		preview->setLabel (i18n ("Preview of rendered R Markdown"));
 
 		command = "if (!nzchar(Sys.which(\"pandoc\"))) {\n"
 		           "	output <- rk.set.output.html.file(%2, silent=TRUE)\n"
@@ -845,17 +846,19 @@ void RKCommandEditorWindow::doRenderPreview () {
 		           "rk.show.html(%2)\n";
 		command = command.arg (RObject::rQuote (save.fileName ()), RObject::rQuote (save.fileName () + ".html"));
 	} else if (actionmenu_preview->currentItem () == RKOutputPreview) {
+		preview->setLabel (i18n ("Preview of generated RKWard output"));
 		QString output_file = preview_dir->filePath ("output.html");
 		command = "output <- rk.set.output.html.file(%2, silent=TRUE)\n"
 		          "try(rk.flush.output(ask=FALSE, style=\"preview\", silent=TRUE))\n"
-		          "try(source(%1))\n"
+		          "try(source(%1, local=TRUE))\n"
 		          "rk.set.output.html.file(output, silent=TRUE)\n"
 		          "rk.show.html(%2)\n";
 		command = command.arg (RObject::rQuote (save.fileName ()), RObject::rQuote (output_file));
 	} else if (actionmenu_preview->currentItem () == GraphPreview) {
+		preview->setLabel (i18n ("Preview of generated plot"));
 		command = "olddev <- dev.cur()\n"
 		          ".rk.startPreviewDevice(%2)\n"
-		          "try(source(%1))\n"
+		          "try(source(%1, local=TRUE))\n"
 		          "if (olddev != 1) dev.set(olddev)\n";
 		command = command.arg (RObject::rQuote (save.fileName ()), RObject::rQuote (preview_manager->previewId ()));
 	} else {
@@ -865,19 +868,18 @@ void RKCommandEditorWindow::doRenderPreview () {
 	preview->wrapperWidget ()->show ();
 
 	if (actionmenu_preview->currentItem () == ConsolePreview) {  // somewhat hacky, I admit...
+		preview->setLabel (i18n ("Preview of script running in interactive R Console"));
 		QString output_file = RObject::rQuote (preview_dir->filePath ("output.html"));
 		RKGlobals::rInterface ()->issueCommand (QString (
-		                                        ".rk.with.window.hints ({\n"
-		                                        "   rk.assign.preview.data(%1, rk.set.output.html.file(%2, silent=TRUE))\n"
-		                                        "   rk.flush.output(ask=FALSE, style=\"preview\", silent=TRUE)\n"
-		                                        "}, \"\", %1, style=\"preview\")\n").arg (RObject::rQuote (preview_manager->previewId ()), output_file), RCommand::App);
+		                                        "rk.assign.preview.data(%1, rk.set.output.html.file(%2, silent=TRUE))\n"
+		                                        "rk.flush.output(ask=FALSE, style=\"preview\", silent=TRUE)\n").arg (RObject::rQuote (preview_manager->previewId ()), output_file), RCommand::App | RCommand::Sync);
 
-		RCommand *rcommand = new RCommand (m_doc->text(), RCommand::User | RCommand::CCCommand | RCommand::CCOutput);
+		RCommand *rcommand = new RCommand (QString ("local({\n%1\n})").arg (m_doc->text()), RCommand::User | RCommand::CCCommand | RCommand::CCOutput);
 		preview_manager->setCommand (rcommand);
 		RKGlobals::rInterface ()->issueCommand (QString ("rk.set.output.html.file(rk.get.preview.data(%1), silent=TRUE)\n"
 		                                        ".rk.with.window.hints ({\n"
 		                                        "	rk.show.html(%2)\n"
-		                                        "}, \"\", %1, style=\"preview\")\n").arg (RObject::rQuote (preview_manager->previewId ()), output_file), RCommand::App);
+		                                        "}, \"\", %1, style=\"preview\")\n").arg (RObject::rQuote (preview_manager->previewId ()), output_file), RCommand::App | RCommand::Sync);
 	} else {
 		RCommand *rcommand = new RCommand (".rk.with.window.hints (local ({\n" + command + QStringLiteral ("}), \"\", ") + RObject::rQuote (preview_manager->previewId ()) + ", style=\"preview\")", RCommand::App);
 		preview_manager->setCommand (rcommand);



More information about the rkward-tracker mailing list