From 99c84cc03acb07b0e6c7bac90b07ce53d25729cf Mon Sep 17 00:00:00 2001 From: ouv Date: Mon, 10 Nov 2008 17:10:26 +0000 Subject: [PATCH] Dump Python extension --- src/SalomeApp/SalomeApp_DoubleSpinBox.cxx | 135 +++++++++++++++----- src/SalomeApp/SalomeApp_DoubleSpinBox.h | 19 ++- src/SalomeApp/SalomeApp_IntSpinBox.cxx | 135 +++++++++++++++----- src/SalomeApp/SalomeApp_IntSpinBox.h | 20 ++- src/SalomeApp/resources/SalomeApp_msg_en.ts | 8 ++ 5 files changed, 249 insertions(+), 68 deletions(-) diff --git a/src/SalomeApp/SalomeApp_DoubleSpinBox.cxx b/src/SalomeApp/SalomeApp_DoubleSpinBox.cxx index 95e4d1c85..6d048f967 100644 --- a/src/SalomeApp/SalomeApp_DoubleSpinBox.cxx +++ b/src/SalomeApp/SalomeApp_DoubleSpinBox.cxx @@ -28,6 +28,8 @@ #include "SALOMEDSClient_ClientFactory.hxx" #include CORBA_SERVER_HEADER(SALOMEDS) +#include + /*! \class SalomeApp_DoubleSpinBox */ @@ -48,6 +50,7 @@ SalomeApp_DoubleSpinBox::SalomeApp_DoubleSpinBox( QWidget* parent ) myMinimum( 0.0 ), myMaximum( 99.99 ) { + connectSignalsAndSlots(); } /*! @@ -69,6 +72,7 @@ SalomeApp_DoubleSpinBox::SalomeApp_DoubleSpinBox( double min, double max, double myMinimum( min ), myMaximum( max ) { + connectSignalsAndSlots(); } /*! @@ -90,6 +94,7 @@ SalomeApp_DoubleSpinBox::SalomeApp_DoubleSpinBox( double min, double max, double myMinimum( min ), myMaximum( max ) { + connectSignalsAndSlots(); } /*! @@ -99,6 +104,41 @@ SalomeApp_DoubleSpinBox::~SalomeApp_DoubleSpinBox() { } +/*! + \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 @@ -107,16 +147,8 @@ SalomeApp_DoubleSpinBox::~SalomeApp_DoubleSpinBox() */ 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(); @@ -150,24 +182,42 @@ QValidator::State SalomeApp_DoubleSpinBox::validate( QString& str, int& pos ) co \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 ); @@ -178,12 +228,34 @@ void SalomeApp_DoubleSpinBox::setRange( const double min, const double 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; } /*! @@ -221,19 +293,12 @@ bool SalomeApp_DoubleSpinBox::findVariable( const QString& name, double& value ) if( SalomeApp_Study* study = dynamic_cast( app->activeStudy() ) ) { _PTR(Study) studyDS = study->studyDS(); - std::vector 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; } } } diff --git a/src/SalomeApp/SalomeApp_DoubleSpinBox.h b/src/SalomeApp/SalomeApp_DoubleSpinBox.h index 7ce95ed2d..867c2d24b 100644 --- a/src/SalomeApp/SalomeApp_DoubleSpinBox.h +++ b/src/SalomeApp/SalomeApp_DoubleSpinBox.h @@ -32,6 +32,8 @@ class SALOMEAPP_EXPORT SalomeApp_DoubleSpinBox : public QtxDoubleSpinBox { Q_OBJECT + enum State { Invalid = 0, NoVariable, Acceptable }; + public: SalomeApp_DoubleSpinBox( QWidget* = 0 ); SalomeApp_DoubleSpinBox( double, double, double = 1, QWidget* = 0 ); @@ -43,22 +45,37 @@ public: 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 diff --git a/src/SalomeApp/SalomeApp_IntSpinBox.cxx b/src/SalomeApp/SalomeApp_IntSpinBox.cxx index 8b631dce1..14b003645 100644 --- a/src/SalomeApp/SalomeApp_IntSpinBox.cxx +++ b/src/SalomeApp/SalomeApp_IntSpinBox.cxx @@ -28,6 +28,8 @@ #include "SALOMEDSClient_ClientFactory.hxx" #include CORBA_SERVER_HEADER(SALOMEDS) +#include + /*! \class SalomeApp_IntSpinBox */ @@ -44,6 +46,7 @@ SalomeApp_IntSpinBox::SalomeApp_IntSpinBox( QWidget* parent ) : QtxIntSpinBox( parent ), myDefaultValue( 0 ) { + connectSignalsAndSlots(); } /*! @@ -61,6 +64,7 @@ SalomeApp_IntSpinBox::SalomeApp_IntSpinBox( int min, int max, int step, QWidget* : QtxIntSpinBox( min, max, step, parent ), myDefaultValue( 0 ) { + connectSignalsAndSlots(); } /*! @@ -70,6 +74,41 @@ SalomeApp_IntSpinBox::~SalomeApp_IntSpinBox() { } +/*! + \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 @@ -78,19 +117,11 @@ SalomeApp_IntSpinBox::~SalomeApp_IntSpinBox() */ 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(); } /*! @@ -121,16 +152,26 @@ QValidator::State SalomeApp_IntSpinBox::validate( QString& str, int& pos ) const \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; } /*! @@ -142,6 +183,37 @@ void SalomeApp_IntSpinBox::setDefaultValue( const int value ) 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 @@ -154,6 +226,15 @@ int SalomeApp_IntSpinBox::defaultValue() const 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 @@ -165,20 +246,12 @@ bool SalomeApp_IntSpinBox::findVariable( const QString& name, int& value ) const if( SalomeApp_Study* study = dynamic_cast( app->activeStudy() ) ) { _PTR(Study) studyDS = study->studyDS(); - std::vector 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; } } } diff --git a/src/SalomeApp/SalomeApp_IntSpinBox.h b/src/SalomeApp/SalomeApp_IntSpinBox.h index 7ade2ced3..000cfb3ff 100644 --- a/src/SalomeApp/SalomeApp_IntSpinBox.h +++ b/src/SalomeApp/SalomeApp_IntSpinBox.h @@ -32,6 +32,8 @@ class SALOMEAPP_EXPORT SalomeApp_IntSpinBox : public QtxIntSpinBox { Q_OBJECT + enum State { Invalid = 0, NoVariable, Acceptable }; + public: SalomeApp_IntSpinBox( QWidget* = 0 ); SalomeApp_IntSpinBox( int, int, int = 1, QWidget* = 0 ); @@ -42,16 +44,32 @@ public: 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 diff --git a/src/SalomeApp/resources/SalomeApp_msg_en.ts b/src/SalomeApp/resources/SalomeApp_msg_en.ts index ccce92110..968bb5a74 100644 --- a/src/SalomeApp/resources/SalomeApp_msg_en.ts +++ b/src/SalomeApp/resources/SalomeApp_msg_en.ts @@ -57,6 +57,14 @@ REFENTRY_COLUMN Ref.Entry + + ERR_INVALID_VALUE + Value hasn't been validated + + + ERR_NO_VARIABLE + Variable with name "%1" doesn't exist + SalomeApp_Application -- 2.39.2