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<const QDoubleValidator*>( 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.
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();
QtxPreferenceItem* parent, const QString& sect,
const QString& param )
: QtxPageNamedPrefItem( title, parent, sect, param ),
- myType( type )
+ myType( String ),
+ myDecimals( 1000 )
{
setControl( myEditor = new QLineEdit() );
updateEditor();
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()
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 );
}
/*!
{
if ( name == "input_type" || name == "type" )
return inputType();
+ else if ( name == "precision" || name == "prec" || name == "decimals" )
+ return decimals();
else
return QtxPageNamedPrefItem::optionValue( name );
}
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 );
}
break;
case Double:
val = new QDoubleValidator( myEditor );
+ dynamic_cast<QDoubleValidator*>( 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 );
}
/*!