• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kaddressbook

comboboxheaderview.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of KAddressBook.
00003    Copyright (C) 2007 Mathias Soeken <msoeken@tzi.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
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 //@cond PRIVATE
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 //@endcond
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   // FIXME a solution with QAbstractItemModel::setHeaderData
00224   //       would be nicer, but that seems not work at moment.
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   /* do not do this, when a popup is open */
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"

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal