Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / Qtx / QtxComboBox.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 // File:      QtxComboBox.cxx
23 // Author:    Sergey TELKOV
24 //
25 #include "QtxComboBox.h"
26
27 #include <QStandardItemModel>
28 #include <QLineEdit>
29 #include <QEvent>
30 #include <QApplication>
31
32 /*!
33   \class QtxComboBox::Model
34   \brief Internal view model, used to process 'cleared' state of the combo box.
35   \internal
36 */
37 class QtxComboBox::Model : public QStandardItemModel
38 {
39 public:
40   Model( QObject* parent = 0 );
41   ~Model();
42   void setCleared( const bool );
43
44   QVariant data( const QModelIndex&, int = Qt::DisplayRole ) const;
45
46 private:
47   bool   myCleared;
48 };
49
50 /*!
51   \brief Constructor
52   \internal
53   \param parent parent object
54 */
55 QtxComboBox::Model::Model( QObject* parent )
56   : QStandardItemModel( 0, 1, parent ),
57     myCleared( false )
58 {
59 }
60
61 /*!
62   \brief Destructor
63   \internal
64 */
65 QtxComboBox::Model::~Model()
66 {
67 }
68
69 /*!
70   \brief Set 'cleared' state
71   \param isClear new 'cleared' state
72   \internal
73 */
74 void QtxComboBox::Model::setCleared( const bool isClear )
75 {
76   if ( myCleared == isClear )
77     return;
78   
79   myCleared = isClear;
80 }
81
82 /*!
83   \brief Get model data.
84   \param index model index
85   \param role data role
86   \return data of role \a role for the \a index
87   \internal
88 */
89 QVariant QtxComboBox::Model::data( const QModelIndex& index, int role ) const
90 {
91   return ( myCleared && ( role == Qt::DisplayRole || role == Qt::DecorationRole ) ) ?
92     QVariant() : QStandardItemModel::data( index, role );
93 }
94
95 /*!
96   \class QtxComboBox::ClearEvent
97   \brief Custom event, used to process 'cleared' state of the combo box
98   in the editable mode.
99   \internal
100 */
101
102 #define CLEAR_EVENT QEvent::Type( QEvent::User + 123 )
103
104 class QtxComboBox::ClearEvent : public QEvent
105 {
106 public:
107   ClearEvent();
108 };
109
110 /*!
111   \brief Constructor
112   \internal
113 */
114 QtxComboBox::ClearEvent::ClearEvent() : QEvent( CLEAR_EVENT )
115 {
116 }
117
118 /*!
119   \class QtxComboBox
120   \brief Enhanced version of Qt combo box class.
121
122   In addition to the QComboBox class, QtxComboBox supports 
123   adding/removing the items with the associated unique identifiers.
124   It also provides a way to set "cleared" state to the combo box -
125   when no item is selected.
126 */
127
128 /*!
129   \brief Constructor.
130   \param parent parent widget
131 */
132 QtxComboBox::QtxComboBox( QWidget* parent )
133 : QComboBox( parent ),
134   myCleared( false )
135 {
136   connect( this, SIGNAL( activated( int ) ),            this, SLOT( onActivated( int ) ) );
137   connect( this, SIGNAL( currentIndexChanged( int ) ),  this, SLOT( onCurrentChanged( int ) ) );
138   setModel( new Model( this ) );
139 }
140
141 /*!
142   \brief Destructor.
143
144   Does nothing currently.
145 */
146 QtxComboBox::~QtxComboBox()
147 {
148 }
149
150 /*!
151   \brief Check if the combo box is in the "cleared" state.
152   \return \c true if combobox is in the "cleared" state
153 */
154 bool QtxComboBox::isCleared() const
155 {
156   return myCleared;
157 }
158
159 /*!
160   \brief Set "cleared" state.
161   \param isClear new "cleared" state
162 */
163 void QtxComboBox::setCleared( const bool isClear )
164 {
165   if ( myCleared == isClear )
166     return;
167     
168   myCleared = isClear;
169
170   if ( lineEdit() )
171     lineEdit()->setText( myCleared ? QString( "" ) : itemText( currentIndex() ) );
172
173   update();
174 }
175
176 /*!
177   \brief Get current item ID.
178   \return item id
179 */
180 int QtxComboBox::currentId() const
181 {
182   return id( currentIndex() );
183 }
184
185 /*!
186   \brief Set current item by ID.
187   \param num item ID
188 */
189 void QtxComboBox::setCurrentId( int num )
190 {
191   setCurrentIndex( index( num ) );
192 }
193
194 /*!
195   \brief Set the identifier to specified item.
196   \param index - index of the item
197   \param id - identifier of the item
198 */
199 void QtxComboBox::setId( const int index, const int id )
200 {
201   setItemData( index, QVariant( id ), (Qt::ItemDataRole)IdRole );
202 }
203
204 /*!
205   \brief Customize paint event.
206   \param e paint event
207 */
208 void QtxComboBox::paintEvent( QPaintEvent* e )
209 {
210   Model* m = dynamic_cast<Model*>( model() );
211   if ( m )
212     m->setCleared( myCleared );
213   QComboBox::paintEvent( e );
214   if ( m )
215     m->setCleared( false );
216 }
217
218 /*!
219   \brief Customize child addition/removal event
220   \param e child event
221 */
222 void QtxComboBox::childEvent( QChildEvent* e )
223 {
224   if ( ( e->added() || e->polished() ) && qobject_cast<QLineEdit*>( e->child() ) )
225     QApplication::postEvent( this, new ClearEvent() );
226 }
227
228 /*!
229   \brief Process custom events
230   \param e custom event
231 */
232 void QtxComboBox::customEvent( QEvent* e )
233 {
234   if ( e->type() == CLEAR_EVENT && lineEdit() && myCleared )
235     lineEdit()->setText( "" );
236 }
237
238 /*!
239   \brief Called when any item is activated by the user.
240   \param idx activated item index
241 */
242 void QtxComboBox::onActivated( int idx )
243 {
244   resetClear();
245   emit activatedId( id( idx ) );
246 }
247
248 /*!
249   \brief Called when current item is chaned (by the user or programmatically).
250   \param idx item being set current
251 */
252 void QtxComboBox::onCurrentChanged( int idx )
253 {
254   if ( idx != -1 )
255     resetClear();
256 }
257
258 /*!
259   \brief Reset "cleared" state and update the combo box.
260 */
261 void QtxComboBox::resetClear()
262 {
263   if ( !myCleared )
264     return;
265   
266   myCleared = false;
267   update();
268 }
269
270 /*!
271   \brief Get item ID by the index.
272   \param idx item index
273   \return item ID or -1 if index is invalid.
274 */
275 int QtxComboBox::id( const int idx ) const
276 {
277   int id = -1;
278   QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole );
279   if ( v.canConvert( QVariant::Int ) )
280     id = v.toInt();
281   return id;
282 }
283
284 /*!
285   \brief Get item index by the ID.
286   \param id item ID
287   \return item index or -1 if ID is invalid.
288 */
289 int QtxComboBox::index( const int ident ) const
290 {
291   int idx = -1;
292   for ( int i = 0; i < (int)count() && idx == -1; i++ )
293   {
294     if ( id( i ) == ident )
295       idx = i;
296   }
297   return idx;
298 }
299
300 /*!
301   \brief Returns true if the item with index has ID.
302   \param idx item index
303 */
304 bool QtxComboBox::hasId( const int idx ) const
305 {
306   QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole );
307   return v.canConvert( QVariant::Int );
308 }
309
310 /*!
311   \fn void QtxComboBox::activatedId( int id )
312   \brief Emitted when the item with identificator \a id is activated.
313   \param id item ID
314 */