partbar.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <qpainter.h>
00024 #include <qdrawutil.h>
00025
00026 #include <kiconloader.h>
00027 #include <kdebug.h>
00028
00029 #include "actionpart.h"
00030 #include "partbar.h"
00031
00032 using namespace KSync;
00033
00034 PartBarItem::PartBarItem( PartBar *parent, ActionPart *part )
00035 : QListBoxPixmap( KIconLoader::unknown() )
00036 {
00037 m_Parents = parent;
00038 m_Part = part;
00039 m_Pixmap = m_Part->pixmap();
00040 setCustomHighlighting( true );
00041 setText( part->title() );
00042
00043 }
00044
00045 PartBarItem::~PartBarItem()
00046 {
00047 }
00048
00049 ActionPart *PartBarItem::part()
00050 {
00051 return m_Part;
00052 }
00053
00054
00055
00056
00057
00058 int PartBarItem::width( const QListBox *listbox) const
00059 {
00060 return listbox->viewport()->width();
00061 }
00062
00063 int PartBarItem::height( const QListBox *listbox) const
00064 {
00065 int min = 0;
00066 min = listbox->fontMetrics().lineSpacing() + pixmap()->height() + 6;
00067 return min;
00068 }
00069
00070 void PartBarItem::paint(QPainter *p)
00071 {
00072 QListBox *box = listBox();
00073 int w = width( box );
00074 static const int margin = 3;
00075 int y = margin;
00076 const QPixmap *pm = pixmap();
00077
00078 if ( !pm->isNull() ) {
00079 int x = (w - pm->width()) / 2;
00080 x = QMAX( x, margin );
00081 p->drawPixmap( x, y, *pm );
00082 }
00083
00084 if ( !text().isEmpty() ) {
00085 QFontMetrics fm = p->fontMetrics();
00086 y += pm->height() + fm.height() - fm.descent();
00087 int x = (w - fm.width( text() )) / 2;
00088 x = QMAX( x, margin );
00089 p->drawText( x, y, text() );
00090 }
00091
00092 if ( isCurrent() || isSelected() ) {
00093 qDrawShadePanel( p, 1, 0, w -2, height(box),
00094 box->colorGroup(), true, 1, 0 );
00095 }
00096 }
00097
00098
00099 PartBar::PartBar(QWidget *parent, const char *name, WFlags f)
00100 : QFrame ( parent, name, f ),
00101 m_listBox( 0 ),
00102 m_activeItem ( 0 )
00103 {
00104 setListBox( 0 );
00105 setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred ) );
00106 }
00107
00108 PartBarItem *PartBar::insertItem( ActionPart *part, int pos )
00109 {
00110
00111 PartBarItem *item = new PartBarItem( this , part );
00112 m_listBox->insertItem( item, pos );
00113 return item;
00114 }
00115
00116 void PartBar::setListBox(KListBox *view)
00117 {
00118 delete m_listBox;
00119
00120 if ( !view ) {
00121 m_listBox = new KListBox( this );
00122 } else {
00123 m_listBox = view;
00124 if ( m_listBox->parentWidget() != this ) {
00125 m_listBox->reparent( this, QPoint( 0, 0 ) );
00126 }
00127 m_listBox->resize( width(), height() );
00128 }
00129
00130 m_listBox->setSelectionMode( KListBox::Single );
00131 QPalette pal = palette();
00132 QColor gray = pal.color(QPalette::Normal, QColorGroup::Mid );
00133 pal.setColor( QPalette::Normal, QColorGroup::Base, gray );
00134 pal.setColor( QPalette::Inactive, QColorGroup::Base, gray );
00135
00136 setPalette( pal );
00137 m_listBox->viewport()->setBackgroundMode( PaletteMid);
00138
00139 connect( m_listBox, SIGNAL( clicked ( QListBoxItem * ) ),
00140 SLOT( slotSelected( QListBoxItem * ) ) );
00141 }
00142
00143 void PartBar::clear()
00144 {
00145 m_listBox->clear();
00146 }
00147
00148 void PartBar::resizeEvent( QResizeEvent *e )
00149 {
00150 QFrame::resizeEvent( e );
00151 m_listBox->resize( width(), height() );
00152 }
00153
00154 QSize PartBar::sizeHint() const
00155 {
00156 int w = 0;
00157 int h = 0;
00158
00159 QListBoxItem *item;
00160
00161 for ( item = m_listBox->firstItem(); item; item = item->next() ) {
00162 w = QMAX(w , item->width( m_listBox ));
00163 h += item->height( m_listBox );
00164 }
00165
00166 if (m_listBox->verticalScrollBar()->isVisible() ) {
00167 w += m_listBox->verticalScrollBar()->width();
00168 }
00169
00170 if ( w == 0 && h == 0) {
00171 return QSize( 100, 200 );
00172 } else {
00173 return QSize( 6 + w , h );
00174 }
00175 }
00176
00177 QSize PartBar::minimumSizeHint() const
00178 {
00179 QSize s = sizeHint();
00180 int h = s.height() + m_listBox->horizontalScrollBar()->height();
00181 int w = s.width() + m_listBox->verticalScrollBar()->width();
00182 return QSize( w, h );
00183 }
00184
00185 void PartBar::slotSelected( QListBoxItem *item )
00186 {
00187 if ( item && item != m_activeItem ) {
00188 PartBarItem* it = static_cast<PartBarItem*>( item );
00189 m_activeItem = it;
00190 emit activated( it->part() );
00191 }
00192 }
00193
00194 PartBarItem * PartBar::currentItem() const
00195 {
00196 QListBoxItem *item = m_listBox->item( m_listBox->currentItem() );
00197 if ( item ) {
00198 return static_cast<PartBarItem *>( item );
00199 } else {
00200 return 0;
00201 }
00202 }
00203
00204 void PartBar::selectPart( const QString &name )
00205 {
00206 for( uint i = 0; i < m_listBox->count(); ++i ) {
00207 PartBarItem *item = static_cast<PartBarItem *>( m_listBox->item( i ) );
00208 if ( item->part()->name() == name ) {
00209 m_listBox->setSelected( item, true );
00210 slotSelected( item );
00211 break;
00212 }
00213 }
00214 }
00215
00216 #include "partbar.moc"
This file is part of the documentation for kitchensync Library Version 3.3.2.