1 // Copyright (C) 2007-2015 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
23 // File: QtxComboBox.cxx
24 // Author: Sergey TELKOV
26 #include "QtxComboBox.h"
28 #include <QStandardItemModel>
31 #include <QApplication>
34 \class QtxComboBox::Model
35 \brief Internal view model, used to process 'cleared' state of the combo box.
38 class QtxComboBox::Model : public QStandardItemModel
41 Model( QObject* parent = 0 );
43 void setCleared( const bool );
45 QVariant data( const QModelIndex&, int = Qt::DisplayRole ) const;
54 \param parent parent object
56 QtxComboBox::Model::Model( QObject* parent )
57 : QStandardItemModel( 0, 1, parent ),
66 QtxComboBox::Model::~Model()
71 \brief Set 'cleared' state
72 \param isClear new 'cleared' state
75 void QtxComboBox::Model::setCleared( const bool isClear )
77 if ( myCleared == isClear )
84 \brief Get model data.
85 \param index model index
87 \return data of role \a role for the \a index
90 QVariant QtxComboBox::Model::data( const QModelIndex& index, int role ) const
92 return ( myCleared && ( role == Qt::DisplayRole || role == Qt::DecorationRole ) ) ?
93 QVariant() : QStandardItemModel::data( index, role );
97 \class QtxComboBox::ClearEvent
98 \brief Custom event, used to process 'cleared' state of the combo box
103 #define CLEAR_EVENT QEvent::Type( QEvent::User + 123 )
105 class QtxComboBox::ClearEvent : public QEvent
115 QtxComboBox::ClearEvent::ClearEvent() : QEvent( CLEAR_EVENT )
121 \brief Enhanced version of Qt combo box class.
123 In addition to the QComboBox class, QtxComboBox supports
124 adding/removing the items with the associated unique identifiers.
125 It also provides a way to set "cleared" state to the combo box -
126 when no item is selected.
131 \param parent parent widget
133 QtxComboBox::QtxComboBox( QWidget* parent )
134 : QComboBox( parent ),
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 current item is chaned (by the user or programmatically).
240 \param idx item being set current
242 void QtxComboBox::onCurrentChanged( int idx )
247 emit activatedId( id( idx ) );
252 \brief Reset "cleared" state and update the combo box.
254 void QtxComboBox::resetClear()
264 \brief Get item ID by the index.
265 \param idx item index
266 \return item ID or -1 if index is invalid.
268 int QtxComboBox::id( const int idx ) const
271 QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole );
272 if ( v.canConvert( QVariant::Int ) )
278 \brief Get item index by the ID.
280 \return item index or -1 if ID is invalid.
282 int QtxComboBox::index( const int ident ) const
285 for ( int i = 0; i < (int)count() && idx == -1; i++ )
287 if ( id( i ) == ident )
294 \brief Returns true if the item with index has ID.
295 \param idx item index
297 bool QtxComboBox::hasId( const int idx ) const
299 QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole );
300 return v.canConvert( QVariant::Int );
304 \fn void QtxComboBox::activatedId( int id )
305 \brief Emitted when the item with identificator \a id is activated.