COPASI API  4.16.103
CQValidator.h
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) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #ifndef COPASI_CQValidator
16 #define COPASI_CQValidator
17 
18 #include <QtGui/QValidator>
19 #include <QtGui/QColor>
20 #include <QtGui/QLineEdit>
21 
22 #include <QtCore/QtDebug>
23 
24 #include "copasi.h"
25 
26 template <typename Type> class CQValidator : public QValidator
27 {
28  // Operations
29 public:
30  CQValidator(Type * parent, QString(Type::*retrieve)(void) const, const char * name = 0):
31  QValidator(parent),
32  mpContainer(parent),
33  mRetrieve(retrieve),
34  mLastAccepted("")
35  {
36  setObjectName(QString::fromUtf8(name));
37  int h, s, v;
38 
39  QPalette palette = mpContainer->palette();
40  QBrush brush = palette.brush(QPalette::Active, QPalette::Base);
41  mSavedColor = brush.color();
42 // mSavedColor = mpLineEdit->paletteBackgroundColor();
43  mSavedColor.getHsv(&h, &s, &v);
44 
45  if (s < 20) s = 20;
46 
47  mAcceptableColor.setHsv(240, s, v);
48  mErrorColor.setHsv(0, s, v);
49  }
50 
51  virtual State validate(QString & input, int & /* pos */) const
52  {
53  if (input == mLastAccepted)
54  {
55  mpContainer->setStyleSheet(QString(mpContainer->metaObject()->className()) + " {background-color:" + mSavedColor.name() + ";}");
56  }
57  else
58  setColor(Acceptable);
59 
60  return Acceptable;
61  }
62 
63  virtual State revalidate()
64  {
65  QString Input = (*mpContainer.*mRetrieve)();
66  int Pos = Input.length();
67 
68  return validate(Input, Pos);
69  }
70 
71  virtual void saved() const
72  {
73  const_cast<CQValidator *>(this)->mLastAccepted = (*mpContainer.*mRetrieve)();
74  const_cast<CQValidator *>(this)->revalidate();
75  }
76 
77  virtual void force(const QString & input) const
78  {
79  const_cast<CQValidator *>(this)->mLastAccepted = input;
80  setColor(Acceptable);
81  }
82 
83 protected:
84  State setColor(const State & state) const
85  {
86  if (state == Invalid)
87  {
88  mpContainer->setStyleSheet(QString(mpContainer->metaObject()->className()) + " {background-color:" + mErrorColor.name() + ";}");
89  }
90  else
91  {
92  mpContainer->setStyleSheet(QString(mpContainer->metaObject()->className()) + " {background-color:" + mAcceptableColor.name() + ";}");
93  }
94 
95  return state;
96  }
97 
98  //Attributes
99  Type * mpContainer;
100 
101  /**
102  * Method to retrieve the content of the container as a QString
103  */
104  QString(Type::*mRetrieve)(void) const;
105 
106  QString mLastAccepted;
107 
108 private:
109  QColor mSavedColor;
110 
112 
113  QColor mErrorColor;
114 };
115 
116 class CQValidatorNotEmpty : public CQValidator< QLineEdit >
117 {
118  // Operations
119 public:
120  CQValidatorNotEmpty(QLineEdit * parent, const char * name = 0);
121 
122  virtual State validate(QString & input, int & pos) const;
123 };
124 
125 class CQValidatorBound : public CQValidator< QLineEdit >
126 {
127  // Operations
128 public:
129  CQValidatorBound(QLineEdit * parent, const QString & sign);
130 
131  virtual State validate(QString & input, int & pos) const;
132 
133  virtual void force(const QString & input) const;
134 
135  //Attributes
136 protected:
137  QDoubleValidator * mpDoubleValidator;
138 
139  QString mSign;
140 
141  QString mValidBound;
142 };
143 
144 class CQValidatorDouble : public CQValidator< QLineEdit >
145 {
146  // Operations
147 public:
148  CQValidatorDouble(QLineEdit * parent, const char * name = 0);
149 
150  virtual State validate(QString & input, int & pos) const;
151 
152  void setRange(const C_FLOAT64 & lowerBound, const C_FLOAT64 & upperBound);
153 
154  //Attributes
155 protected:
156  QDoubleValidator * mpDoubleValidator;
157 };
158 
159 class CQValidatorInt : public CQValidator< QLineEdit >
160 {
161  // Operations
162 public:
163  CQValidatorInt(QLineEdit * parent, const char * name = 0);
164 
165  virtual State validate(QString & input, int & pos) const;
166 
167  void setRange(const int & lowerBound, const int & upperBound);
168 
169  //Attributes
170 protected:
171  QIntValidator * mpIntValidator;
172 };
173 
174 #endif // COPASI_CQValidator
CQValidatorInt(QLineEdit *parent, const char *name=0)
Definition: CQValidator.cpp:83
virtual State validate(QString &input, int &pos) const
Definition: CQValidator.cpp:89
QColor mAcceptableColor
Definition: CQValidator.h:111
QIntValidator * mpIntValidator
Definition: CQValidator.h:171
CQValidatorNotEmpty(QLineEdit *parent, const char *name=0)
Definition: CQValidator.cpp:21
QDoubleValidator * mpDoubleValidator
Definition: CQValidator.h:156
virtual void force(const QString &input) const
Definition: CQValidator.cpp:59
virtual void saved() const
Definition: CQValidator.h:71
virtual State revalidate()
Definition: CQValidator.h:63
QString mValidBound
Definition: CQValidator.h:141
QColor mSavedColor
Definition: CQValidator.h:109
virtual State validate(QString &input, int &pos) const
Definition: CQValidator.cpp:71
QDoubleValidator * mpDoubleValidator
Definition: CQValidator.h:137
QString(Type::* mRetrieve)(void) const
Definition: CQValidator.h:104
virtual void force(const QString &input) const
Definition: CQValidator.h:77
QString mLastAccepted
Definition: CQValidator.h:106
Type * mpContainer
Definition: CQValidator.h:99
CQValidator(Type *parent, QString(Type::*retrieve)(void) const, const char *name=0)
Definition: CQValidator.h:30
#define C_FLOAT64
Definition: copasi.h:92
void setRange(const C_FLOAT64 &lowerBound, const C_FLOAT64 &upperBound)
Definition: CQValidator.cpp:80
void setRange(const int &lowerBound, const int &upperBound)
Definition: CQValidator.cpp:98
CQValidatorBound(QLineEdit *parent, const QString &sign)
Definition: CQValidator.cpp:34
() void(yyvaluep))
QColor mErrorColor
Definition: CQValidator.h:113
virtual State validate(QString &input, int &pos) const
Definition: CQValidator.cpp:41
virtual State validate(QString &input, int &pos) const
Definition: CQValidator.cpp:25
virtual State validate(QString &input, int &) const
Definition: CQValidator.h:51
CQValidatorDouble(QLineEdit *parent, const char *name=0)
Definition: CQValidator.cpp:66
State setColor(const State &state) const
Definition: CQValidator.h:84