#include "SALOMEDSClient_ClientFactory.hxx"
#include CORBA_SERVER_HEADER(SALOMEDS)
+#include <QLineEdit>
+
/*!
\class SalomeApp_DoubleSpinBox
*/
myMinimum( 0.0 ),
myMaximum( 99.99 )
{
+ connectSignalsAndSlots();
}
/*!
myMinimum( min ),
myMaximum( max )
{
+ connectSignalsAndSlots();
}
/*!
myMinimum( min ),
myMaximum( max )
{
+ connectSignalsAndSlots();
}
/*!
{
}
+/*!
+ \brief Connect signals and slots.
+*/
+void SalomeApp_DoubleSpinBox::connectSignalsAndSlots()
+{
+ connect( this, SIGNAL( editingFinished() ),
+ this, SLOT( onEditingFinished() ) );
+
+ connect( lineEdit(), SIGNAL( textChanged( const QString& ) ),
+ this, SLOT( onTextChanged( const QString& ) ) );
+}
+
+/*!
+ \brief This function is called when editing is finished.
+*/
+void SalomeApp_DoubleSpinBox::onEditingFinished()
+{
+ if( myTextValue.isNull() )
+ myTextValue = text();
+
+ lineEdit()->setText( myTextValue );
+}
+
+/*!
+ \brief This function is called when value is changed.
+*/
+void SalomeApp_DoubleSpinBox::onTextChanged( const QString& text )
+{
+ myTextValue = text;
+
+ double value = 0;
+ if( isValid( text, value ) == Acceptable )
+ myCorrectValue = text;
+}
+
/*!
\brief Interpret text entered by the user as a value.
\param text text entered by the user
*/
double SalomeApp_DoubleSpinBox::valueFromText( const QString& text ) const
{
- QString str = text;
-
- bool ok = false;
double value = 0;
- if( findVariable( str, value ) )
- ok = true;
- else
- value = str.toDouble( &ok );
-
- if( ok && checkRange( value ) )
+ if( isValid( text, value ) == Acceptable )
return value;
return defaultValue();
\brief This function is used to determine whether input is valid.
\return validating operation result
*/
-bool SalomeApp_DoubleSpinBox::isValid() const
+bool SalomeApp_DoubleSpinBox::isValid( QString& msg, bool toCorrect )
{
- QString str = text();
+ double value;
+ State aState = isValid( text(), value );
- bool ok = false;
- double value = 0;
- if( findVariable( str, value ) )
- ok = true;
- else
- value = str.toDouble( &ok );
+ if( aState != Acceptable )
+ {
+ if( toCorrect )
+ {
+ if( aState == NoVariable )
+ msg += tr( "ERR_NO_VARIABLE" ).arg( text() ) + "\n";
+ else if( aState == Invalid )
+ msg += tr( "ERR_INVALID_VALUE" ) + "\n";
+
+ lineEdit()->setText( myCorrectValue );
+ }
+ return false;
+ }
- return ok && checkRange( value );
+ return true;
}
/*!
- \brief This function is used to set minimum and maximum values for this spinbox.
+ \brief This function is used to set a default value for this spinbox.
\param value default value
*/
+void SalomeApp_DoubleSpinBox::setDefaultValue( const double value )
+{
+ myDefaultValue = value;
+}
+
+/*!
+ \brief This function is used to set minimum and maximum values for this spinbox.
+ \param min minimum value
+ \param max maximum value
+*/
void SalomeApp_DoubleSpinBox::setRange( const double min, const double max )
{
QtxDoubleSpinBox::setRange( min, max );
}
/*!
- \brief This function is used to set a default value for this spinbox.
- \param value default value
+ \brief This function is used to set a current value for this spinbox.
+ \param value current value
*/
-void SalomeApp_DoubleSpinBox::setDefaultValue( const double value )
+void SalomeApp_DoubleSpinBox::setValue( const double value )
{
- myDefaultValue = value;
+ QtxDoubleSpinBox::setValue( value );
+
+ myCorrectValue = QString::number( value );
+}
+
+/*!
+ \brief This function is used to determine whether input is valid.
+ \return validating operation result
+*/
+SalomeApp_DoubleSpinBox::State SalomeApp_DoubleSpinBox::isValid( const QString& text, double& value ) const
+{
+ if( !findVariable( text, value ) )
+ {
+ bool ok = false;
+ value = text.toDouble( &ok );
+ if( !ok )
+ return NoVariable;
+ }
+
+ if( !checkRange( value ) )
+ return Invalid;
+
+ return Acceptable;
}
/*!
if( SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) )
{
_PTR(Study) studyDS = study->studyDS();
- std::vector<std::string> aVariableNames = studyDS->GetVariableNames();
- for( unsigned int i = 0, n = aVariableNames.size(); i < n; i++ )
+
+ std::string aName = name.toStdString();
+ if( studyDS->IsVariable( aName ) && ( studyDS->IsReal( aName ) || studyDS->IsInteger( aName ) ) )
{
- std::string aName = aVariableNames[ i ];
- if( studyDS->IsReal( aName ) || studyDS->IsInteger( aName ) )
- {
- double aValue = studyDS->GetReal( aName );
- if( aName == name.toStdString() )
- {
- value = aValue;
- return true;
- }
- }
+ value = studyDS->GetReal( aName );
+ return true;
}
}
}
{
Q_OBJECT
+ enum State { Invalid = 0, NoVariable, Acceptable };
+
public:
SalomeApp_DoubleSpinBox( QWidget* = 0 );
SalomeApp_DoubleSpinBox( double, double, double = 1, QWidget* = 0 );
virtual QValidator::State validate( QString&, int& ) const;
- virtual bool isValid() const;
+ virtual bool isValid( QString& msg, bool = false );
virtual void setDefaultValue( const double );
+
virtual void setRange( double, double );
+ virtual void setValue( double );
protected:
+ State isValid( const QString&, double& ) const;
+
double defaultValue() const;
bool checkRange( const double ) const;
+
bool findVariable( const QString&, double& ) const;
+protected slots:
+ void onEditingFinished();
+ void onTextChanged( const QString& );
+
+private:
+ void connectSignalsAndSlots();
+
private:
double myDefaultValue;
bool myIsRangeSet;
double myMinimum;
double myMaximum;
+
+ QString myCorrectValue;
+ QString myTextValue;
};
#endif
#include "SALOMEDSClient_ClientFactory.hxx"
#include CORBA_SERVER_HEADER(SALOMEDS)
+#include <QLineEdit>
+
/*!
\class SalomeApp_IntSpinBox
*/
: QtxIntSpinBox( parent ),
myDefaultValue( 0 )
{
+ connectSignalsAndSlots();
}
/*!
: QtxIntSpinBox( min, max, step, parent ),
myDefaultValue( 0 )
{
+ connectSignalsAndSlots();
}
/*!
{
}
+/*!
+ \brief Connect signals and slots.
+*/
+void SalomeApp_IntSpinBox::connectSignalsAndSlots()
+{
+ connect( this, SIGNAL( editingFinished() ),
+ this, SLOT( onEditingFinished() ) );
+
+ connect( lineEdit(), SIGNAL( textChanged( const QString& ) ),
+ this, SLOT( onTextChanged( const QString& ) ) );
+}
+
+/*!
+ \brief This function is called when editing is finished.
+*/
+void SalomeApp_IntSpinBox::onEditingFinished()
+{
+ if( myTextValue.isNull() )
+ myTextValue = text();
+
+ lineEdit()->setText( myTextValue );
+}
+
+/*!
+ \brief This function is called when value is changed.
+*/
+void SalomeApp_IntSpinBox::onTextChanged( const QString& text )
+{
+ myTextValue = text;
+
+ int value = 0;
+ if( isValid( text, value ) == Acceptable )
+ myCorrectValue = text;
+}
+
/*!
\brief Interpret text entered by the user as a value.
\param text text entered by the user
*/
int SalomeApp_IntSpinBox::valueFromText( const QString& text ) const
{
- QString str = text;
-
int value = 0;
- if( findVariable( str, value ) )
- str = QString::number( value );
-
- int pos = 0;
- if( QtxIntSpinBox::validate( str, pos ) == QValidator::Acceptable )
- value = QtxIntSpinBox::valueFromText( str );
- else
- value = defaultValue();
+ if( isValid( text, value ) == Acceptable )
+ return value;
- return value;
+ return defaultValue();
}
/*!
\brief This function is used to determine whether input is valid.
\return validating operation result
*/
-bool SalomeApp_IntSpinBox::isValid() const
+bool SalomeApp_IntSpinBox::isValid( QString& msg, bool toCorrect )
{
- QString str = text();
+ int value;
+ State aState = isValid( text(), value );
- int value = 0;
- if( findVariable( str, value ) )
- str = QString::number( value );
+ if( aState != Acceptable )
+ {
+ if( toCorrect )
+ {
+ if( aState == NoVariable )
+ msg += tr( "ERR_NO_VARIABLE" ).arg( text() ) + "\n";
+ else if( aState == Invalid )
+ msg += tr( "ERR_INVALID_VALUE" ) + "\n";
- int pos = 0;
- return QtxIntSpinBox::validate( str, pos ) == QValidator::Acceptable;
+ lineEdit()->setText( myCorrectValue );
+ }
+ return false;
+ }
+
+ return true;
}
/*!
myDefaultValue = value;
}
+/*!
+ \brief This function is used to set a current value for this spinbox.
+ \param value current value
+*/
+void SalomeApp_IntSpinBox::setValue( const int value )
+{
+ QtxIntSpinBox::setValue( value );
+
+ myCorrectValue = QString::number( value );
+}
+
+/*!
+ \brief This function is used to determine whether input is valid.
+ \return validating operation result
+*/
+SalomeApp_IntSpinBox::State SalomeApp_IntSpinBox::isValid( const QString& text, int& value ) const
+{
+ if( !findVariable( text, value ) )
+ {
+ bool ok = false;
+ value = text.toInt( &ok );
+ if( !ok )
+ return NoVariable;
+ }
+
+ if( !checkRange( value ) )
+ return Invalid;
+
+ return Acceptable;
+}
+
/*!
\brief This function return a default acceptable value (commonly, 0).
\return default acceptable value
return myDefaultValue;
}
+/*!
+ \brief This function is used to check that string value lies within predefined range.
+ \return check status
+*/
+bool SalomeApp_IntSpinBox::checkRange( const int value ) const
+{
+ return value >= minimum() && value <= maximum();
+}
+
/*!
\brief This function is used to determine whether input is a variable name and to get its value.
\return true if the input is a variable name
if( SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) )
{
_PTR(Study) studyDS = study->studyDS();
- std::vector<std::string> aVariableNames = studyDS->GetVariableNames();
- for( unsigned int i = 0, n = aVariableNames.size(); i < n; i++ )
+
+ std::string aName = name.toStdString();
+ if( studyDS->IsVariable( aName ) && studyDS->IsInteger( aName ) )
{
- std::string aName = aVariableNames[ i ];
- if( studyDS->IsInteger( aName ) )
- {
- int aValue = studyDS->GetInteger( aName );
-
- if( aName == name.toStdString() )
- {
- value = aValue;
- return true;
- }
- }
+ value = studyDS->GetInteger( aName );
+ return true;
}
}
}
{
Q_OBJECT
+ enum State { Invalid = 0, NoVariable, Acceptable };
+
public:
SalomeApp_IntSpinBox( QWidget* = 0 );
SalomeApp_IntSpinBox( int, int, int = 1, QWidget* = 0 );
virtual QValidator::State validate( QString&, int& ) const;
- virtual bool isValid() const;
+ virtual bool isValid( QString& msg, bool = false );
virtual void setDefaultValue( const int );
+ virtual void setValue( int );
+
protected:
+ State isValid( const QString&, int& ) const;
+
int defaultValue() const;
+ bool checkRange( const int ) const;
+
bool findVariable( const QString&, int& ) const;
+protected slots:
+ void onEditingFinished();
+ void onTextChanged( const QString& );
+
+private:
+ void connectSignalsAndSlots();
+
private:
int myDefaultValue;
+
+ QString myCorrectValue;
+ QString myTextValue;
};
#endif
<source>REFENTRY_COLUMN</source>
<translation>Ref.Entry</translation>
</message>
+ <message>
+ <source>ERR_INVALID_VALUE</source>
+ <translation>Value hasn't been validated</translation>
+ </message>
+ <message>
+ <source>ERR_NO_VARIABLE</source>
+ <translation>Variable with name "%1" doesn't exist</translation>
+ </message>
</context>
<context>
<name>SalomeApp_Application</name>