Salome HOME
9ad83744a83ebc1a2e8d6cb18aa5b5c84620d144
[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/
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   Descr: Data control corresponding to line edit
55 */
56
57 /*!
58   Constructor.
59 */
60 QDS_LineEdit::QDS_LineEdit( const QString& id, QWidget* parent, const int flags, const QString& comp )
61 : QDS_Datum( id, parent, flags, comp )
62 {
63 }
64
65 /*!
66   Destructor.
67 */
68 QDS_LineEdit::~QDS_LineEdit()
69 {
70 }
71
72 void QDS_LineEdit::unitSystemChanged( const QString& system )
73 {
74   QDS_Datum::unitSystemChanged( system );
75
76   QLineEdit* le = lineEdit();
77   if ( !le )
78     return;
79   
80   delete le->validator();
81   le->clearValidator();
82   QValidator* valid = validator();
83   if ( valid )
84     le->setValidator( valid );
85
86   QString aFormat = format();
87   int num = 0;
88   int pos = aFormat.find( '%' );
89   if ( pos != -1 )
90   {
91     pos++;
92     QString aLen;
93     while ( pos < (int)aFormat.length() && aFormat.at( pos ).isDigit() )
94       aLen += aFormat.at( pos++ );
95     if ( pos < (int)aFormat.length() && aFormat.at( pos ) == '.' )
96       num += 1;
97     if ( !aLen.isEmpty() )
98       num += aLen.toInt();
99   }
100   
101   int zeroLen = format( format(), type(), 0 ).length();
102   int minLen  = format( format(), type(), minValue() ).length();
103   int maxLen  = format( format(), type(), maxValue() ).length();
104
105   num = QMAX( QMAX( num, zeroLen ), QMAX( minLen, maxLen ) );
106   ((Editor*)le)->setNumber( num );
107 }
108
109 /*!
110   Set the aligment of line edit.
111 */
112 void QDS_LineEdit::setAlignment( const int align, const int type )
113 {
114   if ( ( type & Control ) && lineEdit() )
115     lineEdit()->setAlignment( align );
116
117   QDS_Datum::setAlignment( align, type );
118 }
119
120 /*!
121   Returns string from QLineEdit widget.
122 */
123 QString QDS_LineEdit::getString() const
124 {
125   QString res;
126   if ( lineEdit() )
127     res = lineEdit()->text();
128   return res;
129 }
130
131 /*!
132   Sets the string into QLineEdit widget.
133 */
134 void QDS_LineEdit::setString( const QString& txt )
135 {
136   if ( lineEdit() )
137     lineEdit()->setText( txt );
138 }
139
140 /*!
141   Returns pointer to QLineEdit widget.
142 */
143 QLineEdit* QDS_LineEdit::lineEdit() const
144 {
145   return ::qt_cast<QLineEdit*>( controlWidget() );
146 }
147
148 /*!
149   Create QLineEdit widget as control subwidget.
150 */
151 QWidget* QDS_LineEdit::createControl( QWidget* parent )
152 {
153   Editor* le = new Editor( parent );
154   connect( le, SIGNAL( returnPressed() ), this, SIGNAL( returnPressed() ) );
155   connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
156   return le;
157 }
158
159 /*!
160   Notify about text changing in line edit.
161 */
162 void QDS_LineEdit::onTextChanged( const QString& )
163 {
164   invalidateCache();
165
166   onParamChanged();
167   QString str = getString();
168   emit paramChanged();
169   emit paramChanged( str );
170 }
171
172 /*!
173   Checks the current parameter value on validity.
174 */
175 void QDS_LineEdit::onParamChanged()
176 {
177   QLineEdit* anEdit = lineEdit();
178   if ( !anEdit )
179     return;
180
181   bool aValid = isValid( false );
182
183   QPalette aPal = anEdit->palette();
184   if ( !aValid )
185     aPal.setColor( QPalette::Active, QColorGroup::Text, QColor( 255, 0, 0 ) );
186   else
187     aPal.setColor( QPalette::Active, QColorGroup::Text, QColor( 0, 0, 0 ) );
188
189   anEdit->setPalette( aPal );
190 }