iconsidepane.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 <qstyle.h>
00031 #include <qframe.h>
00032 #include <qdrawutil.h>
00033
00034 #include <kapplication.h>
00035 #include <kconfig.h>
00036 #include <klocale.h>
00037 #include <kiconloader.h>
00038 #include <sidebarextension.h>
00039
00040 #include <kdebug.h>
00041
00042 #include "mainwindow.h"
00043
00044 #include "plugin.h"
00045
00046 #include "iconsidepane.h"
00047
00048 using namespace Kontact;
00049
00050 EntryItem::EntryItem( QListBox *parent, Kontact::Plugin *plugin )
00051 : QListBoxItem( parent ),
00052 mPlugin( plugin )
00053 {
00054 mPixmap = KGlobal::iconLoader()->loadIcon( plugin->icon(),
00055 KIcon::Desktop, 48 );
00056 setCustomHighlighting( true );
00057 setText( plugin->title() );
00058 }
00059
00060 EntryItem::~EntryItem()
00061 {
00062 }
00063
00064 int EntryItem::width( const QListBox *listbox) const
00065 {
00066 int w;
00067 if ( text().isEmpty() )
00068 w = mPixmap.width();
00069 else
00070 w = QMAX( mPixmap.width(), listbox->fontMetrics().width( text() ) );
00071
00072 return w + 18;
00073 }
00074
00075 int EntryItem::height( const QListBox *listbox) const
00076 {
00077 int h;
00078 if ( text().isEmpty() )
00079 h = mPixmap.height();
00080 else
00081 h = mPixmap.height() + listbox->fontMetrics().lineSpacing();
00082
00083 return h + 4;
00084 }
00085
00086 void EntryItem::paint( QPainter *p )
00087 {
00088 QListBox *box = listBox();
00089 int w = box->viewport()->width();
00090 int y = 2;
00091
00092 if ( !mPixmap.isNull() ) {
00093 int x = ( w - mPixmap.width() ) / 2;
00094 p->drawPixmap( x, y, mPixmap );
00095 }
00096
00097 QColor save;
00098 if ( isCurrent() || isSelected() ) {
00099 save = p->pen().color();
00100 p->setPen(listBox()->colorGroup().brightText());
00101 }
00102
00103 if ( !text().isEmpty() ) {
00104 QFontMetrics fm = p->fontMetrics();
00105 y += mPixmap.height() + fm.height() - fm.descent();
00106 int x = ( w - fm.width( text() ) ) / 2;
00107 p->drawText( x, y, text() );
00108 }
00109
00110 if ( isCurrent() || isSelected() ) {
00111 p->setPen(save);
00112 QColorGroup group = box->colorGroup();
00113 group.setColor( QColorGroup::Dark, Qt::black );
00114 qDrawShadePanel( p, 1, 0, w - 2, height( box ),
00115 group, true, 1, 0 );
00116 }
00117 }
00118
00119 Navigator::Navigator( SidePaneBase *parent, const char *name)
00120 : KListBox( parent, name ), mSidePane( parent )
00121 {
00122 setSelectionMode( KListBox::Single );
00123 viewport()->setBackgroundMode( PaletteMid );
00124 setHScrollBarMode( QScrollView::AlwaysOff );
00125 setAcceptDrops( true );
00126
00127 connect( this, SIGNAL( currentChanged( QListBoxItem * ) ),
00128 SLOT( slotExecuted( QListBoxItem * ) ) );
00129 }
00130
00131 QSize Navigator::sizeHint() const
00132 {
00133 return QSize( 100, 100 );
00134 }
00135
00136 void Navigator::setSelected( QListBoxItem *i, bool sel )
00137 {
00138
00139
00140
00141 if (sel) {
00142 EntryItem *entry = static_cast<EntryItem *>( i );
00143 emit pluginActivated( entry->plugin() );
00144 }
00145 }
00146
00147 void Navigator::updatePlugins( QValueList<Kontact::Plugin*> plugins )
00148 {
00149 clear();
00150
00151 int minWidth = 0;
00152 QValueList<Kontact::Plugin*>::ConstIterator end = plugins.end();
00153 QValueList<Kontact::Plugin*>::ConstIterator it = plugins.begin();
00154 for ( ; it != end; ++it ) {
00155 Kontact::Plugin *plugin = *it;
00156 if ( !plugin->showInSideBar() )
00157 continue;
00158
00159 EntryItem *item = new EntryItem( this, plugin );
00160
00161 if ( item->width( this ) > minWidth )
00162 minWidth = item->width( this );
00163 }
00164
00165 parentWidget()->setFixedWidth( minWidth );
00166 }
00167
00168 void Navigator::slotExecuted( QListBoxItem *item )
00169 {
00170 if ( !item ) return;
00171
00172 EntryItem *entry = static_cast<EntryItem *>( item );
00173
00174 emit pluginActivated( entry->plugin() );
00175 }
00176
00177 void Navigator::dragEnterEvent( QDragEnterEvent *event )
00178 {
00179 kdDebug() << "Navigator::dragEnterEvent()" << endl;
00180
00181 dragMoveEvent( event );
00182 }
00183
00184 void Navigator::dragMoveEvent( QDragMoveEvent *event )
00185 {
00186 kdDebug() << "Navigator::dragEnterEvent()" << endl;
00187
00188 kdDebug() << " Format: " << event->format() << endl;
00189
00190 QListBoxItem *item = itemAt( event->pos() );
00191
00192 if ( !item ) {
00193 event->accept( false );
00194 return;
00195 }
00196
00197 EntryItem *entry = static_cast<EntryItem *>( item );
00198
00199 kdDebug() << " PLUGIN: " << entry->plugin()->identifier() << endl;
00200
00201 event->accept( entry->plugin()->canDecodeDrag( event ) );
00202 }
00203
00204 void Navigator::dropEvent( QDropEvent *event )
00205 {
00206 kdDebug() << "Navigator::dropEvent()" << endl;
00207
00208 QListBoxItem *item = itemAt( event->pos() );
00209
00210 if ( !item ) {
00211 return;
00212 }
00213
00214 EntryItem *entry = static_cast<EntryItem *>( item );
00215
00216 kdDebug() << " PLUGIN: " << entry->plugin()->identifier() << endl;
00217
00218 entry->plugin()->processDropEvent( event );
00219 }
00220
00221 void Navigator::resizeEvent( QResizeEvent *event )
00222 {
00223 QListBox::resizeEvent( event );
00224 triggerUpdate( true );
00225 }
00226
00227 IconSidePane::IconSidePane( Core *core, QWidget *parent, const char *name )
00228 : SidePaneBase( core, parent, name )
00229 {
00230 mNavigator = new Navigator( this );
00231 connect( mNavigator, SIGNAL( pluginActivated( Kontact::Plugin * ) ),
00232 SIGNAL( pluginSelected( Kontact::Plugin * ) ) );
00233
00234 setAcceptDrops( true );
00235 }
00236
00237 IconSidePane::~IconSidePane()
00238 {
00239 }
00240
00241 void IconSidePane::updatePlugins()
00242 {
00243 mNavigator->updatePlugins( core()->pluginList() );
00244 }
00245
00246 void IconSidePane::selectPlugin( Kontact::Plugin *plugin )
00247 {
00248 bool blocked = signalsBlocked();
00249 blockSignals( true );
00250
00251 uint i;
00252 for ( i = 0; i < mNavigator->count(); ++i ) {
00253 EntryItem *item = static_cast<EntryItem *>( mNavigator->item( i ) );
00254 if ( item->plugin() == plugin ) {
00255 mNavigator->setCurrentItem( i );
00256 break;
00257 }
00258 }
00259
00260 blockSignals( blocked );
00261 }
00262
00263 void IconSidePane::selectPlugin( const QString &name )
00264 {
00265 bool blocked = signalsBlocked();
00266 blockSignals( true );
00267
00268 uint i;
00269 for ( i = 0; i < mNavigator->count(); ++i ) {
00270 EntryItem *item = static_cast<EntryItem *>( mNavigator->item( i ) );
00271 if ( item->plugin()->identifier() == name ) {
00272 mNavigator->setCurrentItem( i );
00273 break;
00274 }
00275 }
00276
00277 blockSignals( blocked );
00278 }
00279
00280 #include "iconsidepane.moc"
00281
00282
This file is part of the documentation for kontact Library Version 3.2.2.