Salome HOME
cb5ddf1486690e53029822e4b2e4883f4d57bb04
[modules/gui.git] / src / QDS / QDS_LineEdit.cxx
1 // Copyright (C) 2005  CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "QDS_LineEdit.h"
20
21 #include <qlineedit.h>
22 #include <qvalidator.h>
23
24 /*
25   class: QDS_LineEdit::Editor
26   descr: Internal class inherited from line edit
27 */
28
29 class QDS_LineEdit::Editor : public QLineEdit
30 {
31 public:
32   Editor( QWidget* parent = 0 ) : QLineEdit( parent ), myNumber( 2 ) {};
33   virtual ~Editor() {};
34
35   void setNumber( const int num ) { myNumber = num; };
36
37   virtual QSize minimumSizeHint() const
38   {
39     return QLineEdit::minimumSizeHint().
40       expandedTo( QSize( fontMetrics().width( "0" ) * myNumber, 0 ) );
41   }
42   
43   virtual QSize sizeHint() const
44   {
45     return minimumSizeHint();
46   }
47
48 private:
49   int           myNumber;
50 };
51
52 /*
53   \class QDS_LineEdit
54   
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.
59 */
60
61 /*!
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.
65 */
66 QDS_LineEdit::QDS_LineEdit( const QString& id, QWidget* parent, const int flags, const QString& comp )
67 : QDS_Datum( id, parent, flags, comp )
68 {
69 }
70
71 /*!
72   Destructor.
73 */
74 QDS_LineEdit::~QDS_LineEdit()
75 {
76 }
77
78 /*!
79   Notification about active unit system changing. Reimplemented from QDS_Datum.
80   Update validator settings for line edit.
81 */
82 void QDS_LineEdit::unitSystemChanged( const QString& system )
83 {
84   QDS_Datum::unitSystemChanged( system );
85
86   QLineEdit* le = lineEdit();
87   if ( !le )
88     return;
89   
90   delete le->validator();
91   le->clearValidator();
92   QValidator* valid = validator();
93   if ( valid )
94     le->setValidator( valid );
95
96   QString aFormat = format();
97   int num = 0;
98   int pos = aFormat.find( '%' );
99   if ( pos != -1 )
100   {
101     pos++;
102     QString aLen;
103     while ( pos < (int)aFormat.length() && aFormat.at( pos ).isDigit() )
104       aLen += aFormat.at( pos++ );
105     if ( pos < (int)aFormat.length() && aFormat.at( pos ) == '.' )
106       num += 1;
107     if ( !aLen.isEmpty() )
108       num += aLen.toInt();
109   }
110   
111   int zeroLen = format( format(), type(), 0 ).length();
112   int minLen  = format( format(), type(), minValue() ).length();
113   int maxLen  = format( format(), type(), maxValue() ).length();
114
115   num = QMAX( QMAX( num, zeroLen ), QMAX( minLen, maxLen ) );
116   ((Editor*)le)->setNumber( num );
117 }
118
119 /*!
120   Select all text in the editor.
121 */
122 void QDS_LineEdit::selectAll()
123 {
124   if ( lineEdit() )
125     lineEdit()->selectAll();
126 }
127
128 /*!
129   Deselect all text in the editor.
130 */
131 void QDS_LineEdit::deselect()
132 {
133   if ( lineEdit() )
134     lineEdit()->deselect();
135 }
136
137 /*!
138   Select or deselect all text in the editor.
139 */
140 void QDS_LineEdit::setSelection( const bool on )
141 {
142   if ( on )
143     selectAll();
144   else
145     deselect();
146 }
147
148 /*!
149   Returns true if the editor has selected text.
150 */
151 bool QDS_LineEdit::hasSelection() const
152 {
153   return lineEdit() ? lineEdit()->hasSelectedText() : false;
154 }
155
156 /*!
157   Set the aligment of line edit. Reimplemented from QDS_Datum.
158 */
159 void QDS_LineEdit::setAlignment( const int align, const int type )
160 {
161   if ( ( type & Control ) && lineEdit() )
162     lineEdit()->setAlignment( align );
163
164   QDS_Datum::setAlignment( align, type );
165 }
166
167 /*!
168   Returns string value from QLineEdit widget. Reimplemented from QDS_Datum.
169 */
170 QString QDS_LineEdit::getString() const
171 {
172   QString res;
173   if ( lineEdit() )
174     res = lineEdit()->text();
175   return res;
176 }
177
178 /*!
179   Sets the string value into QLineEdit widget. Reimplemented from QDS_Datum.
180 */
181 void QDS_LineEdit::setString( const QString& txt )
182 {
183   if ( lineEdit() )
184     lineEdit()->setText( txt );
185 }
186
187 /*!
188   Returns pointer to QLineEdit widget.
189 */
190 QLineEdit* QDS_LineEdit::lineEdit() const
191 {
192   return ::qt_cast<QLineEdit*>( controlWidget() );
193 }
194
195 /*!
196   Create QLineEdit widget as control subwidget. Reimplemented from QDS_Datum.
197 */
198 QWidget* QDS_LineEdit::createControl( QWidget* parent )
199 {
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& ) ) );
203   return le;
204 }
205
206 /*!
207   Notify about text changing in line edit.
208 */
209 void QDS_LineEdit::onTextChanged( const QString& )
210 {
211   invalidateCache();
212
213   onParamChanged();
214   QString str = getString();
215   emit paramChanged();
216   emit paramChanged( str );
217 }
218
219 /*!
220   Checks the current parameter value on validity. If value is not valid then set text color as red.
221 */
222 void QDS_LineEdit::onParamChanged()
223 {
224   QLineEdit* anEdit = lineEdit();
225   if ( !anEdit )
226     return;
227
228   bool aValid = isValid( false );
229
230   QPalette aPal = anEdit->palette();
231   if ( !aValid )
232     aPal.setColor( QPalette::Active, QColorGroup::Text, QColor( 255, 0, 0 ) );
233   else
234     aPal.setColor( QPalette::Active, QColorGroup::Text, QColor( 0, 0, 0 ) );
235
236   anEdit->setPalette( aPal );
237 }