1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: QtxComboBox.cxx
23 // Author: Sergey TELKOV
25 #include "QtxComboBox.h"
27 #include <QStandardItemModel>
30 #include <QApplication>
33 \class QtxComboBox::Model
34 \brief Internal view model, used to process 'cleared' state of the combo box.
37 class QtxComboBox::Model : public QStandardItemModel
40 Model( QObject* parent = 0 );
42 void setCleared( const bool );
44 QVariant data( const QModelIndex&, int = Qt::DisplayRole ) const;
53 \param parent parent object
55 QtxComboBox::Model::Model( QObject* parent )
56 : QStandardItemModel( 0, 1, parent ),
65 QtxComboBox::Model::~Model()
70 \brief Set 'cleared' state
71 \param isClear new 'cleared' state
74 void QtxComboBox::Model::setCleared( const bool isClear )
76 if ( myCleared == isClear )
83 \brief Get model data.
84 \param index model index
86 \return data of role \a role for the \a index
89 QVariant QtxComboBox::Model::data( const QModelIndex& index, int role ) const
91 return ( myCleared && ( role == Qt::DisplayRole || role == Qt::DecorationRole ) ) ?
92 QVariant() : QStandardItemModel::data( index, role );
96 \class QtxComboBox::ClearEvent
97 \brief Custom event, used to process 'cleared' state of the combo box
102 #define CLEAR_EVENT QEvent::Type( QEvent::User + 123 )
104 class QtxComboBox::ClearEvent : public QEvent
114 QtxComboBox::ClearEvent::ClearEvent() : QEvent( CLEAR_EVENT )
120 \brief Enhanced version of Qt combo box class.
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.
130 \param parent parent widget
132 QtxComboBox::QtxComboBox( QWidget* parent )
133 : QComboBox( parent ),
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 ) );
144 Does nothing currently.
146 QtxComboBox::~QtxComboBox()
151 \brief Check if the combo box is in the "cleared" state.
152 \return \c true if combobox is in the "cleared" state
154 bool QtxComboBox::isCleared() const
160 \brief Set "cleared" state.
161 \param isClear new "cleared" state
163 void QtxComboBox::setCleared( const bool isClear )
165 if ( myCleared == isClear )
171 lineEdit()->setText( myCleared ? QString( "" ) : itemText( currentIndex() ) );
177 \brief Get current item ID.
180 int QtxComboBox::currentId() const
182 return id( currentIndex() );
186 \brief Set current item by ID.
189 void QtxComboBox::setCurrentId( int num )
191 setCurrentIndex( index( num ) );
195 \brief Set the identifier to specified item.
196 \param index - index of the item
197 \param id - identifier of the item
199 void QtxComboBox::setId( const int index, const int id )
201 setItemData( index, QVariant( id ), (Qt::ItemDataRole)IdRole );
205 \brief Customize paint event.
208 void QtxComboBox::paintEvent( QPaintEvent* e )
210 Model* m = dynamic_cast<Model*>( model() );
212 m->setCleared( myCleared );
213 QComboBox::paintEvent( e );
215 m->setCleared( false );
219 \brief Customize child addition/removal event
222 void QtxComboBox::childEvent( QChildEvent* e )
224 if ( ( e->added() || e->polished() ) && qobject_cast<QLineEdit*>( e->child() ) )
225 QApplication::postEvent( this, new ClearEvent() );
229 \brief Process custom events
230 \param e custom event
232 void QtxComboBox::customEvent( QEvent* e )
234 if ( e->type() == CLEAR_EVENT && lineEdit() && myCleared )
235 lineEdit()->setText( "" );
239 \brief Called when any item is activated by the user.
240 \param idx activated item index
242 void QtxComboBox::onActivated( int idx )
245 emit activatedId( id( idx ) );
249 \brief Called when current item is chaned (by the user or programmatically).
250 \param idx item being set current
252 void QtxComboBox::onCurrentChanged( int idx )
259 \brief Reset "cleared" state and update the combo box.
261 void QtxComboBox::resetClear()
271 \brief Get item ID by the index.
272 \param idx item index
273 \return item ID or -1 if index is invalid.
275 int QtxComboBox::id( const int idx ) const
278 QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole );
279 if ( v.canConvert( QVariant::Int ) )
285 \brief Get item index by the ID.
287 \return item index or -1 if ID is invalid.
289 int QtxComboBox::index( const int ident ) const
292 for ( int i = 0; i < (int)count() && idx == -1; i++ )
294 if ( id( i ) == ident )
301 \brief Returns true if the item with index has ID.
302 \param idx item index
304 bool QtxComboBox::hasId( const int idx ) const
306 QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole );
307 return v.canConvert( QVariant::Int );
311 \fn void QtxComboBox::activatedId( int id )
312 \brief Emitted when the item with identificator \a id is activated.