lib Library API Documentation

koKoolBar.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <koKoolBar.h>
00021 #include <kiconloader.h>
00022 
00023 #include <qpainter.h>
00024 #include <qpushbutton.h>
00025 
00026 static int g_koKoolBarId = 0;
00027 
00028 KoKoolBar::KoKoolBar( QWidget *_parent, const char *_name ) :
00029   QWidget( _parent, _name ), m_iActiveGroup( -1 )
00030 {
00031   m_mapGroups.setAutoDelete( true );
00032   m_pBox = new KoKoolBarBox( this );
00033 }
00034 
00035 int KoKoolBar::insertGroup( const QString& _text )
00036 {
00037   KoKoolBarGroup *p = new KoKoolBarGroup( this, _text );
00038   m_mapGroups.insert( p->id(), p );
00039 
00040   if ( m_iActiveGroup == -1 )
00041     setActiveGroup( p->id() );
00042   else
00043     resizeEvent( 0L );
00044   return p->id();
00045 }
00046 
00047 int KoKoolBar::insertItem( int _grp, const QPixmap& _pix, const QString& _text,
00048                QObject *_obj, const char *_slot )
00049 {
00050   KoKoolBarGroup* g = m_mapGroups[ _grp ];
00051   if ( !g )
00052     return -1;
00053   KoKoolBarItem *item = new KoKoolBarItem( g, _pix, _text );
00054 
00055   if ( _obj != 0L && _slot != 0L )
00056     connect( item, SIGNAL( pressed( int, int ) ), _obj, _slot );
00057   g->append( item );
00058 
00059   if ( g->id() == m_iActiveGroup )
00060     m_pBox->update();
00061 
00062   return item->id();
00063 }
00064 
00065 void KoKoolBar::removeGroup( int _grp )
00066 {
00067   KoKoolBarGroup* g = m_mapGroups[ _grp ];
00068   if ( !g )
00069     return;
00070 
00071   m_mapGroups.remove( _grp );
00072 
00073   if ( _grp == m_iActiveGroup )
00074   {
00075     if ( m_mapGroups.count() == 0 )
00076     {
00077       m_iActiveGroup = -1;
00078       m_pBox->setActiveGroup( 0L );
00079     }
00080     else
00081     {
00082       QIntDictIterator<KoKoolBarGroup> it( m_mapGroups );
00083       g = it.current();
00084       m_iActiveGroup = g->id();
00085       m_pBox->setActiveGroup( g );
00086     }
00087   }
00088 
00089   resizeEvent( 0L );
00090 }
00091 
00092 void KoKoolBar::removeItem( int _grp, int _id )
00093 {
00094   KoKoolBarGroup* g = m_mapGroups[ _grp ];
00095   if ( !g )
00096     return;
00097 
00098   g->remove( _id );
00099 
00100   if ( g->id() == m_iActiveGroup )
00101     m_pBox->update();
00102 }
00103 
00104 void KoKoolBar::renameItem( int _grp, int _id, const QString & _text )
00105 {
00106   KoKoolBarGroup* g = m_mapGroups[ _grp ];
00107   if ( !g )
00108     return;
00109 
00110   KoKoolBarItem * item = g->item( _id );
00111   if ( !item )
00112     return;
00113 
00114   item->setText( _text );
00115 
00116   if ( g->id() == m_iActiveGroup )
00117     m_pBox->update();
00118 }
00119 
00120 void KoKoolBar::setActiveGroup( int _grp )
00121 {
00122   KoKoolBarGroup* g = m_mapGroups[ _grp ];
00123   if ( !g )
00124     return;
00125 
00126   m_iActiveGroup = g->id();
00127   m_pBox->setActiveGroup( g );
00128 
00129   resizeEvent( 0L );
00130 }
00131 
00132 void KoKoolBar::resizeEvent( QResizeEvent * ev )
00133 {
00134   if ( m_iActiveGroup == -1 )
00135     return;
00136 
00137   int buttonheight = fontMetrics().height() + 4;
00138 
00139   KoKoolBarGroup *g = m_mapGroups[ m_iActiveGroup ];
00140   if ( !g )
00141     return;
00142 
00143   // Go behind g
00144   QIntDictIterator<KoKoolBarGroup> it( m_mapGroups );
00145   while( it.current() != g )
00146     ++it;
00147   // Position of g
00148   QIntDictIterator<KoKoolBarGroup> pos = it;
00149   ++it;
00150 
00151   // How many left ?
00152   int result = 0;
00153   QIntDictIterator<KoKoolBarGroup> i = it;
00154   while( i.current() )
00155   {
00156     ++result;
00157     ++i;
00158   }
00159 
00160   int y = height() - buttonheight * result;
00161   for( ; it.current(); ++it )
00162   {
00163     it.current()->button()->setGeometry( 0, y, width(), buttonheight );
00164     it.current()->button()->show();
00165     y += buttonheight;
00166   }
00167 
00168   int y2 = 0;
00169   it.toFirst();
00170   ++pos;
00171   while( it.current() != pos.current() )
00172   {
00173     it.current()->button()->setGeometry( 0, y2, width(), buttonheight );
00174     it.current()->button()->show();
00175     ++it;
00176     y2 += buttonheight;
00177   }
00178 
00179   if ( height() - y2 - result * buttonheight >= 0 )
00180   {
00181     m_pBox->show();
00182     m_pBox->setGeometry( 0, y2, width(), height() - y2 - result * buttonheight );
00183     if ( !ev ) // fake event
00184       m_pBox->sizeChanged();
00185   }
00186   else
00187     m_pBox->hide();
00188 
00189 }
00190 
00191 void KoKoolBar::enableItem( int _grp, int _id, bool _enable )
00192 {
00193   KoKoolBarGroup* g = m_mapGroups[ _grp ];
00194   if ( !g )
00195     return;
00196   KoKoolBarItem *item = g->item( _id );
00197   if ( !item )
00198     return;
00199   item->setEnabled( _enable );
00200 }
00201 
00202 void KoKoolBar::enableGroup( int _grp, bool _enable )
00203 {
00204   KoKoolBarGroup* g = m_mapGroups[ _grp ];
00205   if ( !g )
00206     return;
00207   g->setEnabled( _enable );
00208 }
00209 
00210 KoKoolBarBox::KoKoolBarBox( KoKoolBar *_bar ) :
00211   QFrame( _bar ), m_pBar( _bar ),
00212   m_pButtonUp( 0L ), m_pButtonDown( 0L )
00213 {
00214   m_iYOffset = 0;
00215   m_iYIcon = 0;
00216   m_pGroup = 0L;
00217 
00218   setFrameShape( StyledPanel );
00219   setFrameShadow( Sunken );
00220   // setBackgroundMode( PaletteBase );
00221   setBackgroundColor( colorGroup().background() );
00222 }
00223 
00224 void KoKoolBarBox::setActiveGroup( KoKoolBarGroup *_grp )
00225 {
00226   m_pGroup = _grp;
00227   m_iYOffset = 0;
00228   m_iYIcon = 0;
00229   update();
00230 }
00231 
00232 bool KoKoolBarBox::needsScrolling() const
00233 {
00234   if ( m_pGroup == 0L )
00235     return false;
00236 
00237   return ( maxHeight() > height() );
00238 }
00239 
00240 void KoKoolBarBox::resizeEvent( QResizeEvent * )
00241 {
00242   if ( needsScrolling() )
00243   {
00244     if ( m_pButtonUp == 0L )
00245     {
00246       m_pButtonUp = new QPushButton( this );
00247       m_pButtonUp->setPixmap( QPixmap( UserIcon( "koKoolBarUp" ) ) );
00248       connect( m_pButtonUp, SIGNAL( clicked() ), this, SLOT( scrollUp() ) );
00249     }
00250     if ( m_pButtonDown == 0L )
00251     {
00252       m_pButtonDown = new QPushButton( this );
00253       m_pButtonDown->setPixmap( QPixmap( UserIcon( "koKoolBarDown" ) ) );
00254       connect( m_pButtonDown, SIGNAL( clicked() ), this, SLOT( scrollDown() ) );
00255     }
00256     m_pButtonUp->show();
00257     m_pButtonUp->raise();
00258     m_pButtonDown->show();
00259     m_pButtonDown->raise();
00260     updateScrollButtons();
00261   }
00262   else
00263   {
00264     if ( m_pButtonUp )
00265       m_pButtonUp->hide();
00266     if ( m_pButtonDown )
00267       m_pButtonDown->hide();
00268   }
00269 }
00270 
00271 KoKoolBarItem* KoKoolBarBox::findByPos( int _abs_y ) const
00272 {
00273   if ( m_pGroup == 0L )
00274     return 0L;
00275 
00276   int y = 0;
00277 
00278   QIntDictIterator<KoKoolBarItem> it = m_pGroup->iterator();
00279   for ( ; it.current(); ++it )
00280   {
00281     int dy = it.current()->height();
00282     if ( y <= _abs_y && _abs_y <= y + dy )
00283       return it.current();
00284     y += dy;
00285   }
00286 
00287   return 0L;
00288 }
00289 
00290 int KoKoolBarBox::maxHeight() const
00291 {
00292   int y = 0;
00293 
00294   QIntDictIterator<KoKoolBarItem> it = m_pGroup->iterator();
00295   for ( ; it.current(); ++it )
00296     y += it.current()->height();
00297 
00298   return y;
00299 }
00300 
00301 bool KoKoolBarBox::isAtTop() const
00302 {
00303   return ( m_iYIcon == 0 );
00304 }
00305 
00306 bool KoKoolBarBox::isAtBottom() const
00307 {
00308   if ( m_pGroup->items() == 0 )
00309     return true;
00310   int h = maxHeight();
00311   if ( height() + m_iYOffset >= h )
00312     return true;
00313   if ( m_pGroup->items() - 1 == m_iYIcon )
00314     return true;
00315   return false;
00316 }
00317 
00318 void KoKoolBarBox::scrollUp()
00319 {
00320   if ( isAtTop() )
00321     return;
00322 
00323   int y = 0;
00324   int i = 0;
00325   m_iYIcon--;
00326 
00327   QIntDictIterator<KoKoolBarItem> it = m_pGroup->iterator();
00328   for ( ; i < m_iYIcon && it.current(); ++it )
00329   {
00330     y += it.current()->height();
00331     ++i;
00332   }
00333 
00334   int old = m_iYOffset;
00335   m_iYOffset = y;
00336 
00337   QWidget::scroll( 0, old - m_iYOffset, contentsRect() );
00338   updateScrollButtons();
00339 }
00340 
00341 void KoKoolBarBox::scrollDown()
00342 {
00343   if ( isAtBottom() )
00344     return;
00345 
00346   int y = 0;
00347   int i = 0;
00348   m_iYIcon++;
00349 
00350   QIntDictIterator<KoKoolBarItem> it = m_pGroup->iterator();
00351   for ( ; i < m_iYIcon && it.current(); ++it )
00352   {
00353     y += it.current()->height();
00354     i++;
00355   }
00356   int h = maxHeight();
00357   if ( y + height() > h ) // Don't go after last item
00358     y = h - height();
00359 
00360   int old = m_iYOffset;
00361   m_iYOffset = y;
00362 
00363   QWidget::scroll( 0, old - m_iYOffset, contentsRect() );
00364   updateScrollButtons();
00365 }
00366 
00367 void KoKoolBarBox::updateScrollButtons()
00368 {
00369   if ( isAtTop() )
00370     m_pButtonUp->setEnabled( false );
00371   else
00372     m_pButtonUp->setEnabled( true );
00373 
00374   if ( isAtBottom() )
00375     m_pButtonDown->setEnabled( false );
00376   else
00377     m_pButtonDown->setEnabled( true );
00378 
00379   const int bs = 14; // buttonSize
00380   m_pButtonUp->setGeometry( width() - bs, height() - 2 * bs, bs, bs );
00381   m_pButtonDown->setGeometry( width() - bs, height() - bs, bs, bs );
00382 }
00383 
00384 void KoKoolBarBox::drawContents( QPainter * painter )
00385 {
00386   if ( m_pGroup == 0L )
00387     return;
00388 
00389   int y = -m_iYOffset;
00390 
00391   QIntDictIterator<KoKoolBarItem> it = m_pGroup->iterator();
00392   for ( ; it.current(); ++it )
00393   {
00394     if ( y + it.current()->height() >= 0 && y <= height() ) // visible ?
00395     {
00396       painter->drawPixmap( ( width() - it.current()->pixmap().width() ) / 2, y, it.current()->pixmap() );
00397       if ( !it.current()->text().isEmpty() )
00398       {
00399         int y2 = y + it.current()->pixmap().height() + 2;
00400         painter->drawText( ( width() - painter->fontMetrics().width( it.current()->text() ) ) / 2,
00401                 y2 + painter->fontMetrics().ascent(), it.current()->text() );
00402       }
00403     }
00404 
00405     y += it.current()->height();
00406   }
00407 }
00408 
00409 KoKoolBarGroup::KoKoolBarGroup( KoKoolBar *_bar, const QString& _text ) :
00410   m_pBar( _bar )
00411 {
00412   m_mapItems.setAutoDelete( true );
00413 
00414   m_pButton = new QPushButton( _text, _bar );
00415 
00416   m_bEnabled = true;
00417 
00418   connect( m_pButton, SIGNAL( clicked() ), this, SLOT( pressed() ) );
00419   m_id = g_koKoolBarId++;
00420 }
00421 
00422 KoKoolBarGroup::~KoKoolBarGroup()
00423 {
00424   delete m_pButton;
00425 }
00426 
00427 void KoKoolBarGroup::remove( int _id )
00428 {
00429   m_mapItems.remove( _id );
00430 }
00431 
00432 void KoKoolBarGroup::pressed()
00433 {
00434   m_pBar->setActiveGroup( m_id );
00435 }
00436 
00437 KoKoolBarItem::KoKoolBarItem( KoKoolBarGroup *_grp, const QPixmap& _pix, const QString&_text )
00438   : m_pGroup( _grp )
00439 {
00440   m_pixmap = _pix;
00441   m_strText = _text;
00442   m_bEnabled = true;
00443   m_id = g_koKoolBarId++;
00444   calc( _grp->bar() );
00445 }
00446 
00447 void KoKoolBarItem::calc( QWidget *_widget )
00448 {
00449   m_iHeight = pixmap().height() + 8;
00450 
00451   if ( !m_strText.isEmpty() )
00452     m_iHeight += _widget->fontMetrics().height() + 2;
00453 }
00454 
00455 void KoKoolBarItem::press()
00456 {
00457   emit pressed();
00458   emit pressed( m_pGroup->id(), m_id );
00459 }
00460 
00461 /*
00462 
00463 int main( int argc, char **argv )
00464 {
00465   KApplication app( argc, argv );
00466   KoKoolBar bar;
00467   int file = bar.insertGroup("File");
00468   QPixmap pix;
00469   pix.load( "/opt/kde/share/icons/image.xpm" );
00470   bar.insertItem( file, pix );
00471   pix.load( "/opt/kde/share/icons/html.xpm" );
00472   bar.insertItem( file, pix );
00473   pix.load( "/opt/kde/share/icons/txt.xpm" );
00474   bar.insertItem( file, pix );
00475   pix.load( "/opt/kde/share/icons/kfm.xpm" );
00476   bar.insertItem( file, pix );
00477 
00478   bar.insertGroup("Edit");
00479   bar.insertGroup("View");
00480   bar.insertGroup("Layout");
00481   bar.insertGroup("Help");
00482   bar.setGeometry( 100, 100, 80, 300 );
00483   bar.show();
00484 
00485   app.exec();
00486 }
00487 */
00488 
00489 #include <koKoolBar.moc>
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Mar 20 14:25:25 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003