[rkward] rkward/windows: Some fine-tuning to script previews

Thomas Friedrichsmeier null at kde.org
Thu Oct 4 07:30:30 BST 2018


Git commit 233797f6deda2057e5c44de6e3a0b180d1a7497e by Thomas Friedrichsmeier.
Committed on 04/10/2018 at 06:29.
Pushed by tfry into branch 'master'.

Some fine-tuning to script previews

M  +18   -11   rkward/windows/rkcommandeditorwindow.cpp
M  +5    -4    rkward/windows/rkmdiwindow.cpp

https://commits.kde.org/rkward/233797f6deda2057e5c44de6e3a0b180d1a7497e

diff --git a/rkward/windows/rkcommandeditorwindow.cpp b/rkward/windows/rkcommandeditorwindow.cpp
index ef795b1b..00aa875c 100644
--- a/rkward/windows/rkcommandeditorwindow.cpp
+++ b/rkward/windows/rkcommandeditorwindow.cpp
@@ -832,7 +832,13 @@ void RKCommandEditorWindow::doRenderPreview () {
 	if (actionmenu_preview->currentItem () == NoPreview) return;
 	if (!preview_manager->needsCommand ()) return;
 
-	if (!preview_dir) preview_dir = new QTemporaryDir ();
+	if (!preview_dir) {
+		if (!preview->findChild<RKMDIWindow*>()) {
+			// (lazily) initialize the preview window with _something_, as an RKMDIWindow is needed to display messages (important, if there is an error during the first preview)
+			RKGlobals::rInterface()->issueCommand (".rk.with.window.hints (rk.show.html(\"\"), " + RObject::rQuote (preview_manager->previewId ()) + ", style=\"preview\")", RCommand::App | RCommand::Sync);
+		}
+		preview_dir = new QTemporaryDir ();
+	}
 	QFile save (QDir (preview_dir->path()).absoluteFilePath ("script.R"));
 	if (actionmenu_preview->currentItem () == RMarkdownPreview) save.setFileName (QDir (preview_dir->path()).absoluteFilePath ("markdownscript.Rmd"));
 	RK_ASSERT (save.open (QIODevice::WriteOnly));
@@ -841,8 +847,6 @@ void RKCommandEditorWindow::doRenderPreview () {
 	out << m_doc->text ();
 	save.close ();
 
-	// TODO: Make sure to initialze the preview region with some window, so it will be able to display status messages at all!
-
 	QString command;
 	if (actionmenu_preview->currentItem () == RMarkdownPreview) {
 		preview->setLabel (i18n ("Preview of rendered R Markdown"));
@@ -883,17 +887,20 @@ void RKCommandEditorWindow::doRenderPreview () {
 		command = "output <- rk.set.output.html.file(%2, silent=TRUE)\n"
 		          "on.exit(rk.set.output.html.file(output, silent=TRUE))\n"
 		          "try(rk.flush.output(ask=FALSE, style=\"preview\", silent=TRUE))\n"
+		          "exprs <- expression(NULL)\n"
+		          "rk.capture.output(suppress.messages=TRUE)\n"
 		          "try({\n"
 		          "    exprs <- parse (%1, keep.source=TRUE)\n"
-		          "    for (i in 1:length (exprs)) {\n"
-		          "        rk.print.code(as.character(attr(exprs, \"srcref\")[[i]]))\n"
-		          "        rk.capture.output()\n"
-		          "        try({\n"
-		          "            withAutoprint(exprs[[i]], evaluated=TRUE, echo=FALSE)\n"
-			  "        })\n"
-		          "        .rk.cat.output(rk.end.capture.output(TRUE))\n"
-		          "    }\n"
 		          "})\n"
+		          ".rk.cat.output(rk.end.capture.output(TRUE))\n"
+		          "for (i in 1:length (exprs)) {\n"
+		          "    rk.print.code(as.character(attr(exprs, \"srcref\")[[i]]))\n"
+		          "    rk.capture.output(suppress.messages=TRUE)\n"
+		          "    try({\n"
+		          "        withAutoprint(exprs[[i]], evaluated=TRUE, echo=FALSE)\n"
+		          "    })\n"
+		          "    .rk.cat.output(rk.end.capture.output(TRUE))\n"
+		          "}\n"
 		          "rk.set.output.html.file(output, silent=TRUE)\n"
 		          "rk.show.html(%2)\n";
 		command = command.arg (RObject::rQuote (save.fileName ())).arg (output_file);
diff --git a/rkward/windows/rkmdiwindow.cpp b/rkward/windows/rkmdiwindow.cpp
index 9ed49dd2..9a3803ae 100644
--- a/rkward/windows/rkmdiwindow.cpp
+++ b/rkward/windows/rkmdiwindow.cpp
@@ -344,17 +344,18 @@ void RKMDIWindow::setStatusMessage (const QString& message, RCommand *command) {
 		status_popup->setMessageType (KMessageWidget::Warning);
 		layout->addWidget (status_popup);
 		layout->addStretch ();
+
+		// when animation is finished, squeeze the popup-container, so as not to interfere with mouse events in the main window
+		connect (status_popup, &KMessageWidget::showAnimationFinished, [this]() { status_popup_container->resize (QSize(width(), status_popup->height () + 20)); });
+		connect (status_popup, &KMessageWidget::hideAnimationFinished, status_popup_container, &QWidget::hide);
 	}
 
 	if (command) connect (command->notifier (), &RCommandNotifier::commandFinished, this, &RKMDIWindow::clearStatusMessage);
 	if (!message.isEmpty ()) {
 		status_popup_container->resize (size ());
 		status_popup_container->show ();
-		// when animation is finished, squeeze the popup-container, so as not to interfere with mouse events in the main window
-		connect (status_popup, &KMessageWidget::showAnimationFinished, [this]() { status_popup_container->resize (QSize(width(), status_popup->height () + 20)); });
-		connect (status_popup, &KMessageWidget::hideAnimationFinished, status_popup_container, &QWidget::hide);
 		if (status_popup->text () == message) {
-			if (!status_popup->isVisible ()) status_popup->animatedShow ();  // it might have been close by user. And no, simply show() is _not_ good enough. KF5 (5.15.0)
+			if (!status_popup->isVisible ()) status_popup->animatedShow ();  // it might have been closed by user. And no, simply show() is _not_ good enough. KF5 (5.15.0)
 		}
 		if (status_popup->text () != message) {
 			if (status_popup->isVisible ()) status_popup->hide (); // otherwise, the KMessageWidget does not update geometry (KF5, 5.15.0)



More information about the rkward-tracker mailing list