Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

launchertab.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 #include "launchertab.h"
00021 #include <qapplication.h>
00022 
00023 
00024 LauncherTabBar::LauncherTabBar( QWidget *parent, const char *name )
00025     : QTabBar( parent, name )
00026 {
00027     setFocusPolicy( NoFocus );
00028     connect( this, SIGNAL( selected(int) ), this, SLOT( layoutTabs() ) );
00029 }
00030 
00031 LauncherTabBar::~LauncherTabBar()
00032 {
00033 }
00034 
00035 void LauncherTabBar::insertTab( LauncherTab *t, int index )
00036 {
00037     if ( index < 0 )
00038         items.append( t );
00039     else
00040         items.insert( (uint)index, t );
00041     tabs.insert( t->type, t );
00042     QTabBar::insertTab( t, index );
00043 }
00044 
00045 void LauncherTabBar::removeTab( QTab *tab )
00046 {
00047     LauncherTab *t = (LauncherTab *)tab;
00048     tabs.remove( t->type );
00049     items.remove( t );
00050     QTabBar::removeTab( t );
00051 }
00052 
00053 void LauncherTabBar::prevTab()
00054 {
00055     int n = count();
00056     int tab = currentTab();
00057     if ( tab >= 0 )
00058         setCurrentTab( (tab - 1 + n)%n );
00059 }
00060 
00061 void LauncherTabBar::nextTab()
00062 {
00063     int n = count();
00064     int tab = currentTab();
00065     setCurrentTab( (tab + 1)%n );
00066 }
00067 
00068 void LauncherTabBar::showTab( const QString& id )
00069 {
00070     setCurrentTab( tabs[id] );
00071 }
00072 
00073 void LauncherTabBar::layoutTabs()
00074 {
00075     if ( !count() )
00076         return;
00077 
00078     int available = width()-1;
00079 
00080     QFontMetrics fm = fontMetrics();
00081     int hiddenTabWidth = -12;
00082     LauncherTab *current = currentLauncherTab();
00083     int hframe, vframe, overlap;
00084     style().tabbarMetrics( this, hframe, vframe, overlap );
00085     int x = 0;
00086     QRect r;
00087     LauncherTab *t;
00088     QListIterator< LauncherTab > it( items );
00089     int required = 0;
00090     int eventabwidth = (width()-1)/count();
00091     enum Mode { HideBackText, Pack, Even } mode=Even;
00092     for (it.toFirst(); it.current(); ++it ) {
00093         t = it.current();
00094         if ( !t )
00095             continue;
00096         int iw = fm.width( t->text() ) + hframe - overlap;
00097         if ( t != current ) {
00098             available -= hiddenTabWidth + hframe - overlap;
00099             if ( t->iconSet() != 0 )
00100                 available -= t->iconSet()->pixmap().width();
00101         }
00102         if ( t->iconSet() != 0 )
00103             iw += t->iconSet()->pixmap().width();
00104         required += iw;
00105         // As space gets tight, packed looks better than even. "10" must be at least 0.
00106         if ( iw >= eventabwidth-10 )
00107             mode = Pack;
00108     }
00109     if ( mode == Pack && required > width()-1 )
00110         mode = HideBackText;
00111     for ( it.toFirst(); it.current(); ++it ) {
00112         t = it.current();
00113         if ( !t )
00114             continue;
00115         if ( mode != HideBackText ) {
00116             int w = fm.width( t->text() );
00117             int ih = 0;
00118             if ( t->iconSet() != 0 ) {
00119                 w += t->iconSet()->pixmap().width();
00120                 ih = t->iconSet()->pixmap().height();
00121             }
00122             int h = QMAX( fm.height(), ih );
00123             h = QMAX( h, QApplication::globalStrut().height() );
00124 
00125             h += vframe;
00126             w += hframe;
00127 
00128             QRect totr(x, 0,
00129                 mode == Even ? eventabwidth : w * (width()-1)/required, h);
00130             t->setRect(totr);
00131             x += totr.width() - overlap;
00132             r = r.unite(totr);
00133         } else if ( t != current ) {
00134             int w = hiddenTabWidth;
00135             int ih = 0;
00136             if ( t->iconSet() != 0 ) {
00137                 w += t->iconSet()->pixmap().width();
00138                 ih = t->iconSet()->pixmap().height();
00139             }
00140             int h = QMAX( fm.height(), ih );
00141             h = QMAX( h, QApplication::globalStrut().height() );
00142 
00143             h += vframe;
00144             w += hframe;
00145 
00146             t->setRect( QRect(x, 0, w, h) );
00147             x += t->rect().width() - overlap;
00148             r = r.unite( t->rect() );
00149         } else {
00150             int ih = 0;
00151             if ( t->iconSet() != 0 ) {
00152                 ih = t->iconSet()->pixmap().height();
00153             }
00154             int h = QMAX( fm.height(), ih );
00155             h = QMAX( h, QApplication::globalStrut().height() );
00156 
00157             h += vframe;
00158 
00159             t->setRect( QRect(x, 0, available, h) );
00160             x += t->rect().width() - overlap;
00161             r = r.unite( t->rect() );
00162         }
00163     }
00164 
00165     t = it.toLast();
00166     if (t) {
00167         QRect rr = t->rect();
00168         rr.setRight(width()-1);
00169         t->setRect( rr );
00170     }
00171 
00172     for (it.toFirst(); it.current(); ++it ) {
00173         t = it.current();
00174         QRect tr = t->rect();
00175         tr.setHeight( r.height() );
00176         t->setRect( tr );
00177     }
00178 
00179     update();
00180 }
00181 
00182 void LauncherTabBar::paint( QPainter * p, QTab * t, bool selected ) const
00183 {
00184     LauncherTabBar *that = (LauncherTabBar *) this;
00185     LauncherTab *ct = (LauncherTab *)t;
00186     QPalette pal = palette();
00187     bool setPal = FALSE;
00188     if ( ct->bgColor.isValid() ) {
00189         pal.setColor( QPalette::Active, QColorGroup::Background, ct->bgColor );
00190         pal.setColor( QPalette::Active, QColorGroup::Button, ct->bgColor );
00191         pal.setColor( QPalette::Inactive, QColorGroup::Background, ct->bgColor );
00192         pal.setColor( QPalette::Inactive, QColorGroup::Button, ct->bgColor );
00193         that->setUpdatesEnabled( FALSE );
00194         that->setPalette( pal );
00195         setPal = TRUE;
00196     }
00197 #if QT_VERSION >= 0x030000
00198     QStyle::SFlags flags = QStyle::Style_Default;
00199     if ( selected )
00200         flags |= QStyle::Style_Selected;
00201     style().drawControl( QStyle::CE_TabBarTab, p, this, t->rect(),
00202                          colorGroup(), flags, QStyleOption(t) );
00203 #else
00204     style().drawTab( p, this, t, selected );
00205 #endif
00206 
00207     QRect r( t->rect() );
00208     QFont f( font() );
00209     if ( selected )
00210         f.setBold( TRUE );
00211     p->setFont( f );
00212 
00213     if ( ct->fgColor.isValid() ) {
00214         pal.setColor( QPalette::Active, QColorGroup::Foreground, ct->fgColor );
00215         pal.setColor( QPalette::Inactive, QColorGroup::Foreground, ct->fgColor );
00216         that->setUpdatesEnabled( FALSE );
00217         that->setPalette( pal );
00218         setPal = TRUE;
00219     }
00220     int iw = 0;
00221     int ih = 0;
00222     if ( t->iconSet() != 0 ) {
00223         iw = t->iconSet()->pixmap().width() + 2;
00224         ih = t->iconSet()->pixmap().height();
00225     }
00226     int w = iw + p->fontMetrics().width( t->text() ) + 4;
00227     int h = QMAX(p->fontMetrics().height() + 4, ih );
00228     paintLabel( p, QRect( r.left() + (r.width()-w)/2 - 3,
00229                             r.top() + (r.height()-h)/2, w, h ), t,
00230 #if QT_VERSION >= 0x030000
00231                             t->identifier() == keyboardFocusTab()
00232 #else
00233                             t->identitifer() == keyboardFocusTab()
00234 #endif
00235                 );
00236     if ( setPal ) {
00237         that->unsetPalette();
00238         that->setUpdatesEnabled( TRUE );
00239     }
00240 }
00241 
00242 void LauncherTabBar::paintLabel( QPainter* p, const QRect&,
00243                           QTab* t, bool has_focus ) const
00244 {
00245     QRect r = t->rect();
00246     //    if ( t->id != currentTab() )
00247     //r.moveBy( 1, 1 );
00248     //
00249     if ( t->iconSet() ) {
00250         // the tab has an iconset, draw it in the right mode
00251         QIconSet::Mode mode = (t->isEnabled() && isEnabled()) ? QIconSet::Normal : QIconSet::Disabled;
00252         if ( mode == QIconSet::Normal && has_focus )
00253             mode = QIconSet::Active;
00254     QPixmap pixmap;
00255     if ( mode == QIconSet::Disabled )
00256         pixmap = t->iconSet()->pixmap( QIconSet::Automatic, mode );
00257     else
00258         pixmap = t->iconSet()->pixmap();
00259         int pixw = pixmap.width();
00260         int pixh = pixmap.height();
00261         p->drawPixmap( r.left() + 6, r.center().y() - pixh / 2 + 1, pixmap );
00262         r.setLeft( r.left() + pixw + 5 );
00263     }
00264 
00265     QRect tr = r;
00266 
00267     if ( r.width() < 20 )
00268         return;
00269 
00270     if ( t->isEnabled() && isEnabled()  ) {
00271 #if defined(_WS_WIN32_)
00272         if ( colorGroup().brush( QColorGroup::Button ) == colorGroup().brush( QColorGroup::Background ) )
00273             p->setPen( colorGroup().buttonText() );
00274         else
00275             p->setPen( colorGroup().foreground() );
00276 #else
00277         p->setPen( colorGroup().foreground() );
00278 #endif
00279         p->drawText( tr, AlignCenter | AlignVCenter | ShowPrefix, t->text() );
00280     } else {
00281         p->setPen( palette().disabled().foreground() );
00282         p->drawText( tr, AlignCenter | AlignVCenter | ShowPrefix, t->text() );
00283     }
00284 }
00285 

Generated on Sat Nov 5 16:15:30 2005 for OPIE by  doxygen 1.4.2