kitchensync

partbar.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2002 Holger Freyther <zecke@handhelds.org>
00005     Copyright (c) 2002 Maximilian Reiß <harlekin@handhelds.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include <qpainter.h>
00024 #include <qdrawutil.h>
00025 
00026 #include <kglobal.h>
00027 #include <kiconloader.h>
00028 #include <kdebug.h>
00029 
00030 #include "actionpart.h"
00031 #include "partbar.h"
00032 
00033 using namespace KSync;
00034 
00035 PartBarItem::PartBarItem( PartBar *parent, ActionPart *part )
00036   : QListBoxPixmap( KIconLoader::unknown() )
00037 {
00038   m_Parents = parent;
00039   m_Part = part;
00040   m_Pixmap = m_Part->pixmap();
00041   setCustomHighlighting( true );
00042   setText( part->title() );
00043   //tooltip(part->description() );
00044 
00045   mArrowPixmap = KGlobal::iconLoader()->loadIcon( "1downarrow", KIcon::Small );
00046 }
00047 
00048 PartBarItem::~PartBarItem()
00049 {
00050 }
00051 
00052 ActionPart *PartBarItem::part()
00053 {
00054   return m_Part;
00055 }
00056 
00057 //QString PartBarItem::toolTip() const {
00058 //  return ( m_Part->description() );
00059 //}
00060 
00061 int PartBarItem::width( const QListBox *listbox) const
00062 {
00063   return listbox->viewport()->width();
00064 }
00065 
00066 int PartBarItem::height( const QListBox *listbox) const
00067 {
00068   int min = 0;
00069   min = listbox->fontMetrics().lineSpacing() + pixmap()->height() + 
00070         mArrowPixmap.height() + 10;
00071 
00072   return min;
00073 }
00074 
00075 void PartBarItem::paint(QPainter *p)
00076 {
00077   QListBox *box = listBox();
00078   int w = width( box );
00079   static const int margin = 3;
00080   int y = margin;
00081   const QPixmap *pm = pixmap();
00082 
00083   int x = (w - mArrowPixmap.width()) / 2;
00084   x = QMAX( x, margin );
00085   p->drawPixmap( x, y, mArrowPixmap );
00086 
00087   y += mArrowPixmap.height() + 2;
00088 
00089   if ( !pm->isNull() ) {
00090     int x = (w - pm->width()) / 2;
00091     x = QMAX( x, margin );
00092     p->drawPixmap( x, y, *pm );
00093   }
00094 
00095   if ( !text().isEmpty() ) {
00096     QFontMetrics fm = p->fontMetrics();
00097     y += pm->height() + fm.height() - fm.descent();
00098     int x = (w - fm.width( text() )) / 2;
00099     x = QMAX( x, margin );
00100     p->drawText( x, y, text() );
00101   }
00102   // draw sunken
00103   if ( isCurrent() || isSelected() ) {
00104     int top = mArrowPixmap.height() + 2;
00105     qDrawShadePanel( p, 1, top, w - 2, height( box ) - top,
00106                      box->colorGroup(), true, 1, 0 );
00107   }
00108 }
00109 
00110 
00111 PartBar::PartBar(QWidget *parent, const char *name, WFlags f)
00112   : QFrame ( parent, name, f ),
00113     m_listBox( 0 ),
00114     m_activeItem ( 0 )
00115 {
00116   setListBox( 0 );
00117   setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred ) );
00118 }
00119 
00120 PartBarItem *PartBar::insertItem( ActionPart *part, int pos )
00121 {
00122 //  kdDebug(5210) << part->name() << "\n" << part->description() << "\n";
00123   PartBarItem *item = new PartBarItem( this , part );
00124   m_listBox->insertItem( item, pos );
00125   return item;
00126 }
00127 
00128 void PartBar::setListBox(KListBox *view)
00129 {
00130   delete m_listBox;
00131 
00132   if ( !view ) {
00133     m_listBox = new KListBox( this );
00134   } else {
00135     m_listBox = view;
00136     if ( m_listBox->parentWidget() != this ) {
00137       m_listBox->reparent( this, QPoint( 0, 0 ) );
00138     }
00139     m_listBox->resize( width(), height() );
00140   }
00141 
00142   m_listBox->setSelectionMode( KListBox::Single );
00143   QPalette pal = palette();
00144   QColor gray = pal.color(QPalette::Normal, QColorGroup::Mid );
00145   pal.setColor( QPalette::Normal, QColorGroup::Base, gray );
00146   pal.setColor( QPalette::Inactive, QColorGroup::Base, gray );
00147 
00148   setPalette( pal );
00149   m_listBox->viewport()->setBackgroundMode( PaletteMid);
00150 
00151   connect( m_listBox, SIGNAL( clicked ( QListBoxItem * ) ),
00152        SLOT( slotSelected( QListBoxItem * ) ) );
00153 }
00154 
00155 void PartBar::clear()
00156 {
00157   m_listBox->clear();
00158 }
00159 
00160 void PartBar::resizeEvent( QResizeEvent *e )
00161 {
00162   QFrame::resizeEvent( e );
00163   m_listBox->resize( width(), height() );
00164 }
00165 
00166 QSize PartBar::sizeHint() const
00167 {
00168   int w = 0;
00169   int h = 0;
00170 
00171   QListBoxItem *item;
00172 
00173   for ( item = m_listBox->firstItem(); item; item = item->next() ) {
00174     w = QMAX(w , item->width( m_listBox ));
00175     h += item->height( m_listBox );
00176   }
00177 
00178   if (m_listBox->verticalScrollBar()->isVisible() ) {
00179     w += m_listBox->verticalScrollBar()->width();
00180   }
00181 
00182   if ( w == 0 && h == 0) {
00183     return QSize( 100, 200 );
00184   } else {
00185     return QSize( 6 + w , h );
00186   }
00187 }
00188 
00189 QSize PartBar::minimumSizeHint() const
00190 {
00191   QSize s = sizeHint();
00192   int h = s.height() + m_listBox->horizontalScrollBar()->height();
00193   int w = s.width() + m_listBox->verticalScrollBar()->width();
00194   return QSize( w, h );
00195 }
00196 
00197 void PartBar::slotSelected( QListBoxItem *item )
00198 {
00199   if ( item && item != m_activeItem ) {
00200     PartBarItem* it = static_cast<PartBarItem*>( item );
00201     m_activeItem = it;
00202     emit activated( it->part() );
00203   }
00204 }
00205 
00206 PartBarItem * PartBar::currentItem() const
00207 {
00208   QListBoxItem *item = m_listBox->item( m_listBox->currentItem() );
00209   if ( item ) {
00210     return static_cast<PartBarItem *>( item );
00211   } else {
00212     return 0;
00213   }
00214 }
00215 
00216 void PartBar::selectPart( const QString &name )
00217 {
00218   for( uint i = 0; i < m_listBox->count(); ++i ) {
00219     PartBarItem *item = static_cast<PartBarItem *>( m_listBox->item( i ) );
00220     if ( item->part()->name() == name ) {
00221       m_listBox->setSelected( item, true );
00222       slotSelected( item );
00223       break;
00224     }
00225   }
00226 }
00227 
00228 #include "partbar.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys