#include "QtxComboBox.h"
+#include <QStandardItemModel>
#include <QLineEdit>
+#include <QEvent>
+#include <QApplication>
+
+/*!
+ \class QtxComboBox::Model
+ \brief Internal view model, used to process 'cleared' state of the combo box.
+ \internal
+*/
+class QtxComboBox::Model : public QStandardItemModel
+{
+public:
+ Model( QObject* parent = 0 );
+ ~Model();
+ void setCleared( const bool );
+
+ QVariant data( const QModelIndex&, int = Qt::DisplayRole ) const;
+
+private:
+ bool myCleared;
+};
+
+/*!
+ \brief Constructor
+ \internal
+ \param parent parent object
+*/
+QtxComboBox::Model::Model( QObject* parent )
+ : QStandardItemModel( 0, 1, parent ),
+ myCleared( false )
+{
+}
+
+/*!
+ \brief Destructor
+ \internal
+*/
+QtxComboBox::Model::~Model()
+{
+}
+
+/*!
+ \brief Set 'cleared' state
+ \param isClear new 'cleared' state
+ \internal
+*/
+void QtxComboBox::Model::setCleared( const bool isClear )
+{
+ if ( myCleared == isClear )
+ return;
+
+ myCleared = isClear;
+}
+
+/*!
+ \brief Get model data.
+ \param index model index
+ \param role data role
+ \return data of role \a role for the \a index
+ \internal
+*/
+QVariant QtxComboBox::Model::data( const QModelIndex& index, int role ) const
+{
+ return myCleared ? QVariant() : QStandardItemModel::data( index, role );
+}
+
+/*!
+ \class QtxComboBox::ClearEvent
+ \brief Custom event, used to process 'cleared' state of the combo box
+ in the editable mode.
+ \internal
+*/
+
+#define CLEAR_EVENT QEvent::Type( QEvent::User + 123 )
+
+class QtxComboBox::ClearEvent : public QEvent
+{
+public:
+ ClearEvent();
+};
+
+/*!
+ \brief Constructor
+ \internal
+*/
+QtxComboBox::ClearEvent::ClearEvent() : QEvent( CLEAR_EVENT )
+{
+}
/*!
\class QtxComboBox
: QComboBox( parent ),
myCleared( false )
{
- connect( this, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
+ connect( this, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
connect( this, SIGNAL( activated( const QString& ) ), this, SLOT( onActivated( const QString& ) ) );
+ connect( this, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onCurrentChanged( int ) ) );
+ setModel( new Model( this ) );
}
/*!
return;
myCleared = isClear;
-
- if ( isEditable() )
- {
- if ( myCleared )
- lineEdit()->setText( "" );
- else
- lineEdit()->setText( itemText( currentIndex() ) );
- }
-
- update();
-}
-
-/*!
- \brief Set current item.
- Does nothing if the item index is out of range.
+ if ( lineEdit() )
+ lineEdit()->setText( myCleared ? QString( "" ) : itemText( currentIndex() ) );
- \param idx item index
-*/
-void QtxComboBox::setCurrentIndex( int idx )
-{
- if ( idx < 0 || idx >= count() )
- return;
-
- myCleared = false;
- QComboBox::setCurrentIndex( idx );
+ update();
}
/*!
*/
void QtxComboBox::paintEvent( QPaintEvent* e )
{
- if ( !count() || !myCleared || isEditable() )
- QComboBox::paintEvent( e );
- else
- paintClear( e );
+ Model* m = dynamic_cast<Model*>( model() );
+ m->setCleared( myCleared );
+ QComboBox::paintEvent( e );
+ m->setCleared( false );
+}
+
+/*!
+ \brief Customize child addition/removal event
+ \param e child event
+*/
+void QtxComboBox::childEvent( QChildEvent* e )
+{
+ if ( e->added() || e->polished() && qobject_cast<QLineEdit*>( e->child() ) )
+ QApplication::postEvent( this, new ClearEvent() );
+}
+
+/*!
+ \brief Process custom events
+ \param e custom event
+*/
+void QtxComboBox::customEvent( QEvent* e )
+{
+ if ( e->type() == CLEAR_EVENT && lineEdit() && myCleared )
+ lineEdit()->setText( "" );
}
/*!
void QtxComboBox::onActivated( int idx )
{
resetClear();
-
emit activatedId( id( idx ) );
}
/*!
- \brief Called when any item is activated by the user.
- \param txt activated item text (not used)
+ \brief Called when current item is chaned (by the user or programmatically).
+ \param idx item being set current
*/
-void QtxComboBox::onActivated( const QString& /*txt*/ )
+void QtxComboBox::onCurrentChanged( int idx )
{
- resetClear();
+ if ( idx != -1 )
+ resetClear();
}
/*!
update();
}
-/*!
- \brief Draw combobox in the "cleared" state.
- \param e paint event
-*/
-void QtxComboBox::paintClear( QPaintEvent* e )
-{
- int curIndex = currentIndex();
- QString curText = itemText( curIndex );
- QIcon curIcon = itemIcon( curIndex );
-
- bool upd = updatesEnabled();
- setUpdatesEnabled( false );
-
- setItemIcon( curIndex, QIcon() );
- setItemText( curIndex, QString() );
-
- QComboBox::paintEvent( e );
-
- setItemText( curIndex, curText );
- setItemIcon( curIndex, curIcon );
-
- setUpdatesEnabled( upd );
-}
-
/*!
\brief Get item ID by the index.
\param idx item index
\brief Emitted when the item with identificator \a id is activated.
\param id item ID
*/
-
-/*!
- \fn void QtxComboBox::highlightedId( int id )
- \brief Emitted when the item with identificator \a id is highlighted.
- \param id item ID
-*/