From 667bca998aab8af9b8819bf234b921b0c059ac83 Mon Sep 17 00:00:00 2001 From: vsr Date: Thu, 20 Jan 2011 11:33:54 +0000 Subject: [PATCH] 0020997: [CEA 420] Problem with VISU preferences --- src/Qtx/QtxPagePrefMgr.cxx | 85 ++++++++++++++++++++++++++++++-------- src/Qtx/QtxPagePrefMgr.h | 4 ++ 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/src/Qtx/QtxPagePrefMgr.cxx b/src/Qtx/QtxPagePrefMgr.cxx index 979ee0e64..938271307 100644 --- a/src/Qtx/QtxPagePrefMgr.cxx +++ b/src/Qtx/QtxPagePrefMgr.cxx @@ -2053,6 +2053,36 @@ void QtxPagePrefCheckItem::retrieve() for string, integer and double values. */ +static void fixupAndSet( QLineEdit* le, const QString& txt ) +{ + if ( le ) { + QString val = txt; + if ( le->validator() ) { + const QDoubleValidator* de = dynamic_cast( le->validator() ); + if ( de ) { + int dec = de->decimals(); + int idx = val.lastIndexOf( QRegExp( QString("[.|%1]").arg( le->locale().decimalPoint () ) ) ); + if ( idx >= 0 ) { + QString tmp = val.mid( idx+1 ); + QString exp; + val = val.left( idx+1 ); + idx = tmp.indexOf( QRegExp( QString("[e|E]") ) ); + if ( idx >= 0 ) { + exp = tmp.mid( idx ); + tmp = tmp.left( idx ); + } + tmp.truncate( dec ); + val = val + tmp + exp; + } + } + int pos = 0; + if ( le->validator()->validate( val, pos ) == QValidator::Invalid ) + val.clear(); + } + le->setText( val ); + } +} + /*! \brief Constructor. @@ -2066,7 +2096,8 @@ void QtxPagePrefCheckItem::retrieve() QtxPagePrefEditItem::QtxPagePrefEditItem( const QString& title, QtxPreferenceItem* parent, const QString& sect, const QString& param ) : QtxPageNamedPrefItem( title, parent, sect, param ), - myType( String ) + myType( String ), + myDecimals( 1000 ) { setControl( myEditor = new QLineEdit() ); updateEditor(); @@ -2088,7 +2119,8 @@ QtxPagePrefEditItem::QtxPagePrefEditItem( const int type, const QString& title, QtxPreferenceItem* parent, const QString& sect, const QString& param ) : QtxPageNamedPrefItem( title, parent, sect, param ), - myType( type ) + myType( type ), + myDecimals( 1000 ) { setControl( myEditor = new QLineEdit() ); updateEditor(); @@ -2125,6 +2157,30 @@ void QtxPagePrefEditItem::setInputType( const int type ) updateEditor(); } +/*! + \brief Get number of digits after decimal point (for Double input type) + \return preference item decimals value + \sa setDecimals() +*/ +int QtxPagePrefEditItem::decimals() const +{ + return myDecimals; +} + +/*! + \brief Set number of digits after decimal point (for Double input type) + \param dec new preference item decimals value + \sa decimals() +*/ +void QtxPagePrefEditItem::setDecimals( const int dec ) +{ + if ( myDecimals == dec ) + return; + + myDecimals = dec; + updateEditor(); +} + /*! \brief Store preference item to the resource manager. \sa retrieve() @@ -2141,13 +2197,7 @@ void QtxPagePrefEditItem::store() void QtxPagePrefEditItem::retrieve() { QString txt = getString(); - if ( myEditor->validator() ) - { - int pos = 0; - if ( myEditor->validator()->validate( txt, pos ) == QValidator::Invalid ) - txt.clear(); - } - myEditor->setText( txt ); + fixupAndSet( myEditor, txt ); } /*! @@ -2160,6 +2210,8 @@ QVariant QtxPagePrefEditItem::optionValue( const QString& name ) const { if ( name == "input_type" || name == "type" ) return inputType(); + else if ( name == "precision" || name == "prec" || name == "decimals" ) + return decimals(); else return QtxPageNamedPrefItem::optionValue( name ); } @@ -2177,6 +2229,10 @@ void QtxPagePrefEditItem::setOptionValue( const QString& name, const QVariant& v if ( val.canConvert( QVariant::Int ) ) setInputType( val.toInt() ); } + else if ( name == "precision" || name == "prec" || name == "decimals" ) { + if ( val.canConvert( QVariant::Int ) ) + setDecimals( val.toInt() ); + } else QtxPageNamedPrefItem::setOptionValue( name, val ); } @@ -2194,21 +2250,16 @@ void QtxPagePrefEditItem::updateEditor() break; case Double: val = new QDoubleValidator( myEditor ); + dynamic_cast( val )->setDecimals( myDecimals < 0 ? 1000 : myDecimals ); break; default: break; } - if ( !myEditor->text().isEmpty() && val ) - { - int pos = 0; - QString str = myEditor->text(); - if ( val->validate( str, pos ) == QValidator::Invalid ) - myEditor->clear(); - } - + QString txt = myEditor->text(); delete myEditor->validator(); myEditor->setValidator( val ); + fixupAndSet( myEditor, txt ); } /*! diff --git a/src/Qtx/QtxPagePrefMgr.h b/src/Qtx/QtxPagePrefMgr.h index 5b7ee10eb..592e8da35 100644 --- a/src/Qtx/QtxPagePrefMgr.h +++ b/src/Qtx/QtxPagePrefMgr.h @@ -405,6 +405,9 @@ public: int inputType() const; void setInputType( const int ); + int decimals() const; + void setDecimals( const int ); + virtual void store(); virtual void retrieve(); @@ -417,6 +420,7 @@ private: private: int myType; + int myDecimals; QLineEdit* myEditor; }; -- 2.39.2