[Rinside-commits] r198 - in pkg: . inst/examples/qt/qtdensitySVG inst/examples/standard

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Apr 2 05:15:50 CEST 2011


Author: edd
Date: 2011-04-02 05:15:49 +0200 (Sat, 02 Apr 2011)
New Revision: 198

Modified:
   pkg/ChangeLog
   pkg/inst/examples/qt/qtdensitySVG/qtdensity.cpp
   pkg/inst/examples/qt/qtdensitySVG/qtdensity.h
   pkg/inst/examples/standard/rinside_sample9.cpp
Log:
merged the SVG and PNG versions, will fold two subdirs into one later


Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog	2011-03-25 15:50:26 UTC (rev 197)
+++ pkg/ChangeLog	2011-04-02 03:15:49 UTC (rev 198)
@@ -1,7 +1,11 @@
+2011-04-01  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/examples/qt/qtdensity.cpp: Merged PNG and SVG version
+
 2011-03-16  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/examples/qt/qtdensity.h: Small rearrangements and
-	simplifications 
+	simplifications
 
 2011-03-15  Dirk Eddelbuettel  <edd at debian.org>
 

Modified: pkg/inst/examples/qt/qtdensitySVG/qtdensity.cpp
===================================================================
--- pkg/inst/examples/qt/qtdensitySVG/qtdensity.cpp	2011-03-25 15:50:26 UTC (rev 197)
+++ pkg/inst/examples/qt/qtdensitySVG/qtdensity.cpp	2011-04-02 03:15:49 UTC (rev 198)
@@ -17,8 +17,9 @@
     m_R["bw"] = m_bw;		// pass bandwidth to R, and have R compute a temp.file name
     m_tempfile = QString::fromStdString(Rcpp::as<std::string>(m_R.parseEval("tfile <- tempfile()")));
     m_svgfile = QString::fromStdString(Rcpp::as<std::string>(m_R.parseEval("sfile <- tempfile()")));
-    m_R.parseEvalQ("library(cairoDevice)");
-
+    m_has_svg = false; //m_R.parseEval("require(cairoDevice)");
+    if ( ! m_has_svg)
+	std::cerr << "Consider installing the 'cairoDevice' package from CRAN for SVG graphics." << std::endl;
     setupDisplay();
 }
 
@@ -66,7 +67,15 @@
     kernelGroup->addButton(radio5, 4);
     QObject::connect(kernelGroup, SIGNAL(buttonClicked(int)), this, SLOT(getKernel(int)));
 
-    m_svg = new QSvgWidget();
+    if (m_has_svg) {		// svg case
+	m_svg = new QSvgWidget();
+    } else {			// png default
+	m_imageLabel = new QLabel;
+	m_imageLabel->setBackgroundRole(QPalette::Base);
+	m_imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
+	m_imageLabel->setScaledContents(true);
+	m_image = new QImage(m_tempfile);
+    }
     runRandomDataCmd();	   	// also calls plot()
 
     QGroupBox *estimationBox = new QGroupBox("Density estimation bandwidth (scaled by 100)");
@@ -85,27 +94,44 @@
     upperlayout->addWidget(kernelRadioBox);
     upperlayout->addWidget(estimationBox);
 
-    QHBoxLayout *svglayout = new QHBoxLayout;
-    svglayout->addWidget(m_svg);
+    QHBoxLayout *lowerlayout = new QHBoxLayout;
+    if (m_has_svg) {
+	lowerlayout->addWidget(m_svg);
+    } else {
+	lowerlayout->addWidget(m_imageLabel);
+    }
 
     QVBoxLayout *outer = new QVBoxLayout;
     outer->addLayout(upperlayout);
-    outer->addLayout(svglayout);
+    outer->addLayout(lowerlayout);
     window->setLayout(outer);
     window->show();
+    if (!m_has_svg) {
+	window->resize(650, 750);
+    }
 }
 
 void QtDensity::plot(void) {
     const char *kernelstrings[] = { "gaussian", "epanechnikov", "rectangular", "triangular", "cosine" };
     m_R["bw"] = m_bw;
     m_R["kernel"] = kernelstrings[m_kernel]; // that passes the string to R
-    std::string cmd1 = "Cairo(width=6,height=6,pointsize=10,surface='svg',filename=tfile); "
-                       "plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
+    std::string cmd0;
+    if (m_has_svg) {	     // select device based on whether cairoDevice is available or not
+	cmd0 = "Cairo(width=6,height=6,pointsize=10,surface='svg',filename=tfile); ";
+    } else {
+	cmd0 = "png(filename=tfile,width=600,height=600);";
+    }
+    std::string cmd1 = "plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
     std::string cmd2 = "\"); points(y, rep(0, length(y)), pch=16, col=rgb(0,0,0,1/4));  dev.off()";
-    std::string cmd = cmd1 + kernelstrings[m_kernel] + cmd2; // stick the selected kernel in the middle
+    std::string cmd = cmd0 + cmd1 + kernelstrings[m_kernel] + cmd2; // stick the selected kernel in the middle
     m_R.parseEvalQ(cmd);
-    filterFile();		// we need to simplify the svg file for display by Qt 
-    m_svg->load(m_svgfile);
+    if (m_has_svg) {
+	filterFile();		// we need to simplify the svg file for display by Qt 
+	m_svg->load(m_svgfile);
+    } else {
+	m_image->load(m_tempfile);
+	m_imageLabel->setPixmap(QPixmap::fromImage(*m_image));
+    }
 }
 
 void QtDensity::getBandwidth(int bw) {

Modified: pkg/inst/examples/qt/qtdensitySVG/qtdensity.h
===================================================================
--- pkg/inst/examples/qt/qtdensitySVG/qtdensity.h	2011-03-25 15:50:26 UTC (rev 197)
+++ pkg/inst/examples/qt/qtdensitySVG/qtdensity.h	2011-04-02 03:15:49 UTC (rev 198)
@@ -38,11 +38,15 @@
 
     QSvgWidget *m_svg;		// the SVG device
 
+    QLabel *m_imageLabel;	// image display in non-SVG case
+    QImage *m_image;
+
     RInside & m_R;		// reference to the R instance passed to constructor
     QString m_tempfile;		// name of file used by R for plots
     QString m_svgfile;		// another temp file, this time from Qt
     int m_bw, m_kernel;		// parameters used to estimate the density
     QString m_cmd;		// random draw command string
+    bool m_has_svg;		// create SVG if cairoDevice can be loaded
 };
 
 #endif

Modified: pkg/inst/examples/standard/rinside_sample9.cpp
===================================================================
--- pkg/inst/examples/standard/rinside_sample9.cpp	2011-03-25 15:50:26 UTC (rev 197)
+++ pkg/inst/examples/standard/rinside_sample9.cpp	2011-04-02 03:15:49 UTC (rev 198)
@@ -15,7 +15,7 @@
 
 int main(int argc, char *argv[]) {
 
-	// create an embedded R instance
+    // create an embedded R instance
     RInside R(argc, argv);               
 
     // expose the "hello" function in the global environment



More information about the Rinside-commits mailing list