author | drw <drw> | 2003-02-16 21:54:23 (UTC) |
---|---|---|
committer | drw <drw> | 2003-02-16 21:54:23 (UTC) |
commit | 8ecc375c803dc57b160bd0335891fcaf4f6de1df (patch) (side-by-side diff) | |
tree | a89c1ccc6e3a469bb8fa435dfedce9386333cb72 | |
parent | e0eb3f016d7f8a1e1e5548ef8aa115fef8999697 (diff) | |
download | opie-8ecc375c803dc57b160bd0335891fcaf4f6de1df.zip opie-8ecc375c803dc57b160bd0335891fcaf4f6de1df.tar.gz opie-8ecc375c803dc57b160bd0335891fcaf4f6de1df.tar.bz2 |
Added changeTab() to OTabWidget to dynamically change tab name and/or icon (stumbled upon request for this in TinyKate todo)
-rw-r--r-- | libopie/otabinfo.h | 16 | ||||
-rw-r--r-- | libopie/otabwidget.cpp | 48 | ||||
-rw-r--r-- | libopie/otabwidget.h | 10 |
3 files changed, 68 insertions, 6 deletions
diff --git a/libopie/otabinfo.h b/libopie/otabinfo.h index 8dbbcc2..6589638 100644 --- a/libopie/otabinfo.h +++ b/libopie/otabinfo.h @@ -44,73 +44,89 @@ class QWidget; * * OTabInfo provides the following information about a widget added to an * OTabWidget control: * * ID - integer tab bar ID * Control - QWidget pointer to child widget * Label - QString text label for OTabWidget selection control * Icon - QString name of icon file */ class OTabInfo { public: /** * @fn OTabInfo() * @brief Object constructor. * * @param parent Pointer to parent of this control. * @param name Name of control. * @param s Style of widget selection control. * @param p Position of the widget selection control. */ OTabInfo() : i( -1 ), c( 0 ), p( 0 ), l( QString::null ) {} /** * @fn OTabInfo( int id, QWidget *control, const QString &icon, const QString &label ) * @brief Object constructor. * * @param id TabBar identifier for widget. * @param control QWidget pointer to widget. * @param icon QString name of icon file. * @param label QString text label for OTabWidget selection control. */ OTabInfo( int id, QWidget *control, const QString &icon, const QString &label ) : i( id ), c( control ), p( icon ), l( label ) {} /** * @fn id() * @brief Returns TabBar ID. */ int id() const { return i; } /** * @fn label() * @brief Returns text label for widget. */ const QString &label() const { return l; } /** + * @fn setLabel( const QString &label ) + * @brief Set label for tab. + * + * @param label QString text label for OTabWidget selection control. + */ + void setLabel( const QString &label ) { l = label; } + +/** * @fn control() * @brief Returns pointer to widget. */ QWidget *control() const { return c; } /** * @fn icon() * @brief Returns name of icon file. */ const QString &icon() const { return p; } +/** + * @fn setIcon( const QString &icon ) + * @brief Set icon for tab. + * + * @param icon QString name of icon file. + */ + void setIcon( const QString &icon ) { p = icon; } + private: int i; QWidget *c; QString p; QString l; }; /** * @class OTabInfoList * @brief A list of OTabInfo objects used by OTabWidget. */ typedef QList<OTabInfo> OTabInfoList; #endif diff --git a/libopie/otabwidget.cpp b/libopie/otabwidget.cpp index bee3f35..99bf067 100644 --- a/libopie/otabwidget.cpp +++ b/libopie/otabwidget.cpp @@ -53,166 +53,202 @@ OTabWidget::OTabWidget( QWidget *parent, const char *name, TabStyle s, TabPositi QString pos = config.readEntry( "TabPosition", "Top"); if ( pos == "Bottom" ) { p = Bottom; } else { p = Top; } } widgetStack = new QWidgetStack( this, "widgetstack" ); widgetStack->setFrameStyle( QFrame::NoFrame ); widgetStack->setLineWidth( style().defaultFrameWidth() ); tabBarStack = new QWidgetStack( this, "tabbarstack" ); tabBar = new OTabBar( tabBarStack, "tabbar" ); tabBarStack->addWidget( tabBar, 0 ); connect( tabBar, SIGNAL( selected( int ) ), this, SLOT( slotTabBarSelected( int ) ) ); tabList = new QComboBox( false, tabBarStack, "tablist" ); tabBarStack->addWidget( tabList, 1 ); connect( tabList, SIGNAL( activated( int ) ), this, SLOT( slotTabListSelected( int ) ) ); setTabStyle( s ); setTabPosition( p ); currentTab= 0x0; } OTabWidget::~OTabWidget() { } void OTabWidget::addTab( QWidget *child, const QString &icon, const QString &label ) { QPixmap iconset = loadSmooth( icon ); QTab *tab = new QTab(); if ( tabBarStyle == IconTab ) { tab->label = QString::null; } else { tab->label = label; } - if ( tabBarStyle == IconTab || tabBarStyle == IconList) + if ( tabBarStyle == IconTab || tabBarStyle == IconList ) { tab->iconset = new QIconSet( iconset ); } int tabid = tabBar->addTab( tab ); if ( tabBarStyle == IconTab || tabBarStyle == IconList ) { tabList->insertItem( iconset, label, -1 ); } else { tabList->insertItem( label ); } widgetStack->addWidget( child, tabid ); widgetStack->raiseWidget( child ); widgetStack->setFrameStyle( QFrame::StyledPanel | QFrame::Raised ); OTabInfo *tabinfo = new OTabInfo( tabid, child, icon, label ); tabs.append( tabinfo ); selectTab( tabinfo ); } void OTabWidget::removePage( QWidget *childwidget ) { if ( childwidget ) { OTabInfo *tab = tabs.first(); while ( tab && tab->control() != childwidget ) { tab = tabs.next(); } if ( tab && tab->control() == childwidget ) { tabBar->setTabEnabled( tab->id(), FALSE ); tabBar->removeTab( tabBar->tab( tab->id() ) ); int i = 0; while ( i < tabList->count() && tabList->text( i ) != tab->label() ) { i++; } if ( tabList->text( i ) == tab->label() ) { tabList->removeItem( i ); } widgetStack->removeWidget( childwidget ); tabs.remove( tab ); delete tab; currentTab = tabs.current(); if ( !currentTab ) { widgetStack->setFrameStyle( QFrame::NoFrame ); } setUpLayout(); } } } +void OTabWidget::changeTab( QWidget *widget, const QString &iconset, const QString &label) +{ + OTabInfo *currtab = tabs.first(); + while ( currtab && currtab->control() != widget ) + { + currtab = tabs.next(); + } + if ( currtab && currtab->control() == widget ) + { + QTab *tab = tabBar->tab( currtab->id() ); + QPixmap icon( loadSmooth( iconset ) ); + tab->setText( label ); + if ( tabBarStyle == IconTab ) + tab->setIconSet( icon ); + int i = 0; + while ( i < tabList->count() && tabList->text( i ) != currtab->label() ) + { + i++; + } + if ( i < tabList->count() && tabList->text( i ) == currtab->label() ) + { + if ( tabBarStyle == IconTab || tabBarStyle == IconList ) + { + tabList->changeItem( icon, label, i ); + } + else + { + tabList->changeItem( label, i ); + } + } + currtab->setLabel( label ); + currtab->setIcon( iconset ); + } + setUpLayout(); +} + void OTabWidget::setCurrentTab( QWidget *childwidget ) { - OTabInfo *newtab = tabs.first(); - while ( newtab && newtab->control() != childwidget ) + OTabInfo *currtab = tabs.first(); + while ( currtab && currtab->control() != childwidget ) { - newtab = tabs.next(); + currtab = tabs.next(); } - if ( newtab && newtab->control() == childwidget ) + if ( currtab && currtab->control() == childwidget ) { - selectTab( newtab ); + selectTab( currtab ); } } void OTabWidget::setCurrentTab( const QString &tabname ) { OTabInfo *newtab = tabs.first(); while ( newtab && newtab->label() != tabname ) { newtab = tabs.next(); } if ( newtab && newtab->label() == tabname ) { selectTab( newtab ); } } void OTabWidget::setCurrentTab(int tabindex) { OTabInfo *newtab = tabs.first(); while ( newtab && newtab->id() != tabindex ) { newtab = tabs.next(); } if ( newtab && newtab->id() == tabindex ) { selectTab( newtab ); } } OTabWidget::TabStyle OTabWidget::tabStyle() const { return tabBarStyle; } void OTabWidget::setTabStyle( TabStyle s ) { tabBarStyle = s; if ( tabBarStyle == TextTab || tabBarStyle == IconTab ) { QTab *currtab; for ( OTabInfo *tabinfo = tabs.first(); tabinfo; tabinfo = tabs.next() ) { currtab = tabBar->tab( tabinfo->id() ); if ( tabBarStyle == IconTab ) { currtab->iconset = new QIconSet( loadSmooth( tabinfo->icon() ) ); if ( tabinfo == currentTab ) currtab->setText( tabinfo->label() ); diff --git a/libopie/otabwidget.h b/libopie/otabwidget.h index 74d683b..0aa9bb8 100644 --- a/libopie/otabwidget.h +++ b/libopie/otabwidget.h @@ -81,96 +81,106 @@ public: * @enum TabPosition * @brief Defines where the widget selection control is drawn. * * Valid values: * - Top: Widget selection control is drawn above widgets * - Bottom: Widget selection control is drawn below widgets */ enum TabPosition { Top, Bottom }; /** * @fn OTabWidget( QWidget *parent = 0, const char *name = 0, TabStyle s = Global, TabPosition p = Top ) * @brief Object constructor. * * @param parent Pointer to parent of this control. * @param name Name of control. * @param s Style of widget selection control. * @param p Position of the widget selection control. * * Constructs a new OTabWidget control with parent and name. The style and position parameters * determine how the widget selection control will be displayed. */ OTabWidget( QWidget * = 0, const char * = 0, TabStyle = Global, TabPosition = Top ); /** * @fn ~OTabWidget() * @brief Object destructor. */ ~OTabWidget(); /** * @fn addTab( QWidget *child, const QString &icon, const QString &label ) * @brief Add new widget to control. * * @param child Widget control. * @param icon Path to icon. * @param label Text label. */ void addTab( QWidget *, const QString &, const QString & ); /** * @fn removePage( QWidget *widget ) * @brief Remove widget from control. Does not delete widget. * * @param widget Widget control to be removed. */ void removePage( QWidget * ); /** + * @fn changeTab( QWidget *widget, const QIconSet &icon, const QString &label ) + * @brief Change text and/or icon for existing tab + * + * @param child Widget control. + * @param icon Path to icon. + * @param label Text label. + */ + void changeTab( QWidget *, const QString &, const QString & ); + +/** * @fn tabStyle() * @brief Returns current widget selection control style. */ TabStyle tabStyle() const; /** * @fn setTabStyle( TabStyle s ) * @brief Set the current widget selection control style. * * @param s New style to be used. */ void setTabStyle( TabStyle ); /** * @fn tabPosition() * @brief Returns current widget selection control position. */ TabPosition tabPosition() const; /** * @fn setTabPosition( TabPosition p ) * @brief Set the current widget selection control position. * * @param p New position of widget selection control. */ void setTabPosition( TabPosition ); /** * @fn setCurrentTab( QWidget *childwidget ) * @brief Selects and brings to top the desired widget by using widget pointer. * * @param childwidget Widget to select. */ void setCurrentTab( QWidget * ); /** * @fn setCurrentTab( const QString &tabname ) * @brief Selects and brings to top the desired widget, by using label. * * @param tabname Text label for widget to select. */ void setCurrentTab( const QString & ); /** * @fn setCurrentTab( int ) * @brief Selects and brings to top the desired widget, by using id. * * @param tab id for widget to select. |