1 // Copyright (C) 2007-2013 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
23 #include "QDS_LineEdit.h"
29 \class QDS_LineEdit::Editor
31 \brief Improved version of QLineEdit.
34 class QDS_LineEdit::Editor : public QLineEdit
37 Editor( QWidget* parent = 0 ) : QLineEdit( parent ), myNumber( 2 ) {};
40 void setNumber( const int num ) { myNumber = num; };
42 virtual QSize minimumSizeHint() const
44 return QLineEdit::minimumSizeHint().
45 expandedTo( QSize( fontMetrics().width( "0" ) * myNumber, 0 ) );
48 virtual QSize sizeHint() const
50 return minimumSizeHint();
60 \brief Datum with control corresponding to the line edit.
62 User can enter parameter value in single line editor.
63 The value entered by the user is checked by the validator according to item type
64 according to the item properties (minimum, maximum, filter, precision, etc).
66 If user input is not valid, the value is displayed in red color.
72 Create line edit datum object with datum identifier \a id and parent widget \a parent.
74 Parameter \a flags defines behaviour of datum and set of created
75 subwidgets. Default value of this parameter is QDS::All.
77 Parameter \a comp specifies the component name which will be used
78 when searching the dictionary item.
80 \param id datum identifier
81 \param parent parent widget
82 \param flags datum flags
85 QDS_LineEdit::QDS_LineEdit( const QString& id, QWidget* parent, const int flags, const QString& comp )
86 : QDS_Datum( id, parent, flags, comp )
93 QDS_LineEdit::~QDS_LineEdit()
98 \brief Process notification about active units system changing.
100 Update validator settings for line edit.
102 \param system new active units system
104 void QDS_LineEdit::unitSystemChanged( const QString& system )
106 QDS_Datum::unitSystemChanged( system );
108 QLineEdit* le = lineEdit();
112 delete le->validator();
114 QValidator* valid = validator();
116 le->setValidator( valid );
118 QString aFormat = format();
120 int pos = aFormat.indexOf( '%' );
125 while ( pos < (int)aFormat.length() && aFormat.at( pos ).isDigit() )
126 aLen += aFormat.at( pos++ );
127 if ( pos < (int)aFormat.length() && aFormat.at( pos ) == '.' )
129 if ( !aLen.isEmpty() )
133 int zeroLen = format( format(), type(), 0 ).length();
134 int minLen = format( format(), type(), minValue() ).length();
135 int maxLen = format( format(), type(), maxValue() ).length();
137 num = qMax( qMax( num, zeroLen ), qMax( minLen, maxLen ) );
138 ((Editor*)le)->setNumber( num );
142 \brief Select all text in the editor.
144 void QDS_LineEdit::selectAll()
147 lineEdit()->selectAll();
151 \brief Deselect all text in the editor.
153 void QDS_LineEdit::deselect()
156 lineEdit()->deselect();
160 \brief Select/deselect all text in the editor.
161 \param on select text if \c true and deselect if \c false
163 void QDS_LineEdit::setSelection( const bool on )
172 \brief Check if there is selection in the editor.
173 \return \c true if the editor has selected text
175 bool QDS_LineEdit::hasSelection() const
177 return lineEdit() ? lineEdit()->hasSelectedText() : false;
181 \brief Set the aligment for the line edit.
182 \param align alignment type (Qt::Alignment)
183 \param type ORed subwidget flags
185 void QDS_LineEdit::setAlignment( const int align, const int type )
187 if ( ( type & Control ) && lineEdit() )
188 lineEdit()->setAlignment( Qt::Alignment(align) );
190 QDS_Datum::setAlignment( align, type );
194 \brief Get string value from datum.
195 \return datum string value
197 QString QDS_LineEdit::getString() const
201 res = lineEdit()->text();
206 \brief Set string value to datum.
207 \param txt new datum string value
209 void QDS_LineEdit::setString( const QString& txt )
212 lineEdit()->setText( txt );
216 \brief Get line edit widget.
217 \return pointer to the QLineEdit widget
219 QLineEdit* QDS_LineEdit::lineEdit() const
221 return ::qobject_cast<QLineEdit*>( controlWidget() );
225 \brief Create line edit widget as control subwidget.
226 \param parent parent widget
227 \return created line edit widget
229 QWidget* QDS_LineEdit::createControl( QWidget* parent )
231 Editor* le = new Editor( parent );
232 connect( le, SIGNAL( returnPressed() ), this, SIGNAL( returnPressed() ) );
233 connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
238 \brief Called when text in the edit box is modified by the user.
240 Notify about text changing in the line edit.
242 \param txt current text in the line edit widget (not used)
244 void QDS_LineEdit::onTextChanged( const QString& /*txt*/ )
249 QString str = getString();
251 emit paramChanged( str );
255 \brief Called when text is changed.
257 Validate the current parameter value. If value is not valid then set text color as red.
259 Emits signal paramChanged() to notify about changing of the control state.
261 void QDS_LineEdit::onParamChanged()
263 QLineEdit* anEdit = lineEdit();
267 bool aValid = isValid( false );
269 QPalette aPal = anEdit->palette();
271 aPal.setColor( QPalette::Active, QPalette::Text, QColor( 255, 0, 0 ) );
273 aPal.setColor( QPalette::Active, QPalette::Text, QColor( 0, 0, 0 ) );
275 anEdit->setPalette( aPal );
279 \brief void QDS_LineEdit::returnPressed();
280 \brief The signal is emitted when user presses \c Enter key in the line edit.