COPASI API  4.16.103
plotwindow.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2003 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 // the window containing the plot and buttons for supported operations
16 
17 #include <QtSvg/QtSvg>
18 
19 #include <QtGui/QToolBar>
20 #include <QtGui/QPrinter>
21 #include <QtGui/QPixmap>
22 #include <QtGui/QPicture>
23 #include <QtSvg/QSvgGenerator>
24 
25 #include "plotwindow.h"
26 #include "CopasiPlot.h"
28 #include "COutputHandlerPlot.h"
29 
30 #include "UI/copasiui3window.h"
31 #include "UI/CQMessageBox.h"
32 #include "UI/qtUtilities.h"
34 
35 #ifdef DEBUG_UI
36 #include <QtCore/QtDebug>
37 #endif
38 
39 #include <qwt_plot.h>
40 #include <qwt_scale_engine.h>
41 
42 // taken from qwt examples/bode
43 class PrintFilter: public QwtPlotPrintFilter
44 {
45 public:
47 
48  virtual QFont font(const QFont &f, Item, int) const
49  {
50  QFont f2 = f;
51  f2.setPointSize((int)(f.pointSize() * 0.75));
52  return f2;
53  }
54 };
55 
56 //-----------------------------------------------------------------------------
59  mpPlot(NULL),
60  mpHandler(pHandler),
61  mpMainWindow(pMainWindow),
62  mpWindowMenu(NULL),
63  mpaCloseWindow(NULL),
64  mpaShowAll(NULL),
65  mpaHideAll(NULL),
66  mpaPrint(NULL),
67  mpaSaveImage(NULL),
68  mpaSaveData(NULL),
69  mpaZoomOut(NULL),
70  mpaToggleLogX(NULL),
71  mpaToggleLogY(NULL),
72  initializing(false)
73 {
74  this->resize(640, 480);
75  this->setWindowTitle(("COPASI Plot: " + ptrSpec->getTitle()).c_str());
76 
77 #ifndef Darwin
79 #endif // not Darwin
80 
81  // set up the GUI - the toolbar
82  createActions();
83  createMenus();
84  createToolBar();
85 
86  mpPlot = new CopasiPlot(ptrSpec, this);
87  setCentralWidget(mpPlot);
88 
89  initializing = true;
90  mpaToggleLogX->setChecked(ptrSpec->isLogX());
91  mpaToggleLogY->setChecked(ptrSpec->isLogY());
92  initializing = false;
93 
95 }
96 
98 {
99  QMenu *fileMenu = menuBar()->addMenu("&File");
100  fileMenu->addAction(mpaSaveImage);
101  fileMenu->addAction(mpaSaveData);
102  fileMenu->addAction(mpaPrint);
103  fileMenu->addSeparator();
104  fileMenu->addAction(mpaCloseWindow);
105 
106  QMenu *viewMenu = menuBar()->addMenu("&View");
107  viewMenu->addAction(mpaShowAll);
108  viewMenu->addAction(mpaHideAll);
109  viewMenu->addSeparator();
110  viewMenu->addAction(mpaToggleLogX);
111  viewMenu->addAction(mpaToggleLogY);
112  viewMenu->addSeparator();
113  viewMenu->addAction(mpaZoomOut);
114 
115  // add a place holder menu, to be filled by the main window
116  mpWindowMenu = menuBar()->addMenu("&Window");
117 }
119 {
120  return mpWindowMenu;
121 }
122 
124 {
125  mpaToggleLogX = new QAction("Log &X", this);
126  mpaToggleLogX->setCheckable(true);
127  mpaToggleLogX->setToolTip("Toggle x-axis logscale.");
128  connect(mpaToggleLogX, SIGNAL(toggled(bool)), this, SLOT(toggleLogX(bool)));
129 
130  mpaToggleLogY = new QAction("Log &Y", this);
131  mpaToggleLogY->setCheckable(true);
132  mpaToggleLogY->setToolTip("Toggle y-axis logscale.");
133  connect(mpaToggleLogY, SIGNAL(toggled(bool)), this, SLOT(toggleLogY(bool)));
134 
135  mpaPrint = new QAction("Print", this);
136  mpaPrint ->setToolTip("Print Plot");
137  mpaPrint -> setShortcut(Qt::CTRL + Qt::Key_P);
138  connect(mpaPrint, SIGNAL(triggered()), this, SLOT(printPlot()));
139 
140  mpaSaveImage = new QAction("Save Image", this);
141  mpaSaveImage ->setShortcut(Qt::CTRL + Qt::Key_S);
142  mpaSaveImage ->setToolTip("Save Plot as Image");
143  connect(mpaSaveImage, SIGNAL(triggered()), this, SLOT(printAsImage()));
144 
145  mpaSaveData = new QAction("Save Data", this);
146  mpaSaveData ->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_S);
147  mpaSaveData ->setToolTip("Save Data");
148  connect(mpaSaveData, SIGNAL(triggered()), this, SLOT(slotSaveData()));
149 
150  mpaZoomOut = new QAction("Zoom out", this);
151  mpaZoomOut ->setShortcut(Qt::CTRL + Qt::Key_0);
152  mpaZoomOut ->setToolTip("Zoom out");
153  connect(mpaZoomOut, SIGNAL(triggered()), this, SLOT(slotZoomOut()));
154 
155  mpaShowAll = new QAction("Show All", this);
156  mpaShowAll ->setShortcut(Qt::CTRL + Qt::Key_A);
157  mpaShowAll ->setToolTip("Show all curves");
158  connect(mpaShowAll, SIGNAL(triggered()), this, SLOT(slotSelectAll()));
159 
160  mpaHideAll = new QAction("Hide All", this);
161  mpaHideAll ->setShortcut(Qt::CTRL + Qt::Key_D);
162  mpaHideAll ->setToolTip("Hide all curves");
163  connect(mpaHideAll, SIGNAL(triggered()), this, SLOT(slotDeselectAll()));
164 
165  mpaCloseWindow = new QAction("Close", this);
166  mpaCloseWindow->setObjectName("close");
167  mpaCloseWindow->setShortcut(Qt::CTRL + Qt::Key_W);
168  connect(mpaCloseWindow, SIGNAL(triggered()), this, SLOT(slotCloseWindow()));
169 }
170 
172 {
173  QToolBar * plotTools = addToolBar("plot operations");
174 
175  plotTools->addAction(mpaPrint);
176  plotTools->addAction(mpaSaveImage);
177  plotTools->addAction(mpaSaveData);
178  plotTools->addAction(mpaZoomOut);
179 
180  plotTools->addSeparator();
181  plotTools->addAction(mpaToggleLogX);
182  plotTools->addAction(mpaToggleLogY);
183 
184  plotTools->addSeparator();
185 
186  plotTools->addAction(mpaShowAll);
187  plotTools->addAction(mpaHideAll);
188 
189  plotTools->addSeparator();
190 
191  plotTools->addAction(mpaCloseWindow);
192 
193  //TODO button icons...
194 
195  setUnifiedTitleAndToolBarOnMac(true);
196 // plotTools->setStretchableWidget(new QWidget(plotTools));
197 }
198 
200 {
201  this->setWindowTitle(("COPASI Plot: " + ptrSpec->getTitle()).c_str());
202  bool result = mpPlot->initFromSpec(ptrSpec);
203 
204  if (result)
205  {
206  initializing = true;
207  mpaToggleLogX->setChecked(ptrSpec->isLogX());
208  mpaToggleLogY->setChecked(ptrSpec->isLogY());
209  initializing = false;
210  }
211 
212  return result;
213 }
214 
215 // toggle log X
216 void PlotWindow::toggleLogX(bool logX)
217 {
218  if (initializing) return;
219 
220  if (logX)
221  {
222  mpPlot->setAxisScaleEngine(QwtPlot::xBottom, new QwtLog10ScaleEngine());
223  }
224  else
225  {
226  mpPlot->setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine());
227  }
228 
229  mpPlot->setAxisAutoScale(QwtPlot::xBottom);
230  mpPlot->updateAxes();
231  mpPlot->replot();
232  mpPlot->update();
233 }
234 
235 // toggle log Y
236 void PlotWindow::toggleLogY(bool logY)
237 {
238  if (initializing) return;
239 
240  if (logY)
241  {
242  mpPlot->setAxisScaleEngine(QwtPlot::yLeft, new QwtLog10ScaleEngine());
243  }
244  else
245  {
246  mpPlot->setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine());
247  }
248 
249  mpPlot->setAxisAutoScale(QwtPlot::yLeft);
250  mpPlot->updateAxes();
251  mpPlot->replot();
252  mpPlot->update();
253 }
254 
255 //-----------------------------------------------------------------------------
256 
257 /*void PlotWindow::enableZoom()
258 {
259  zoomButton->setEnabled(false);
260  mpPlot->enableZoom(true);
261 }*/
262 
263 //-----------------------------------------------------------------------------
264 
265 /*void PlotWindow::mouseReleased(const QMouseEvent &e)
266 {
267  //TODO: if midbutton is clicked and we're zoomed out completely, zoomButton need to be enabled as well
268 
269  if (e.button() == RightButton)
270  zoomButton->setEnabled(true);
271 }*/
272 
273 //-----------------------------------------------------------------------------
274 void PlotWindow::saveToFile(const QString& fileName) const
275 {
276  QRect rect;
277  rect.setSize(this->size());
278 
279  if (fileName.endsWith(".png"))
280  {
281  QPixmap pixmap(rect.width(), rect.height());
282  pixmap.fill();
283  QPainter painter(&pixmap);
284  painter.begin(&pixmap);
285  mpPlot->print(&painter, rect, PrintFilter());
286  painter.end();
287 
288  pixmap.save(fileName, "PNG");
289  }
290  else if (fileName.endsWith(".svg"))
291  {
292  QSvgGenerator generator;
293  generator.setFileName(fileName);
294  QPainter painter(&generator);
295  painter.begin(&generator);
296  mpPlot->print(&painter, rect, PrintFilter());
297  painter.end();
298  }
299  else if (fileName.endsWith(".pdf"))
300  {
301  QPrinter printer;
302  printer.setOutputFileName(fileName);
303  printer.setOutputFormat(QPrinter::PdfFormat);
304  QPainter painter(&printer);
305  painter.begin(&printer);
306  mpPlot->print(&painter, rect, PrintFilter());
307  painter.end();
308  }
309 }
310 
312 {
313  // take a name from QFileDialog
314 
315  C_INT32 Answer = QMessageBox::No;
316  QString fileName, extensionName;
317 
318  while (Answer == QMessageBox::No)
319  {
320 
321 // QString userFilter = new QString;
322  /*
323  fileName = CopasiFileDialog::getSaveFileNameAndFilter(filter, this, "Save File Dialog",
324  QString::null, "PNG Files (*.png);;SVG Files (*.svg)", "Save to");
325  */
326  fileName = CopasiFileDialog::getSaveFileName(this, "Save File Dialog",
327  "untitled.png", "PDF Files (*.pdf);;PNG Files (*.png);;SVG Files (*.svg)", "Save Plot as Image", new QString);
328 
329  if (fileName.isEmpty()) return;
330 
331  /*
332  QFileInfo fileInfo(fileName);
333  extensionName = fileInfo.extension();
334  */
335 #ifdef DEBUG_UI
336 // qDebug() << "extensionName = " << extensionName;
337 #endif
338 
339  // check whether the file exists
340  Answer = checkSelection(fileName);
341 
342  if (Answer == QMessageBox::Cancel) return;
343  }
344 
345  // print plot as an image
346  saveToFile(fileName);
347 }
348 
350 {
351  QPrinter printer;
352 
353  QString docName = mpPlot->title().text();
354 
355  if (docName.isEmpty())
356  {
357  //docName.replace (QRegExp (QString::fromLatin1 ("\n")), tr (" -- "));
358  docName = QString::fromLatin1("A plot of selected COPASI output");
359  printer.setDocName(docName);
360  }
361 
362  printer.setCreator("COPASI");
363  printer.setOrientation(QPrinter::Landscape);
364 
365  QPrintDialog dialog(&printer);
366 
367  if (dialog.exec())
368  mpPlot->print(printer, PrintFilter());
369 }
370 
372 {
373  C_INT32 Answer = QMessageBox::No;
374  QString fileName;
375 
376  while (Answer == QMessageBox::No)
377  {
378  fileName =
379  CopasiFileDialog::getSaveFileName(this, "Save File Dialog", "untitled.txt", "TEXT Files (*.txt)", "Save Plot Data to");
380 
381  if (fileName.isNull()) return;
382 
383  // Checks whether the file exists
384  Answer = checkSelection(fileName);
385 
386  if (Answer == QMessageBox::Cancel) return;
387  }
388 
389  bool success = false;
390 
391  if (mpPlot)
392  {
393  QCursor oldCursor = cursor();
394  setCursor(Qt::WaitCursor);
395  success = mpPlot->saveData(TO_UTF8(fileName));
396  setCursor(oldCursor);
397  }
398 
399  if (!success)
400  {
401  std::string s = "Could not save data to ";
402  s += TO_UTF8(fileName);
403  CQMessageBox::critical(this, "Save Error", FROM_UTF8(s), QMessageBox::Ok, QMessageBox::NoButton);
404  }
405 }
406 
407 #include "scrollzoomer.h"
408 
410 {
411  if (mpPlot && mpPlot->mpZoomer)
412  mpPlot->mpZoomer->zoom(0);
413 }
414 
415 //-----------------------------------------------------------------------------
416 
418 {
419  if (mpHandler)
420  mpHandler->removeInterface(this);
421 
422  if (mpMainWindow != NULL)
424 }
425 
426 bool PlotWindow::compile(std::vector< CCopasiContainer * > listOfContainer,
427  const CCopasiDataModel* pDataModel)
428 {
429  mObjects.clear();
430  bool success = true;
431 
432  if (mpPlot != NULL)
433  success = mpPlot->compile(listOfContainer, pDataModel);
434 
435  return success;
436 };
437 
438 void PlotWindow::output(const Activity & activity)
439 {if (mpPlot) mpPlot->output(activity);}
440 
441 void PlotWindow::separate(const Activity & activity)
442 {if (mpPlot) mpPlot->separate(activity);};
443 
445 {if (mpPlot) mpPlot->finish();};
446 
447 const std::set< const CCopasiObject * > & PlotWindow::getObjects() const
448 {
449  if (mpPlot)
450  return mpPlot->getObjects();
451 
452  return mObjects;
453 }
454 
456 {
457  // We show all curves in mpPlot
459 }
460 
462 {
463  // We hide all curves in mpPlot
464  mpPlot->setCurvesVisibility(false);
465 }
466 
468 {
469  QWidget::close();
470 }
471 
472 void PlotWindow::closeEvent(QCloseEvent *closeEvent)
473 {
474  mpMainWindow->removeWindow(this);
475  mpHandler->removeInterface(this);
476 }
477 
479 {
480  return mpPlot;
481 }
void removeFromMainWindow(CopasiUI3Window *window=NULL)
void slotSaveData()
Save data into a file.
Definition: plotwindow.cpp:371
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
virtual void separate(const Activity &activity)
QAction * mpaCloseWindow
Definition: plotwindow.h:46
QAction * mpaShowAll
Definition: plotwindow.h:47
virtual const std::set< const CCopasiObject * > & getObjects() const
bool initializing
Definition: plotwindow.h:56
QAction * mpaSaveData
Definition: plotwindow.h:51
virtual void separate(const Activity &activity)
Definition: plotwindow.cpp:441
void slotCloseWindow()
Definition: plotwindow.cpp:467
PlotWindow(COutputHandlerPlot *pHandler, const CPlotSpecification *ptrSpec, CopasiUI3Window *pMainWindow)
Definition: plotwindow.cpp:57
void setCurvesVisibility(const bool &visibility)
#define C_INT32
Definition: copasi.h:90
virtual void output(const Activity &activity)
Definition: plotwindow.cpp:438
virtual void closeEvent(QCloseEvent *closeEvent)
Definition: plotwindow.cpp:472
CopasiPlot * mpPlot
Definition: plotwindow.h:41
void createToolBar()
Definition: plotwindow.cpp:171
virtual QMenu * getWindowMenu() const
Definition: plotwindow.cpp:118
virtual void replot()
void slotSelectAll()
Definition: plotwindow.cpp:455
void printAsImage()
Definition: plotwindow.cpp:311
COutputHandlerPlot * mpHandler
Definition: plotwindow.h:42
virtual QFont font(const QFont &f, Item, int) const
Definition: plotwindow.cpp:48
virtual bool compile(std::vector< CCopasiContainer * > listOfContainer, const CCopasiDataModel *pDataModel)
void toggleLogX(bool)
Definition: plotwindow.cpp:216
QwtPlotZoomer * mpZoomer
Definition: CopasiPlot.h:455
C_INT32 checkSelection(const QString &file)
static const QIcon & icon(const IconID &id)
QAction * mpaPrint
Definition: plotwindow.h:49
QAction * mpaToggleLogX
Definition: plotwindow.h:53
QAction * mpaZoomOut
Definition: plotwindow.h:52
virtual void finish()
Definition: plotwindow.cpp:444
virtual void saveToFile(const QString &fileName) const
Definition: plotwindow.cpp:274
virtual bool compile(std::vector< CCopasiContainer * > listOfContainer, const CCopasiDataModel *pDataModel)
Definition: plotwindow.cpp:426
std::set< const CCopasiObject * > mObjects
QAction * mpaToggleLogY
Definition: plotwindow.h:54
QAction * mpaHideAll
Definition: plotwindow.h:48
void createActions()
Definition: plotwindow.cpp:123
void slotDeselectAll()
Definition: plotwindow.cpp:461
static StandardButton critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
CopasiUI3Window * mpMainWindow
Definition: plotwindow.h:43
bool initFromSpec(const CPlotSpecification *plotspec)
Definition: CopasiPlot.cpp:836
virtual const std::set< const CCopasiObject * > & getObjects() const
Definition: plotwindow.cpp:447
void printPlot()
Definition: plotwindow.cpp:349
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
bool initFromSpec(const CPlotSpecification *ptrSpec)
Definition: plotwindow.cpp:199
bool saveData(const std::string &filename)
void createMenus()
Definition: plotwindow.cpp:97
QMenu * mpWindowMenu
Definition: plotwindow.h:44
void addToMainWindow(CopasiUI3Window *window=NULL)
const std::string & getTitle() const
Definition: CPlotItem.cpp:228
void slotZoomOut()
Zoom out.
Definition: plotwindow.cpp:409
virtual ~PlotWindow()
Definition: plotwindow.cpp:417
QAction * mpaSaveImage
Definition: plotwindow.h:50
virtual void finish()
virtual void output(const Activity &activity)
void toggleLogY(bool)
Definition: plotwindow.cpp:236
CopasiPlot * getPlot() const
Definition: plotwindow.cpp:478
static QString getSaveFileName(QWidget *parent=0, const char *name=0, const QString &startWith=QString::null, const QString &filter=QString::null, const QString &caption=QString::null, QString *pSelectedFilter=NULL, QFileDialog::Options options=0)
virtual void removeInterface(COutputInterface *pInterface)
void removeWindow(QMainWindow *pWindow)