COPASI API  4.16.103
CQExpressionWidget.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) 2006 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <iostream>
16 
17 #include <QtGui/QLabel>
18 #include <QtGui/QComboBox>
19 //Added by qt3to4:
20 #include <QtGui/QKeyEvent>
21 
22 #include "CQExpressionWidget.h"
23 #include "CQMessageBox.h"
24 #include "CCopasiSelectionDialog.h"
25 #include "qtUtilities.h"
26 
27 #include "copasi.h"
28 
30 #include "function/CExpression.h"
31 #include "function/CFunctionDB.h"
32 #include "function/CMassAction.h"
34 #include "model/CModel.h"
35 #include "CQMatrixDialog.h"
36 #include "qtUtilities.h"
39 
40 #define DEBUG_UI
41 
42 #ifdef DEBUG_UI
43 std::ostream &operator<<(std::ostream &os, const QTextCursor & d)
44 {
45  os << "Position: " << d.position() << std::endl;
46  os << "Selection Start: " << d.selectionStart() << std::endl;
47  os << "Selection End: " << d.selectionEnd() << std::endl;
48  os << "Anchor: " << d.anchor() << std::endl;
49  os << "Selected Text: " << TO_UTF8(d.selectedText()) << std::endl;
50 
51  return os;
52 }
53 #endif // DEBUG_UI
54 
56  : QSyntaxHighlighter(ew),
57  mObjectDisplayPattern(CQExpressionWidget::DisplayPattern)
58 {
59  // COPASI object format
60  mObjectDisplayFormat.setForeground(QColor(100, 0, 200));
61 }
62 
64 {}
65 
66 void CQExpressionHighlighter::highlightBlock(const QString &text)
67 {
68  int index = mObjectDisplayPattern.indexIn(text);
69 
70  while (index >= 0)
71  {
72  int length = mObjectDisplayPattern.matchedLength();
73  setFormat(index, length, mObjectDisplayFormat);
74  index = mObjectDisplayPattern.indexIn(text, index + length);
75  }
76 }
77 
78 //***********************************************************************
79 
80 CQValidatorExpression::CQValidatorExpression(QTextEdit * parent, const char * name, bool isBoolean):
81  CQValidator< QTextEdit >(parent, &QTextEdit::toPlainText, name),
82  mExpression()
83 {
85  assert(pDataModel != NULL);
86 
87  mExpression.setObjectParent(pDataModel);
88  mExpression.setIsBoolean(isBoolean);
89 }
90 
91 /**
92  * This function ensures that any characters on Expression Widget are validated
93  * to go to further processes.
94  */
95 QValidator::State CQValidatorExpression::validate(QString & input, int & pos) const
96 {
97  // The input is the display version of the infix string.
98  // We must first convert the display string to infix.
99 
100  CQExpressionWidget * pExpressionWidget =
101  static_cast< CQExpressionWidget * >(parent());
102 
103  if (pExpressionWidget != NULL)
104  {
105  if (const_cast< CExpression * >(&mExpression)->setInfix(pExpressionWidget->getExpression()) &&
106  const_cast< CExpression * >(&mExpression)->compile())
107  {
108  QString Input = (*mpContainer.*mRetrieve)();
109  return CQValidator< QTextEdit >::validate(input, pos);
110  }
111  }
112 
113  setColor(Invalid);
114  return Intermediate;
115 }
116 
117 /**
118  * Function to get CExpression object
119  */
121 {
122  return &mExpression;
123 }
124 
126 {
127  mExpression.setIsBoolean(booleanRequired);
128 }
129 
130 //***********************************************************************
131 
132 CQValidatorFunction::CQValidatorFunction(QTextEdit * parent, const char * name):
133  CQValidator< QTextEdit >(parent, &QTextEdit::toPlainText, name),
134  mFunction()
135 {
137  assert(pDataModel != NULL);
138 
139  mFunction.setObjectParent(pDataModel);
140 }
141 
142 /**
143  * This function ensures that any characters on Expression Widget are validated
144  * to go to further processes.
145  */
146 QValidator::State CQValidatorFunction::validate(QString & input, int & pos) const
147 {
148  // The input is the display version of the infix string.
149  // We must first convert the display string to infix.
150 
151  State CurrentState = Invalid;
152 
153  CQExpressionWidget * pExpressionWidget =
154  static_cast< CQExpressionWidget * >(parent());
155 
156  if (pExpressionWidget != NULL)
157  {
158  std::string Infix = pExpressionWidget->getFunction();
159 
160  if (Infix == CMassAction::Infix[0] ||
161  Infix == CMassAction::Infix[1] ||
162  (const_cast< CFunction * >(&mFunction)->setInfix(Infix) &&
163  const_cast< CFunction * >(&mFunction)->compile()))
164  {
165  QString Input = (*mpContainer.*mRetrieve)();
166  CurrentState = CQValidator< QTextEdit >::validate(input, pos);
167  }
168  }
169 
170  if (CurrentState != Acceptable)
171  {
172  setColor(Invalid);
173  CurrentState = Intermediate;
174  }
175 
176  emit stateChanged(CurrentState == Acceptable);
177 
178  return CurrentState;
179 }
180 
182 {
183  return &mFunction;
184 }
185 
186 //***********************************************************************
187 
188 // static
189 const char CQExpressionWidget::InfixPattern[] = "<(CN=([^\\\\>]|\\\\.)*)>";
190 
191 // static
192 const char CQExpressionWidget::DisplayPattern[] = "\\{(([^\\\\\\}]|\\\\.)*)\\}";
193 
194 CQExpressionWidget::CQExpressionWidget(QWidget * parent, const char * name)
195  : QTextEdit(parent),
196  mpValidatorExpression(NULL),
197  mpValidatorFunction(NULL),
198  mObjectClasses(TransientExpression),
199  mpCurrentObject(NULL)
200 {
201  setObjectName(QString::fromUtf8(name));
202  setTabChangesFocus(true);
203 
205 
206  int h, s, v;
207 
208  mSavedColor = palette().color(backgroundRole());
209  mSavedColor.getHsv(&h, &s, &v);
210 
211  if (s < 20) s = 20;
212 
213  mChangedColor.setHsv(240, s, v);
214 
215  connect(this, SIGNAL(textChanged()),
216  this, SLOT(slotTextChanged()));
217 }
218 
220 {}
221 
223 {
224  if (mpValidatorExpression != NULL)
225  {
226  return mpValidatorExpression;
227  }
228 
229  return mpValidatorFunction;
230 }
231 
232 void CQExpressionWidget::writeMathML(std::ostream & out) const
233 {
234  if (mpValidatorExpression != NULL)
236  else if (mpValidatorFunction != NULL)
237  {
238  std::string Infix = getFunction();
239 
240  if (Infix == CMassAction::Infix[0])
241  {
242  CMassAction Function(TriTrue, NULL);
243  std::vector<std::vector<std::string> > params;
244 
245  Function.createListOfParametersForMathML(params);
246  out << Function.writeMathML(params, true, false);
247  }
248  else if (Infix == CMassAction::Infix[1])
249  {
250  CMassAction Function(TriFalse, NULL);
251  std::vector<std::vector<std::string> > params;
252 
253  Function.createListOfParametersForMathML(params);
254  out << Function.writeMathML(params, true, false);
255  }
256  else
257  {
258  std::vector<std::vector<std::string> > params;
260  out << mpValidatorFunction->getFunction()->writeMathML(params, true, false);
261  }
262  }
263 
264  return;
265 }
266 
268 {
269  QTextEdit::mouseReleaseEvent(e);
270 
271  mCursor = textCursor();
272 
273  int Left;
274  int Right;
275 
276  if (objectBoundaries(mCursor.selectionStart(), Left, Right))
277  {
278  // Move the selection start out of the object.
279  mCursor.setPosition(mCursor.selectionEnd());
280  mCursor.setPosition(Left, QTextCursor::KeepAnchor);
281 
282  setTextCursor(mCursor);
283  }
284 
285  if (objectBoundaries(mCursor.selectionEnd(), Left, Right))
286  {
287  // Move the selection end out of the object.
288  mCursor.setPosition(mCursor.selectionStart());
289  mCursor.setPosition(Right, QTextCursor::KeepAnchor);
290 
291  setTextCursor(mCursor);
292  }
293 }
294 
295 void CQExpressionWidget::dropEvent(QDropEvent * e)
296 {
297  QString SelectedText = textCursor().selectedText();
298 
299  QTextEdit::dropEvent(e);
300 
301  int Left;
302  int Right;
303 
304  if (objectBoundaries(textCursor().position() - SelectedText.length(), Left, Right))
305  {
306  mCursor = textCursor();
307 
308  // Remove the inserted text.
309  mCursor.setPosition(mCursor.position() - SelectedText.length(), QTextCursor::KeepAnchor);
310  mCursor.removeSelectedText();
311 
312  int CurrentPosition = mCursor.position();
313 
314  // Determine the insertion point
315  objectBoundaries(mCursor.position(), Left, Right);
316 
317  if (CurrentPosition - Left < Right - CurrentPosition)
318  {
319  mCursor.setPosition(Left);
320  }
321  else
322  {
323  mCursor.setPosition(Right);
324  }
325 
326  mCursor.insertText(SelectedText);
327  setTextCursor(mCursor);
328  }
329 }
330 
332 {
333  int Left;
334  int Right;
335  bool isAdvancedEditing = CCopasiRootContainer::getConfiguration()->useAdvancedEditing();
336 
337  if (e == QKeySequence::SelectNextChar && !isAdvancedEditing)
338  {
339  mCursor = textCursor();
340  mCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
341 
342  if (objectBoundaries(mCursor.position(), Left, Right))
343  {
344  mCursor.setPosition(Right - 1, QTextCursor::KeepAnchor);
345  setTextCursor(mCursor);
346  }
347 
348  QTextEdit::keyPressEvent(e);
349 
350  return;
351  }
352 
353  if (e == QKeySequence::SelectPreviousChar && !isAdvancedEditing)
354  {
355  mCursor = textCursor();
356  mCursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
357 
358  if (objectBoundaries(mCursor.position(), Left, Right))
359  {
360  mCursor.setPosition(Left + 1, QTextCursor::KeepAnchor);
361  setTextCursor(mCursor);
362  }
363 
364  QTextEdit::keyPressEvent(e);
365 
366  return;
367  }
368 
369  switch (e->key())
370  {
371  case Qt::Key_Backspace:
372 
373  if (isAdvancedEditing) break;
374 
375  mCursor = textCursor();
376 
377  if (mCursor.selectedText().isEmpty())
378  {
379  mCursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
380 
381  if (objectBoundaries(mCursor.position(), Left, Right))
382  {
383  mCursor.setPosition(Left + 1, QTextCursor::KeepAnchor);
384  mCursor.deleteChar();
385  setTextCursor(mCursor);
386  }
387  }
388 
389  // delete the selected object or just the character
390  QTextEdit::keyPressEvent(e);
391 
392  return;
393  break;
394 
395  case Qt::Key_Delete:
396 
397  if (isAdvancedEditing) break;
398 
399  mCursor = textCursor();
400 
401  if (mCursor.selectedText().isEmpty())
402  {
403  mCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
404 
405  if (objectBoundaries(mCursor.position(), Left, Right))
406  {
407  mCursor.setPosition(Right - 1, QTextCursor::KeepAnchor);
408  mCursor.deleteChar();
409  setTextCursor(mCursor);
410  }
411  }
412 
413  // delete the selected object or just the character
414  QTextEdit::keyPressEvent(e);
415 
416  return;
417  break;
418 
419  case Qt::Key_Left:
420 
421  if (isAdvancedEditing) break;
422 
423  mCursor = textCursor();
424 
425  // We check whether the new position is in an object.
426  mCursor.movePosition(QTextCursor::PreviousCharacter);
427 
428  if (objectBoundaries(mCursor.position(), Left, Right))
429  {
430  mCursor.setPosition(Left + 1, QTextCursor::KeepAnchor);
431  setTextCursor(mCursor);
432  }
433 
434  QTextEdit::keyPressEvent(e);
435 
436  return;
437  break;
438 
439  case Qt::Key_Right:
440 
441  if (isAdvancedEditing) break;
442 
443  mCursor = textCursor();
444 
445  // We check whether the new position is in an object.
446  mCursor.movePosition(QTextCursor::NextCharacter);
447 
448  if (objectBoundaries(mCursor.position(), Left, Right))
449  {
450  mCursor.setPosition(Right - 1, QTextCursor::KeepAnchor);
451  setTextCursor(mCursor);
452  }
453 
454  QTextEdit::keyPressEvent(e);
455 
456  return;
457  break;
458 
459  case Qt::Key_BraceLeft:
460  case Qt::Key_BraceRight:
461 
462  if (isAdvancedEditing) break;
463 
464  e->ignore();
465  return;
466  break;
467  }
468 
469  if (!e->text().isEmpty())
470  {
471  // set format original
472  QTextCharFormat f;
473  f.setForeground(QColor(0, 0, 0));
474 
475  setCurrentCharFormat(f);
476  QTextEdit::keyPressEvent(e);
477 
478  return;
479  }
480 
481  QTextEdit::keyPressEvent(e);
482 }
483 
484 /**
485  * This slot checks any characters that are newly typed on Expression Widget.
486  */
488 {
489  CQValidator< QTextEdit > * pValidator = NULL;
490 
491  if (mpValidatorExpression != NULL)
492  pValidator = mpValidatorExpression;
493  else if (mpValidatorFunction != NULL)
494  pValidator = mpValidatorFunction;
495  else
496  return;
497 
498  int pos = 0;
499  QString Input = toPlainText();
500 
501  emit valid(pValidator->validate(Input, pos) == QValidator::Acceptable);
502 }
503 
504 bool CQExpressionWidget::objectBoundaries(const int & position, int & left, int & right) const
505 {
506  static QRegExp ObjectDisplayPattern(CQExpressionWidget::DisplayPattern);
507 
508  int Index = ObjectDisplayPattern.indexIn(toPlainText());
509 
510  while (0 <= Index && Index <= position)
511  {
512  left = Index;
513  Index += ObjectDisplayPattern.matchedLength();
514  right = Index;
515 
516  if (left < position && position < right)
517  {
518  return true;
519  }
520 
521  Index = ObjectDisplayPattern.indexIn(toPlainText(), Index);
522  }
523 
524  left = -1;
525  right = -1;
526 
527  return false;
528 }
529 
530 void CQExpressionWidget::setFunction(const std::string & function)
531 {
532  if (mpValidatorFunction == NULL)
533  {
536  }
537 
538  // clear the text edit
539  clear();
540 
541  mCursor = textCursor();
542 
543  QTextCharFormat f1;
544  f1.setForeground(QColor(0, 0, 0));
545 
546  setCurrentCharFormat(f1);
547  insertPlainText(FROM_UTF8(function));
548 }
549 
551 {
552  return TO_UTF8(toPlainText());
553 }
554 
555 void CQExpressionWidget::setExpression(const std::string & expression)
556 {
557  if (mpValidatorExpression == NULL)
558  {
561  }
562 
563  // Reset the parse list.
564  mParseList.clear();
565 
566  mCursor = textCursor();
567 
569  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
571  assert(pDataModel != NULL);
572  std::vector<CCopasiContainer*> containers;
573  containers.push_back(pDataModel);
574  containers.push_back(pFunDB);
575 
576  const QString Infix(FROM_UTF8(expression));
577  QString Display;
578 
579  QRegExp InfixObjectPattern(CQExpressionWidget::InfixPattern);
580 
581  int Index = 0;
582  QString::const_iterator it = Infix.begin();
583  QString::const_iterator end;
584 
585  while (true)
586  {
587  Index = InfixObjectPattern.indexIn(Infix, Index);
588 
589  if (Index < 0)
590  {
591  end = Infix.end();
592  }
593  else
594  {
595  end = Infix.begin() + Index;
596  }
597 
598  // Copy the non-object part
599  for (; it != end; ++it)
600  {
601  Display.append(*it);
602  }
603 
604  if (InfixObjectPattern.matchedLength() < 0)
605  break;
606 
607  Index += InfixObjectPattern.matchedLength();
608  it += InfixObjectPattern.matchedLength();
609 
610  CCopasiObjectName InfixName(TO_UTF8(InfixObjectPattern.cap(1)));
611  CCopasiObject * pObject = pDataModel->ObjectFromName(containers, InfixName);
612 
613  if (pObject != NULL)
614  {
615  std::string DisplayName = pObject->getObjectDisplayName();
616 
617  // We need to escape '\' and '}'
618  std::string::size_type pos = DisplayName.find_first_of("\\}");
619 
620  while (pos != std::string::npos)
621  {
622  DisplayName.insert(pos, "\\");
623  pos += 2;
624  pos = DisplayName.find_first_of("\\}", pos);
625  }
626 
627  mParseList[DisplayName] = pObject;
628 
629  Display += '{' + FROM_UTF8(DisplayName) + '}';
630  }
631  else
632  {
633  std::string DisplayName = InfixName;
634 
635  // We need to escape '\' and '}'
636  std::string::size_type pos = DisplayName.find_first_of("\\}");
637 
638  while (pos != std::string::npos)
639  {
640  DisplayName.insert(pos, "\\");
641  pos += 2;
642  pos = DisplayName.find_first_of("\\}", pos);
643  }
644 
645  Display += '{' + FROM_UTF8(DisplayName) + '}';
646  }
647  }
648 
649  setText(Display);
651 
652  return;
653 }
654 
655 const CCopasiObject* findObjectByDisplayName(const CCopasiDataModel* dataModel, const std::string displayString)
656 {
657  if (dataModel == NULL || displayString.empty()) return NULL;
658 
659  const CModel* model = dataModel->getModel();
660 
661  if (displayString == "Time") return model;
662 
663  if (displayString == "Avogadro Constant") return dynamic_cast<const CCopasiObject* >(model->getObject("Reference=" + displayString));
664 
665  if (displayString == "Quantity Conversion Factor") return dynamic_cast<const CCopasiObject* >(model->getObject("Reference=" + displayString));
666 
667  size_t pos = displayString.find("Compartments[");
668 
669  if (pos != std::string::npos)
670  {
671  const CCopasiVectorN< CCompartment > & compartments = model->getCompartments();
672 
673  for (CCopasiVectorN< CCompartment >::const_iterator it = compartments.begin(); it != compartments.end(); ++it)
674  {
675  const CCompartment *current = *it;
676 
677  if (current->getObjectDisplayName() == displayString)
678  {
679  return current;
680  }
681  else if (current->getInitialValueReference() != NULL &&
682  current->getInitialValueReference()->getObjectDisplayName() == displayString)
683  {
684  return current->getInitialValueReference();
685  }
686  else if (current->getValueReference() != NULL &&
687  current->getValueReference()->getObjectDisplayName() == displayString)
688  {
689  return current->getValueReference();
690  }
691  else if (current->getRateReference() != NULL &&
692  current->getRateReference()->getObjectDisplayName() == displayString)
693  {
694  return current->getRateReference();
695  }
696  }
697  }
698 
699  pos = displayString.find("Values[");
700 
701  if (pos != std::string::npos)
702  {
703  const CCopasiVectorN< CModelValue > & values = model->getModelValues();
704 
705  for (CCopasiVectorN< CModelValue >::const_iterator it = values.begin(); it != values.end(); ++it)
706  {
707  const CModelValue *current = *it;
708 
709  if (current->getObjectDisplayName() == displayString)
710  {
711  return current;
712  }
713  else if (current->getInitialValueReference() != NULL &&
714  current->getInitialValueReference()->getObjectDisplayName() == displayString)
715  {
716  return current->getInitialValueReference();
717  }
718  else if (current->getValueReference() != NULL &&
719  current->getValueReference()->getObjectDisplayName() == displayString)
720  {
721  return current->getValueReference();
722  }
723  else if (current->getRateReference() != NULL &&
724  current->getRateReference()->getObjectDisplayName() == displayString)
725  {
726  return current->getRateReference();
727  }
728  }
729  }
730 
731  // no reasonable check for metabolites, so lets just go through them
732  {
733  const CCopasiVector< CMetab > & metabs = model->getMetabolites();
734 
735  for (CCopasiVector< CMetab >::const_iterator it = metabs.begin(); it != metabs.end(); ++it)
736  {
737  const CMetab *current = *it;
738 
739  if (current->getObjectDisplayName() == displayString)
740  {
741  return current;
742  }
743  else if (current->getInitialValueReference() != NULL &&
744  current->getInitialValueReference()->getObjectDisplayName() == displayString)
745  {
746  return current->getInitialValueReference();
747  }
748  else if (current->getInitialConcentrationReference() != NULL &&
749  current->getInitialConcentrationReference()->getObjectDisplayName() == displayString)
750  {
751  return current->getInitialConcentrationReference();
752  }
753  else if (current->getValueReference() != NULL &&
754  current->getValueReference()->getObjectDisplayName() == displayString)
755  {
756  return current->getValueReference();
757  }
758  else if (current->getConcentrationReference() != NULL &&
759  current->getConcentrationReference()->getObjectDisplayName() == displayString)
760  {
761  return current->getConcentrationReference();
762  }
763  else if (current->getRateReference() != NULL &&
764  current->getRateReference()->getObjectDisplayName() == displayString)
765  {
766  return current->getRateReference();
767  }
768  else if (current->getConcentrationRateReference() != NULL &&
769  current->getConcentrationRateReference()->getObjectDisplayName() == displayString)
770  {
771  return current->getConcentrationRateReference();
772  }
773  }
774  }
775 
776  return NULL;
777 }
778 
780 {
781  QString Infix;
782  const QString Display(toPlainText());
783 
784  QRegExp DisplayObjectPattern(CQExpressionWidget::DisplayPattern);
785 
786  int Index = 0;
787  QString::const_iterator it = Display.begin();
788  QString::const_iterator end;
789 
790  while (true)
791  {
792  Index = DisplayObjectPattern.indexIn(Display, Index);
793 
794  if (Index < 0)
795  {
796  end = Display.end();
797  }
798  else
799  {
800  end = Display.begin() + Index;
801  }
802 
803  // Copy the non-object part
804  for (; it != end; ++it)
805  {
806  Infix.append(*it);
807  }
808 
809  if (DisplayObjectPattern.matchedLength() < 0)
810  break;
811 
812  Index += DisplayObjectPattern.matchedLength();
813  it += DisplayObjectPattern.matchedLength();
814 
815  std::string DisplayName(TO_UTF8(DisplayObjectPattern.cap(1)));
816  std::map< std::string, const CCopasiObject *>::const_iterator itObject = mParseList.find(DisplayName);
817 
819  {
820 
821  // the object pattern does not match the species name if
822  // the species is in a different compartment, in that case we
823  // have to remove the backslash in the displayname.
824  std::string::size_type bsPos = DisplayName.find("\\}");
825 
826  if (bsPos != std::string::npos)
827  DisplayName.erase(bsPos, 1);
828 
829  // here we don't have an object recognized, what we ought to do is to find it in the model
830  const CCopasiObject* object = findObjectByDisplayName(
832  DisplayName);
833 
834  if (object != NULL)
835  {
836  const_cast<CQExpressionWidget*>(this)->mParseList[DisplayName] = object;
837  itObject = mParseList.find(DisplayName);
838  }
839  }
840 
841  if (itObject != mParseList.end())
842  Infix += "<" + FROM_UTF8(itObject->second->getCN()) + ">";
843  else
844  {
845  Infix += "<";
846 
847  // We need to escape '\'
848  std::string CN = TO_UTF8(DisplayObjectPattern.cap(1));
849  std::string::const_iterator it = CN.begin();
850  std::string::const_iterator end = CN.end();
851 
852  for (; it < end; ++it)
853  {
854  if (*it == '\\')
855  {
856  ++it;
857  }
858 
859  Infix += *it;
860  }
861 
862  Infix += ">";
863  }
864  }
865 
866  return TO_UTF8(Infix);
867 }
868 
870 {
871  mObjectClasses = expressionType;
872 }
873 
874 void CQExpressionWidget::setBoolean(bool isBoolean)
875 {
876  if (mpValidatorExpression == NULL)
877  {
880  }
881 
883 };
884 
886 {
887  if (mpValidatorExpression != NULL)
888  return mpValidatorExpression->revalidate() == QValidator::Acceptable;
889  else if (mpValidatorFunction != NULL)
890  return mpValidatorFunction->revalidate() == QValidator::Acceptable;
891 
892  return true;
893 }
894 
896 {
897  const CCopasiObject * pObject =
899 
900  if (pObject)
901  {
902  // Check whether the object is valid
904  {
905  CQMessageBox::critical(this, "Invalid Selection",
906  "The use of the selected object is not allowed in this type of expression.");
907  return;
908  }
909 
910  std::string Insert = pObject->getObjectDisplayName();
911 
912  // We need to escape >
913  std::string::size_type pos = Insert.find_first_of("}");
914 
915  while (pos != std::string::npos)
916  {
917  Insert.insert(pos, "\\");
918  pos += 2;
919  pos = Insert.find_first_of("}", pos);
920  }
921 
922  mParseList[Insert] = pObject;
923 
924  QTextCharFormat f1;
925  f1.setForeground(QColor(0, 0, 0));
926 
927  QTextCharFormat f = mpExpressionHighlighter->mObjectDisplayFormat;
928  //QColor color2 = f.foreground().color();
929 
930  setCurrentCharFormat(f);
931  insertPlainText(FROM_UTF8("{" + Insert + "}"));
932  setCurrentCharFormat(f1);
933  }
934 }
Header file of class CExpression.
CCopasiDataModel * getObjectDataModel()
virtual bool setObjectParent(const CCopasiContainer *pParent)
CQValidatorFunction(QTextEdit *parent, const char *name=0)
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
CQExpressionHighlighter(CQExpressionWidget *ew)
static const char * Infix[]
Definition: CMassAction.h:34
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
void highlightBlock(const QString &text)
const CCopasiVector< CMetab > & getMetabolites() const
Definition: CModel.cpp:1051
static const char DisplayPattern[]
virtual std::string writeMathML(const std::vector< std::vector< std::string > > &variables, bool expand=true, bool fullExpand=true) const
const CCopasiVectorN< CModelValue > & getModelValues() const
Definition: CModel.cpp:1060
virtual void mouseReleaseEvent(QMouseEvent *e)
CCopasiObject * getInitialValueReference() const
virtual CExpression * getExpression()
iterator begin()
static const char InfixPattern[]
void setExpression(const std::string &expression)
std::string getFunction() const
virtual void keyPressEvent(QKeyEvent *e)
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
Definition: CMetab.cpp:974
virtual CFunction * getFunction()
virtual State validate(QString &input, int &pos) const
const CCopasiObject * findObjectByDisplayName(const CCopasiDataModel *dataModel, const std::string displayString)
void setIsBoolean(const bool &booleanRequired)
Definition: CExpression.cpp:58
Definition: CMetab.h:178
The class for highlighting the expression syntax.
static bool filter(const ObjectClasses &classes, const CCopasiObject *pObject)
static CConfigurationFile * getConfiguration()
iterator end()
std::map< std::string, const CCopasiObject * > mParseList
virtual void saved() const
Definition: CQValidator.h:71
bool useAdvancedEditing() const
CQValidatorFunction * mpValidatorFunction
virtual State revalidate()
Definition: CQValidator.h:63
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
void stateChanged(bool) const
const CCopasiObject * mpCurrentObject
CCopasiObject * getConcentrationRateReference() const
Definition: CMetab.cpp:867
virtual void writeMathML(std::ostream &out, size_t l=0) const
Definition: CFunction.cpp:389
void setExpressionType(const ExpressionType &expressionType)
QString(QTextEdit::* mRetrieve)(void) const
Definition: CQValidator.h:104
QTextCharFormat mObjectDisplayFormat
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
void createListOfParametersForMathML(std::vector< std::vector< std::string > > &env)
Definition: CFunction.cpp:340
static CFunctionDB * getFunctionList()
CQValidatorExpression * mpValidatorExpression
void writeMathML(std::ostream &out) const
CQSimpleSelectionTree::ObjectClasses mObjectClasses
std::string getExpression() const
CCopasiVectorNS< CCompartment > & getCompartments()
Definition: CModel.cpp:1145
Header file of class CArrayAnnotation.
virtual State validate(QString &input, int &pos) const
void setBooleanRequired(bool booleanRequired)
static StandardButton critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
virtual void dropEvent(QDropEvent *e)
CQValidatorExpression(QTextEdit *parent, const char *name=0, bool isBoolean=false)
void valid(bool valid)
void setFunction(const std::string &function)
The class for handling a chemical kinetic function.
Definition: CFunction.h:29
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
CQExpressionHighlighter * mpExpressionHighlighter
void writeMathML(std::ostream &out, bool fullExpand, size_t l) const
Definition: CModel.h:50
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
std::ostream & operator<<(std::ostream &os, const QTextCursor &d)
CQValidator< QTextEdit > * getValidator()
virtual State validate(QString &input, int &) const
Definition: CQValidator.h:51
CConcentrationReference * getInitialConcentrationReference() const
Definition: CMetab.cpp:861
CCopasiObject * getRateReference() const
CCopasiObject * ObjectFromName(const std::vector< CCopasiContainer * > &listOfContainer, const CCopasiObjectName &CN) const
State setColor(const State &state) const
Definition: CQValidator.h:84
CQExpressionWidget(QWidget *parent=0, const char *name=0)
static const CCopasiObject * getObjectSingle(QWidget *pParent, const CQSimpleSelectionTree::ObjectClasses &classes, const CCopasiObject *pCurrentObject=NULL)
void setBoolean(bool isBoolean)
CConcentrationReference * getConcentrationReference() const
Definition: CMetab.cpp:864
bool objectBoundaries(const int &position, int &left, int &right) const
CCopasiObject * getValueReference() const