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();
55 Datum with control corresponding to line edit. User can enter parameter value in single line editor.
56 User inputted values will be checked by validator according to type if value and parameter properties
57 (minimum, maximum, filter, precision, etc). If user input not valid value then this value will be
58 displayed in red color.
62 Constructor. Create line edit datum object with datum identifier \aid under widget \aparent. Parameter \aflags
63 define behaviour of datum and set of created subwidgets. Default value of this parameter is QDS::All.
64 Parameter \acomp specify the component name which will be used during search of dictionary item.
66 QDS_LineEdit::QDS_LineEdit( const QString& id, QWidget* parent, const int flags, const QString& comp )
67 : QDS_Datum( id, parent, flags, comp )
74 QDS_LineEdit::~QDS_LineEdit()
79 Notification about active unit system changing. Reimplemented from QDS_Datum.
80 Update validator settings for line edit.
82 void QDS_LineEdit::unitSystemChanged( const QString& system )
84 QDS_Datum::unitSystemChanged( system );
86 QLineEdit* le = lineEdit();
90 delete le->validator();
92 QValidator* valid = validator();
94 le->setValidator( valid );
96 QString aFormat = format();
98 int pos = aFormat.find( '%' );
103 while ( pos < (int)aFormat.length() && aFormat.at( pos ).isDigit() )
104 aLen += aFormat.at( pos++ );
105 if ( pos < (int)aFormat.length() && aFormat.at( pos ) == '.' )
107 if ( !aLen.isEmpty() )
111 int zeroLen = format( format(), type(), 0 ).length();
112 int minLen = format( format(), type(), minValue() ).length();
113 int maxLen = format( format(), type(), maxValue() ).length();
115 num = QMAX( QMAX( num, zeroLen ), QMAX( minLen, maxLen ) );
116 ((Editor*)le)->setNumber( num );
120 Select all text in the editor.
122 void QDS_LineEdit::selectAll()
125 lineEdit()->selectAll();
129 Deselect all text in the editor.
131 void QDS_LineEdit::deselect()
134 lineEdit()->deselect();
138 Select or deselect all text in the editor.
140 void QDS_LineEdit::setSelection( const bool on )
149 Returns true if the editor has selected text.
151 bool QDS_LineEdit::hasSelection() const
153 return lineEdit() ? lineEdit()->hasSelectedText() : false;
157 Set the aligment of line edit. Reimplemented from QDS_Datum.
159 void QDS_LineEdit::setAlignment( const int align, const int type )
161 if ( ( type & Control ) && lineEdit() )
162 lineEdit()->setAlignment( align );
164 QDS_Datum::setAlignment( align, type );
168 Returns string value from QLineEdit widget. Reimplemented from QDS_Datum.
170 QString QDS_LineEdit::getString() const
174 res = lineEdit()->text();
179 Sets the string value into QLineEdit widget. Reimplemented from QDS_Datum.
181 void QDS_LineEdit::setString( const QString& txt )
184 lineEdit()->setText( txt );
188 Returns pointer to QLineEdit widget.
190 QLineEdit* QDS_LineEdit::lineEdit() const
192 return ::qt_cast<QLineEdit*>( controlWidget() );
196 Create QLineEdit widget as control subwidget. Reimplemented from QDS_Datum.
198 QWidget* QDS_LineEdit::createControl( QWidget* parent )
200 Editor* le = new Editor( parent );
201 connect( le, SIGNAL( returnPressed() ), this, SIGNAL( returnPressed() ) );
202 connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
207 Notify about text changing in line edit.
209 void QDS_LineEdit::onTextChanged( const QString& )
214 QString str = getString();
216 emit paramChanged( str );
220 Checks the current parameter value on validity. If value is not valid then set text color as red.
222 void QDS_LineEdit::onParamChanged()
224 QLineEdit* anEdit = lineEdit();
228 bool aValid = isValid( false );
230 QPalette aPal = anEdit->palette();
232 aPal.setColor( QPalette::Active, QColorGroup::Text, QColor( 255, 0, 0 ) );
234 aPal.setColor( QPalette::Active, QColorGroup::Text, QColor( 0, 0, 0 ) );
236 anEdit->setPalette( aPal );