]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxComboBox.cxx
Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / Qtx / QtxComboBox.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, 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 // File:      QtxComboBox.cxx
20 // Author:    Sergey TELKOV
21
22 #include "QtxComboBox.h"
23
24 #include <qpixmap.h>
25 #include <qlineedit.h>
26 #include <qvaluelist.h>
27
28 /*!
29   Constructor
30 */
31 QtxComboBox::QtxComboBox( QWidget* parent, const char* name )
32 : QComboBox( parent, name ),
33 myCleared( false )
34 {
35     connect( this, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
36     connect( this, SIGNAL( activated( const QString& ) ), this, SLOT( onActivated( const QString& ) ) );
37 }
38
39 /*!
40   Constructor
41 */
42 QtxComboBox::QtxComboBox( bool rw, QWidget* parent, const char* name )
43 : QComboBox( rw, parent, name ),
44 myCleared( false )
45 {
46     connect( this, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
47     connect( this, SIGNAL( activated( const QString& ) ), this, SLOT( onActivated( const QString& ) ) );
48 }
49
50 /*!
51   Destructor
52 */
53 QtxComboBox::~QtxComboBox()
54 {
55 }
56
57 /*!
58   \return true if combobox is cleared
59 */
60 bool QtxComboBox::isCleared() const
61 {
62     return myCleared;
63 }
64
65 /*!
66   Sets cleared status
67   \param isClear - new status
68 */
69 void QtxComboBox::setCleared( const bool isClear )
70 {
71     if ( myCleared == isClear )
72         return;
73     
74     myCleared = isClear;
75     
76     if ( editable() )
77     {
78         if ( myCleared )
79             lineEdit()->setText( "" );
80         else
81             lineEdit()->setText( text( currentItem() ) );
82     }
83     
84     update();
85 }
86
87 /*!
88   Sets currently selected item
89   \param idx - index of item
90 */
91 void QtxComboBox::setCurrentItem( int idx )
92 {
93     if ( idx < 0 || idx >= count() )
94         return;
95     
96     myCleared = false;
97     QComboBox::setCurrentItem( idx );
98 }
99
100 /*!
101   Sets current text
102   \param txt - new current text
103 */
104 void QtxComboBox::setCurrentText( const QString& txt )
105 {
106     myCleared = false;
107 #if QT_VER < 3
108     int i = -1;
109     for ( int j = 0; j < count() && i == -1; j++ )
110         if ( text( j ) == txt )
111             i = j;
112     if ( i >= 0 && i < count() )
113         setCurrentItem( i );
114     else if ( editable() )
115         lineEdit()->setText( txt );
116     else
117         changeItem( txt, currentItem() );
118 #else
119     QComboBox::setCurrentText( txt );
120 #endif
121 }
122
123 /*!
124   \return current selected id
125 */
126 int QtxComboBox::currentId() const
127 {
128     return id( currentItem() );
129 }
130
131 /*!
132   Sets current selected id
133 */
134 void QtxComboBox::setCurrentId( int num )
135 {
136     setCurrentItem( index( num ) );
137 }
138
139 /*!
140   Custom paint event handler
141 */
142 void QtxComboBox::paintEvent( QPaintEvent* e )
143 {
144     if ( !count() || !myCleared || editable() )
145         QComboBox::paintEvent( e );
146     else
147         paintClear( e );
148 }
149
150 /*!
151   SLOT: called if some item is activated
152   \param idx - index of activated item
153 */
154 void QtxComboBox::onActivated( int idx )
155 {
156     resetClear();
157     
158     if ( myIndexId.contains( idx ) )
159         emit activatedId( myIndexId[idx] );
160 }
161
162 /*!
163   SLOT: called if some item is activated
164 */void QtxComboBox::onActivated( const QString& )
165 {
166     resetClear();
167 }
168
169 /*!
170   Strips "cleared" state and updates
171 */
172 void QtxComboBox::resetClear()
173 {
174     if ( !myCleared )
175         return;
176     
177     myCleared = false;
178     update();
179 }
180
181 /*!
182   Draws combobox when it is cleared or isn't editable
183 */
184 void QtxComboBox::paintClear( QPaintEvent* e )
185 {
186     int curIndex = currentItem();
187     QString curText = text( curIndex );
188     
189     QPixmap curPix;
190     if ( pixmap( curIndex ) )
191         curPix = *pixmap( curIndex );
192     
193     bool upd = isUpdatesEnabled();
194     setUpdatesEnabled( false );
195     
196     changeItem( "", curIndex );
197     QComboBox::paintEvent( e );
198     
199     if ( curPix.isNull() )
200         changeItem( curText, curIndex );
201     else
202         changeItem( curPix, curText, curIndex );
203     
204     setUpdatesEnabled( upd );
205 }
206
207 /*!
208   \return id by index
209 */
210 int QtxComboBox::id( const int idx ) const
211 {
212     int id = -1;
213     if ( myIndexId.contains( idx ) )
214         id = myIndexId[idx];
215     return id;
216 }
217
218 /*!
219   \return index by id
220 */
221 int QtxComboBox::index( const int id ) const
222 {
223     int idx = -1;
224     for ( IndexIdMap::ConstIterator it = myIndexId.begin();
225     it != myIndexId.end() && idx == -1; ++it )
226         if ( it.data() == id )
227             idx = it.key();
228         return idx;
229 }