kontact

iconsidepane.cpp

00001 /*
00002   This file is part of KDE Kontact.
00003 
00004   Copyright (C) 2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006   This program is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU General Public
00008   License as published by the Free Software Foundation; either
00009   version 2 of the License, or (at your option) any later version.
00010 
00011   This program is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   General Public License for more details.
00015 
00016   You should have received a copy of the GNU General Public License
00017   along with this program; see the file COPYING.  If not, write to
00018   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019   Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include <qptrlist.h>
00023 #include <qwidgetstack.h>
00024 #include <qsignal.h>
00025 #include <qobjectlist.h>
00026 #include <qlabel.h>
00027 #include <qimage.h>
00028 #include <qpainter.h>
00029 #include <qbitmap.h>
00030 #include <qfontmetrics.h>
00031 #include <qsignalmapper.h>
00032 #include <qstyle.h>
00033 #include <qframe.h>
00034 #include <qdrawutil.h>
00035 #include <qcursor.h>
00036 #include <qtimer.h>
00037 #include <qtooltip.h>
00038 
00039 #include <kpopupmenu.h>
00040 #include <kapplication.h>
00041 #include <kdialog.h>
00042 #include <klocale.h>
00043 #include <kiconloader.h>
00044 #include <sidebarextension.h>
00045 
00046 #include <kdebug.h>
00047 
00048 #include "mainwindow.h"
00049 
00050 #include "plugin.h"
00051 
00052 #include "prefs.h"
00053 #include "iconsidepane.h"
00054 
00055 namespace Kontact
00056 {
00057 
00058 //ugly wrapper class for adding an operator< to the Plugin class
00059 
00060 class PluginProxy
00061 {
00062   public:
00063     PluginProxy()
00064       : mPlugin( 0 )
00065     { }
00066 
00067     PluginProxy( Plugin *plugin )
00068       : mPlugin( plugin )
00069     { }
00070 
00071     PluginProxy & operator=( Plugin *plugin )
00072     {
00073       mPlugin = plugin;
00074       return *this;
00075     }
00076 
00077     bool operator<( PluginProxy &rhs ) const
00078     {
00079       return mPlugin->weight() < rhs.mPlugin->weight();
00080     }
00081 
00082     Plugin *plugin() const
00083     {
00084       return mPlugin;
00085     }
00086 
00087   private:
00088     Plugin *mPlugin;
00089 };
00090 
00091 } //namespace
00092 
00093 using namespace Kontact;
00094 
00095 EntryItem::EntryItem( Navigator *parent, Kontact::Plugin *plugin )
00096   : QListBoxItem( parent ),
00097     mPlugin( plugin ),
00098     mHasHover( false ),
00099     mPaintActive( false )
00100 {
00101   reloadPixmap();
00102   setCustomHighlighting( true );
00103   setText( plugin->title() );
00104 }
00105 
00106 EntryItem::~EntryItem()
00107 {
00108 }
00109 
00110 void EntryItem::reloadPixmap()
00111 {
00112   int size = (int)navigator()->viewMode();
00113   if ( size != 0 )
00114     mPixmap = KGlobal::iconLoader()->loadIcon( mPlugin->icon(),
00115                                                KIcon::Desktop, size,
00116                                                mPlugin->disabled() ? 
00117                                                  KIcon::DisabledState 
00118                                                : KIcon::DefaultState);
00119   else
00120     mPixmap = QPixmap();
00121 }
00122 
00123 Navigator* EntryItem::navigator() const
00124 {
00125   return static_cast<Navigator*>( listBox() );
00126 }
00127 
00128 int EntryItem::width( const QListBox *listbox ) const
00129 {
00130   int w = 0;
00131   if( navigator()->showIcons() ) {
00132     w = navigator()->viewMode();
00133     if ( navigator()->viewMode() == SmallIcons )
00134       w += 4;
00135   }
00136   if( navigator()->showText() ) {
00137     if ( navigator()->viewMode() == SmallIcons )
00138       w += listbox->fontMetrics().width( text() );
00139     else
00140       w = QMAX( w, listbox->fontMetrics().width( text() ) );
00141   }
00142   return w + ( KDialog::marginHint() * 2 );
00143 }
00144 
00145 int EntryItem::height( const QListBox *listbox ) const
00146 {
00147   int h = 0;
00148   if ( navigator()->showIcons() )
00149     h = (int)navigator()->viewMode() + 4;
00150   if ( navigator()->showText() ) {
00151     if ( navigator()->viewMode() == SmallIcons || !navigator()->showIcons() )
00152       h = QMAX( h, listbox->fontMetrics().lineSpacing() ) + KDialog::spacingHint() * 2;
00153     else
00154       h = (int)navigator()->viewMode() + listbox->fontMetrics().lineSpacing() + 4;
00155   }
00156   return h;
00157 }
00158 
00159 void EntryItem::paint( QPainter *p )
00160 {
00161   reloadPixmap();
00162 
00163   QListBox *box = listBox();
00164   bool iconAboveText = ( navigator()->viewMode() > SmallIcons )
00165                      && navigator()->showIcons();
00166   int w = box->viewport()->width();
00167   int y = iconAboveText ? 2 :
00168                         ( ( height( box ) - mPixmap.height() ) / 2 );
00169 
00170   // draw selected
00171   if ( isCurrent() || isSelected() || mHasHover || mPaintActive ) {
00172     int h = height( box );
00173 
00174     QBrush brush;
00175     if ( isCurrent() || isSelected() || mPaintActive )
00176       brush = box->colorGroup().brush( QColorGroup::Highlight );
00177     else
00178       brush = box->colorGroup().highlight().light( 115 );
00179     p->fillRect( 1, 0, w - 2, h - 1, brush );
00180     QPen pen = p->pen();
00181     QPen oldPen = pen;
00182     pen.setColor( box->colorGroup().mid() );
00183     p->setPen( pen );
00184 
00185     p->drawPoint( 1, 0 );
00186     p->drawPoint( 1, h - 2 );
00187     p->drawPoint( w - 2, 0 );
00188     p->drawPoint( w - 2, h - 2 );
00189 
00190     p->setPen( oldPen );
00191   }
00192 
00193   if ( !mPixmap.isNull() && navigator()->showIcons() ) {
00194       int x = iconAboveText ? ( ( w - mPixmap.width() ) / 2 ) :
00195                               KDialog::marginHint();
00196     p->drawPixmap( x, y, mPixmap );
00197   }
00198 
00199   QColor shadowColor = listBox()->colorGroup().background().dark(115);
00200   if ( isCurrent() || isSelected() ) {
00201     p->setPen( box->colorGroup().highlightedText() );
00202   }
00203 
00204   if ( !text().isEmpty() && navigator()->showText() ) {
00205     QFontMetrics fm = p->fontMetrics();
00206 
00207     int x = 0;
00208     if ( iconAboveText ) {
00209       x = ( w - fm.width( text() ) ) / 2;
00210       y += fm.height() - fm.descent();
00211       if ( navigator()->showIcons() )
00212         y += mPixmap.height();
00213     } else {
00214       x = KDialog::marginHint() + 4;
00215       if( navigator()->showIcons() ) {
00216         x += mPixmap.width();
00217       }
00218 
00219       if ( !navigator()->showIcons() || mPixmap.height() < fm.height() )
00220         y = height( box )/2 - fm.height()/2 + fm.ascent();
00221       else
00222         y += mPixmap.height()/2 - fm.height()/2 + fm.ascent();
00223     }
00224 
00225     if ( plugin()->disabled() ) {
00226       p->setPen( box->palette().disabled().text( ) );
00227     } else if ( isCurrent() || isSelected() || mHasHover ) {
00228       p->setPen( box->colorGroup().highlight().dark(115) );
00229       p->drawText( x + ( QApplication::reverseLayout() ? -1 : 1),
00230           y + 1, text() );
00231       p->setPen( box->colorGroup().highlightedText() );
00232     }
00233     else
00234       p->setPen( box->colorGroup().text() );
00235 
00236     p->drawText( x, y, text() );
00237   }
00238 
00239   // ensure that we don't have a stale flag around
00240   if (  isCurrent() || isSelected() ) mHasHover = false;
00241 }
00242 
00243 void EntryItem::setHover( bool hasHover )
00244 {
00245   mHasHover = hasHover;
00246 }
00247 
00248 void EntryItem::setPaintActive( bool paintActive )
00249 {
00250   mPaintActive = paintActive;
00251 }
00252 
00253 Navigator::Navigator( SidePaneBase *parent, const char *name )
00254   : KListBox( parent, name ), mSidePane( parent ),
00255     mShowIcons( true ), mShowText( true )
00256 {
00257   mMouseOn = 0;
00258   mHighlightItem = 0;
00259   mViewMode = sizeIntToEnum( Prefs::self()->sidePaneIconSize() );
00260   mShowIcons = Prefs::self()->sidePaneShowIcons();
00261   mShowText = Prefs::self()->sidePaneShowText();
00262   setSelectionMode( KListBox::Single );
00263   viewport()->setBackgroundMode( PaletteBackground );
00264   setFrameStyle( QFrame::NoFrame );
00265   setHScrollBarMode( QScrollView::AlwaysOff );
00266   setAcceptDrops( true );
00267 
00268   setFocusPolicy( NoFocus );
00269 
00270   connect( this, SIGNAL( selectionChanged( QListBoxItem* ) ),
00271            SLOT( slotExecuted( QListBoxItem* ) ) );
00272   connect( this, SIGNAL( rightButtonPressed( QListBoxItem*, const QPoint& ) ),
00273            SLOT( slotShowRMBMenu( QListBoxItem*, const QPoint& ) ) );
00274   connect( this, SIGNAL( onItem( QListBoxItem * ) ),
00275             SLOT(  slotMouseOn( QListBoxItem * ) ) );
00276   connect( this, SIGNAL( onViewport() ), SLOT(  slotMouseOff() ) );
00277 
00278   mMapper = new QSignalMapper( this );
00279   connect( mMapper, SIGNAL( mapped( int ) ), SLOT( shortCutSelected( int ) ) );
00280 
00281   QToolTip::remove( this );
00282   if ( !mShowText )
00283     new EntryItemToolTip( this );
00284 
00285 }
00286 
00287 QSize Navigator::sizeHint() const
00288 {
00289   return QSize( 100, 100 );
00290 }
00291 
00292 void Navigator::highlightItem( EntryItem * item )
00293 {
00294   mHighlightItem = item;
00295 
00296   setPaintActiveItem( mHighlightItem, true );
00297 
00298   QTimer::singleShot( 2000, this, SLOT( slotStopHighlight() ) );
00299 }
00300 
00301 void Navigator::slotStopHighlight()
00302 {
00303   setPaintActiveItem( mHighlightItem, false );
00304 }
00305 
00306 void Navigator::setSelected( QListBoxItem *item, bool selected )
00307 {
00308   // Reimplemented to avoid the immediate activation of
00309   // the item. might turn out it doesn't work, we check that
00310   // an confirm from MainWindow::selectPlugin()
00311   if ( selected ) {
00312     EntryItem *entry = static_cast<EntryItem*>( item );
00313     emit pluginActivated( entry->plugin() );
00314   }
00315 }
00316 
00317 void Navigator::updatePlugins( QValueList<Kontact::Plugin*> plugins_ )
00318 {
00319   QValueList<Kontact::PluginProxy> plugins;
00320   QValueList<Kontact::Plugin*>::ConstIterator end_ = plugins_.end();
00321   QValueList<Kontact::Plugin*>::ConstIterator it_ = plugins_.begin();
00322   for ( ; it_ != end_; ++it_ )
00323     plugins += PluginProxy( *it_ );
00324 
00325   clear();
00326 
00327   mActions.setAutoDelete( true );
00328   mActions.clear();
00329   mActions.setAutoDelete( false );
00330 
00331   int counter = 0;
00332   int minWidth = 0;
00333   qBubbleSort( plugins );
00334   QValueList<Kontact::PluginProxy>::ConstIterator end = plugins.end();
00335   QValueList<Kontact::PluginProxy>::ConstIterator it = plugins.begin();
00336   for ( ; it != end; ++it ) {
00337     Kontact::Plugin *plugin = ( *it ).plugin();
00338     if ( !plugin->showInSideBar() )
00339       continue;
00340 
00341     EntryItem *item = new EntryItem( this, plugin );
00342     item->setSelectable( !plugin->disabled() );
00343 
00344     if ( item->width( this ) > minWidth )
00345       minWidth = item->width( this );
00346 
00347     QString name = QString( "CTRL+%1" ).arg( counter + 1 );
00348     KAction *action = new KAction( plugin->title(), plugin->icon(), KShortcut( name ),
00349                                    mMapper, SLOT( map() ),
00350                                    mSidePane->actionCollection(), name.latin1() );
00351     mActions.append( action );
00352     mMapper->setMapping( action, counter );
00353     counter++;
00354   }
00355 
00356   parentWidget()->setFixedWidth( minWidth );
00357 }
00358 
00359 void Navigator::dragEnterEvent( QDragEnterEvent *event )
00360 {
00361   kdDebug(5600) << "Navigator::dragEnterEvent()" << endl;
00362 
00363   dragMoveEvent( event );
00364 }
00365 
00366 void Navigator::dragMoveEvent( QDragMoveEvent *event )
00367 {
00368   kdDebug(5600) << "Navigator::dragEnterEvent()" << endl;
00369 
00370   kdDebug(5600) << "  Format: " << event->format() << endl;
00371 
00372   QListBoxItem *item = itemAt( event->pos() );
00373 
00374   if ( !item ) {
00375     event->accept( false );
00376     return;
00377   }
00378 
00379   EntryItem *entry = static_cast<EntryItem*>( item );
00380 
00381   kdDebug(5600) << "  PLUGIN: " << entry->plugin()->identifier() << endl;
00382 
00383   event->accept( entry->plugin()->canDecodeDrag( event ) );
00384 }
00385 
00386 void Navigator::dropEvent( QDropEvent *event )
00387 {
00388   kdDebug(5600) << "Navigator::dropEvent()" << endl;
00389 
00390   QListBoxItem *item = itemAt( event->pos() );
00391 
00392   if ( !item ) {
00393     return;
00394   }
00395 
00396   EntryItem *entry = static_cast<EntryItem*>( item );
00397 
00398   kdDebug(5600) << "  PLUGIN: " << entry->plugin()->identifier() << endl;
00399 
00400   entry->plugin()->processDropEvent( event );
00401 }
00402 
00403 void Navigator::resizeEvent( QResizeEvent *event )
00404 {
00405   QListBox::resizeEvent( event );
00406   triggerUpdate( true );
00407 }
00408 
00409 void Navigator::enterEvent( QEvent *event )
00410 {
00411   // work around Qt behaviour: onItem is not emmitted in enterEvent()
00412   KListBox::enterEvent( event );
00413   emit onItem( itemAt( mapFromGlobal( QCursor::pos() ) ) );
00414 }
00415 
00416 void Navigator::leaveEvent( QEvent *event )
00417 {
00418   KListBox::leaveEvent( event );
00419   slotMouseOn( 0 );
00420   mMouseOn = 0;
00421 }
00422 
00423 void Navigator::slotExecuted( QListBoxItem *item )
00424 {
00425   if ( !item )
00426     return;
00427 
00428   EntryItem *entry = static_cast<EntryItem*>( item );
00429 
00430   emit pluginActivated( entry->plugin() );
00431 }
00432 
00433 IconViewMode Navigator::sizeIntToEnum(int size) const
00434 {
00435   switch ( size ) {
00436     case int(LargeIcons):
00437       return LargeIcons;
00438       break;
00439     case int(NormalIcons):
00440       return NormalIcons;
00441       break;
00442     case int(SmallIcons):
00443       return SmallIcons;
00444       break;
00445     default:
00446       // Stick with sane values
00447       return NormalIcons;
00448       kdDebug() << "View mode not implemented!" << endl;
00449       break;
00450   }
00451 }
00452 
00453 void Navigator::slotShowRMBMenu( QListBoxItem *, const QPoint &pos )
00454 {
00455   KPopupMenu menu;
00456   menu.insertTitle( i18n( "Icon Size" ) );
00457   menu.insertItem( i18n( "Large" ), (int)LargeIcons );
00458   menu.setItemEnabled( (int)LargeIcons, mShowIcons );
00459   menu.insertItem( i18n( "Normal" ), (int)NormalIcons );
00460   menu.setItemEnabled( (int)NormalIcons, mShowIcons );
00461   menu.insertItem( i18n( "Small" ), (int)SmallIcons );
00462   menu.setItemEnabled( (int)SmallIcons, mShowIcons );
00463 
00464   menu.setItemChecked( (int)mViewMode, true );
00465   menu.insertSeparator();
00466 
00467   menu.insertItem( i18n( "Show Icons" ), (int)ShowIcons );
00468   menu.setItemChecked( (int)ShowIcons, mShowIcons );
00469   menu.setItemEnabled( (int)ShowIcons, mShowText );
00470   menu.insertItem( i18n( "Show Text" ), (int)ShowText );
00471   menu.setItemChecked( (int)ShowText, mShowText );
00472   menu.setItemEnabled( (int)ShowText, mShowIcons );
00473   int choice = menu.exec( pos );
00474 
00475   if ( choice == -1 )
00476     return;
00477 
00478   if ( choice >= SmallIcons ) {
00479     mViewMode = sizeIntToEnum( choice );
00480     Prefs::self()->setSidePaneIconSize( choice );
00481   } else {
00482     // either icons or text were toggled
00483     if ( choice == ShowIcons ) {
00484       mShowIcons = !mShowIcons;
00485       Prefs::self()->setSidePaneShowIcons( mShowIcons );
00486       QToolTip::remove( this );
00487       if ( !mShowText )
00488         new EntryItemToolTip( this );
00489     } else {
00490       mShowText = !mShowText;
00491       Prefs::self()->setSidePaneShowText( mShowText );
00492       QToolTip::remove( this );
00493     }
00494   }
00495   int maxWidth = 0;
00496   QListBoxItem* it = 0;
00497   for (int i = 0; (it = item(i)) != 0; ++i)
00498   {
00499     int width = it->width(this);
00500     if (width > maxWidth)
00501       maxWidth = width;
00502   }
00503   parentWidget()->setFixedWidth( maxWidth );
00504 
00505   triggerUpdate( true );
00506 }
00507 
00508 void Navigator::shortCutSelected( int pos )
00509 {
00510   setCurrentItem( pos );
00511 }
00512 
00513 void Navigator::setHoverItem( QListBoxItem* item, bool hover )
00514 {
00515     static_cast<EntryItem*>( item )->setHover( hover );
00516     updateItem( item );
00517 }
00518 
00519 void Navigator::setPaintActiveItem( QListBoxItem* item, bool paintActive )
00520 {
00521     static_cast<EntryItem*>( item )->setPaintActive( paintActive );
00522     updateItem( item );
00523 }
00524 
00525 void Navigator::slotMouseOn( QListBoxItem* newItem )
00526 {
00527   QListBoxItem* oldItem = mMouseOn;
00528   if ( oldItem == newItem ) return;
00529 
00530   if ( oldItem && !oldItem->isCurrent() && !oldItem->isSelected() )
00531   {
00532     setHoverItem( oldItem, false );
00533   }
00534 
00535   if ( newItem && !newItem->isCurrent() && !newItem->isSelected() )
00536   {
00537     setHoverItem( newItem, true );
00538   }
00539   mMouseOn = newItem;
00540 }
00541 
00542 void Navigator::slotMouseOff()
00543 {
00544   slotMouseOn( 0 );
00545 }
00546 
00547 IconSidePane::IconSidePane( Core *core, QWidget *parent, const char *name )
00548   : SidePaneBase( core, parent, name )
00549 {
00550   mNavigator = new Navigator( this );
00551   connect( mNavigator, SIGNAL( pluginActivated( Kontact::Plugin* ) ),
00552            SIGNAL( pluginSelected( Kontact::Plugin* ) ) );
00553 
00554   setAcceptDrops( true );
00555 }
00556 
00557 IconSidePane::~IconSidePane()
00558 {
00559 }
00560 
00561 void IconSidePane::updatePlugins()
00562 {
00563   mNavigator->updatePlugins( core()->pluginList() );
00564 }
00565 
00566 void IconSidePane::selectPlugin( Kontact::Plugin *plugin )
00567 {
00568   bool blocked = signalsBlocked();
00569   blockSignals( true );
00570 
00571   for ( uint i = 0; i < mNavigator->count(); ++i ) {
00572     EntryItem *item = static_cast<EntryItem*>( mNavigator->item( i ) );
00573     if ( item->plugin() == plugin ) {
00574       mNavigator->setCurrentItem( i );
00575       break;
00576     }
00577   }
00578 
00579   blockSignals( blocked );
00580 }
00581 
00582 void IconSidePane::selectPlugin( const QString &name )
00583 {
00584   bool blocked = signalsBlocked();
00585   blockSignals( true );
00586 
00587   for ( uint i = 0; i < mNavigator->count(); ++i ) {
00588     EntryItem *item = static_cast<EntryItem*>( mNavigator->item( i ) );
00589     if ( item->plugin()->identifier() == name ) {
00590       mNavigator->setCurrentItem( i );
00591       break;
00592     }
00593   }
00594 
00595   blockSignals( blocked );
00596 }
00597 
00598 void IconSidePane::indicateForegrunding( Kontact::Plugin *plugin )
00599 {
00600   for ( uint i = 0; i < mNavigator->count(); ++i ) {
00601     EntryItem *item = static_cast<EntryItem*>( mNavigator->item( i ) );
00602     if ( item->plugin() == plugin ) {
00603       mNavigator->highlightItem( item );
00604       break;
00605     }
00606   }
00607 
00608 
00609 }
00610 #include "iconsidepane.moc"
00611 
00612 // vim: sw=2 sts=2 et tw=80
KDE Home | KDE Accessibility Home | Description of Access Keys