[rkward/work/generalized_preview] rkward: Rename 'html' preview to 'output preview'. Strip output headers for preview. Suppress some plot warnings.
Thomas Friedrichsmeier
thomas.friedrichsmeier at ruhr-uni-bochum.de
Mon Jan 11 12:24:31 UTC 2016
Git commit d17a89a4720ecb1ea4ec1994d9d3ccb1f3b0a887 by Thomas Friedrichsmeier.
Committed on 11/01/2016 at 12:24.
Pushed by tfry into branch 'work/generalized_preview'.
Rename 'html' preview to 'output preview'. Strip output headers for preview. Suppress some plot warnings.
M +12 -11 rkward/plugin/rkpreviewbox.cpp
M +2 -2 rkward/plugin/rkpreviewbox.h
M +1 -1 rkward/plugins/testing/preview.xml
M +2 -1 rkward/rbackend/rpackages/rkward/R/internal_graphics.R
M +71 -64 rkward/rbackend/rpackages/rkward/R/rk.filename-functions.R
http://commits.kde.org/rkward/d17a89a4720ecb1ea4ec1994d9d3ccb1f3b0a887
diff --git a/rkward/plugin/rkpreviewbox.cpp b/rkward/plugin/rkpreviewbox.cpp
index 467541c..7ffa5bb 100644
--- a/rkward/plugin/rkpreviewbox.cpp
+++ b/rkward/plugin/rkpreviewbox.cpp
@@ -44,7 +44,7 @@ RKPreviewBox::RKPreviewBox (const QDomElement &element, RKComponent *parent_comp
// get xml-helper
XMLHelper *xml = parent_component->xmlHelper ();
- preview_mode = (PreviewMode) xml->getMultiChoiceAttribute (element, "mode", "plot;data;html;custom", 0, DL_INFO);
+ preview_mode = (PreviewMode) xml->getMultiChoiceAttribute (element, "mode", "plot;data;output;custom", 0, DL_INFO);
placement = (PreviewPlacement) xml->getMultiChoiceAttribute (element, "placement", "default;attached;detached;docked", (preview_mode == PlotPreview) ? 0 : 3, DL_INFO);
preview_active = xml->getBoolAttribute (element, "active", false, DL_INFO);
idprop = RObject::rQuote (QString ().sprintf ("%p", this));
@@ -80,14 +80,14 @@ RKPreviewBox::RKPreviewBox (const QDomElement &element, RKComponent *parent_comp
RKWorkplace::mainWorkplace ()->registerNamedWindow (idprop, this, container);
uicomp->addDockedPreview (container, state, toggle_preview_box->text ());
- if (preview_mode == HtmlPreview) {
+ if (preview_mode == OutputPreview) {
RKGlobals::rInterface ()->issueCommand ("local ({\n"
"outfile <- tempfile (fileext='html')\n"
"rk.assign.preview.data(" + idprop + ", list (filename=outfile, on.delete=function (id) {\n"
" rk.flush.output (outfile, ask=FALSE)\n"
" unlink (outfile)\n"
"}))\n"
- "oldfile <- rk.set.output.html.file (outfile) # for initialization\n"
+ "oldfile <- rk.set.output.html.file (outfile, style='preview') # for initialization\n"
"rk.set.output.html.file (oldfile)\n"
"})\n" + placement_command + "rk.show.html(rk.get.preview.data (" + idprop + ")$filename)" + placement_end, RCommand::Plugin | RCommand::Sync);
} else {
@@ -197,10 +197,10 @@ void RKPreviewBox::tryPreviewNow () {
RKGlobals::rInterface ()->issueCommand ("local({\n" + code_property->preview () + "})\n", RCommand::Plugin | RCommand::Sync, QString (), this, DO_PREVIEW);
} else if (preview_mode == DataPreview) {
RKGlobals::rInterface ()->issueCommand ("local({try({\n" + code_property->preview () + "\n})\nif(!exists(\"preview_data\",inherits=FALSE)) preview_data <- data.frame ('ERROR')\nrk.assign.preview.data(" + idprop + ", preview_data)\n})\n" + placement_command + "rk.edit(rkward::.rk.variables$.rk.preview.data[[" + idprop + "]])" + placement_end, RCommand::Plugin | RCommand::Sync, QString (), this, DO_PREVIEW);
- } else if (preview_mode == HtmlPreview) {
+ } else if (preview_mode == OutputPreview) {
RKGlobals::rInterface ()->issueCommand (placement_command + "local({\n"
- " oldfile <- rk.set.output.html.file(rk.get.preview.data (" + idprop + ")$filename)\n"
- " rk.flush.output(ask=FALSE)\n"
+ " oldfile <- rk.set.output.html.file(rk.get.preview.data (" + idprop + ")$filename, style='preview')\n"
+ " rk.flush.output(ask=FALSE, style='preview')\n"
" local({try({\n" + code_property->preview () + "\n})})\n" // nested local to make sure "oldfile" is not overwritten.
" rk.set.output.html.file(oldfile)\n})\n"
"rk.show.html(rk.get.preview.data (" + idprop + ")$filename)" + placement_end, RCommand::Plugin | RCommand::Sync, QString (), this, DO_PREVIEW);
@@ -222,21 +222,22 @@ void RKPreviewBox::setStatusMessage(const QString& status) {
window->setStatusMessage (status);
}
-void RKPreviewBox::killPreview (bool force) {
+void RKPreviewBox::killPreview (bool cleanup) {
RK_TRACE (PLUGIN);
if (!preview_active) return;
preview_active = false;
- // hmm, lots of special casing, here. But the reasons are:
- // - docked preview are only hidden, really, and their window should not be destroyed
- // - for HTML previews, removing the file has no effect in the first place, other than making code more difficult...
- if (force || (placement != DockedPreview && preview_mode != HtmlPreview)) {
+ if (cleanup) {
QString command;
if (preview_mode == PlotPreview) command = ".rk.killPreviewDevice (" + idprop + ')';
else command = "rk.discard.preview.data (" + idprop + ')';
RKGlobals::rInterface ()->issueCommand (command, RCommand::Plugin | RCommand::Sync);
}
+ if (placement != DockedPreview) {
+ RKMDIWindow *window = RKWorkplace::mainWorkplace ()->getNamedWindow (idprop);
+ if (window) window->deleteLater ();
+ }
prior_preview_done = true;
new_preview_pending = false;
diff --git a/rkward/plugin/rkpreviewbox.h b/rkward/plugin/rkpreviewbox.h
index e5760f3..c5028c9 100644
--- a/rkward/plugin/rkpreviewbox.h
+++ b/rkward/plugin/rkpreviewbox.h
@@ -55,13 +55,13 @@ private:
bool prior_preview_done;
bool new_preview_pending;
void tryPreview ();
- void killPreview (bool force = false);
+ void killPreview (bool cleanup = false);
void updateStatusLabel ();
void setStatusMessage (const QString& status);
enum PreviewMode {
PlotPreview,
DataPreview,
- HtmlPreview,
+ OutputPreview,
CustomPreview
} preview_mode;
enum PreviewPlacement {
diff --git a/rkward/plugins/testing/preview.xml b/rkward/plugins/testing/preview.xml
index 6e76267..6fbcdf4 100644
--- a/rkward/plugins/testing/preview.xml
+++ b/rkward/plugins/testing/preview.xml
@@ -7,7 +7,7 @@
<spinbox id="size" type="integer" min="0" label="Size" initial="3"/>
<spinbox id="value" type="real" min="0" label="Value" initial="1.23"/>
<!-- <preview mode="data" label="Preview data"/> -->
- <preview mode="html" label="Preview data"/>
+ <preview mode="output" label="Preview data"/>
</dialog>
<wizard label="Testing various preview features">
<page>
diff --git a/rkward/rbackend/rpackages/rkward/R/internal_graphics.R b/rkward/rbackend/rpackages/rkward/R/internal_graphics.R
index ec10fe4..ffcee92 100644
--- a/rkward/rbackend/rpackages/rkward/R/internal_graphics.R
+++ b/rkward/rbackend/rpackages/rkward/R/internal_graphics.R
@@ -15,7 +15,8 @@
# Fetch the current size of the given RK() device from the frontend, and redraw
"RK.resize" <- function (devnum) {
- .Call ("rk.graphics.device.resize", as.integer (devnum)-1, PACKAGE="(embedding)")
+ # Note: RK.resize() often fails, if something is currently being plotted. That's usually benign, and should not produce warning messages.
+ suppressMessages (try (.Call ("rk.graphics.device.resize", as.integer (devnum)-1, PACKAGE="(embedding)")))
}
#' @include internal.R
diff --git a/rkward/rbackend/rpackages/rkward/R/rk.filename-functions.R b/rkward/rbackend/rpackages/rkward/R/rk.filename-functions.R
index 8cce2c7..d62cb5d 100644
--- a/rkward/rbackend/rpackages/rkward/R/rk.filename-functions.R
+++ b/rkward/rbackend/rpackages/rkward/R/rk.filename-functions.R
@@ -29,8 +29,10 @@
#' @param additional.header.contents NULL or an additional string to add to the HTML header section.
#' This could be scripts or additional CSS definitions, for example. Note that
#' \emph{nothing} will be added to the header, if the file already exists.
+#' @param style Currently either "regular" or "preview". The latter omits table of contents and date.
#' @param flush.images. If true, any images used in the output file will be deleted as well.
#' @param ask Logical: Whether to ask before flushing the output file.
+#' @param ... Further parameters passed to rk.set.output.html.file()
#' @return \code{rk.get.tempfile.name}, \code{rk.get.output.html.file}, and
#' \code{rk.get.workspace.url} return a string while
#' \code{rk.set.output.html.file} returns the \bold{previous} output html file.
@@ -77,8 +79,9 @@
#' @export
#' @rdname rk.get.tempfile.name
-"rk.set.output.html.file" <- function (x, additional.header.contents = getOption ("rk.html.header.additions")) {
+"rk.set.output.html.file" <- function (x, additional.header.contents = getOption ("rk.html.header.additions"), style=c ("regular", "preview")) {
stopifnot (is.character (x))
+ style <- match.arg (style)
oldfile <- rk.get.output.html.file ()
assign (".rk.output.html.file", x, .rk.variables)
@@ -86,76 +89,80 @@
.rk.cat.output (paste ("<?xml version=\"1.0\" encoding=\"", .Call ("rk.locale.name", PACKAGE="(embedding)"), "\"?>\n", sep=""))
.rk.cat.output (paste ("<html><head>\n<title>RKWard Output</title>\n", .rk.do.plain.call ("getCSSlink"), sep=""))
# the next part defines a JavaScript function to add individual results to a global table of contents menu in the document
- .rk.cat.output (paste ("\t<script type=\"text/javascript\">
- <!--
- function addToTOC(id, level){
- var fullHeader = document.getElementById(id);
- var resultsTOC = document.getElementById('RKWardResultsTOCShown');
- var headerName = fullHeader.getAttribute('name');
- var headerText = fullHeader.firstChild.data;
- var headerTitle = fullHeader.getAttribute('title');
- var newDiv = document.createElement('div');
- // create new anchor for TOC
- var newAnchor = '<a href=\"#' + headerName + '\" title=\"' + headerTitle + '\"';
- // indent anchor depending on header level
- if(level > 1){
- newDiv.style.textIndent = level-1 + 'em';
- newDiv.className = 'level' + level;
- newAnchor = '• ' + newAnchor + '>' + headerText + '</a>';
- } else {
- newAnchor = newAnchor + '>' + headerText + '</a>';
+ if (style != "preview") {
+ .rk.cat.output (paste ("\t<script type=\"text/javascript\">
+ <!--
+ function addToTOC(id, level){
+ var fullHeader = document.getElementById(id);
+ var resultsTOC = document.getElementById('RKWardResultsTOCShown');
+ var headerName = fullHeader.getAttribute('name');
+ var headerText = fullHeader.firstChild.data;
+ var headerTitle = fullHeader.getAttribute('title');
+ var newDiv = document.createElement('div');
+ // create new anchor for TOC
+ var newAnchor = '<a href=\"#' + headerName + '\" title=\"' + headerTitle + '\"';
+ // indent anchor depending on header level
+ if(level > 1){
+ newDiv.style.textIndent = level-1 + 'em';
+ newDiv.className = 'level' + level;
+ newAnchor = '• ' + newAnchor + '>' + headerText + '</a>';
+ } else {
+ newAnchor = newAnchor + '>' + headerText + '</a>';
+ }
+ newDiv.innerHTML = newAnchor;
+ resultsTOC.appendChild(newDiv);
}
- newDiv.innerHTML = newAnchor;
- resultsTOC.appendChild(newDiv);
- }
- function switchVisible(show, hide) {
- document.getElementById(show).style.display = 'inline';
- document.getElementById(hide).style.display = 'none';
- }
- function showMLevel(nodes){
- for(var i=0; i < nodes.length; i++) {
- nodes[i].style.display = 'block';
+ function switchVisible(show, hide) {
+ document.getElementById(show).style.display = 'inline';
+ document.getElementById(hide).style.display = 'none';
}
- }
- function hideMLevel(nodes){
- for(var i=0; i < nodes.length; i++) {
- nodes[i].style.display = 'none';
+ function showMLevel(nodes){
+ for(var i=0; i < nodes.length; i++) {
+ nodes[i].style.display = 'block';
+ }
}
- }
- function maxLevel(level){
- if(level > 5){
- return false;
+ function hideMLevel(nodes){
+ for(var i=0; i < nodes.length; i++) {
+ nodes[i].style.display = 'none';
+ }
}
- for(var i=1; i < 6; i++) {
- if(i <= level){
- showMLevel(document.getElementsByClassName('level' + i));
- } else {
- hideMLevel(document.getElementsByClassName('level' + i));
+ function maxLevel(level){
+ if(level > 5){
+ return false;
+ }
+ for(var i=1; i < 6; i++) {
+ if(i <= level){
+ showMLevel(document.getElementsByClassName('level' + i));
+ } else {
+ hideMLevel(document.getElementsByClassName('level' + i));
+ }
}
}
- }
- // -->\n\t</script>\n", sep=""))
- # positioning of the TOC is done by CSS, default state is hidden
- # see $SRC/rkward/pages/rkward_output.css
+ // -->\n\t</script>\n", sep=""))
+ # positioning of the TOC is done by CSS, default state is hidden
+ # see $SRC/rkward/pages/rkward_output.css
+ }
if (!is.null (additional.header.contents)) .rk.cat.output (as.character (additional.header.contents))
.rk.cat.output ("</head>\n<body>\n")
- # This initial output mostly to indicate the output is really there, just empty for now
- .rk.cat.output (paste ("<a name=\"top\"></a>\n<pre>RKWard output initialized on", .rk.date (), "</pre>\n"))
- # an empty <div> where the TOC menu gets added to dynamically, and a second one to toggle show/hide
- .rk.cat.output (paste (
- "<div id=\"RKWardResultsTOCShown\" class=\"RKTOC\">\n",
- "\t<a onclick=\"javascript:switchVisible('RKWardResultsTOCHidden','RKWardResultsTOCShown'); return false;\" href=\"\" class=\"toggleTOC\">Hide TOC</a>\n",
- "\t<span class=\"right\"><a href=\"#top\" class=\"toggleTOC\">Go to top</a></span>\n<br />",
- "\t\t<span class=\"center\">\n\t\t\t<a onclick=\"javascript:maxLevel('1'); return false;\" href=\"\" title=\"TOC level 1\">1</a> •\n",
- "\t\t\t<a onclick=\"javascript:maxLevel('2'); return false;\" href=\"\" title=\"TOC level 2\">2</a> •\n",
- "\t\t\t<a onclick=\"javascript:maxLevel('3'); return false;\" href=\"\" title=\"TOC level 3\">3</a> •\n",
- "\t\t\t<a onclick=\"javascript:maxLevel('4'); return false;\" href=\"\" title=\"TOC level 4\">4</a>\n\t\t</span>\n",
- "\t<!-- the TOC menu goes here -->\n</div>\n",
- "<div id=\"RKWardResultsTOCHidden\" class=\"RKTOC RKTOChidden\">\n",
- "\t<a onclick=\"javascript:switchVisible('RKWardResultsTOCShown','RKWardResultsTOCHidden'); return false;\" href=\"\" class=\"toggleTOC\">Show TOC</a>\n",
- "\t<span class=\"right\"><a href=\"#top\" class=\"toggleTOC\">Go to top</a></span>\n",
- "</div>\n", sep=""))
+ if (style != "preview") {
+ # This initial output mostly to indicate the output is really there, just empty for now
+ .rk.cat.output (paste ("<a name=\"top\"></a>\n<pre>RKWard output initialized on", .rk.date (), "</pre>\n"))
+ # an empty <div> where the TOC menu gets added to dynamically, and a second one to toggle show/hide
+ .rk.cat.output (paste (
+ "<div id=\"RKWardResultsTOCShown\" class=\"RKTOC\">\n",
+ "\t<a onclick=\"javascript:switchVisible('RKWardResultsTOCHidden','RKWardResultsTOCShown'); return false;\" href=\"\" class=\"toggleTOC\">Hide TOC</a>\n",
+ "\t<span class=\"right\"><a href=\"#top\" class=\"toggleTOC\">Go to top</a></span>\n<br />",
+ "\t\t<span class=\"center\">\n\t\t\t<a onclick=\"javascript:maxLevel('1'); return false;\" href=\"\" title=\"TOC level 1\">1</a> •\n",
+ "\t\t\t<a onclick=\"javascript:maxLevel('2'); return false;\" href=\"\" title=\"TOC level 2\">2</a> •\n",
+ "\t\t\t<a onclick=\"javascript:maxLevel('3'); return false;\" href=\"\" title=\"TOC level 3\">3</a> •\n",
+ "\t\t\t<a onclick=\"javascript:maxLevel('4'); return false;\" href=\"\" title=\"TOC level 4\">4</a>\n\t\t</span>\n",
+ "\t<!-- the TOC menu goes here -->\n</div>\n",
+ "<div id=\"RKWardResultsTOCHidden\" class=\"RKTOC RKTOChidden\">\n",
+ "\t<a onclick=\"javascript:switchVisible('RKWardResultsTOCShown','RKWardResultsTOCHidden'); return false;\" href=\"\" class=\"toggleTOC\">Show TOC</a>\n",
+ "\t<span class=\"right\"><a href=\"#top\" class=\"toggleTOC\">Go to top</a></span>\n",
+ "</div>\n", sep=""))
+ }
}
# needs to come after initialization, so initialization alone does not trigger an update during startup
@@ -194,7 +201,7 @@
#' @export
#' @rdname rk.get.tempfile.name
-"rk.flush.output" <- function (x=rk.get.output.html.file (), flush.images=TRUE, ask=TRUE) {
+"rk.flush.output" <- function (x=rk.get.output.html.file (), flush.images=TRUE, ask=TRUE, ...) {
images <- character (0)
if (flush.images) images <- .rk.get.images.in.html.file (x)
@@ -214,5 +221,5 @@
}
)
- rk.set.output.html.file (x)
+ rk.set.output.html.file (x, ...)
}
More information about the rkward-tracker
mailing list