1 // Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "QDS_LineEdit.h"
21 #include <qlineedit.h>
22 #include <qvalidator.h>
25 Class: QDS_LineEdit::Editor
26 Descr: Internal class inherited from line edit
29 class QDS_LineEdit::Editor : public QLineEdit
32 Editor( QWidget* parent = 0 ) : QLineEdit( parent ), myNumber( 2 ) {};
35 void setNumber( const int num ) { myNumber = num; };
37 virtual QSize minimumSizeHint() const
39 return QLineEdit::minimumSizeHint().
40 expandedTo( QSize( fontMetrics().width( "0" ) * myNumber, 0 ) );
43 virtual QSize sizeHint() const
45 return minimumSizeHint();
54 Descr: Data control corresponding to line edit
60 QDS_LineEdit::QDS_LineEdit( const QString& id, QWidget* parent, const int flags, const QString& comp )
61 : QDS_Datum( id, parent, flags, comp )
68 QDS_LineEdit::~QDS_LineEdit()
72 void QDS_LineEdit::unitSystemChanged( const QString& system )
74 QDS_Datum::unitSystemChanged( system );
76 QLineEdit* le = lineEdit();
80 delete le->validator();
82 QValidator* valid = validator();
84 le->setValidator( valid );
86 QString aFormat = format();
88 int pos = aFormat.find( '%' );
93 while ( pos < (int)aFormat.length() && aFormat.at( pos ).isDigit() )
94 aLen += aFormat.at( pos++ );
95 if ( pos < (int)aFormat.length() && aFormat.at( pos ) == '.' )
97 if ( !aLen.isEmpty() )
101 int zeroLen = format( format(), type(), 0 ).length();
102 int minLen = format( format(), type(), minValue() ).length();
103 int maxLen = format( format(), type(), maxValue() ).length();
105 num = QMAX( QMAX( num, zeroLen ), QMAX( minLen, maxLen ) );
106 ((Editor*)le)->setNumber( num );
110 Set the aligment of line edit.
112 void QDS_LineEdit::setAlignment( const int align, const int type )
114 if ( ( type & Control ) && lineEdit() )
115 lineEdit()->setAlignment( align );
117 QDS_Datum::setAlignment( align, type );
121 Returns string from QLineEdit widget.
123 QString QDS_LineEdit::getString() const
127 res = lineEdit()->text();
132 Sets the string into QLineEdit widget.
134 void QDS_LineEdit::setString( const QString& txt )
137 lineEdit()->setText( txt );
141 Returns pointer to QLineEdit widget.
143 QLineEdit* QDS_LineEdit::lineEdit() const
145 return ::qt_cast<QLineEdit*>( controlWidget() );
149 Create QLineEdit widget as control subwidget.
151 QWidget* QDS_LineEdit::createControl( QWidget* parent )
153 Editor* le = new Editor( parent );
154 connect( le, SIGNAL( returnPressed() ), this, SIGNAL( returnPressed() ) );
155 connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
160 Notify about text changing in line edit.
162 void QDS_LineEdit::onTextChanged( const QString& )
167 QString str = getString();
169 emit paramChanged( str );
173 Checks the current parameter value on validity.
175 void QDS_LineEdit::onParamChanged()
177 QLineEdit* anEdit = lineEdit();
181 bool aValid = isValid( false );
183 QPalette aPal = anEdit->palette();
185 aPal.setColor( QPalette::Active, QColorGroup::Text, QColor( 255, 0, 0 ) );
187 aPal.setColor( QPalette::Active, QColorGroup::Text, QColor( 0, 0, 0 ) );
189 anEdit->setPalette( aPal );