COPASI API  4.16.103
CQNotes.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 /*
7  * CQNotes.cpp
8  *
9  * Created on: Aug 11, 2010
10  * Author: shoops
11  */
12 
13 #include <QtWebKit/QWebFrame>
14 #include <QtXml/QXmlInputSource>
15 #include <QtXml/QXmlSimpleReader>
16 #include <QtGui/QDesktopServices>
17 
18 #include "CQNotes.h"
20 #include "CQMessageBox.h"
21 #include "qtUtilities.h"
22 
23 #include "model/CModelValue.h"
24 #include "model/CReaction.h"
25 #include "model/CEvent.h"
26 #include "function/CFunction.h"
27 #include "report/CKeyFactory.h"
31 
32 CQValidatorXML::CQValidatorXML(QPlainTextEdit * parent, const char * name):
33  CQValidator< QPlainTextEdit >(parent, &QPlainTextEdit::toPlainText, name),
34  mIsFreeText(true),
35  mNeedsWrap(false)
36 {}
37 
38 // virtual
39 QValidator::State CQValidatorXML::validate(QString & input, int & pos) const
40 {
41  QXmlSimpleReader Validator;
42  QXmlInputSource Input;
43  CQNotesContentHandler ContentHandler;
44 
45  Validator.setContentHandler(&ContentHandler);
46 
47  // We like to allow free text and therefore wrap the text to create valid XML.
48  Input.setData("<Validate:XML xmlns:Validate=\"http://www.copasi.org/Validate\">" + input + "</Validate:XML>");
49 
50  if (Validator.parse(Input))
51  {
52  mIsFreeText = ContentHandler.isFreeText();
53  mNeedsWrap = ContentHandler.needsWrap();
54  return CQValidator< QPlainTextEdit >::validate(input, pos);
55  }
56 
57  mIsFreeText = true;
58  mNeedsWrap = false;
59  setColor(Invalid);
60  return Intermediate;
61 }
62 
63 const bool & CQValidatorXML::isFreeText() const
64 {
65  return mIsFreeText;
66 }
67 
68 const bool & CQValidatorXML::needsWrap() const
69 {
70  return mNeedsWrap;
71 }
72 
74  QXmlDefaultHandler(),
75  mIsFreeText(true),
76  mNeedsWrap(true),
77  mLevel(0)
78 {}
79 
81 {}
82 
83 // virtual
85 {
86  mIsFreeText = true;
87  mNeedsWrap = true;
88  mLevel = 0;
89  return true;
90 }
91 
92 // virtual
93 bool CQNotesContentHandler::startElement(const QString & namespaceURI,
94  const QString & localName,
95  const QString & qName,
96  const QXmlAttributes & atts)
97 {
98  if (namespaceURI != "http://www.copasi.org/Validate" ||
99  qName != "Validate:XML")
100  mIsFreeText = false;
101 
102  if (mLevel == 1 &&
103  namespaceURI == "http://www.w3.org/1999/xhtml" &&
104  (localName == "html" || localName == "body"))
105  mNeedsWrap = false;
106 
107  mLevel++;
108 
109  return QXmlDefaultHandler::startElement(namespaceURI, localName, qName, atts);
110 }
111 
112 // virtual
113 bool CQNotesContentHandler::endElement(const QString & namespaceURI,
114  const QString & localName,
115  const QString & qName)
116 {
117  mLevel--;
118 
119  return QXmlDefaultHandler::endElement(namespaceURI, localName, qName);
120 }
121 
123 {
124  return mIsFreeText;
125 }
126 
128 {
129  return (mNeedsWrap && !mIsFreeText);
130 }
131 
132 CQNotes::CQNotes(QWidget* parent, const char* name) :
133  CopasiWidget(parent, name),
134  mEditMode(false),
135  mChanged(false),
136  mpValidatorXML(NULL),
137  mValidity(QValidator::Acceptable),
138  mKeyToCopy("")
139 
140 {
141  setupUi(this);
142 
143  mpValidatorXML = new CQValidatorXML(mpEdit);
144 
145  mEditMode = false;
146  mpEdit->hide();
147  mpWebView->show();
148  mpBtnToggleEdit->setIcon(CQIconResource::icon(CQIconResource::edit));
149 
150  mpWebView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
151 }
152 
154 {}
155 
157 {
158  mKeyToCopy = mKey;
159 }
160 
161 // virtual
163 {
164  switch (action)
165  {
166  case ListViews::CHANGE:
167 
168  if (key == mKey)
169  {
170  load();
171  }
172 
173  break;
174 
175  case ListViews::DELETE:
176 
177  if (key == mKey || objectType == ListViews::MODEL)
178  {
179  mpObject = NULL;
180  mKey = "";
181  }
182 
183  break;
184 
185  default:
186  break;
187  }
188 
189  if (objectType == ListViews::MODEL &&
190  action == ListViews::DELETE)
191  {
192  mEditMode = false;
193  }
194 
195  return true;
196 }
197 
198 // virtual
200 {
201  mpBtnToggleEdit->setFocus();
202 
204 
205  if (mpObject != NULL)
206  {
207  save();
208  }
209  else
210  {
211  mKey = "";
212  mpDataModel = NULL;
213  }
214 
215  return true;
216 }
217 
218 // virtual
220 {
221  if (mKeyToCopy == "")
222  {
223  load();
224  }
225  else
226  {
228  load();
230  save();
231  mKeyToCopy = "";
232  }
233 
234  return true;
235 }
236 
238 {
239  mEditMode = !mEditMode;
240 
241  if (mEditMode)
242  {
243  mpWebView->hide();
244  mpEdit->show();
245  mpBtnToggleEdit->setIcon(CQIconResource::icon(CQIconResource::renderMarkup));
246  }
247  else
248  {
249  save();
250  load();
251 
252  mpEdit->hide();
253  mpWebView->show();
254  mpBtnToggleEdit->setIcon(CQIconResource::icon(CQIconResource::edit));
255  }
256 }
257 
259 {
260  QString Input = mpEdit->toPlainText();
261  int pos = 0;
262 
263  mValidity = mpValidatorXML->validate(Input, pos);
264 
265  if (mpValidatorXML->isFreeText())
266  {
267  mEditMode = true;
268  mpBtnToggleEdit->hide();
269  }
270  else
271  {
272  mpBtnToggleEdit->show();
273  }
274 
275  if (mEditMode)
276  {
277  mpWebView->hide();
278  mpEdit->show();
279  mpBtnToggleEdit->setIcon(CQIconResource::icon(CQIconResource::renderMarkup));
280  }
281  else
282  {
283  mpEdit->hide();
284  mpWebView->show();
285  mpBtnToggleEdit->setIcon(CQIconResource::icon(CQIconResource::edit));
286  }
287 }
288 
290 {
291  if (mpObject != NULL)
292  {
293  QString Notes;
294 
296  CReportDefinition * pReportDefinition = static_cast< CReportDefinition * >(mpObject);
297 
298  if (pAnnotation != NULL)
299  {
300  Notes = FROM_UTF8(pAnnotation->getNotes());
301  }
302  else if (pReportDefinition != NULL)
303  {
304  Notes = FROM_UTF8(pReportDefinition->getComment());
305  }
306 
307  // The notes are UTF8 encoded however the html does not specify an encoding
308  // thus Qt uses locale settings.
309  mpWebView->setHtml(Notes);
310  mpEdit->setPlainText(Notes);
312  slotValidateXML();
313 
315  {
316  slotToggleMode();
317  }
318 
319  mValidity = QValidator::Acceptable;
320  }
321 
322  mChanged = false;
323 
324  return;
325 }
326 
328 {
329  if (mpObject != NULL &&
330  mValidity == QValidator::Acceptable)
331  {
332  QString Notes;
333 
335  CReportDefinition * pReportDefinition = static_cast< CReportDefinition * >(mpObject);
336 
337  if (pAnnotation != NULL)
338  {
339  Notes = FROM_UTF8(pAnnotation->getNotes());
340  }
341  else if (pReportDefinition != NULL)
342  {
343  Notes = FROM_UTF8(pReportDefinition->getComment());
344  }
345 
346  if (mpEdit->toPlainText() != Notes)
347  {
348  std::string PlainText = TO_UTF8(mpEdit->toPlainText());
349 
350  if (mpValidatorXML->needsWrap())
351  {
352  // We wrap the HTML in a body element if it does not contain a top level html or body element.
353  PlainText = "<body xmlns=\"http://www.w3.org/1999/xhtml\">" + PlainText + "</body>";
354  }
355 
356  if (pAnnotation != NULL)
357  {
358  pAnnotation->setNotes(PlainText);
359  }
360  else if (pReportDefinition != NULL)
361  {
362  pReportDefinition->setComment(PlainText);
363  }
364 
365  mChanged = true;
366  }
367  }
368 
369  if (mChanged)
370  {
371  if (mpDataModel != NULL)
372  {
373  mpDataModel->changed();
374  }
375 
377  mChanged = false;
378  }
379 
380  return;
381 }
382 
383 void CQNotes::slotOpenUrl(const QUrl & url)
384 {
385  QDesktopServices::openUrl(url);
386  return;
387 }
Header file of class CModelEntity and CModelValue.
objectType
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
const bool & isFreeText() const
Definition: CQNotes.cpp:63
bool needsWrap() const
Definition: CQNotes.cpp:127
virtual bool leave()
Definition: CQNotes.cpp:199
CCopasiObject * get(const std::string &key)
CQValidatorXML(QPlainTextEdit *parent, const char *name=0)
Definition: CQNotes.cpp:32
void setNotes(const std::string &notes)
CCopasiObject * mpObject
Definition: copasiWidget.h:64
bool mChanged
Definition: CQNotes.h:92
void save()
Definition: CQNotes.cpp:327
void changed(const bool &changed=true)
const std::string & getComment() const
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
Definition: CQNotes.cpp:162
bool mIsFreeText
Definition: CQNotes.h:30
void slotOpenUrl(const QUrl &url)
Definition: CQNotes.cpp:383
CQValidatorXML * mpValidatorXML
Definition: CQNotes.h:93
static CAnnotation * castObject(CCopasiObject *pObject)
Definition: CAnnotation.cpp:32
bool mEditMode
Definition: CQNotes.h:91
virtual void saved() const
Definition: CQValidator.h:71
const bool & needsWrap() const
Definition: CQNotes.cpp:68
void setComment(const std::string &comment)
const std::string & getNotes() const
static const QIcon & icon(const IconID &id)
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
const bool & isFreeText() const
Definition: CQNotes.cpp:122
CCopasiDataModel * mpDataModel
Definition: copasiWidget.h:65
virtual bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName)
Definition: CQNotes.cpp:113
static CKeyFactory * getKeyFactory()
void slotToggleMode()
Definition: CQNotes.cpp:237
virtual bool enterProtected()
Definition: CQNotes.cpp:219
void load()
Definition: CQNotes.cpp:289
~CQNotes()
Definition: CQNotes.cpp:153
CQNotes(QWidget *parent=0, const char *name=0)
Definition: CQNotes.cpp:132
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
virtual State validate(QString &input, int &pos) const
Definition: CQNotes.cpp:39
virtual bool startDocument()
Definition: CQNotes.cpp:84
void slotValidateXML()
Definition: CQNotes.cpp:258
Header file of class CEvent.
std::string mKeyToCopy
Definition: CQNotes.h:96
virtual State validate(QString &input, int &) const
Definition: CQValidator.h:51
virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
Definition: CQNotes.cpp:93
std::string mKey
Definition: copasiWidget.h:63
void slotBtnCopy()
Definition: CQNotes.cpp:156
QValidator::State mValidity
Definition: CQNotes.h:94
State setColor(const State &state) const
Definition: CQValidator.h:84
bool mNeedsWrap
Definition: CQNotes.h:32