00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef _OSBLINEEDITS_H_
00031 #define _OSBLINEEDITS_H_
00032
00033
00034
00035
00036 #include <sstream>
00037 #include <limits>
00038
00039
00040 #include <qlineedit.h>
00041 #include <qvalidator.h>
00042
00043
00044
00045
00046
00047 namespace OSB_LIB {
00048 class Decimal;
00049 }
00050
00051
00052
00053 namespace OSB_GUI {
00054
00055
00056
00057
00065 template<typename T, typename Limits = std::numeric_limits<T> >
00066 class RangeValidator : public QValidator {
00067 public:
00069 explicit RangeValidator(
00070 QObject* parent,
00071 const char* name = "podValidator_"
00072 ) : QValidator(parent, name),
00073 bottom_(Limits::min()), top_(Limits::max())
00074 {
00075 if (!Limits::is_integer) bottom_ = -top_;
00076 }
00078 RangeValidator(
00079 const T& bottom,
00080 const T& top,
00081 QObject* parent,
00082 const char* name = "podValidator_"
00083 ) : QValidator(parent, name), bottom_(bottom), top_(top)
00084 {
00085
00086 }
00087 private:
00089 RangeValidator(const RangeValidator&);
00091 RangeValidator& operator=(const RangeValidator&);
00092 public:
00094
00095
00096 const T& bottom() const
00097 {
00098 return bottom_;
00099 }
00101 const T& top() const
00102 {
00103 return top_;
00104 }
00114 bool getValue(T& t, const QString& s) const;
00116
00118
00119
00120 void setBottom(const T& bottom)
00121 {
00122 bottom_ = bottom;
00123 }
00125 void setTop(const T& top)
00126 {
00127 top_ = top;
00128 }
00130 void setRange(const T& bottom, const T& top)
00131 {
00132 bottom_ = bottom;
00133 top_ = top;
00134 }
00136 public:
00147 QValidator::State validate(QString& input, int& pos) const;
00148 private:
00150 T bottom_;
00152 T top_;
00153 };
00154
00167 template<typename T, typename Limits = std::numeric_limits<T> >
00168 class RangeLineEdit : public QLineEdit {
00170 typedef RangeValidator<T, Limits> Validator;
00171 public:
00173 explicit RangeLineEdit(
00174 QWidget* parent,
00175 const char* name = "podLineEdit_"
00176 ) : QLineEdit(parent, name)
00177 {
00178 validator_ = new Validator(this);
00179 setValidator(validator_);
00180 }
00182 RangeLineEdit(
00183 const T& bottom,
00184 const T& top,
00185 QWidget* parent,
00186 const char* name = "podLineEdit_"
00187 ) : QLineEdit(parent, name)
00188 {
00189 validator_ = new Validator(bottom, top, this);
00190 }
00191 private:
00193 RangeLineEdit(const RangeLineEdit&);
00195 RangeLineEdit& operator=(const RangeLineEdit&);
00196 public:
00198 const T& bottom() const
00199 {
00200 return validator_->bottom();
00201 }
00203 const T& top() const
00204 {
00205 return validator_->top();
00206 }
00215 T value(bool* ok = 0) const
00216 {
00217 T rc;
00218 bool ret = validator_->getValue(rc, text());
00219 if (ok) *ok = ret;
00220 return rc;
00221 }
00222
00224 void setValue(const T& t)
00225 {
00226 std::ostringstream os;
00227 os << t;
00228 setText(os.str());
00229 }
00231 void setBottom(const T& bottom)
00232 {
00233 validator_->setBottom(bottom);
00234 }
00236 void setTop(const T& top)
00237 {
00238 validator_->setTop(top);
00239 }
00241 void setRange(const T& bottom, const T& top)
00242 {
00243 validator_->setRange(bottom, top);
00244 }
00245 private:
00247 Validator* validator_;
00248 };
00249
00253 class LeFixedWidth : public QLineEdit {
00254 public:
00256 LeFixedWidth(
00257 QWidget* parent,
00258 const char* name
00259 );
00266 void setText(const QString& text);
00268 void clear();
00269 };
00270
00274 class LeId : public LeFixedWidth {
00280 static const int minWidth_ = 30;
00281 public:
00283 LeId(
00284 QWidget* parent,
00285 const char* name
00286 );
00293 template<typename Id>
00294 void setId(const Id& id);
00295 };
00296
00300 class LeDecimal : public QLineEdit {
00301 Q_OBJECT
00302 public:
00304 LeDecimal(
00305 QWidget* parent,
00306 const char* name = "leDecimal_"
00307 );
00308 public:
00310 OSB_LIB::Decimal value() const;
00312 void setValue(const OSB_LIB::Decimal& d);
00313
00315 int precision() const;
00317 bool positive() const;
00325 int setPrecision(int p);
00331 void setPositive(bool posOnly);
00339 void setup(int p, bool posOnly);
00340 private:
00342 double toDouble() const;
00344 void setValue(double d);
00346 void setRegExp();
00347 private slots:
00349 void fixDecimals();
00350 private:
00352 bool positive_;
00354 int precision_;
00356 QRegExpValidator* validator_;
00357 };
00358 }
00359
00360
00361
00362 namespace OSB_GUI {
00363
00364
00365
00366 template<typename T, typename Limits>
00367 QValidator::State
00368 RangeValidator<T, Limits>::validate(
00369 QString& input,
00370 int&
00371 ) const
00372 {
00373 if (input.isEmpty()) return QValidator::Intermediate;
00374
00375 T t;
00376 bool ret = getValue(t, input);
00377 if (ret) return QValidator::Acceptable;
00378
00379
00380 if (Limits::is_signed) {
00381 if ("-" == input) return QValidator::Intermediate;
00382 }
00383 return QValidator::Invalid;
00384 }
00385
00386 template<typename T, typename Limits>
00387 bool RangeValidator<T, Limits>::getValue(
00388 T& t,
00389 const QString& s
00390 ) const
00391 {
00392 bool rc = true;
00393 std::istringstream is(s);
00394 is >> std::noskipws >> t;
00395
00396 if (!is) rc = false;
00397
00398 if (rc) {
00399 char c;
00400 is >> c;
00401 if (is) rc = false;
00402 }
00403 if (rc) {
00404 if (t < bottom_) rc = false;
00405 }
00406 if (rc) {
00407 if (t > top_) rc = false;
00408 }
00409
00410 if (!rc) t = T();
00411 return rc;
00412 }
00413
00414
00415
00416 template<typename Id>
00417 void LeId::setId(const Id& id)
00418 {
00419 if (id.notSet()) clear();
00420 else {
00421 std::ostringstream os;
00422 os << id;
00423 setText(os.str());
00424 }
00425 }
00426 }
00427 #endif // #ifndef _OSBLINEEDITS_H_