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

otabwidget.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003 
00004                              Copyright (C) 2002, 2005 Dan Williams <drw@handhelds.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 #include <opie2/otabwidget.h>
00032 
00033 /* OPIE */
00034 #include <opie2/oresource.h>
00035 #include <opie2/otabbar.h>
00036 
00037 #include <qpe/config.h>
00038 
00039 /* QT */
00040 #include <qcombobox.h>
00041 #include <qwidgetstack.h>
00042 
00043 using namespace Opie::Ui;
00044 
00045 OTabWidget::OTabWidget( QWidget *parent, const char *name, TabStyle s, TabPosition p )
00046         : QWidget( parent, name )
00047         , m_currTab( 0l )
00048         , m_tabBarStyle( Global )
00049         , m_tabBarPosition( Top )
00050         , m_usingTabs( true )
00051         , m_tabBar( 0l )
00052         , m_tabList( 0l )
00053 {
00054     if ( s == Global )
00055     {
00056         // Read Opie global settings for style and position
00057         Config config( "qpe" );
00058         config.setGroup( "Appearance" );
00059 
00060         // Style
00061         s = ( TabStyle ) config.readNumEntry( "TabStyle", (int) IconTab );
00062         if ( s <= Global || s > IconList)
00063             s = IconTab;
00064 
00065         // Position
00066         ( config.readEntry( "TabPosition", "Top" ) == "Bottom" ) ? p = Bottom
00067                                                                  : p = Top;
00068     }
00069 
00070     // Initialize widget stack for tab widgets
00071     m_widgetStack = new QWidgetStack( this );
00072     m_widgetStack->setFrameStyle( QFrame::NoFrame );
00073     m_widgetStack->setLineWidth( style().defaultFrameWidth() );
00074 
00075     // Set initial selector control style and position
00076     setTabStyle( s );
00077     setTabPosition( p );
00078 }
00079 
00080 OTabWidget::~OTabWidget()
00081 {
00082     m_tabs.setAutoDelete( true );
00083     m_tabs.clear();
00084 }
00085 
00086 void OTabWidget::addTab( QWidget *child, const QString &icon, const QString &label )
00087 {
00088     int tabid = -1;
00089 
00090     if ( m_usingTabs )
00091     {
00092         // Create new tab in tab bar
00093         QTab *tab = new QTab();
00094 
00095         // Set label (and icon if necessary)
00096         if ( m_tabBarStyle == IconTab )
00097         {
00098             tab->label = QString::null;
00099             tab->iconset = new QIconSet( Opie::Core::OResource::loadPixmap( icon, Opie::Core::OResource::SmallIcon ) );
00100         }
00101         else
00102             tab->label = label;
00103 
00104         tabid = m_tabBar->addTab( tab );
00105     }
00106     else
00107     {
00108         // Insert entry (with icon if necessary) into drop down list
00109         if ( m_tabBarStyle == IconList )
00110             m_tabList->insertItem( Opie::Core::OResource::loadPixmap( icon, Opie::Core::OResource::SmallIcon ), label, -1 );
00111         else
00112             m_tabList->insertItem( label );
00113     }
00114 
00115     // Add widget to stack
00116     m_widgetStack->addWidget( child, tabid );
00117     m_widgetStack->raiseWidget( child );
00118     m_widgetStack->setFrameStyle( QFrame::StyledPanel | QFrame::Raised );
00119 
00120     // Keep track of tab information
00121     OTabInfo *tabinfo = new OTabInfo( tabid, child, icon, label );
00122     m_tabs.append( tabinfo );
00123 
00124     // Force resizing of child controls
00125     resizeEvent( 0x0 );
00126 
00127     // Make newly added tab the current one displayed
00128     selectTab( tabinfo );
00129 }
00130 
00131 void OTabWidget::removePage( QWidget *childwidget )
00132 {
00133     if ( childwidget )
00134     {
00135         // Find tab information for desired widget
00136         OTabInfo *tab = m_tabs.first();
00137         while ( tab && tab->control() != childwidget )
00138             tab = m_tabs.next();
00139 
00140         if ( tab && tab->control() == childwidget )
00141         {
00142             if ( m_usingTabs )
00143             {
00144                 // Remove tab from tab bar
00145                 m_tabBar->setTabEnabled( tab->id(), false );
00146                 m_tabBar->removeTab( m_tabBar->tab( tab->id() ) );
00147             }
00148             else
00149             {
00150                 // Remove entry from drop down list
00151                 int i = 0;
00152                 while ( i < m_tabList->count() && m_tabList->text( i ) != tab->label() )
00153                     i++;
00154                 if ( m_tabList->text( i ) == tab->label() )
00155                     m_tabList->removeItem( i );
00156             }
00157 
00158             // Remove widget from stack
00159             m_widgetStack->removeWidget( childwidget );
00160 
00161             // Get rid of tab information
00162             m_tabs.remove( tab );
00163             delete tab;
00164 
00165             // Reset current tab
00166             m_currTab = m_tabs.current();
00167             if ( !m_currTab )
00168                 m_widgetStack->setFrameStyle( QFrame::NoFrame );
00169 
00170             // Redraw widget
00171             setUpLayout();
00172         }
00173 
00174         // Force resizing of child controls
00175         resizeEvent( 0x0 );
00176     }
00177 }
00178 
00179 void OTabWidget::changeTab( QWidget *widget, const QString &iconset, const QString &label)
00180 {
00181     // Find tab information for desired widget
00182     OTabInfo *currtab = m_tabs.first();
00183     while ( currtab && currtab->control() != widget )
00184         currtab = m_tabs.next();
00185 
00186     if ( currtab && currtab->control() == widget )
00187     {
00188         QPixmap icon( Opie::Core::OResource::loadPixmap( iconset, Opie::Core::OResource::SmallIcon ) );
00189 
00190         if ( m_usingTabs )
00191         {
00192             // Update tab label and icon (if necessary)
00193             QTab *tab = m_tabBar->tab( currtab->id() );
00194             tab->setText( label );
00195             if ( m_tabBarStyle == IconTab )
00196                 tab->setIconSet( icon );
00197         }
00198         else
00199         {
00200             // Update entry label and icon (if necessary)
00201             int i = 0;
00202             while ( i < m_tabList->count() && m_tabList->text( i ) != currtab->label() )
00203                 i++;
00204             if ( i < m_tabList->count() && m_tabList->text( i ) == currtab->label() )
00205             {
00206                 if ( m_tabBarStyle == IconList )
00207                     m_tabList->changeItem( icon, label, i );
00208                 else
00209                     m_tabList->changeItem( label, i );
00210             }
00211         }
00212 
00213         // Update tab information
00214         currtab->setLabel( label );
00215         currtab->setIcon( iconset );
00216 
00217         // Force resizing of child controls
00218         resizeEvent( 0x0 );
00219 
00220         // Redraw widget
00221         setUpLayout();
00222     }
00223 }
00224 
00225 void OTabWidget::setCurrentTab( QWidget *childwidget )
00226 {
00227     OTabInfo *currtab = m_tabs.first();
00228     while ( currtab && currtab->control() != childwidget )
00229     {
00230         currtab = m_tabs.next();
00231     }
00232     if ( currtab && currtab->control() == childwidget )
00233     {
00234         selectTab( currtab );
00235     }
00236 }
00237 
00238 void OTabWidget::setCurrentTab( const QString &tabname )
00239 {
00240     OTabInfo *newtab = m_tabs.first();
00241     while ( newtab && newtab->label() != tabname )
00242     {
00243         newtab = m_tabs.next();
00244     }
00245     if ( newtab && newtab->label() == tabname )
00246     {
00247         selectTab( newtab );
00248     }
00249 }
00250 
00251 void OTabWidget::setCurrentTab(int tabindex)
00252 {
00253     OTabInfo *newtab = m_tabs.first();
00254     while ( newtab && newtab->id() != tabindex )
00255     {
00256         newtab = m_tabs.next();
00257     }
00258     if ( newtab && newtab->id() == tabindex )
00259     {
00260         selectTab( newtab );
00261     }
00262 }
00263 
00264 
00265 OTabWidget::TabStyle OTabWidget::tabStyle() const
00266 {
00267     return m_tabBarStyle;
00268 }
00269 
00270 void OTabWidget::setTabStyle( TabStyle s )
00271 {
00272     // Get out if new and current styles are the same
00273     if ( s == m_tabBarStyle )
00274         return;
00275 
00276     // Delete current selector control
00277     if ( m_usingTabs )
00278     {
00279         delete m_tabBar;
00280         m_tabBar = 0l;
00281     }
00282     else
00283     {
00284         delete m_tabList;
00285         m_tabList = 0l;
00286     }
00287 
00288     // Set new style information
00289     m_tabBarStyle = s;
00290     m_usingTabs = ( m_tabBarStyle == TextTab || m_tabBarStyle == IconTab );
00291 
00292     // Create new selector control and populate with tab information
00293     if ( m_usingTabs )
00294     {
00295         // Create new tab bar selector
00296         m_tabBar = new OTabBar( this );
00297         connect( m_tabBar, SIGNAL(selected(int)), this, SLOT(slotTabBarSelected(int)) );
00298 
00299         // Add all current tabs to tab bar
00300         for (  OTabInfo *tabinfo = m_tabs.first(); tabinfo; tabinfo = m_tabs.next() )
00301         {
00302             // Create new tab in tab bar
00303             QTab *tab = new QTab();
00304 
00305             // Set label (and icon if necessary)
00306             if ( m_tabBarStyle == IconTab )
00307             {
00308                 tab->label = QString::null;
00309                 tab->iconset = new QIconSet( Opie::Core::OResource::loadPixmap( tabinfo->icon(), Opie::Core::OResource::SmallIcon ) );
00310             }
00311             else
00312                 tab->label = tabinfo->label();
00313 
00314             // Add tab and save its Id
00315             int tabid = m_tabBar->addTab( tab );
00316             tabinfo->setId( tabid );
00317         }
00318     }
00319     else
00320     {
00321         // Create new drop down list selector
00322         m_tabList = new QComboBox( false, this );
00323         connect( m_tabList, SIGNAL(activated(int)), this, SLOT(slotTabListSelected(int)) );
00324 
00325         // Add all current tabs to drop down list
00326         for (  OTabInfo *tabinfo = m_tabs.first(); tabinfo; tabinfo = m_tabs.next() )
00327         {
00328             if ( m_tabBarStyle == IconList )
00329                 m_tabList->insertItem( Opie::Core::OResource::loadPixmap( tabinfo->icon(), Opie::Core::OResource::SmallIcon ),
00330                                        tabinfo->label() );
00331             else
00332                 m_tabList->insertItem( tabinfo->label() );
00333         }
00334     }
00335 
00336     // Redraw widget
00337     setUpLayout();
00338 }
00339 
00340 OTabWidget::TabPosition OTabWidget::tabPosition() const
00341 {
00342     return m_tabBarPosition;
00343 }
00344 
00345 void OTabWidget::setTabPosition( TabPosition p )
00346 {
00347     m_tabBarPosition = p;
00348 
00349     // If using the tab bar selector, set its shape
00350     if ( m_usingTabs )
00351     {
00352         ( m_tabBarPosition == Top ) ? m_tabBar->setShape( QTabBar::RoundedAbove )
00353                                     : m_tabBar->setShape( QTabBar::RoundedBelow );
00354     }
00355 
00356     // Redraw widget
00357     setUpLayout();
00358 }
00359 
00360 void OTabWidget::slotTabBarSelected( int id )
00361 {
00362     OTabInfo *newtab = m_tabs.first();
00363     while ( newtab && newtab->id() != id )
00364         newtab = m_tabs.next();
00365 
00366     if ( newtab && newtab->id() == id )
00367         selectTab( newtab );
00368 }
00369 
00370 void OTabWidget::slotTabListSelected( int index )
00371 {
00372     OTabInfo *newtab = m_tabs.at( index );
00373     if ( newtab )
00374         selectTab( newtab );
00375 }
00376 
00377 void OTabWidget::selectTab( OTabInfo *tab )
00378 {
00379     if ( m_tabBarStyle == IconTab )
00380     {
00381         // Remove text label from currently selected tab
00382         if ( m_currTab )
00383         {
00384             m_tabBar->tab( m_currTab->id() )->setText( QString::null );
00385             //setUpLayout();
00386         }
00387 
00388         // Set text label for newly selected tab
00389         m_tabBar->tab( tab->id() )->setText( tab->label() );
00390         m_tabBar->setCurrentTab( tab->id() );
00391 
00392         setUpLayout();
00393 
00394         QSize t;
00395 
00396         t = m_tabBar->sizeHint();
00397         if ( t.width() > width() )
00398             t.setWidth( width() );
00399         int lw = m_widgetStack->lineWidth();
00400         if ( m_tabBarPosition == Bottom )
00401             m_tabBar->setGeometry( QMAX(0, lw-2), height() - t.height() - lw, t.width(), t.height() );
00402         else
00403             m_tabBar->setGeometry( QMAX(0, lw-2), 0, t.width(), t.height() );
00404     }
00405     else if ( m_tabBarStyle == TextTab )
00406     {
00407         m_tabBar->setCurrentTab( tab->id() );
00408     }
00409 
00410     m_widgetStack->raiseWidget( tab->control() );
00411 
00412     emit currentChanged( tab->control() );
00413 
00414     m_currTab = tab;
00415 }
00416 
00417 void OTabWidget::setUpLayout()
00418 {
00419     if ( m_usingTabs )
00420     {
00421         m_tabBar->update();
00422         m_tabBar->layoutTabs();
00423     }
00424 }
00425 
00426 void OTabWidget::resizeEvent( QResizeEvent * )
00427 {
00428     QSize t;
00429 
00430     if ( m_usingTabs )
00431     {
00432         m_tabBar->layoutTabs();
00433         t = m_tabBar->sizeHint();
00434         if ( t.width() > width() )
00435             t.setWidth( width() );
00436     }
00437     else
00438     {
00439         t = m_tabList->sizeHint();
00440         t.setWidth( width() );
00441     }
00442 
00443     int lw = m_widgetStack->lineWidth();
00444     if ( m_tabBarPosition == Bottom )
00445     {
00446         if ( m_usingTabs )
00447             m_tabBar->setGeometry( QMAX(0, lw-2), height() - t.height() - lw, t.width(), t.height() );
00448         else
00449             m_tabList->setGeometry( QMAX(0, lw-2), height() - t.height() - lw, t.width(), t.height() );
00450 
00451         m_widgetStack->setGeometry( 0, 0, width(), height()-t.height()+QMAX(0, lw-2) );
00452     }
00453     else
00454     {
00455         if ( m_usingTabs )
00456             m_tabBar->setGeometry( QMAX(0, lw-2), 0, t.width(), t.height() );
00457         else
00458             m_tabList->setGeometry( QMAX(0, lw-2), 0, t.width(), t.height() );
00459 
00460         m_widgetStack->setGeometry( 0, t.height()-lw, width(), height()-t.height()+QMAX( 0, lw-2 ) );
00461     }
00462 
00463     if ( autoMask() )
00464         updateMask();
00465 }
00466 
00467 int OTabWidget::currentTab()
00468 {
00469     if ( m_currTab )
00470     {
00471         return m_currTab->id();
00472     }
00473     return -1;
00474 }
00475 
00476 QWidget* OTabWidget::currentWidget()const
00477 {
00478     if ( m_currTab )
00479     {
00480         return m_currTab->control();
00481     }
00482 
00483     return 0;
00484 }

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