kontact Library API Documentation

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., 59 Temple Place - Suite 330,
00019   Boston, MA 02111-1307, 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 <qpainter.h>
00028 #include <qbitmap.h>
00029 #include <qfontmetrics.h>
00030 #include <qsignalmapper.h>
00031 #include <qstyle.h>
00032 #include <qframe.h>
00033 #include <qdrawutil.h>
00034 
00035 #include <kapplication.h>
00036 #include <kconfig.h>
00037 #include <klocale.h>
00038 #include <kiconloader.h>
00039 #include <sidebarextension.h>
00040 
00041 #include <kdebug.h>
00042 
00043 #include "mainwindow.h"
00044 
00045 #include "plugin.h"
00046 
00047 #include "iconsidepane.h"
00048 
00049 using namespace Kontact;
00050 
00051 EntryItem::EntryItem( QListBox *parent, Kontact::Plugin *plugin )
00052   : QListBoxItem( parent ),
00053     mPlugin( plugin )
00054 {
00055   mPixmap = KGlobal::iconLoader()->loadIcon( plugin->icon(),
00056                                              KIcon::Desktop, 48 );
00057   setCustomHighlighting( true );
00058   setText( plugin->title() );
00059 }
00060 
00061 EntryItem::~EntryItem()
00062 {
00063 }
00064 
00065 int EntryItem::width( const QListBox *listbox) const
00066 {
00067   int w;
00068   if ( text().isEmpty() )
00069     w = mPixmap.width();
00070   else
00071     w = QMAX( mPixmap.width(), listbox->fontMetrics().width( text() ) );
00072 
00073   return w + 18;
00074 }
00075 
00076 int EntryItem::height( const QListBox *listbox) const
00077 {
00078   int h;
00079   if ( text().isEmpty() )
00080     h =  mPixmap.height();
00081   else 
00082     h = mPixmap.height() + listbox->fontMetrics().lineSpacing();
00083 
00084   return h + 4;
00085 }
00086 
00087 void EntryItem::paint( QPainter *p )
00088 {
00089   QListBox *box = listBox();
00090   int w = box->viewport()->width();
00091   int y = 2;
00092 
00093   if ( !mPixmap.isNull() ) {
00094     int x = ( w - mPixmap.width() ) / 2;
00095     p->drawPixmap( x, y, mPixmap );
00096   }
00097 
00098   QColor save;
00099   if ( isCurrent() || isSelected() ) {
00100     save = p->pen().color();
00101     p->setPen(listBox()->colorGroup().brightText());
00102   }
00103 
00104   if ( !text().isEmpty() ) {
00105     QFontMetrics fm = p->fontMetrics();
00106     y += mPixmap.height() + fm.height() - fm.descent();
00107     int x = ( w - fm.width( text() ) ) / 2;
00108     p->drawText( x, y, text() );
00109   }
00110   // draw sunken
00111   if ( isCurrent() || isSelected() ) {
00112     p->setPen(save);
00113     QColorGroup group = box->colorGroup();
00114     group.setColor( QColorGroup::Dark, Qt::black );
00115     qDrawShadePanel( p, 1, 0, w - 2, height( box ),
00116                      group, true, 1, 0 );
00117   }
00118 }
00119 
00120 Navigator::Navigator( SidePaneBase *parent, const char *name)
00121   : KListBox( parent, name ), mSidePane( parent )
00122 {
00123   setSelectionMode( KListBox::Single );
00124   viewport()->setBackgroundMode( PaletteMid );
00125   setHScrollBarMode( QScrollView::AlwaysOff );
00126   setAcceptDrops( true );
00127 
00128   connect( this, SIGNAL( currentChanged( QListBoxItem * ) ),
00129            SLOT( slotExecuted( QListBoxItem * ) ) );
00130 
00131   mMapper = new QSignalMapper( this );
00132   connect( mMapper, SIGNAL( mapped( int ) ), SLOT( shortCutSelected( int ) ) );
00133 }
00134 
00135 QSize Navigator::sizeHint() const
00136 {
00137   return QSize( 100, 100 );
00138 }
00139 
00140 void Navigator::setSelected( QListBoxItem *i, bool sel )
00141 {
00142   // Reimplmemented to avoid the immediate activation of
00143   // the item. might turn out it doesn't work, we check that
00144   // an confirm from MainWindow::selectPlugin()
00145   if (sel) {
00146     EntryItem *entry = static_cast<EntryItem *>( i );
00147     emit pluginActivated( entry->plugin() );
00148   }
00149 }
00150 
00151 void Navigator::updatePlugins( QValueList<Kontact::Plugin*> plugins )
00152 {
00153   clear();
00154 
00155   mActions.setAutoDelete( true );
00156   mActions.clear();
00157   mActions.setAutoDelete( false );
00158 
00159   int counter = 0;
00160   int minWidth = 0;
00161   QValueList<Kontact::Plugin*>::ConstIterator end = plugins.end();
00162   QValueList<Kontact::Plugin*>::ConstIterator it = plugins.begin();
00163   for ( ; it != end; ++it ) {
00164     Kontact::Plugin *plugin = *it;
00165     if ( !plugin->showInSideBar() )
00166       continue;
00167 
00168     EntryItem *item = new EntryItem( this, plugin );
00169 
00170     if ( item->width( this ) > minWidth )
00171       minWidth = item->width( this );
00172 
00173     QString name = QString( "CTRL+%1" ).arg( counter + 1 );
00174     KAction *action = new KAction( plugin->title(), KShortcut( name ),
00175                                    mMapper, SLOT( map() ),
00176                                    mSidePane->actionCollection(), name.latin1() );
00177     mMapper->setMapping( action, counter );
00178     counter++;
00179   }
00180 
00181   parentWidget()->setFixedWidth( minWidth );
00182 }
00183 
00184 void Navigator::slotExecuted( QListBoxItem *item )
00185 {
00186   if ( !item ) return;
00187   
00188   EntryItem *entry = static_cast<EntryItem *>( item );
00189 
00190   emit pluginActivated( entry->plugin() );
00191 }
00192 
00193 void Navigator::dragEnterEvent( QDragEnterEvent *event )
00194 {
00195   kdDebug(5600) << "Navigator::dragEnterEvent()" << endl;
00196 
00197   dragMoveEvent( event );
00198 }
00199 
00200 void Navigator::dragMoveEvent( QDragMoveEvent *event )
00201 {
00202   kdDebug(5600) << "Navigator::dragEnterEvent()" << endl;
00203   
00204   kdDebug(5600) << "  Format: " << event->format() << endl;
00205 
00206   QListBoxItem *item = itemAt( event->pos() );
00207 
00208   if ( !item ) {
00209     event->accept( false );
00210     return;
00211   }
00212 
00213   EntryItem *entry = static_cast<EntryItem *>( item );
00214   
00215   kdDebug(5600) << "  PLUGIN: " << entry->plugin()->identifier() << endl;
00216 
00217   event->accept( entry->plugin()->canDecodeDrag( event ) );
00218 }
00219 
00220 void Navigator::dropEvent( QDropEvent *event )
00221 {
00222   kdDebug(5600) << "Navigator::dropEvent()" << endl;
00223 
00224   QListBoxItem *item = itemAt( event->pos() );
00225 
00226   if ( !item ) {
00227     return;
00228   }
00229 
00230   EntryItem *entry = static_cast<EntryItem *>( item );
00231   
00232   kdDebug(5600) << "  PLUGIN: " << entry->plugin()->identifier() << endl;
00233 
00234   entry->plugin()->processDropEvent( event );  
00235 }
00236 
00237 void Navigator::resizeEvent( QResizeEvent *event )
00238 {
00239   QListBox::resizeEvent( event );
00240   triggerUpdate( true );
00241 }
00242 
00243 void Navigator::shortCutSelected( int pos )
00244 {
00245   setCurrentItem( pos );
00246 }
00247 
00248 
00249 IconSidePane::IconSidePane( Core *core, QWidget *parent, const char *name )
00250   : SidePaneBase( core, parent, name )
00251 {
00252   mNavigator = new Navigator( this );
00253   connect( mNavigator, SIGNAL( pluginActivated( Kontact::Plugin * ) ),
00254            SIGNAL( pluginSelected( Kontact::Plugin * ) ) );
00255 
00256   setAcceptDrops( true );
00257 }
00258 
00259 IconSidePane::~IconSidePane()
00260 {
00261 }
00262 
00263 void IconSidePane::updatePlugins()
00264 {
00265   mNavigator->updatePlugins( core()->pluginList() );
00266 }
00267 
00268 void IconSidePane::selectPlugin( Kontact::Plugin *plugin )
00269 {
00270   bool blocked = signalsBlocked();
00271   blockSignals( true );
00272 
00273   uint i;
00274   for ( i = 0; i < mNavigator->count(); ++i ) {
00275     EntryItem *item = static_cast<EntryItem *>( mNavigator->item( i ) );
00276     if ( item->plugin() == plugin ) {
00277       mNavigator->setCurrentItem( i );
00278       break;
00279     }
00280   }
00281 
00282   blockSignals( blocked );
00283 }
00284 
00285 void IconSidePane::selectPlugin( const QString &name )
00286 {
00287   bool blocked = signalsBlocked();
00288   blockSignals( true );
00289 
00290   uint i;
00291   for ( i = 0; i < mNavigator->count(); ++i ) {
00292     EntryItem *item = static_cast<EntryItem *>( mNavigator->item( i ) );
00293     if ( item->plugin()->identifier() == name ) {
00294       mNavigator->setCurrentItem( i );
00295       break;
00296     }
00297   }
00298 
00299   blockSignals( blocked );
00300 }
00301 
00302 #include "iconsidepane.moc"
00303 
00304 // vim: sw=2 sts=2 et tw=80
KDE Logo
This file is part of the documentation for kontact Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:56:56 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003