1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "QDS_LineEdit.h"
28 \class QDS_LineEdit::Editor
30 \brief Improved version of QLineEdit.
33 class QDS_LineEdit::Editor : public QLineEdit
36 Editor( QWidget* parent = 0 ) : QLineEdit( parent ), myNumber( 2 ) {};
39 void setNumber( const int num ) { myNumber = num; };
41 virtual QSize minimumSizeHint() const
43 return QLineEdit::minimumSizeHint().
44 expandedTo( QSize( fontMetrics().width( "0" ) * myNumber, 0 ) );
47 virtual QSize sizeHint() const
49 return minimumSizeHint();
59 \brief Datum with control corresponding to the line edit.
61 User can enter parameter value in single line editor.
62 The value entered by the user is checked by the validator according to item type
63 according to the item properties (minimum, maximum, filter, precision, etc).
65 If user input is not valid, the value is displayed in red color.
71 Create line edit datum object with datum identifier \a id and parent widget \a parent.
73 Parameter \a flags defines behaviour of datum and set of created
74 subwidgets. Default value of this parameter is QDS::All.
76 Parameter \a comp specifies the component name which will be used
77 when searching the dictionary item.
79 \param id datum identifier
80 \param parent parent widget
81 \param flags datum flags
84 QDS_LineEdit::QDS_LineEdit( const QString& id, QWidget* parent, const int flags, const QString& comp )
85 : QDS_Datum( id, parent, flags, comp )
92 QDS_LineEdit::~QDS_LineEdit()
97 \brief Process notification about active units system changing.
99 Update validator settings for line edit.
101 \param system new active units system
103 void QDS_LineEdit::unitSystemChanged( const QString& system )
105 QDS_Datum::unitSystemChanged( system );
107 QLineEdit* le = lineEdit();
111 delete le->validator();
113 QValidator* valid = validator();
115 le->setValidator( valid );
117 QString aFormat = format();
119 int pos = aFormat.indexOf( '%' );
124 while ( pos < (int)aFormat.length() && aFormat.at( pos ).isDigit() )
125 aLen += aFormat.at( pos++ );
126 if ( pos < (int)aFormat.length() && aFormat.at( pos ) == '.' )
128 if ( !aLen.isEmpty() )
132 int zeroLen = format( format(), type(), 0 ).length();
133 int minLen = format( format(), type(), minValue() ).length();
134 int maxLen = format( format(), type(), maxValue() ).length();
136 num = qMax( qMax( num, zeroLen ), qMax( minLen, maxLen ) );
137 ((Editor*)le)->setNumber( num );
141 \brief Select all text in the editor.
143 void QDS_LineEdit::selectAll()
146 lineEdit()->selectAll();
150 \brief Deselect all text in the editor.
152 void QDS_LineEdit::deselect()
155 lineEdit()->deselect();
159 \brief Select/deselect all text in the editor.
160 \param on select text if \c true and deselect if \c false
162 void QDS_LineEdit::setSelection( const bool on )
171 \brief Check if there is selection in the editor.
172 \return \c true if the editor has selected text
174 bool QDS_LineEdit::hasSelection() const
176 return lineEdit() ? lineEdit()->hasSelectedText() : false;
180 \brief Set the aligment for the line edit.
181 \param align alignment type (Qt::Alignment)
182 \param type ORed subwidget flags
184 void QDS_LineEdit::setAlignment( const int align, const int type )
186 if ( ( type & Control ) && lineEdit() )
187 lineEdit()->setAlignment( Qt::Alignment(align) );
189 QDS_Datum::setAlignment( align, type );
193 \brief Get string value from datum.
194 \return datum string value
196 QString QDS_LineEdit::getString() const
200 res = lineEdit()->text();
205 \brief Set string value to datum.
206 \param txt new datum string value
208 void QDS_LineEdit::setString( const QString& txt )
211 lineEdit()->setText( txt );
215 \brief Get line edit widget.
216 \return pointer to the QLineEdit widget
218 QLineEdit* QDS_LineEdit::lineEdit() const
220 return ::qobject_cast<QLineEdit*>( controlWidget() );
224 \brief Create line edit widget as control subwidget.
225 \param parent parent widget
226 \return created line edit widget
228 QWidget* QDS_LineEdit::createControl( QWidget* parent )
230 Editor* le = new Editor( parent );
231 connect( le, SIGNAL( returnPressed() ), this, SIGNAL( returnPressed() ) );
232 connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
237 \brief Called when text in the edit box is modified by the user.
239 Notify about text changing in the line edit.
241 \param txt current text in the line edit widget (not used)
243 void QDS_LineEdit::onTextChanged( const QString& /*txt*/ )
248 QString str = getString();
250 emit paramChanged( str );
254 \brief Called when text is changed.
256 Validate the current parameter value. If value is not valid then set text color as red.
258 Emits signal paramChanged() to notify about changing of the control state.
260 void QDS_LineEdit::onParamChanged()
262 QLineEdit* anEdit = lineEdit();
266 bool aValid = isValid( false );
268 QPalette aPal = anEdit->palette();
270 aPal.setColor( QPalette::Active, QPalette::Text, QColor( 255, 0, 0 ) );
272 aPal.setColor( QPalette::Active, QPalette::Text, QColor( 0, 0, 0 ) );
274 anEdit->setPalette( aPal );
278 \brief void QDS_LineEdit::returnPressed();
279 \brief The signal is emitted when user presses \c Enter key in the line edit.