Salome HOME
15cc3f0570e5aa74494c18a4373e670bc53f03a2
[modules/gui.git] / src / QDS / QDS_LineEdit.cxx
1 #include "QDS_LineEdit.h"
2
3 #include <qlineedit.h>
4 #include <qvalidator.h>
5
6 /*
7   Class: QDS_LineEdit::Editor
8   Descr: Internal class inherited from line edit
9 */
10
11 class QDS_LineEdit::Editor : public QLineEdit
12 {
13 public:
14   Editor( QWidget* parent = 0 ) : QLineEdit( parent ), myNumber( 2 ) {};
15   virtual ~Editor() {};
16
17   void setNumber( const int num ) { myNumber = num; };
18
19   virtual QSize minimumSizeHint() const
20   {
21     return QLineEdit::minimumSizeHint().
22       expandedTo( QSize( fontMetrics().width( "0" ) * myNumber, 0 ) );
23   }
24   
25   virtual QSize sizeHint() const
26   {
27     return minimumSizeHint();
28   }
29
30 private:
31   int           myNumber;
32 };
33
34 /*
35   Class: QDS_LineEdit
36   Descr: Data control corresponding to line edit
37 */
38
39 /*!
40   Constructor.
41 */
42 QDS_LineEdit::QDS_LineEdit( const QString& id, QWidget* parent, const int flags, const QString& comp )
43 : QDS_Datum( id, parent, flags, comp )
44 {
45 }
46
47 /*!
48   Destructor.
49 */
50 QDS_LineEdit::~QDS_LineEdit()
51 {
52 }
53
54 void QDS_LineEdit::unitSystemChanged( const QString& system )
55 {
56   QDS_Datum::unitSystemChanged( system );
57
58   QLineEdit* le = lineEdit();
59   if ( !le )
60     return;
61   
62   delete le->validator();
63   le->clearValidator();
64   QValidator* valid = validator();
65   if ( valid )
66     le->setValidator( valid );
67
68   QString aFormat = format();
69   int num = 0;
70   int pos = aFormat.find( '%' );
71   if ( pos != -1 )
72   {
73     pos++;
74     QString aLen;
75     while ( pos < (int)aFormat.length() && aFormat.at( pos ).isDigit() )
76       aLen += aFormat.at( pos++ );
77     if ( pos < (int)aFormat.length() && aFormat.at( pos ) == '.' )
78       num += 1;
79     if ( !aLen.isEmpty() )
80       num += aLen.toInt();
81   }
82   
83   int zeroLen = format( format(), type(), 0 ).length();
84   int minLen  = format( format(), type(), minValue() ).length();
85   int maxLen  = format( format(), type(), maxValue() ).length();
86
87   num = QMAX( QMAX( num, zeroLen ), QMAX( minLen, maxLen ) );
88   ((Editor*)le)->setNumber( num );
89 }
90
91 /*!
92   Set the aligment of line edit.
93 */
94 void QDS_LineEdit::setAlignment( const int align, const int type )
95 {
96   if ( ( type & Control ) && lineEdit() )
97     lineEdit()->setAlignment( align );
98
99   QDS_Datum::setAlignment( align, type );
100 }
101
102 /*!
103   Returns string from QLineEdit widget.
104 */
105 QString QDS_LineEdit::getString() const
106 {
107   QString res;
108   if ( lineEdit() )
109     res = lineEdit()->text();
110   return res;
111 }
112
113 /*!
114   Sets the string into QLineEdit widget.
115 */
116 void QDS_LineEdit::setString( const QString& txt )
117 {
118   if ( lineEdit() )
119     lineEdit()->setText( txt );
120 }
121
122 /*!
123   Returns pointer to QLineEdit widget.
124 */
125 QLineEdit* QDS_LineEdit::lineEdit() const
126 {
127   return ::qt_cast<QLineEdit*>( controlWidget() );
128 }
129
130 /*!
131   Create QLineEdit widget as control subwidget.
132 */
133 QWidget* QDS_LineEdit::createControl( QWidget* parent )
134 {
135   Editor* le = new Editor( parent );
136   connect( le, SIGNAL( returnPressed() ), this, SIGNAL( returnPressed() ) );
137   connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
138   return le;
139 }
140
141 /*!
142   Notify about text changing in line edit.
143 */
144 void QDS_LineEdit::onTextChanged( const QString& )
145 {
146   invalidateCache();
147
148   onParamChanged();
149   QString str = getString();
150   emit paramChanged();
151   emit paramChanged( str );
152 }
153
154 /*!
155   Checks the current parameter value on validity.
156 */
157 void QDS_LineEdit::onParamChanged()
158 {
159   QLineEdit* anEdit = lineEdit();
160   if ( !anEdit )
161     return;
162
163   bool aValid = isValid( false );
164
165   QPalette aPal = anEdit->palette();
166   if ( !aValid )
167     aPal.setColor( QPalette::Active, QColorGroup::Text, QColor( 255, 0, 0 ) );
168   else
169     aPal.setColor( QPalette::Active, QColorGroup::Text, QColor( 0, 0, 0 ) );
170
171   anEdit->setPalette( aPal );
172 }