COPASI API  4.16.103
CQExpressionMmlStackedWidget.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) 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 
12 
13 #include <QtCore/QString>
14 #include <QtGui/QPainter>
15 
17 
18 #include "CQMessageBox.h"
19 #include "qtUtilities.h" // for UTF8
20 #include "CopasiFileDialog.h"
21 #include "tex/CMathMLToTeX.h"
24 
26 // turns out that the fatalError definition in copasi message is incompatible
27 // with the mml widget
28 #undef fatalError
29 
30 #ifdef HAVE_MML
31 # include <qtmmlwidget.h>
32 #endif // HAVE_MML
33 
34 #ifdef DEBUG_UI
35 #include <QtCoe/QtDebug>
36 #endif
37 
38 /*
39  * Constructs a CQExpressionMmlStackedWidget as a child of 'parent', with the
40  * name 'name' and widget flags set to 'f'.
41  */
43  : QStackedWidget(parent)
44 {
45  setupUi(this);
46 
47  mpBtnExpressionObject->setIcon(CQIconResource::icon(CQIconResource::copasi));
48  mpBtnViewExpression->setIcon(CQIconResource::icon(CQIconResource::renderMathML));
49  mpBtnEditExpression->setIcon(CQIconResource::icon(CQIconResource::edit));
50  mpBtnSaveExpression->setIcon(CQIconResource::icon(CQIconResource::fileExport));
51 
52  mpMmlScrollView->setStyleSheet(QString(mpMmlScrollView->widget()->metaObject()->className()) + "{background-color:" + QColor(Qt::white).name() + ";}");
53 
54 #ifdef WIN32
55  // on windows there ought to be a border around the MML widget
56  // otherwise it is difficult to distinguish the formula from the rest of the
57  // dialog
58  mpMmlScrollView->setFrameShape(QFrame::Panel);
59  mpMmlScrollView->setFrameStyle(QFrame::Panel | QFrame::Plain);
60 #endif
61 
62  init();
63 }
64 
65 /*
66  * Destroys the object and frees any allocated resources
67  */
69 {
70  // no need to delete child widgets, Qt does it all for us
71 }
72 
74 {
75 #ifdef DEBUG_UI
76  qDebug() << "- here on CQExpressionMmlStackedWidget::slotGoExpressionWidget() -";
77  qDebug() << "L" << __LINE__ << " on CQEMSW mpExpressionWidget->text() = " << mpExpressionWidget->text();
78 #endif
79 
80  setCurrentWidget(mpExpressionPage);
81 
82 #ifdef DEBUG_UI
83  qDebug() << "L" << __LINE__ << " on CQEMSW mpExpressionWidget->text() = " << mpExpressionWidget->text();
84  qDebug() << "L" << __LINE__ << " on CQEMSW: activeWidget NEW = " << currentIndex();
85 #endif
86 }
87 
89 {
90 #ifdef DEBUG_UI
91  qDebug() << "L" << __LINE__ << " on CQEMSW mpExpressionWidget->text() = " << mpExpressionWidget->text();
92 #endif
93 
94  setCurrentWidget(mpMmlPage);
95 
96 #ifdef DEBUG_UI
97  qDebug() << "L" << __LINE__ << " on CQEMSW mpExpressionWidget->text() = " << mpExpressionWidget->text();
98  qDebug() << "L" << __LINE__ << " on CQEMSW: activeWidget NEW = " << currentIndex();
99 #endif
100 }
101 
103 {
104 #ifdef DEBUG_UI
105  qDebug() << "- here on CQExpressionMmlStackedWidget::updateWidget() -";
106 #endif
107 
108 #ifdef HAVE_MML
109 
110  std::ostringstream mml;
111  std::vector<std::vector<std::string> > params;
112 
113 #ifdef DEBUG_UI
114  qDebug() << "L" << __LINE__ << " on CQEMSW mpExpressionWidget->text() = " << mpExpressionWidget->text();
115  qDebug() << "L" << __LINE__ << " on CQEMSW: activeWidget NEW = " << currentIndex();
116 #endif
117 
118  if (mpExpressionWidget->toPlainText().isEmpty() || !mpExpressionWidget->isValid())
119  setCurrentWidget(mpExpressionPage);
120  else
121  {
122  mpExpressionWidget->writeMathML(mml);
123 
124 #ifdef DEBUG_UI
125  qDebug() << "mml.str() = " << FROM_UTF8(mml.str());
126 #endif
127 
128  setCurrentWidget(mpMmlPage);
129  mpMmlScrollView->updateWidget(mml);
130  }
131 
132 #endif /* HAVE_MML */
133 
134 #ifdef DEBUG_UI
135  qDebug() << "L" << __LINE__ << " on CQEMSW: activeWidget = " << currentIndex();
136 #endif
137 }
138 
139 void CQExpressionMmlStackedWidget::setReadOnly(const bool & readOnly)
140 {
141  if (readOnly)
142  mpBtnEditExpression->hide();
143  else
144  mpBtnEditExpression->show();
145 }
146 
148 {
149  return mpExpressionWidget->toPlainText();
150 }
151 
153 {
154 #ifdef DEBUG_UI
155  qDebug() << "L" << __LINE__ << " on CQEMSW mpExpressionWidget->text() = " << mpExpressionWidget->text();
156  qDebug() << "L" << __LINE__ << " on CQEMSW: activeWidget = " << currentIndex();
157 #endif
158 
159 #ifndef HAVE_MML
160  mpBtnViewExpression->hide();
161 #endif // not HAVE_MML
162 }
163 
165 {
166  QString *filter = new QString;
167  QString outfilename;
168 
169  C_INT32 Answer = QMessageBox::No;
170 
171  while (Answer == QMessageBox::No)
172  {
173  outfilename =
175  "Save File Dialog",
176  "untitled.mml",
177  "MathML (*.mml);;TeX (*.tex);;PNG (*.png)",
178 // "Save Expression to Disk", new QString);
179  "Save Expression to Disk", filter);
180 
181  if (outfilename.isEmpty()) return;
182 
183  // Checks whether the file exists
184  Answer = checkSelection(outfilename);
185 
186  if (Answer == QMessageBox::Cancel)
187  return;
188  }
189 
190 #ifdef DEBUG_UI
191  qDebug() << "\non CQEMSW::slotSaveExpression -> filter = " << *filter << "\n";
192 #endif
193 
194  if (filter->contains(".tex"))
195  saveTeX(outfilename);
196  else if (filter->contains(".png"))
197  savePNG(outfilename);
198  else
199  saveMML(outfilename);
200 }
201 
202 void CQExpressionMmlStackedWidget::savePNG(const QString outfilename)
203 {
204  std::ostringstream mml;
205  mpExpressionWidget->writeMathML(mml);
206  QtMmlDocument doc;
207  doc.setBaseFontPointSize(20);
208  doc.setFontName(QtMmlWidget::NormalFont, qApp->font().family());
209  doc.setContent(FROM_UTF8(mml.str()));
210 
211  const QSize &size = doc.size();
212  QPixmap pixmap(size.width(), size.height());
213  QPainter painter(&pixmap);
214  painter.setRenderHint(QPainter::Antialiasing);
215  painter.setRenderHint(QPainter::SmoothPixmapTransform);
216  painter.setRenderHint(QPainter::HighQualityAntialiasing);
217  painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
218  doc.paint(&painter, QPoint(0, 0));
219  pixmap.save(outfilename, "PNG");
220 }
221 
222 void CQExpressionMmlStackedWidget::saveMML(const QString outfilename)
223 {
224  std::ofstream ofile;
225  ofile.open(CLocaleString::fromUtf8(TO_UTF8(outfilename)).c_str(), std::ios::trunc);
226 
227  ofile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
228  ofile << "<!DOCTYPE math PUBLIC \"-//W3C//DTD MathML 2.0//EN\" \"http://www.w3.org/Math/DTD/mathml2/mathml2.dtd\">" << std::endl;
229  ofile << "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">" << std::endl;
230 
231  mpExpressionWidget->writeMathML(ofile);
232 
233  ofile << "</math>" << std::endl;
234 
235  ofile.close();
236 }
237 
238 void CQExpressionMmlStackedWidget::saveTeX(const QString outfilename)
239 {
240  std::ostringstream mml;
241  mpExpressionWidget->writeMathML(mml);
242 
243  QString latexStr(FROM_UTF8(mml.str()));
244 
245  CMathMLToTeX::convert(latexStr);
246 
247  std::ofstream ofile;
248  ofile.open(CLocaleString::fromUtf8(TO_UTF8(outfilename)).c_str(), std::ios::trunc);
249 
250  ofile << TO_UTF8(latexStr);
251 
252  ofile.close();
253 }
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
#define C_INT32
Definition: copasi.h:90
static void convert(QString &text)
convert to TeX
C_INT32 checkSelection(const QString &file)
static const QIcon & icon(const IconID &id)
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
static CLocaleString fromUtf8(const std::string &utf8)
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)