Salome HOME
updated copyright message
[modules/gui.git] / src / QDS / QDS_LineEdit.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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, or (at your option) any later version.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "QDS_LineEdit.h"
24
25 #include <QLineEdit>
26 #include <QValidator>
27
28 /*!
29   \class QDS_LineEdit::Editor
30   \internal
31   \brief Improved version of QLineEdit.
32 */
33
34 class QDS_LineEdit::Editor : public QLineEdit
35 {
36 public:
37   Editor( QWidget* parent = 0 ) : QLineEdit( parent ), myNumber( 2 ) {};
38   virtual ~Editor() {};
39
40   void setNumber( const int num ) { myNumber = num; };
41
42   virtual QSize minimumSizeHint() const
43   {
44     return QLineEdit::minimumSizeHint().
45       expandedTo( QSize( fontMetrics().width( "0" ) * myNumber, 0 ) );
46   }
47   
48   virtual QSize sizeHint() const
49   {
50     return minimumSizeHint();
51   }
52
53 private:
54   int           myNumber;
55 };
56
57 /*
58   \class QDS_LineEdit
59   
60   \brief Datum with control corresponding to the line edit. 
61
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).
65
66   If user input is not valid, the value is displayed in red color.
67 */
68
69 /*!
70   \brief Constructor. 
71
72   Create line edit datum object with datum identifier \a id and parent widget \a parent. 
73
74   Parameter \a flags defines behaviour of datum and set of created
75   subwidgets. Default value of this parameter is QDS::All.
76
77   Parameter \a comp specifies the component name which will be used
78   when searching the dictionary item.
79
80   \param id datum identifier
81   \param parent parent widget
82   \param flags datum flags
83   \param comp component
84 */
85 QDS_LineEdit::QDS_LineEdit( const QString& id, QWidget* parent, const int flags, const QString& comp )
86 : QDS_Datum( id, parent, flags, comp )
87 {
88 }
89
90 /*!
91   \brief Destructor.
92 */
93 QDS_LineEdit::~QDS_LineEdit()
94 {
95 }
96
97 /*!
98   \brief Process notification about active units system changing.
99
100   Update validator settings for line edit.
101
102   \param system new active units system
103 */
104 void QDS_LineEdit::unitSystemChanged( const QString& system )
105 {
106   QDS_Datum::unitSystemChanged( system );
107
108   QLineEdit* le = lineEdit();
109   if ( !le )
110     return;
111   
112   delete le->validator();
113   le->setValidator(0);
114   QValidator* valid = validator();
115   if ( valid )
116     le->setValidator( valid );
117
118   QString aFormat = format();
119   int num = 0;
120   int pos = aFormat.indexOf( '%' );
121   if ( pos != -1 )
122   {
123     pos++;
124     QString aLen;
125     while ( pos < (int)aFormat.length() && aFormat.at( pos ).isDigit() )
126       aLen += aFormat.at( pos++ );
127     if ( pos < (int)aFormat.length() && aFormat.at( pos ) == '.' )
128       num += 1;
129     if ( !aLen.isEmpty() )
130       num += aLen.toInt();
131   }
132   
133   int zeroLen = format( format(), type(), 0 ).length();
134   int minLen  = format( format(), type(), minValue() ).length();
135   int maxLen  = format( format(), type(), maxValue() ).length();
136
137   num = qMax( qMax( num, zeroLen ), qMax( minLen, maxLen ) );
138   ((Editor*)le)->setNumber( num );
139 }
140
141 /*!
142   \brief Select all text in the editor.
143 */
144 void QDS_LineEdit::selectAll()
145 {
146   if ( lineEdit() )
147     lineEdit()->selectAll();
148 }
149
150 /*!
151   \brief Deselect all text in the editor.
152 */
153 void QDS_LineEdit::deselect()
154 {
155   if ( lineEdit() )
156     lineEdit()->deselect();
157 }
158
159 /*!
160   \brief Select/deselect all text in the editor.
161   \param on select text if \c true and deselect if \c false
162 */
163 void QDS_LineEdit::setSelection( const bool on )
164 {
165   if ( on )
166     selectAll();
167   else
168     deselect();
169 }
170
171 /*!
172   \brief Check if there is selection in the editor.
173   \return \c true if the editor has selected text
174 */
175 bool QDS_LineEdit::hasSelection() const
176 {
177   return lineEdit() ? lineEdit()->hasSelectedText() : false;
178 }
179
180 /*!
181   \brief Set the aligment for the line edit.
182   \param align alignment type (Qt::Alignment)
183   \param type ORed subwidget flags
184 */
185 void QDS_LineEdit::setAlignment( const int align, const int type )
186 {
187   if ( ( type & Control ) && lineEdit() )
188     lineEdit()->setAlignment( Qt::Alignment(align) );
189
190   QDS_Datum::setAlignment( align, type );
191 }
192
193 /*!
194   \brief Get string value from datum.
195   \return datum string value
196 */
197 QString QDS_LineEdit::getString() const
198 {
199   QString res;
200   if ( lineEdit() )
201     res = lineEdit()->text();
202   return res;
203 }
204
205 /*!
206   \brief Set string value to datum.
207   \param txt new datum string value
208 */
209 void QDS_LineEdit::setString( const QString& txt )
210 {
211   if ( lineEdit() )
212     lineEdit()->setText( txt );
213 }
214
215 /*!
216   \brief Get line edit widget.
217   \return pointer to the QLineEdit widget
218 */
219 QLineEdit* QDS_LineEdit::lineEdit() const
220 {
221   return ::qobject_cast<QLineEdit*>( controlWidget() );
222 }
223
224 /*!
225   \brief Create line edit widget as control subwidget.
226   \param parent parent widget
227   \return created line edit widget
228 */
229 QWidget* QDS_LineEdit::createControl( QWidget* parent )
230 {
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& ) ) );
234   return le;
235 }
236
237 /*!
238   \brief Called when text in the edit box is modified by the user.
239
240   Notify about text changing in the line edit.
241
242   \param txt current text in the line edit widget (not used)
243 */
244 void QDS_LineEdit::onTextChanged( const QString& /*txt*/ )
245 {
246   invalidateCache();
247
248   onParamChanged();
249   QString str = getString();
250   emit paramChanged();
251   emit paramChanged( str );
252 }
253
254 /*!
255   \brief Called when text is changed.
256
257   Validate the current parameter value. If value is not valid then set text color as red.
258
259   Emits signal paramChanged() to notify about changing of the control state.
260 */
261 void QDS_LineEdit::onParamChanged()
262 {
263   QLineEdit* anEdit = lineEdit();
264   if ( !anEdit )
265     return;
266
267   bool aValid = isValid( false );
268
269   QPalette aPal = anEdit->palette();
270   if ( !aValid )
271     aPal.setColor( QPalette::Active, QPalette::Text, QColor( 255, 0, 0 ) );
272   else
273     aPal.setColor( QPalette::Active, QPalette::Text, QColor( 0, 0, 0 ) );
274
275   anEdit->setPalette( aPal );
276 }
277
278 /*!
279   \brief void QDS_LineEdit::returnPressed();
280   \brief The signal is emitted when user presses \c Enter key in the line edit.
281 */