COPASI API  4.16.103
CQDifferentialEquations.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 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) 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
16 
17 #include <QtCore/QVariant>
18 #include <QtGui/QPainter>
19 
21 
22 #include <sstream>
23 #include <set>
24 #include <vector>
25 
26 #include <QtGui/QMessageBox>
27 #include <QtCore/QRegExp>
28 
29 #ifdef HAVE_MML
30 # include <qtmmlwidget.h>
31 #endif // HAVE_MML
32 
33 #include "model/CMMLOutput.h"
35 #include "qtUtilities.h"
36 #include "utilities/utility.h"
37 #include "tex/CMathMLToTeX.h"
40 
41 #include "CopasiFileDialog.h"
42 
43 #ifdef DEBUG_UI
44 #include <QtCore/QtDebug>
45 #endif
46 
47 /*
48  * Constructs a CQDifferentialEquations which is a child of 'parent', with the
49  * name 'name'.'
50  */
51 CQDifferentialEquations::CQDifferentialEquations(QWidget* parent, const char* name)
52  : CopasiWidget(parent, name)
53 {
54  setupUi(this);
55 
56 #ifdef WIN32
57  // on windows there ought to be a border around the MML widget
58  // otherwise it is difficult to distinguish the formula from the rest of the
59  // dialog
60  mpScrollView->setFrameShape(QFrame::Panel);
61  mpScrollView->setFrameStyle(QFrame::Panel | QFrame::Plain);
62 #endif
63 
64  init();
65 }
66 
67 /*
68  * Destroys the object and frees any allocated resources
69  */
71 {
72  // no need to delete child widgets, Qt does it all for us
73 }
74 
76 {
77  comboBoxFunctions->setCurrentIndex(1);
78 }
79 
80 void CQDifferentialEquations::saveMML(const QString outfilename)
81 {
82  std::ofstream ofile;
83  ofile.open(CLocaleString::fromUtf8(TO_UTF8(outfilename)).c_str(), std::ios::trunc);
84 
85  ofile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
86  ofile << "<!DOCTYPE math PUBLIC \"-//W3C//DTD MathML 2.0//EN\" \"http://www.w3.org/Math/DTD/mathml2/mathml2.dtd\">" << std::endl;
87  ofile << "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">" << std::endl;
88 
89  ofile << mml.str();
90 
91  ofile << "</math>" << std::endl;
92 
93  ofile.close();
94 }
95 
97 {
98  bool hasContents = true;
99  //std::ostringstream mml;
100  mml.str("");
101 
102  bool expand, expandAll;
103 
104  switch (comboBoxFunctions->currentIndex())
105  {
106  case 0:
107  expand = false;
108  expandAll = false;
109  break;
110 
111  case 1:
112  expand = true;
113  expandAll = false;
114  break;
115 
116  default:
117  expand = true;
118  expandAll = true;
119  break;
120  };
121 
122  bool parameterAsNumbers = false;
123 
124  if (comboBoxParameters->currentIndex() == 0)
125  parameterAsNumbers = true;
126 
127  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
128  CMMLOutput::writeDifferentialEquations(mml, (*CCopasiRootContainer::getDatamodelList())[0]->getModel(), parameterAsNumbers, expand, expandAll);
129 
130  QWidget* tmp = dynamic_cast<QWidget*>(parent());
131 
132  if (tmp) tmp->setCursor(Qt::WaitCursor);
133 
134  mpMMLWidget->setContent(FROM_UTF8(mml.str()));
135  mpMMLWidget->setBaseFontPointSize(qApp->font().pointSize());
136  mpMMLWidget->setFontName(QtMmlWidget::NormalFont, qApp->font().family());
137  mpMMLWidget->setFixedSize(mpMMLWidget->sizeHint().expandedTo(QSize(0, 0)));
138 
139  if (tmp) tmp->unsetCursor();
140 
141  mpSaveButton->setEnabled(hasContents);
142 }
143 
145 {
147  return true;
148 }
149 
151 {}
152 
153 void CQDifferentialEquations::saveTeX(const QString outfilename)
154 {
155  QString latexStr(FROM_UTF8(mml.str()));
156 
157 #ifdef DEBUG_UI
158  qDebug() << latexStr;
159 #endif
160 
161  CMathMLToTeX::convert(latexStr);
162 
163  std::ofstream ofile;
164  ofile.open(CLocaleString::fromUtf8(TO_UTF8(outfilename)).c_str(), std::ios::trunc);
165  ofile << TO_UTF8(latexStr);
166  ofile.close();
167 }
168 
169 void CQDifferentialEquations::savePNG(const QString outfilename)
170 {
171  QtMmlDocument doc;
172  doc.setBaseFontPointSize(20);
173  doc.setFontName(QtMmlWidget::NormalFont, qApp->font().family());
174  doc.setContent(FROM_UTF8(mml.str()));
175 
176  const QSize &size = doc.size();
177  QPixmap pixmap(size.width(), size.height());
178  QPainter painter(&pixmap);
179  painter.setRenderHint(QPainter::Antialiasing);
180  painter.setRenderHint(QPainter::SmoothPixmapTransform);
181  painter.setRenderHint(QPainter::HighQualityAntialiasing);
182  painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
183  doc.paint(&painter, QPoint(0, 0));
184  pixmap.save(outfilename, "PNG");
185 }
186 
188 {
189  QString outfilename;
190 
191  C_INT32 Answer = QMessageBox::No;
192 
193  while (Answer == QMessageBox::No)
194  {
195  outfilename =
197  "Save File Dialog",
198  "untitled.mml",
199  "MathML (*.mml);;TeX (*.tex);;PNG (*.png)",
200  "Save Formula to Disk", new QString);
201 
202  if (outfilename.isEmpty()) return;
203 
204  // Checks whether the file exists
205  Answer = checkSelection(outfilename);
206 
207  if (Answer == QMessageBox::Cancel)
208  return;
209  }
210 
211 #ifdef DEBUG_UI
212  qDebug() << "outfilename = " << outfilename;
213 #endif
214 
215  QApplication::setOverrideCursor(Qt::WaitCursor);
216 
217  if (outfilename.contains(".tex"))
218  {
219  saveTeX(outfilename);
220  }
221  else if (outfilename.contains(".png"))
222  {
223  savePNG(outfilename);
224  }
225  else
226  {
227  saveMML(outfilename);
228  }
229 
230  QApplication::restoreOverrideCursor();
231 }
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
CQDifferentialEquations(QWidget *parent=0, const char *name=0)
#define C_INT32
Definition: copasi.h:90
static void convert(QString &text)
convert to TeX
C_INT32 checkSelection(const QString &file)
virtual void savePNG(const QString filename)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
virtual void saveMML(const QString filename)
virtual void saveTeX(const QString filename)
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
static CLocaleString fromUtf8(const std::string &utf8)
static void writeDifferentialEquations(std::ostream &mml, CModel *model, bool localParameterNumbers, bool expand, bool expandFull)
Definition: CMMLOutput.cpp:279
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)