00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "comboboxheaderview.h"
00021
00022 #include <QtCore/QAbstractItemModel>
00023 #include <QtCore/QEvent>
00024 #include <QtGui/QComboBox>
00025 #include <QtGui/QMouseEvent>
00026 #include <QtGui/QResizeEvent>
00027 #include <QtGui/QTableWidget>
00028
00029 class ComboBoxHeaderView::ComboBox : public QComboBox {
00030 friend class ComboBoxHeaderView;
00031
00032 public:
00033 ComboBox( int index, ComboBoxHeaderView *parent )
00034 : QComboBox( parent ), mIndex( index ), mParent( parent )
00035 {
00036 }
00037
00038 protected:
00039 virtual void enterEvent( QEvent * ) {
00040 setCurrentIndex(
00041 findText(
00042 mParent->model()->headerData( mIndex,
00043 mParent->orientation() ).toString() ) );
00044
00045 mParent->setCurrentIndex( mIndex );
00046 }
00047
00048 private:
00049 int mIndex;
00050 ComboBoxHeaderView *mParent;
00051 };
00052
00053
00054 class ComboBoxHeaderView::Private {
00055 public:
00056 Private() :
00057 mComboBox( 0 ),
00058 mCurrentIndex( -1 ),
00059 mHoverStyle( true ),
00060 mMargin( 1 ),
00061 mScrollOffset( 0 ) {}
00062
00063 QStringList mItems;
00064
00065 QComboBox *mComboBox;
00066 QList<ComboBox*> mBoxes;
00067
00068 int mCurrentIndex;
00069
00070 bool mHoverStyle;
00071 int mMargin;
00072 int mScrollOffset;
00073 };
00074
00075
00076 ComboBoxHeaderView::ComboBoxHeaderView( QStringList items,
00077 QTableWidget *parent,
00078 bool hoverStyle ) :
00079 QHeaderView( Qt::Horizontal, parent ), d( new Private() )
00080 {
00081 d->mItems = items;
00082 d->mHoverStyle = hoverStyle;
00083
00084 initialize();
00085
00086 connect( this, SIGNAL( sectionCountChanged( int, int ) ),
00087 this, SLOT( initialize() ) );
00088 connect( this, SIGNAL( sectionResized( int, int, int ) ),
00089 this, SLOT( initialize() ) );
00090 }
00091
00092 ComboBoxHeaderView::~ComboBoxHeaderView()
00093 {
00094 delete d;
00095 }
00096
00097 QString ComboBoxHeaderView::headerLabel( int logicalIndex ) const
00098 {
00099 return model()->headerData( logicalIndex, orientation() ).toString();
00100 }
00101
00102 QStringList ComboBoxHeaderView::items() const
00103 {
00104 return d->mItems;
00105 }
00106
00107 int ComboBoxHeaderView::margin() const
00108 {
00109 return d->mMargin;
00110 }
00111
00112 void ComboBoxHeaderView::setMargin( int margin)
00113 {
00114 d->mMargin = margin;
00115
00116 if ( !( d->mHoverStyle ) ) {
00117 initialize();
00118 }
00119 }
00120
00121 int ComboBoxHeaderView::indexOfHeaderLabel( int logicalIndex ) const
00122 {
00123 return d->mItems.indexOf( headerLabel( logicalIndex ) );
00124 }
00125
00126 QString ComboBoxHeaderView::valueOfHeaderLabel( int logicalIndex ) const
00127 {
00128 return d->mItems[ indexOfHeaderLabel( logicalIndex ) ];
00129 }
00130
00131 QRect ComboBoxHeaderView::sectionRect( int logicalIndex ) const
00132 {
00133 return QRect( sectionPosition( logicalIndex ) + d->mMargin, 0,
00134 sectionSize( logicalIndex ) - 2 * d->mMargin, height() );
00135 }
00136
00137 void ComboBoxHeaderView::adjustComboBoxIndex( QComboBox *comboBox,
00138 int logicalIndex )
00139 {
00140 comboBox->setCurrentIndex(
00141 comboBox->findText(
00142 model()->headerData( logicalIndex, orientation() ).toString() ) );
00143 }
00144
00145 void ComboBoxHeaderView::adjustComboBoxIndex( int logicalIndex )
00146 {
00147 if ( d->mHoverStyle ) {
00148 adjustComboBoxIndex( d->mComboBox, logicalIndex );
00149 } else {
00150 adjustComboBoxIndex( static_cast< QComboBox* >( d->mBoxes[ logicalIndex ] ),
00151 logicalIndex );
00152 }
00153 }
00154
00155 bool ComboBoxHeaderView::isViewVisible() const
00156 {
00157 if ( d->mHoverStyle ) {
00158 if ( d->mComboBox->view()->isVisible() ) {
00159 return true;
00160 }
00161 } else {
00162 Q_FOREACH ( ComboBox *box, d->mBoxes ) {
00163 if ( box->view()->isVisible() ) {
00164 return true;
00165 }
00166 }
00167 }
00168
00169 return false;
00170 }
00171
00172 void ComboBoxHeaderView::initialize()
00173 {
00174 Q_FOREACH( ComboBox *box, d->mBoxes ) {
00175 box->setVisible( false );
00176 }
00177
00178 if ( d->mHoverStyle ) {
00179 if ( !( d->mComboBox ) ) {
00180 d->mComboBox = new QComboBox( this );
00181 d->mComboBox->addItems( d->mItems );
00182 d->mComboBox->setVisible( false );
00183
00184 connect( d->mComboBox, SIGNAL( activated( int ) ),
00185 d->mComboBox, SLOT( hide() ) );
00186 connect( d->mComboBox, SIGNAL( activated( const QString & ) ),
00187 this, SLOT( slotActivated( const QString & ) ) );
00188 }
00189 } else {
00190 ComboBox *box = 0;
00191 bool toBeAdded;
00192
00193 for ( int i = 0; i < count(); ++i ) {
00194 toBeAdded = ( i >= d->mBoxes.count() );
00195
00196 if ( toBeAdded ) {
00197 box = new ComboBox( i, this );
00198 box->addItems( d->mItems );
00199 adjustComboBoxIndex( static_cast< QComboBox* >( box ), i );
00200 d->mBoxes.append( box );
00201 connect( box, SIGNAL( activated( const QString & ) ),
00202 this, SLOT( slotActivated( const QString & ) ) );
00203 } else {
00204 box = d->mBoxes[ i ];
00205 }
00206
00207 QRect rect = sectionRect( i );
00208 rect.moveLeft( rect.x() + d->mScrollOffset );
00209 box->setGeometry( rect );
00210 box->setVisible( true );
00211 }
00212 }
00213 }
00214
00215 void ComboBoxHeaderView::adaptMove( int shift )
00216 {
00217 d->mScrollOffset = -shift;
00218 initialize();
00219 }
00220
00221 void ComboBoxHeaderView::slotActivated( const QString &text )
00222 {
00223
00224
00225 QTableWidget *view = static_cast<QTableWidget *>( parent() );
00226 if ( view && d->mCurrentIndex >= 0 ) {
00227 QTableWidgetItem *item = view->horizontalHeaderItem( d->mCurrentIndex );
00228 if ( !item ) {
00229 item = new QTableWidgetItem;
00230 view->setHorizontalHeaderItem( d->mCurrentIndex, item );
00231 }
00232 item->setText( text );
00233 }
00234 }
00235
00236 void ComboBoxHeaderView::setCurrentIndex( int index )
00237 {
00238 if ( !( isViewVisible() ) ) {
00239 d->mCurrentIndex = index;
00240 }
00241 }
00242
00243 void ComboBoxHeaderView::slotResetTexts()
00244 {
00245 if ( !( d->mHoverStyle ) ) {
00246 for ( int i = 0; i < count(); ++i ) {
00247 adjustComboBoxIndex( i );
00248 }
00249 }
00250 }
00251
00252 void ComboBoxHeaderView::mouseMoveEvent( QMouseEvent *event )
00253 {
00254
00255 if ( !( d->mHoverStyle ) || isViewVisible() ) {
00256 QHeaderView::mouseMoveEvent( event );
00257 return;
00258 }
00259
00260 bool found = false;
00261 d->mCurrentIndex = -1;
00262
00263 int index = logicalIndexAt( event->pos() );
00264 if ( index >= 0 ) {
00265 found = true;
00266 d->mCurrentIndex = index;
00267 }
00268
00269 if ( found ) {
00270 d->mComboBox->setGeometry( sectionRect( index ) );
00271 adjustComboBoxIndex( d->mComboBox, index );
00272 }
00273 d->mComboBox->setVisible( found );
00274
00275 QHeaderView::mouseMoveEvent( event );
00276 }
00277
00278 void ComboBoxHeaderView::leaveEvent( QEvent *event )
00279 {
00280 if ( d->mHoverStyle && !( d->mComboBox->view()->isVisible() ) ) {
00281 d->mComboBox->setVisible( false );
00282 d->mCurrentIndex = -1;
00283 }
00284
00285 QHeaderView::leaveEvent( event );
00286 }
00287
00288 void ComboBoxHeaderView::resizeEvent( QResizeEvent *event )
00289 {
00290 if ( !( d->mHoverStyle ) ) {
00291 initialize();
00292 }
00293
00294 QHeaderView::resizeEvent( event );
00295 }
00296
00297 void ComboBoxHeaderView::setModel( QAbstractItemModel *model )
00298 {
00299 QHeaderView::setModel( model );
00300 connect( model, SIGNAL( headerDataChanged( Qt::Orientation, int, int ) ),
00301 this, SLOT( slotResetTexts() ) );
00302 }
00303
00304 #include "comboboxheaderview.moc"