[Rinside-commits] r191 - pkg/inst/examples/qt
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Mar 25 04:08:28 CET 2011
Author: edd
Date: 2011-03-25 04:08:27 +0100 (Fri, 25 Mar 2011)
New Revision: 191
Modified:
pkg/inst/examples/qt/qtdensity.cpp
pkg/inst/examples/qt/qtdensity.h
pkg/inst/examples/qt/qtdensity.pro
Log:
cairo and svg-ified
Modified: pkg/inst/examples/qt/qtdensity.cpp
===================================================================
--- pkg/inst/examples/qt/qtdensity.cpp 2011-03-24 18:43:02 UTC (rev 190)
+++ pkg/inst/examples/qt/qtdensity.cpp 2011-03-25 03:08:27 UTC (rev 191)
@@ -6,7 +6,6 @@
// Copyright (C) 2011 Dirk Eddelbuettel and Romain Francois
#include <QtGui>
-//#include <QSvgWidget>
#include "qtdensity.h"
@@ -18,7 +17,8 @@
m_R["bw"] = m_bw; // pass bandwidth to R, and have R compute a temp.file name
m_tempfile = Rcpp::as<std::string>(m_R.parseEval("tfile <- tempfile()"));
- //m_R.parseEvalQ("library(cairoDevice)");
+ m_svgfile = Rcpp::as<std::string>(m_R.parseEval("sfile <- tempfile()"));
+ m_R.parseEvalQ("library(cairoDevice)");
setupDisplay();
}
@@ -67,14 +67,14 @@
kernelGroup->addButton(radio5, 4);
QObject::connect(kernelGroup, SIGNAL(buttonClicked(int)), this, SLOT(getKernel(int)));
- imageLabel = new QLabel;
- imageLabel->setBackgroundRole(QPalette::Base);
- imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
- imageLabel->setScaledContents(true);
+ //imageLabel = new QLabel;
+ //imageLabel->setBackgroundRole(QPalette::Base);
+ //imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
+ //imageLabel->setScaledContents(true);
- image = new QImage(QString(m_tempfile.c_str()));
- runRandomDataCmd();
- plot(); // now that we have an image, we can diplay
+ //image = new QImage(QString(m_tempfile.c_str()));
+ svg = new QSvgWidget();
+ runRandomDataCmd(); // also calls plot()
QGroupBox *estimationBox = new QGroupBox("Density estimation bandwidth (scaled by 100)");
QHBoxLayout *spinners = new QHBoxLayout;
@@ -92,17 +92,16 @@
upperlayout->addWidget(kernelRadioBox);
upperlayout->addWidget(estimationBox);
- QHBoxLayout *lowerlayout = new QHBoxLayout;
- lowerlayout->addWidget(imageLabel);
+ //QHBoxLayout *lowerlayout = new QHBoxLayout;
+ //lowerlayout->addWidget(imageLabel);
- //QSvgWidget *svg = new QSvgWidget("/tmp/foo.svg");
- //QHBoxLayout *svglayout = new QHBoxLayout;
- //svglayout->addWidget(svg);
+ QHBoxLayout *svglayout = new QHBoxLayout;
+ svglayout->addWidget(svg);
QVBoxLayout *outer = new QVBoxLayout;
outer->addLayout(upperlayout);
- outer->addLayout(lowerlayout);
- //outer->addLayout(svglayout);
+ //outer->addLayout(lowerlayout);
+ outer->addLayout(svglayout);
window->setLayout(outer);
window->show();
window->resize(625, 725);
@@ -118,13 +117,15 @@
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 = "png(tfile,600,600); plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
- //std::string cmd1 = "Cairo_svg(tfile,6,6); plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
+ //std::string cmd1 = "png(tfile,600,600); plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
+ std::string cmd1 = "Cairo_svg(tfile,6,6); 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;
m_R.parseEvalQ(cmd);
- image->load(QString(m_tempfile.c_str()));
- imageLabel->setPixmap(QPixmap::fromImage(*image));
+ filterFile();
+ //image->load(QString(m_tempfile.c_str()));
+ //imageLabel->setPixmap(QPixmap::fromImage(*image));
+ svg->load(QString(m_svgfile.c_str()));
}
void QtDensity::getBandwidth(int bw) {
@@ -154,3 +155,26 @@
m_R.parseEvalQ(cmd);
plot();
}
+
+void QtDensity::filterFile() {
+ // cairoDevice creates richer SVG than Qt can display
+ // but per Michaele Lawrence, a simple trick is to s/symbol/g/ which we do here
+
+ QFile infile(m_tempfile.c_str());
+ infile.open(QFile::ReadOnly);
+ QFile outfile(m_svgfile.c_str());
+ outfile.open(QFile::WriteOnly | QFile::Truncate);
+
+ QTextStream in(&infile);
+ QTextStream out(&outfile);
+ QRegExp rx1("<symbol");
+ QRegExp rx2("</symbol");
+ while (!in.atEnd()) {
+ QString line = in.readLine();
+ line.replace(rx1, "<g");
+ line.replace(rx2, "</g");
+ out << line << "\n";
+ }
+ infile.close();
+ outfile.close();
+}
Modified: pkg/inst/examples/qt/qtdensity.h
===================================================================
--- pkg/inst/examples/qt/qtdensity.h 2011-03-24 18:43:02 UTC (rev 190)
+++ pkg/inst/examples/qt/qtdensity.h 2011-03-25 03:08:27 UTC (rev 191)
@@ -15,6 +15,8 @@
#include <QSlider>
#include <QSpinBox>
#include <QLabel>
+#include <QTemporaryFile>
+#include <QSvgWidget>
class QtDensity : public QMainWindow
{
@@ -33,12 +35,15 @@
private:
void setupDisplay(void); // standard GUI boilderplate of arranging things
void plot(void); // run a density plot in R and update the
+ void filterFile(void); // modify the richer SVG produced by R
- QLabel *imageLabel;
- QImage *image;
+ //QLabel *imageLabel;
+ //QImage *image;
+ QSvgWidget *svg;
RInside & m_R; // reference to the R instance passed to constructor
std::string m_tempfile; // name of file used by R for plots
+ std::string m_svgfile; // another temp file, this time from Qt
int m_bw, m_kernel; // parameters used to estimate the density
std::string m_cmd; // random draw command string
};
Modified: pkg/inst/examples/qt/qtdensity.pro
===================================================================
--- pkg/inst/examples/qt/qtdensity.pro 2011-03-24 18:43:02 UTC (rev 190)
+++ pkg/inst/examples/qt/qtdensity.pro 2011-03-25 03:08:27 UTC (rev 191)
@@ -9,7 +9,7 @@
HEADERS = qtdensity.h
SOURCES = qtdensity.cpp main.cpp
-#QT += svg
+QT += svg
## comment this out if you need a different version of R,
## and set set R_HOME accordingly as an environment variable
More information about the Rinside-commits
mailing list