libopie API Documentation

otabwidget.cpp

00001 /*
00002                             This file is part of the Opie Project
00003 
00004                              Copyright (c)  2002 Dan Williams <williamsdr@acm.org>
00005               =.
00006             .=l.
00007            .>+-=
00008  _;:,     .>    :=|.         This program is free software; you can
00009 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00010 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00011 .="- .-=="i,     .._         License as published by the Free Software
00012  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00013      ._= =}       :          or (at your option) any later version.
00014     .%`+i>       _;_.
00015     .i_,=:_.      -<s.       This program is distributed in the hope that
00016      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00017     : ..    .:,     . . .    without even the implied warranty of
00018     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00019   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00020 ..}^=.=       =       ;      Library General Public License for more
00021 ++=   -.     .`     .:       details.
00022  :     =  ...= . :.=-
00023  -.   .:....=;==+<;          You should have received a copy of the GNU
00024   -_. . .   )=.  =           Library General Public License along with
00025     --        :-=`           this library; see the file COPYING.LIB.
00026                              If not, write to the Free Software Foundation,
00027                              Inc., 59 Temple Place - Suite 330,
00028                              Boston, MA 02111-1307, USA.
00029 
00030 */
00031 
00032 #include "otabwidget.h"
00033 
00034 #include <qpe/applnk.h>
00035 #include <qpe/config.h>
00036 #include <qpe/resource.h>
00037 #include <opie/otabbar.h>
00038 
00039 #include <qcombobox.h>
00040 #include <qwidgetstack.h>
00041 
00042 OTabWidget::OTabWidget( QWidget *parent, const char *name, TabStyle s, TabPosition p )
00043     : QWidget( parent, name )
00044 {
00045     if ( s == Global )
00046     {
00047         Config config( "qpe" );
00048         config.setGroup( "Appearance" );
00049         s = ( TabStyle ) config.readNumEntry( "TabStyle", (int) IconTab );
00050         if ( s <= Global || s > IconList)
00051         {
00052             s = IconTab;
00053         }
00054         QString pos = config.readEntry( "TabPosition", "Top");
00055         if ( pos == "Bottom" )
00056         {
00057             p = Bottom;
00058         }
00059         else
00060         {
00061             p = Top;
00062         }
00063     }
00064 
00065     widgetStack = new QWidgetStack( this, "widgetstack" );
00066     widgetStack->setFrameStyle( QFrame::NoFrame );
00067     widgetStack->setLineWidth( style().defaultFrameWidth() );
00068 
00069     tabBarStack = new QWidgetStack( this, "tabbarstack" );
00070 
00071     tabBar = new OTabBar( tabBarStack, "tabbar" );
00072     tabBarStack->addWidget( tabBar, 0 );
00073     connect( tabBar, SIGNAL( selected( int ) ), this, SLOT( slotTabBarSelected( int ) ) );
00074 
00075     tabList = new QComboBox( false, tabBarStack, "tablist" );
00076     tabBarStack->addWidget( tabList, 1 );
00077     connect( tabList, SIGNAL( activated( int ) ), this, SLOT( slotTabListSelected( int ) ) );
00078 
00079     tabBarPosition = p;
00080     setTabStyle( s );
00081     setTabPosition( p );
00082 
00083     currTab= 0x0;
00084 }
00085 
00086 OTabWidget::~OTabWidget()
00087 {
00088 }
00089 
00090 void OTabWidget::addTab( QWidget *child, const QString &icon, const QString &label )
00091 {
00092     QPixmap iconset = loadSmooth( icon );
00093 
00094     QTab *tab = new QTab();
00095     if ( tabBarStyle == IconTab )
00096     {
00097         tab->label = QString::null;
00098     }
00099     else
00100     {
00101         tab->label = label;
00102     }
00103     if ( tabBarStyle == IconTab || tabBarStyle == IconList )
00104     {
00105         tab->iconset = new QIconSet( iconset );
00106     }
00107     int tabid = tabBar->addTab( tab );
00108 
00109     if ( tabBarStyle == IconTab || tabBarStyle == IconList )
00110     {
00111         tabList->insertItem( iconset, label, -1 );
00112     }
00113     else
00114     {
00115         tabList->insertItem( label );
00116     }
00117 
00118     widgetStack->addWidget( child, tabid );
00119     widgetStack->raiseWidget( child );
00120     widgetStack->setFrameStyle( QFrame::StyledPanel | QFrame::Raised );
00121 
00122     OTabInfo *tabinfo = new OTabInfo( tabid, child, icon, label );
00123     tabs.append( tabinfo );
00124     selectTab( tabinfo );
00125 }
00126 
00127 void OTabWidget::removePage( QWidget *childwidget )
00128 {
00129   if ( childwidget )
00130     {
00131         OTabInfo *tab = tabs.first();
00132         while ( tab && tab->control() != childwidget )
00133         {
00134             tab = tabs.next();
00135         }
00136         if ( tab && tab->control() == childwidget )
00137         {
00138             tabBar->setTabEnabled( tab->id(), FALSE );
00139             tabBar->removeTab( tabBar->tab( tab->id() ) );
00140             int i = 0;
00141             while ( i < tabList->count() && tabList->text( i ) != tab->label() )
00142             {
00143                 i++;
00144             }
00145             if ( tabList->text( i ) == tab->label() )
00146             {
00147                 tabList->removeItem( i );
00148             }
00149             widgetStack->removeWidget( childwidget );
00150             tabs.remove( tab );
00151             delete tab;
00152             currTab = tabs.current();
00153             if ( !currTab )
00154             {
00155                 widgetStack->setFrameStyle( QFrame::NoFrame );
00156             }
00157 
00158             setUpLayout();
00159         }
00160     }
00161 }
00162 
00163 void OTabWidget::changeTab( QWidget *widget, const QString &iconset, const QString &label)
00164 {
00165     OTabInfo *currtab = tabs.first();
00166     while ( currtab && currtab->control() != widget )
00167     {
00168         currtab = tabs.next();
00169     }
00170     if ( currtab && currtab->control() == widget )
00171     {
00172         QTab *tab = tabBar->tab( currtab->id() );
00173         QPixmap icon( loadSmooth( iconset ) );
00174         tab->setText( label );
00175         if ( tabBarStyle == IconTab )
00176             tab->setIconSet( icon );
00177         int i = 0;
00178         while ( i < tabList->count() && tabList->text( i ) != currtab->label() )
00179         {
00180             i++;
00181         }
00182         if ( i < tabList->count() && tabList->text( i ) == currtab->label() )
00183         {
00184             if ( tabBarStyle == IconTab || tabBarStyle == IconList )
00185             {
00186                 tabList->changeItem( icon, label, i );
00187             }
00188             else
00189             {
00190                 tabList->changeItem( label, i );
00191             }
00192         }
00193         currtab->setLabel( label );
00194         currtab->setIcon( iconset );
00195     }
00196     setUpLayout();
00197 }
00198 
00199 void OTabWidget::setCurrentTab( QWidget *childwidget )
00200 {
00201     OTabInfo *currtab = tabs.first();
00202     while ( currtab && currtab->control() != childwidget )
00203     {
00204         currtab = tabs.next();
00205     }
00206     if ( currtab && currtab->control() == childwidget )
00207     {
00208         selectTab( currtab );
00209     }
00210 }
00211 
00212 void OTabWidget::setCurrentTab( const QString &tabname )
00213 {
00214     OTabInfo *newtab = tabs.first();
00215     while ( newtab && newtab->label() != tabname )
00216     {
00217         newtab = tabs.next();
00218     }
00219     if ( newtab && newtab->label() == tabname )
00220     {
00221         selectTab( newtab );
00222     }
00223 }
00224 
00225 void OTabWidget::setCurrentTab(int tabindex) {
00226     OTabInfo *newtab = tabs.first();
00227     while ( newtab && newtab->id() != tabindex )
00228     {
00229         newtab = tabs.next();
00230     }
00231     if ( newtab && newtab->id() == tabindex )
00232     {
00233         selectTab( newtab );
00234     }
00235 }
00236 
00237 
00238 OTabWidget::TabStyle OTabWidget::tabStyle() const
00239 {
00240     return tabBarStyle;
00241 }
00242 
00243 void OTabWidget::setTabStyle( TabStyle s )
00244 {
00245     tabBarStyle = s;
00246     if ( tabBarStyle == TextTab || tabBarStyle == IconTab )
00247     {
00248         QTab *currtab;
00249         for (  OTabInfo *tabinfo = tabs.first(); tabinfo; tabinfo = tabs.next() )
00250         {
00251             currtab = tabBar->tab( tabinfo->id() );
00252             if ( tabBarStyle == IconTab )
00253             {
00254                 currtab->iconset = new QIconSet( loadSmooth( tabinfo->icon() ) );
00255                 if ( tabinfo == currTab )
00256                     currtab->setText( tabinfo->label() );
00257                 else
00258                     currtab->setText( QString::null );
00259             }
00260             else
00261             {
00262                 currtab->iconset = 0x0;
00263                 currtab->setText( tabinfo->label() );
00264             }
00265         }
00266         tabBarStack->raiseWidget( tabBar );
00267     }
00268     else if ( tabBarStyle == TextList || tabBarStyle == IconList )
00269     {
00270         tabList->clear();
00271         for (  OTabInfo *tabinfo = tabs.first(); tabinfo; tabinfo = tabs.next() )
00272         {
00273             if ( tabBarStyle == IconList )
00274             {
00275                 tabList->insertItem( loadSmooth( tabinfo->icon() ), tabinfo->label() );
00276             }
00277             else
00278             {
00279                 tabList->insertItem( tabinfo->label() );
00280             }
00281         }
00282         tabBarStack->raiseWidget( tabList );
00283     }
00284     setUpLayout();
00285 }
00286 
00287 OTabWidget::TabPosition OTabWidget::tabPosition() const
00288 {
00289     return tabBarPosition;
00290 }
00291 
00292 void OTabWidget::setTabPosition( TabPosition p )
00293 {
00294     tabBarPosition = p;
00295     if ( tabBarPosition == Top )
00296     {
00297         tabBar->setShape( QTabBar::RoundedAbove );
00298     }
00299     else
00300     {
00301         tabBar->setShape( QTabBar::RoundedBelow );
00302     }
00303     setUpLayout();
00304 }
00305 
00306 void OTabWidget::slotTabBarSelected( int id )
00307 {
00308     OTabInfo *newtab = tabs.first();
00309     while ( newtab && newtab->id() != id )
00310     {
00311         newtab = tabs.next();
00312     }
00313     if ( newtab && newtab->id() == id )
00314     {
00315         selectTab( newtab );
00316     }
00317 }
00318 
00319 void OTabWidget::slotTabListSelected( int index )
00320 {
00321     OTabInfo *newtab = tabs.at( index );
00322     if ( newtab )
00323     {
00324         selectTab( newtab );
00325     }
00326 }
00327 
00328 QPixmap OTabWidget::loadSmooth( const QString &name )
00329 {
00330     QPixmap p;
00331     p.convertFromImage( Resource::loadImage( name ).smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) );
00332     return p;
00333 }
00334 
00335 void OTabWidget::selectTab( OTabInfo *tab )
00336 {
00337     if ( tabBarStyle == IconTab )
00338     {
00339         if ( currTab )
00340         {
00341             tabBar->tab( currTab->id() )->setText( QString::null );
00342             setUpLayout();
00343         }
00344         tabBar->tab( tab->id() )->setText( tab->label() );
00345         tabBar->setCurrentTab( tab->id() );
00346         setUpLayout();
00347         tabBar->update();
00348     }
00349     else
00350     {
00351         tabBar->setCurrentTab( tab->id() );
00352     }
00353 
00354     widgetStack->raiseWidget( tab->control() );
00355 
00356     emit currentChanged( tab->control() );
00357 
00358     currTab = tab;
00359 }
00360 
00361 void OTabWidget::setUpLayout()
00362 {
00363     tabBar->layoutTabs();
00364     QSize t( tabBarStack->sizeHint() );
00365     if ( tabBarStyle == IconTab )
00366     {
00367         if ( t.width() > width() )
00368             t.setWidth( width() );
00369     }
00370     else
00371     {
00372         t.setWidth( width() );
00373     }
00374     int lw = widgetStack->lineWidth();
00375     if ( tabBarPosition == Bottom )
00376     {
00377         tabBarStack->setGeometry( QMAX(0, lw-2), height() - t.height() - lw, t.width(), t.height() );
00378         widgetStack->setGeometry( 0, 0, width(), height()-t.height()+QMAX(0, lw-2) );
00379     }
00380     else
00381     {
00382         tabBarStack->setGeometry( QMAX(0, lw-2), 0, t.width(), t.height() );
00383         widgetStack->setGeometry( 0, t.height()-lw, width(), height()-t.height()+QMAX( 0, lw-2 ) );
00384     }
00385 
00386     if ( autoMask() )
00387         updateMask();
00388 }
00389 
00390 QSize OTabWidget::sizeHint() const
00391 {
00392     QSize s( widgetStack->sizeHint() );
00393     QSize t( tabBarStack->sizeHint() );
00394     return QSize( QMAX( s.width(), t.width() ), s.height() + t.height() );
00395 }
00396 
00397 void OTabWidget::resizeEvent( QResizeEvent * )
00398 {
00399     setUpLayout();
00400 }
00401 
00402 int OTabWidget::currentTab()
00403 {
00404     if ( currTab )
00405     {
00406         return currTab->id();
00407     }
00408     return -1;
00409 }
00410 
00411 QWidget* OTabWidget::currentWidget()const
00412 {
00413     if ( currTab )
00414     {
00415         return currTab->control();
00416     }
00417 
00418     return 0;
00419 }
KDE Logo
This file is part of the documentation for OPIE Version 1.1.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:45 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001