[rkward] rkward/windows: WIP: Add .Rmd render preview. This is just a first test, both technically, and UI-wise.
Thomas Friedrichsmeier
null at kde.org
Fri Sep 28 21:28:34 BST 2018
Git commit 0b8ccd54879cc64fd030db495f26de15e35f1840 by Thomas Friedrichsmeier.
Committed on 28/09/2018 at 20:16.
Pushed by tfry into branch 'master'.
WIP: Add .Rmd render preview. This is just a first test, both technically, and UI-wise.
M +33 -1 rkward/windows/rkcommandeditorwindow.cpp
M +9 -1 rkward/windows/rkcommandeditorwindow.h
M +3 -1 rkward/windows/rkcommandeditorwindowpart.rc
https://commits.kde.org/rkward/0b8ccd54879cc64fd030db495f26de15e35f1840
diff --git a/rkward/windows/rkcommandeditorwindow.cpp b/rkward/windows/rkcommandeditorwindow.cpp
index 437bca7f..94654428 100644
--- a/rkward/windows/rkcommandeditorwindow.cpp
+++ b/rkward/windows/rkcommandeditorwindow.cpp
@@ -36,6 +36,7 @@
#include <QAction>
#include <QTemporaryFile>
#include <QDir>
+#include <QSplitter>
#include <KLocalizedString>
#include <kmessagebox.h>
@@ -52,6 +53,7 @@
#include "../misc/rkstandardactions.h"
#include "../misc/rkxmlguisyncer.h"
#include "../misc/rkjobsequence.h"
+#include "../misc/rkxmlguipreviewarea.h"
#include "../core/robjectlist.h"
#include "../rbackend/rkrinterface.h"
#include "../settings/rksettings.h"
@@ -172,7 +174,9 @@ 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 (this);
m_view = m_doc->createView (this);
+ RKWorkplace::mainWorkplace()->registerNamedWindow (QString ().sprintf ("%p", this).remove ('%'), this, preview);
if (!url.isEmpty ()) {
KConfigGroup viewconf (RKWorkplace::mainWorkplace ()->workspaceConfig (), QString ("SkriptViewSettings %1").arg (RKWorkplace::mainWorkplace ()->portableUrl (url)));
m_view->readSessionConfig (viewconf);
@@ -192,7 +196,10 @@ RKCommandEditorWindow::RKCommandEditorWindow (QWidget *parent, const QUrl _url,
QHBoxLayout *layout = new QHBoxLayout (this);
layout->setContentsMargins (0, 0, 0, 0);
- layout->addWidget(m_view);
+ preview_splitter = new QSplitter (this);
+ preview_splitter->addWidget (preview);
+ preview_splitter->addWidget (m_view);
+ layout->addWidget(preview_splitter);
connect (m_doc, &KTextEditor::Document::documentUrlChanged, this, &RKCommandEditorWindow::updateCaption);
connect (m_doc, &KTextEditor::Document::modifiedChanged, this, &RKCommandEditorWindow::updateCaption); // of course most of the time this causes a redundant call to updateCaption. Not if a modification is undone, however.
@@ -320,6 +327,10 @@ void RKCommandEditorWindow::initializeActions (KActionCollection* ac) {
action_setwd_to_script->setToolTip (action_setwd_to_script->statusTip ());
action_setwd_to_script->setIcon (RKStandardIcons::getIcon (RKStandardIcons::ActionCDToScript));
+ action_render_preview = ac->addAction ("render_preview", this, SLOT (renderPreview()));
+ action_render_preview->setText ("Render Preview");
+ action_render_preview->setCheckable (true);
+
file_save = findAction (m_view, "file_save");
if (file_save) file_save->setText (i18n ("Save Script..."));
file_save_as = findAction (m_view, "file_save_as");
@@ -764,6 +775,27 @@ void RKCommandEditorWindow::copyLinesToOutput () {
RKCommandHighlighter::copyLinesToOutput (m_view, RKCommandHighlighter::RScript);
}
+void RKCommandEditorWindow::renderPreview () {
+ RK_TRACE (COMMANDEDITOR);
+
+ QString id = QString ().sprintf ("%p", this).remove ('%');
+ QTemporaryFile save (QDir::tempPath () + QStringLiteral ("/rkward_XXXXXX") + id + QStringLiteral (".Rmd"));
+ RK_ASSERT (save.open ());
+ QTextStream out (&save);
+ out.setCodec ("UTF-8"); // make sure that all characters can be saved, without nagging the user
+ out << m_doc->text ();
+ save.close ();
+ save.setAutoRemove (false);
+
+ QString command ("require(knitr)\n"
+ "require(markdown)\n"
+ "rk.show.html (knitr::knit2html(%1))");
+ command = command.arg (RObject::rQuote (save.fileName ()));
+
+ RKGlobals::rInterface ()->issueCommand (".rk.with.window.hints ({\n" + command + QStringLiteral ("}, \"\", ") + RObject::rQuote (id) + ')', RCommand::App);
+ preview->show ();
+}
+
void RKCommandEditorWindow::runAll () {
RK_TRACE (COMMANDEDITOR);
diff --git a/rkward/windows/rkcommandeditorwindow.h b/rkward/windows/rkcommandeditorwindow.h
index 187075bc..4869eccf 100644
--- a/rkward/windows/rkcommandeditorwindow.h
+++ b/rkward/windows/rkcommandeditorwindow.h
@@ -2,7 +2,7 @@
rkcommandeditorwindow - description
-------------------
begin : Mon Aug 30 2004
- copyright : (C) 2004-2017 by Thomas Friedrichsmeier
+ copyright : (C) 2004-2018 by Thomas Friedrichsmeier
email : thomas.friedrichsmeier at kdemail.net
***************************************************************************/
@@ -127,6 +127,8 @@ private:
class QTimer;
class RKJobSequence;
+class QSplitter;
+class RKXMLGUIPreviewArea;
/**
\brief Provides an editor window for R-commands, as well as a text-editor window in general.
@@ -188,6 +190,8 @@ public slots:
void runCurrent ();
/** run the entire script */
void runAll ();
+/** Render the (.Rmd) current script */
+ void renderPreview ();
/** insert line break and run the (previous) line */
void enterAndSubmit ();
void copyLinesToOutput ();
@@ -258,6 +262,7 @@ private:
QAction* action_run_all;
QAction* action_run_current;
+ QAction* action_render_preview;
QAction* action_setwd_to_script;
@@ -268,6 +273,9 @@ private:
QString _id;
static QMap<QString, KTextEditor::Document*> unnamed_documents;
+
+ QSplitter *preview_splitter;
+ RKXMLGUIPreviewArea *preview;
};
/** Simple class to provide HTML highlighting for arbitrary R code. */
diff --git a/rkward/windows/rkcommandeditorwindowpart.rc b/rkward/windows/rkcommandeditorwindowpart.rc
index f85f6244..eaa18ca9 100644
--- a/rkward/windows/rkcommandeditorwindowpart.rc
+++ b/rkward/windows/rkcommandeditorwindowpart.rc
@@ -1,5 +1,5 @@
<!DOCTYPE kpartgui>
-<kpartgui name="rkward_commandeditor" version="650">
+<kpartgui name="rkward_commandeditor" version="710">
<MenuBar>
<Menu name="file"><text>&File</text></Menu> <!-- Define placeholder to work around menu ordering bug -->
<Menu name="edit"><text>&Edit</text>
@@ -11,6 +11,7 @@
</Menu>
<Menu name="run"><text>&Run</text>
<Action name="run_block" group="postrun_actions_merge"/>
+ <Action name="render_preview" group="postrun_actions_merge"/>
<Separator group="postrun_actions_merge"/>
<Action name="setwd_to_script" group="postrun_actions_merge"/>
</Menu>
@@ -21,6 +22,7 @@
<ToolBar fullWidth="true" name="mainToolBar">
<Action name="run_block" group="posttoolbar_actions_merge"/>
+ <Action name="render_preview" group="posttoolbar_actions_merge"/>
<Action name="setwd_to_script" group="posttoolbar_actions_merge"/>
</ToolBar>
More information about the rkward-tracker
mailing list