1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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 \a role for given \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( currentIndexChanged( int ) ), this, SLOT( onCurrentChanged( int ) ) );
137 setModel( new Model( this ) );
143 QtxComboBox::~QtxComboBox()
148 \brief Check if combo box is in "cleared" state.
149 \return \c true if combo box is in "cleared" state or \c false otherwise
151 bool QtxComboBox::isCleared() const
157 \brief Set "cleared" state.
158 \param isClear new "cleared" state
160 void QtxComboBox::setCleared( const bool isClear )
162 if ( myCleared == isClear )
168 lineEdit()->setText( myCleared ? QString( "" ) : itemText( currentIndex() ) );
174 \brief Get current item's identifier.
175 \return current item's identifier
177 QVariant QtxComboBox::currentId() const
179 return id( currentIndex() );
183 \brief Set current item by identifier.
184 \param ident item's identifier
186 void QtxComboBox::setCurrentId( const QVariant& ident )
188 setCurrentIndex( index( ident ) );
192 \brief Assign identifier to specified item.
193 \param idx item's index
194 \param ident item's identifier
196 void QtxComboBox::setId( const int idx, const QVariant& ident )
198 setItemData( idx, ident, (Qt::ItemDataRole)IdRole );
202 \brief Customize paint event.
205 void QtxComboBox::paintEvent( QPaintEvent* e )
207 Model* m = dynamic_cast<Model*>( model() );
209 m->setCleared( myCleared );
210 QComboBox::paintEvent( e );
212 m->setCleared( false );
216 \brief Customize child addition/removal event.
219 void QtxComboBox::childEvent( QChildEvent* e )
221 if ( ( e->added() || e->polished() ) && qobject_cast<QLineEdit*>( e->child() ) )
222 QApplication::postEvent( this, new ClearEvent() );
226 \brief Process custom events
227 \param e custom event
229 void QtxComboBox::customEvent( QEvent* e )
231 if ( e->type() == CLEAR_EVENT && lineEdit() && myCleared )
232 lineEdit()->setText( "" );
236 \brief Called when current item is changed (by user or programmatically).
237 \param idx index of item being set current
239 void QtxComboBox::onCurrentChanged( int idx )
244 QVariant ident = id( idx );
245 emit activatedId( id( idx ) );
246 if ( ident.type() == QVariant::Int )
247 emit activatedId( ident.toInt() );
248 else if ( ident.type() == QVariant::String )
249 emit activatedId( ident.toString() );
254 \brief Reset "cleared" state and update combo box.
256 void QtxComboBox::resetClear()
266 \brief Get item's identifier by index.
267 \param idx item's index
268 \return item's identifier or invalid QVariant if index is out of range
269 or identifier is not assigned to item
271 QVariant QtxComboBox::id( const int idx ) const
273 return itemData( idx, (Qt::ItemDataRole)IdRole );
277 \brief Get item index by identifier.
278 \param ident item's identifier
279 \return item's index or -1 if \a ident is invalid of if item is not found
281 int QtxComboBox::index( const QVariant& ident ) const
283 if ( !ident.isValid() ) return -1;
284 return findData( ident, (Qt::ItemDataRole)IdRole );
288 \brief Check if item has assigned identifier.
289 \param idx item's index
290 \return \c true if item with given index has identifier of \c false otherwise
292 bool QtxComboBox::hasId( const int idx ) const
294 QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole );
299 \fn void QtxComboBox::activatedId( QVariant ident )
300 \brief Emitted when item with identificator \a ident is activated.
301 \param ident item's identifier
305 \fn void QtxComboBox::activatedId( int ident )
306 \brief Emitted when item with integer identificator \a ident is activated.
307 \param ident item's identifier
311 \fn void QtxComboBox::activatedId( QString ident )
312 \brief Emitted when item with string identificator \a ident is activated.
313 \param ident item's identifier