From c1baada7e41840963aa8db1f4ad6b09ba9a00fa3 Mon Sep 17 00:00:00 2001 From: vsr Date: Thu, 10 May 2007 09:03:54 +0000 Subject: [PATCH] Porting to Qt4 --- src/Qtx/QtxComboBox.cxx | 223 ++++++++++++++++++-------------------- src/Qtx/QtxComboBox.h | 50 ++++----- src/Qtx/QtxDblSpinBox.cxx | 6 +- 3 files changed, 132 insertions(+), 147 deletions(-) diff --git a/src/Qtx/QtxComboBox.cxx b/src/Qtx/QtxComboBox.cxx index a2c95f251..ac57d6752 100755 --- a/src/Qtx/QtxComboBox.cxx +++ b/src/Qtx/QtxComboBox.cxx @@ -21,209 +21,200 @@ #include "QtxComboBox.h" -#include -#include -#include +#include +#include +#include /*! - 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; } diff --git a/src/Qtx/QtxComboBox.h b/src/Qtx/QtxComboBox.h index 82c8989e0..f064810ee 100755 --- a/src/Qtx/QtxComboBox.h +++ b/src/Qtx/QtxComboBox.h @@ -24,8 +24,8 @@ #include "Qtx.h" -#include -#include +#include +#include #ifdef WIN32 #pragma warning( disable:4251 ) @@ -33,45 +33,43 @@ class QTX_EXPORT QtxComboBox : public QComboBox { - Q_OBJECT + Q_OBJECT - typedef QMap IndexIdMap; + typedef QMap 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 diff --git a/src/Qtx/QtxDblSpinBox.cxx b/src/Qtx/QtxDblSpinBox.cxx index 59f9f6192..fdd756b1d 100755 --- a/src/Qtx/QtxDblSpinBox.cxx +++ b/src/Qtx/QtxDblSpinBox.cxx @@ -348,11 +348,7 @@ void QtxDblSpinBox::setCleared( const bool on ) */ void QtxDblSpinBox::selectAll() { -#if QT_VER >= 3 - QSpinBox::selectAll(); -#else - editor()->selectAll(); -#endif + QSpinBox::selectAll(); } /*! -- 2.39.2