Back to index

salome-gui  6.5.0
QDS_LineEdit.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "QDS_LineEdit.h"
00024 
00025 #include <QLineEdit>
00026 #include <QValidator>
00027 
00034 class QDS_LineEdit::Editor : public QLineEdit
00035 {
00036 public:
00037   Editor( QWidget* parent = 0 ) : QLineEdit( parent ), myNumber( 2 ) {};
00038   virtual ~Editor() {};
00039 
00040   void setNumber( const int num ) { myNumber = num; };
00041 
00042   virtual QSize minimumSizeHint() const
00043   {
00044     return QLineEdit::minimumSizeHint().
00045       expandedTo( QSize( fontMetrics().width( "0" ) * myNumber, 0 ) );
00046   }
00047   
00048   virtual QSize sizeHint() const
00049   {
00050     return minimumSizeHint();
00051   }
00052 
00053 private:
00054   int           myNumber;
00055 };
00056 
00057 /*
00058   \class QDS_LineEdit
00059   
00060   \brief Datum with control corresponding to the line edit. 
00061 
00062   User can enter parameter value in single line editor.
00063   The value entered by the user is checked by the validator according to item type
00064   according to the item properties (minimum, maximum, filter, precision, etc).
00065 
00066   If user input is not valid, the value is displayed in red color.
00067 */
00068 
00085 QDS_LineEdit::QDS_LineEdit( const QString& id, QWidget* parent, const int flags, const QString& comp )
00086 : QDS_Datum( id, parent, flags, comp )
00087 {
00088 }
00089 
00093 QDS_LineEdit::~QDS_LineEdit()
00094 {
00095 }
00096 
00104 void QDS_LineEdit::unitSystemChanged( const QString& system )
00105 {
00106   QDS_Datum::unitSystemChanged( system );
00107 
00108   QLineEdit* le = lineEdit();
00109   if ( !le )
00110     return;
00111   
00112   delete le->validator();
00113   le->setValidator(0);
00114   QValidator* valid = validator();
00115   if ( valid )
00116     le->setValidator( valid );
00117 
00118   QString aFormat = format();
00119   int num = 0;
00120   int pos = aFormat.indexOf( '%' );
00121   if ( pos != -1 )
00122   {
00123     pos++;
00124     QString aLen;
00125     while ( pos < (int)aFormat.length() && aFormat.at( pos ).isDigit() )
00126       aLen += aFormat.at( pos++ );
00127     if ( pos < (int)aFormat.length() && aFormat.at( pos ) == '.' )
00128       num += 1;
00129     if ( !aLen.isEmpty() )
00130       num += aLen.toInt();
00131   }
00132   
00133   int zeroLen = format( format(), type(), 0 ).length();
00134   int minLen  = format( format(), type(), minValue() ).length();
00135   int maxLen  = format( format(), type(), maxValue() ).length();
00136 
00137   num = qMax( qMax( num, zeroLen ), qMax( minLen, maxLen ) );
00138   ((Editor*)le)->setNumber( num );
00139 }
00140 
00144 void QDS_LineEdit::selectAll()
00145 {
00146   if ( lineEdit() )
00147     lineEdit()->selectAll();
00148 }
00149 
00153 void QDS_LineEdit::deselect()
00154 {
00155   if ( lineEdit() )
00156     lineEdit()->deselect();
00157 }
00158 
00163 void QDS_LineEdit::setSelection( const bool on )
00164 {
00165   if ( on )
00166     selectAll();
00167   else
00168     deselect();
00169 }
00170 
00175 bool QDS_LineEdit::hasSelection() const
00176 {
00177   return lineEdit() ? lineEdit()->hasSelectedText() : false;
00178 }
00179 
00185 void QDS_LineEdit::setAlignment( const int align, const int type )
00186 {
00187   if ( ( type & Control ) && lineEdit() )
00188     lineEdit()->setAlignment( Qt::Alignment(align) );
00189 
00190   QDS_Datum::setAlignment( align, type );
00191 }
00192 
00197 QString QDS_LineEdit::getString() const
00198 {
00199   QString res;
00200   if ( lineEdit() )
00201     res = lineEdit()->text();
00202   return res;
00203 }
00204 
00209 void QDS_LineEdit::setString( const QString& txt )
00210 {
00211   if ( lineEdit() )
00212     lineEdit()->setText( txt );
00213 }
00214 
00219 QLineEdit* QDS_LineEdit::lineEdit() const
00220 {
00221   return ::qobject_cast<QLineEdit*>( controlWidget() );
00222 }
00223 
00229 QWidget* QDS_LineEdit::createControl( QWidget* parent )
00230 {
00231   Editor* le = new Editor( parent );
00232   connect( le, SIGNAL( returnPressed() ), this, SIGNAL( returnPressed() ) );
00233   connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
00234   return le;
00235 }
00236 
00244 void QDS_LineEdit::onTextChanged( const QString& /*txt*/ )
00245 {
00246   invalidateCache();
00247 
00248   onParamChanged();
00249   QString str = getString();
00250   emit paramChanged();
00251   emit paramChanged( str );
00252 }
00253 
00261 void QDS_LineEdit::onParamChanged()
00262 {
00263   QLineEdit* anEdit = lineEdit();
00264   if ( !anEdit )
00265     return;
00266 
00267   bool aValid = isValid( false );
00268 
00269   QPalette aPal = anEdit->palette();
00270   if ( !aValid )
00271     aPal.setColor( QPalette::Active, QPalette::Text, QColor( 255, 0, 0 ) );
00272   else
00273     aPal.setColor( QPalette::Active, QPalette::Text, QColor( 0, 0, 0 ) );
00274 
00275   anEdit->setPalette( aPal );
00276 }
00277