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