#include "QtxComboBox.h"
-#include <qpixmap.h>
-#include <qlineedit.h>
-#include <qvaluelist.h>
+#include <QtCore/qlist.h>
+#include <QtGui/qpixmap.h>
+#include <QtGui/qlineedit.h>
/*!
- Constructor
+ \class QtxComboBox
+ \brief Enhanced version of Qt combo box class.
+ \warning The implementation is not yet finished!
+
+ In addition to the QComboBox class, QtxComboBox supports
+ adding/removing the items with the associated unique identifiers.
+ It also provides a way to set "cleared" state to the combo box -
+ when no item is selected.
+
+ \todo Finalize the implementation: support adding/removing items by ID.
*/
-QtxComboBox::QtxComboBox( QWidget* parent, const char* name )
-: QComboBox( parent, name ),
-myCleared( false )
-{
- connect( this, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
- connect( this, SIGNAL( activated( const QString& ) ), this, SLOT( onActivated( const QString& ) ) );
-}
/*!
- Constructor
+ \brief Constructor.
+ \param parent parent widget
*/
-QtxComboBox::QtxComboBox( bool rw, QWidget* parent, const char* name )
-: QComboBox( rw, parent, name ),
-myCleared( false )
+QtxComboBox::QtxComboBox( QWidget* parent )
+: QComboBox( parent ),
+ myCleared( false )
{
- connect( this, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
- connect( this, SIGNAL( activated( const QString& ) ), this, SLOT( onActivated( const QString& ) ) );
+ connect( this, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
+ connect( this, SIGNAL( activated( const QString& ) ), this, SLOT( onActivated( const QString& ) ) );
}
/*!
- Destructor
+ \brief Destructor.
+
+ Does nothing currently.
*/
QtxComboBox::~QtxComboBox()
{
}
/*!
- \return true if combobox is cleared
+ \brief Check if the combo box is in the "cleared" state.
+ \return \c true if combobox is in the "cleared" state
*/
bool QtxComboBox::isCleared() const
{
- return myCleared;
+ return myCleared;
}
/*!
- Sets cleared status
- \param isClear - new status
+ \brief Set "cleared" state.
+ \param isClear new "cleared" state
*/
void QtxComboBox::setCleared( const bool isClear )
{
- if ( myCleared == isClear )
- return;
+ if ( myCleared == isClear )
+ return;
- myCleared = isClear;
+ myCleared = isClear;
- if ( editable() )
- {
- if ( myCleared )
- lineEdit()->setText( "" );
- else
- lineEdit()->setText( text( currentItem() ) );
- }
+ if ( isEditable() )
+ {
+ if ( myCleared )
+ lineEdit()->setText( "" );
+ else
+ lineEdit()->setText( itemText( currentIndex() ) );
+ }
- update();
+ update();
}
/*!
- Sets currently selected item
- \param idx - index of item
-*/
-void QtxComboBox::setCurrentItem( int idx )
-{
- if ( idx < 0 || idx >= count() )
- return;
-
- myCleared = false;
- QComboBox::setCurrentItem( idx );
-}
+ \brief Set current item.
-/*!
- Sets current text
- \param txt - new current text
+ Does nothing if the item index is out of range.
+
+ \param idx item index
*/
-void QtxComboBox::setCurrentText( const QString& txt )
+void QtxComboBox::setCurrentIndex( int idx )
{
- myCleared = false;
-#if QT_VER < 3
- int i = -1;
- for ( int j = 0; j < count() && i == -1; j++ )
- if ( text( j ) == txt )
- i = j;
- if ( i >= 0 && i < count() )
- setCurrentItem( i );
- else if ( editable() )
- lineEdit()->setText( txt );
- else
- changeItem( txt, currentItem() );
-#else
- QComboBox::setCurrentText( txt );
-#endif
+ if ( idx < 0 || idx >= count() )
+ return;
+
+ myCleared = false;
+ QComboBox::setCurrentIndex( idx );
}
/*!
- \return current selected id
+ \brief Get current item ID.
+ \return item id
*/
int QtxComboBox::currentId() const
{
- return id( currentItem() );
+ return id( currentIndex() );
}
/*!
- Sets current selected id
+ \brief Set current item by ID.
+ \param num item ID
*/
void QtxComboBox::setCurrentId( int num )
{
- setCurrentItem( index( num ) );
+ setCurrentIndex( index( num ) );
}
/*!
- Custom paint event handler
+ \brief Customize paint event.
+ \param e paint event
*/
void QtxComboBox::paintEvent( QPaintEvent* e )
{
- if ( !count() || !myCleared || editable() )
- QComboBox::paintEvent( e );
- else
- paintClear( e );
+ if ( !count() || !myCleared || isEditable() )
+ QComboBox::paintEvent( e );
+ else
+ paintClear( e );
}
/*!
- SLOT: called if some item is activated
- \param idx - index of activated item
+ \brief Called when any item is activated by the user.
+ \param idx activated item index (not used)
*/
-void QtxComboBox::onActivated( int idx )
+void QtxComboBox::onActivated( int /*idx*/ )
{
- resetClear();
-
- if ( myIndexId.contains( idx ) )
- emit activatedId( myIndexId[idx] );
+ resetClear();
}
/*!
- SLOT: called if some item is activated
-*/void QtxComboBox::onActivated( const QString& )
+ \brief Called when any item is activated by the user.
+ \param txt activated item text (not used)
+*/
+void QtxComboBox::onActivated( const QString& /*txt*/ )
{
- resetClear();
+ resetClear();
}
/*!
- Strips "cleared" state and updates
+ \brief Reset "cleared" state and update the combo box.
*/
void QtxComboBox::resetClear()
{
- if ( !myCleared )
- return;
-
- myCleared = false;
- update();
+ if ( !myCleared )
+ return;
+
+ myCleared = false;
+ update();
}
/*!
- Draws combobox when it is cleared or isn't editable
+ \brief Draw combobox in the "cleared" state.
+ \param e paint event
*/
void QtxComboBox::paintClear( QPaintEvent* e )
{
- int curIndex = currentItem();
- QString curText = text( curIndex );
-
- QPixmap curPix;
- if ( pixmap( curIndex ) )
- curPix = *pixmap( curIndex );
+ int curIndex = currentIndex();
+ QString curText = itemText( curIndex );
+ QIcon curIcon = itemIcon( curIndex );
- bool upd = isUpdatesEnabled();
- setUpdatesEnabled( false );
+ bool upd = updatesEnabled();
+ setUpdatesEnabled( false );
- changeItem( "", curIndex );
- QComboBox::paintEvent( e );
+ setItemIcon( curIndex, QIcon() );
+ setItemText( curIndex, QString::null );
+
+ QComboBox::paintEvent( e );
- if ( curPix.isNull() )
- changeItem( curText, curIndex );
- else
- changeItem( curPix, curText, curIndex );
+ setItemText( curIndex, curText );
+ setItemIcon( curIndex, curIcon );
- setUpdatesEnabled( upd );
+ setUpdatesEnabled( upd );
}
/*!
- \return id by index
+ \brief Get item ID by the index.
+ \param idx item index
+ \return item ID or -1 if index is invalid.
*/
int QtxComboBox::id( const int idx ) const
{
- int id = -1;
- if ( myIndexId.contains( idx ) )
- id = myIndexId[idx];
- return id;
+ int id = -1;
+ if ( myIndexId.contains( idx ) )
+ id = myIndexId[idx];
+ return id;
}
/*!
- \return index by id
+ \brief Get item index by the ID.
+ \param id item ID
+ \return item index or -1 if ID is invalid.
*/
int QtxComboBox::index( const int id ) const
{
- int idx = -1;
- for ( IndexIdMap::ConstIterator it = myIndexId.begin();
- it != myIndexId.end() && idx == -1; ++it )
- if ( it.data() == id )
- idx = it.key();
- return idx;
+ int idx = -1;
+ for ( IndexIdMap::ConstIterator it = myIndexId.begin(); it != myIndexId.end() && idx == -1; ++it )
+ {
+ if ( it.value() == id )
+ idx = it.key();
+ }
+ return idx;
}
#include "Qtx.h"
-#include <qmap.h>
-#include <qcombobox.h>
+#include <QtCore/qmap.h>
+#include <QtGui/qcombobox.h>
#ifdef WIN32
#pragma warning( disable:4251 )
class QTX_EXPORT QtxComboBox : public QComboBox
{
- Q_OBJECT
+ Q_OBJECT
- typedef QMap<int, int> IndexIdMap;
+ typedef QMap<int, int> IndexIdMap; //!< ID to item index map
public:
- QtxComboBox( QWidget* = 0, const char* = 0 );
- QtxComboBox( bool, QWidget* = 0, const char* = 0 );
- virtual ~QtxComboBox();
+ QtxComboBox( QWidget* = 0 );
+ virtual ~QtxComboBox();
- bool isCleared() const;
- void setCleared( const bool );
+ bool isCleared() const;
+ void setCleared( const bool );
- virtual void setCurrentItem( int );
- virtual void setCurrentText( const QString& );
+ virtual void setCurrentIndex( int );
- int currentId() const;
- void setCurrentId( int );
+ int currentId() const;
+ void setCurrentId( int );
-signals:
- void activatedId( int );
- void highlightedId( int );
+Q_SIGNALS:
+ void activatedId( int );
+ void highlightedId( int );
-private slots:
- void onActivated( int );
- void onActivated( const QString& );
+private Q_SLOTS:
+ void onActivated( int );
+ void onActivated( const QString& );
protected:
- virtual void paintEvent( QPaintEvent* );
+ virtual void paintEvent( QPaintEvent* );
private:
- int id( const int ) const;
- int index( const int ) const;
+ int id( const int ) const;
+ int index( const int ) const;
- void resetClear();
- void paintClear( QPaintEvent* );
+ void resetClear();
+ void paintClear( QPaintEvent* );
private:
- bool myCleared;
- IndexIdMap myIndexId;
+ bool myCleared; //!< "cleared" state
+ IndexIdMap myIndexId; //!< ID to item index map
};
#ifdef WIN32