//============================================================================= // // File : class_tabwidget.cpp // Creation date : Fri Mar 18 14:30:48 CEST 2005 // by Tonino Imbesi(Grifisx) and Alessandro Carbone(Noldor) // // This file is part of the KVirc irc client distribution // Copyright (C) 2005-2008 Alessandro Carbone (elfonol at gmail dot com) // // This program is FREE software. You can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your opinion) any later version. // // This program is distributed in the HOPE that it will be USEFUL, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // See the GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, write to the Free Software Foundation, // Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // //============================================================================= #include "class_tabwidget.h" #include "kvi_error.h" #include "kvi_debug.h" #include "kvi_locale.h" #include "kvi_iconmanager.h" #include /* @doc: tabwidget @keyterms: tabwidget object class, @title: tabwidget class @type: class @short: Provides a Tab Widget. @inherits: [class]object[/class] [class]widget[/class] @description: The tabwidget class provides a stack of tabbed widgets.[br] Each tab is associated with a different widget (called a `page').[br] Only the current tab's page is shown in the page area; all the others tabs pages are hidden.[br] The user can show a different page by clicking on its tab.[br] @functions: !fn: $addTab(,,[]) Adds another page to the tab view with the text label and optional icon. !fn: $insertTab(,,) Inserts another tab and page to the tab view with name and label-text specified.[br] If index is not specified, the tab is simply appended.[br] Otherwise the new tab is inserted at the specified position. !fn: $setTabToolTip(,) Sets the tab tool tip for the tabwidget to . !fn: $removeTabToolTip() Removes the tab tool tip for the tab. If the page does not have a tip, nothing happens. !fn: $setTabLabel(, ) Sets the tab label for to label. !fn: $changeTab(,,[]) Defines a new label, and optional icon, for the tab . !fn: $setCurrentPage() Sets the index position of the current tab page to . !fn: $currentPageIndex() Returns the current page index. !fn: $label() Retunrs the label to the . !fn: $currentTabLabel() Returns the label of the current tab. !fn: $removePage() Remove the page . !fn: $setTabPosition() Sets TabPosition. Valid parameters are : Top, Bottom. !fn: $count() This property holds the number of tabs in the tab bar. !fn: $currentChangedEvent() This function is invoched when the current tab was changed.[br] The default implementation emits the [classfnc]$currentChange[/classfnc]() signal. @examples: [example] # First we'll create the main tabwidget. %Tabwidget=$new(tabwidget) %Tabwidget->$setToolTip("Example of TabWidget class") %Tabwidget->$setMargin(30) %Tabwidget->$setTabPosition(Top) # Now we'll create the new widgets and put they in to the main tabwidget. %firsttab=$new(widget,%Tabwidget) %secondtab=$new(widget,%Tabwidget) # Now we'll create the item to put in to tab's pages. %layoutfirsttab=$new(layout,%firsttab) %labelbt=$new(label,%firsttab) %labelbt->$settext(Botton Tab) %labeltt=$new(label,%firsttab) %labeltt->$settext(Top Tab) %buttontb=$new(button,%firsttab) %buttontb->$settext("To &Botton") %buttontt=$new(button,%firsttab) %buttontt->$settext("To &Top") # Now we'll give a layout to all items. # This also allows to use privateimpl without making buttons global variables %layoutfirsttab->$addwidget(%labelbt,0,0) %layoutfirsttab->$addwidget(%labeltt,0,1) %layoutfirsttab->$addwidget(%buttontb,1,0) %layoutfirsttab->$addwidget(%buttontt,1,1) # Add the page to the main tab. %Tabwidget->$addTab(%firsttab,Top&Button,33) # Implementing the action to do when the user click the buttons. privateimpl(%buttontb,mousepressevent) { %Tabwidget->$setTabPosition(Bottom) } privateimpl(%buttontt,mousepressevent) { %Tabwidget->$setTabPosition(Top) } # We do the same work with the second tab's page. %layoutsecondtab=$new(layout,%secondtab) %labelwp=$new(label,%secondtab) %labelwp->$settext("Enjoy the new Class provided by") %layoutsecondtab->$addwidget(%labelwp,0,0) %labelwp->$setalignment("Center") %labelgen=$new(label,%secondtab) %labelgen->$settext(Grifisx \& Noldor) %labelgen->$setalignment("Center") %layoutsecondtab->$addwidget(%labelgen,1,0) %Tabwidget->$addTab(%secondtab,&About,50) # Let's show our example. %Tabwidget->$show() [/example] @signals: !sg: $currentChange() This signal is emitted by the default implementation of [classfnc]$currentChangedEvent[/classfnc]().[br] If you reimplement that function you will have to emit the signal manually (if you still need it).[br] */ KVSO_BEGIN_REGISTERCLASS(KviKvsObject_tabwidget,"tabwidget","widget") KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"addTab", functionaddTab) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"insertTab", functioninsertTab) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"setTabToolTip", functionsetTabToolTip) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"removeTabToolTip", functionremoveTabToolTip) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"setTabLabel", functionsetTabLabel) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"changeTab", functionchangeTab) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"setCurrentPage", functionsetCurrentPage) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"currentPageIndex", functioncurrentPageIndex) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"label", functiontabLabel) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"currentTabLabel", functioncurrentTabLabel) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"count", functioncount) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"removePage", functionremovePage) KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"setTabPosition", functionsetTabPosition) // events KVSO_REGISTER_HANDLER(KviKvsObject_tabwidget,"currentChangedEvent", functionCurrentChangedEvent) KVSO_END_REGISTERCLASS(KviKvsObject_tabwidget) KVSO_BEGIN_CONSTRUCTOR(KviKvsObject_tabwidget,KviKvsObject_widget) KVSO_END_CONSTRUCTOR(KviKvsObject_tabwidget) KVSO_BEGIN_DESTRUCTOR(KviKvsObject_tabwidget) KVSO_END_CONSTRUCTOR(KviKvsObject_tabwidget) bool KviKvsObject_tabwidget::init(KviKvsRunTimeContext * pContext,KviKvsVariantList *pParams) { SET_OBJECT(QTabWidget) connect(widget(),SIGNAL(currentChanged(int)),this,SLOT(slotcurrentChanged(int))); return true; } bool KviKvsObject_tabwidget::functionaddTab(KviKvsObjectFunctionCall *c) { KviKvsObject *ob; QString szLabel,szIcon; kvs_hobject_t hObject; KVSO_PARAMETERS_BEGIN(c) KVSO_PARAMETER("widget",KVS_PT_HOBJECT,0,hObject) KVSO_PARAMETER("label",KVS_PT_STRING,0,szLabel) KVSO_PARAMETER("icon_id",KVS_PT_STRING,KVS_PF_OPTIONAL,szIcon) KVSO_PARAMETERS_END(c) ob=KviKvsKernel::instance()->objectController()->lookupObject(hObject); if(!widget())return true; if (!ob) { c->warning(__tr2qs("Widget parameter is not an object")); return true; } if (!ob->object()) { c->warning(__tr2qs("Widget parameter is not a valid object")); return true; } if(!ob->object()->isWidgetType()) { c->warning(__tr2qs("Can't add a non-widget object")); return true; } QPixmap * pix = g_pIconManager->getImage(szIcon); if(pix){ ((QTabWidget *)widget())->addTab(((QWidget *)(ob->object())),QIcon(*pix),szLabel); } else((QTabWidget *)widget())->addTab(((QWidget *)(ob->object())),szLabel); return true; } bool KviKvsObject_tabwidget::functioninsertTab(KviKvsObjectFunctionCall *c) { KviKvsObject *ob; QString szLabel,szIcon; kvs_int_t iIndex; kvs_hobject_t hObject; KVSO_PARAMETERS_BEGIN(c) KVSO_PARAMETER("tab_widget",KVS_PT_HOBJECT,0,hObject) KVSO_PARAMETER("label",KVS_PT_STRING,0,szLabel) KVSO_PARAMETER("index",KVS_PT_UNSIGNEDINTEGER,0,iIndex) KVSO_PARAMETER("icon_id",KVS_PT_STRING,KVS_PF_OPTIONAL,szIcon) KVSO_PARAMETERS_END(c) ob=KviKvsKernel::instance()->objectController()->lookupObject(hObject); if(!widget())return true; if (!ob) { c->warning(__tr2qs("Widget parameter is not an object")); return true; } if (!ob->object()) { c->warning(__tr2qs("Widget parameter is not a valid object")); return true; } if(!ob->object()->isWidgetType()) { c->warning(__tr2qs("Can't add a non-widget object")); return true; } QPixmap * pix = g_pIconManager->getImage(szIcon); if(pix){ ((QTabWidget *)widget())->insertTab(iIndex,(QWidget *)(ob->object()),QIcon(*pix),szLabel); } else ((QTabWidget *)widget())->insertTab(iIndex,((QWidget *)(ob->object())),szLabel); return true; } bool KviKvsObject_tabwidget::functionsetCurrentPage(KviKvsObjectFunctionCall *c) { kvs_int_t iIndex; KVSO_PARAMETERS_BEGIN(c) KVSO_PARAMETER("index",KVS_PT_UNSIGNEDINTEGER,0,iIndex) KVSO_PARAMETERS_END(c) if (widget()) ((QTabWidget *)widget())->setCurrentIndex(iIndex); return true; } bool KviKvsObject_tabwidget::functionsetTabToolTip(KviKvsObjectFunctionCall *c) { KviKvsObject *ob; QString szTooltip; kvs_hobject_t hObject; KVSO_PARAMETERS_BEGIN(c) KVSO_PARAMETER("tab_widget",KVS_PT_HOBJECT,0,hObject) KVSO_PARAMETER("tooltip",KVS_PT_STRING,0,szTooltip) KVSO_PARAMETERS_END(c) ob=KviKvsKernel::instance()->objectController()->lookupObject(hObject); if(!widget())return true; if (!ob) { c->warning(__tr2qs("Widget parameter is not an object")); return true; } if (!ob->object()) { c->warning(__tr2qs("Widget parameter is not a valid object")); return true; } if(!ob->object()->isWidgetType()) { c->warning(__tr2qs("Not a widget object")); return true; } int ctrl = ((QTabWidget *)widget())->indexOf (((QWidget *)(ob->object()))); if (ctrl == -1) { c->warning(__tr2qs("Can't find the tab ")); return true; } ((QTabWidget *)widget())->setTabToolTip(ctrl,szTooltip); return true; } bool KviKvsObject_tabwidget::functionremoveTabToolTip(KviKvsObjectFunctionCall *c) { KviKvsObject *ob; kvs_hobject_t hObject; KVSO_PARAMETERS_BEGIN(c) KVSO_PARAMETER("tab_widget",KVS_PT_HOBJECT,0,hObject) KVSO_PARAMETERS_END(c) ob=KviKvsKernel::instance()->objectController()->lookupObject(hObject); if (!ob) { c->warning(__tr2qs("Widget parameter is not an object")); return true; } if (!ob->object()) { c->warning(__tr2qs("Widget parameter is not a valid object")); return true; } if(!widget()) return true; if(!ob->object()->isWidgetType()) { c->warning(__tr2qs("Not a widget object")); return true; } int ctrl = ((QTabWidget *)widget())->indexOf (((QWidget *)(ob->object()))) ; if (ctrl==-1) { c->warning(__tr2qs("Can't find the tab ")); return true; } ((QTabWidget *)widget())->setTabToolTip(ctrl,QString()); return true; } bool KviKvsObject_tabwidget::functionsetTabLabel(KviKvsObjectFunctionCall *c) { KviKvsObject *ob; QString szLabel; kvs_hobject_t hObject; KVSO_PARAMETERS_BEGIN(c) KVSO_PARAMETER("tab_widget",KVS_PT_HOBJECT,0, hObject) KVSO_PARAMETER("tooltip",KVS_PT_STRING,0,szLabel) KVSO_PARAMETERS_END(c) ob=KviKvsKernel::instance()->objectController()->lookupObject(hObject); if (!ob) { c->warning(__tr2qs("Widget parameter is not an object")); return true; } if (!ob->object()) { c->warning(__tr2qs("Widget parameter is not a valid object")); return true; } if(!widget())return true; if(!ob->object()->isWidgetType()) { c->warning(__tr2qs("Not a widget object")); return true; } int ctrl = ((QTabWidget *)widget())->indexOf (((QWidget *)(ob->object()))); if (ctrl == -1) { c->warning(__tr2qs("Can't find the tab ")); return true; } ((QTabWidget *)widget())->setTabText(ctrl,szLabel); return true; } bool KviKvsObject_tabwidget::functioncurrentPageIndex(KviKvsObjectFunctionCall *c) { int index=((QTabWidget *)widget())->currentIndex(); if (widget()) c->returnValue()->setInteger(index); return true; } bool KviKvsObject_tabwidget::functiontabLabel(KviKvsObjectFunctionCall *c) { kvs_uint_t uIndex; KVSO_PARAMETERS_BEGIN(c) KVSO_PARAMETER("index",KVS_PT_INT,0,uIndex) KVSO_PARAMETERS_END(c) if (!widget()) return true; QString label=((QTabWidget *)widget())->tabText(uIndex); c->returnValue()->setString(label); return true; } bool KviKvsObject_tabwidget::functioncount(KviKvsObjectFunctionCall *c) { if (!widget()) return true; int count = ((QTabWidget *)widget())->count(); c->returnValue()->setInteger(count); return true; } bool KviKvsObject_tabwidget::functioncurrentTabLabel(KviKvsObjectFunctionCall *c) { if (widget()) { int i= ((QTabWidget *)widget())->currentIndex(); QString label=((QTabWidget *)widget())->tabText(i); c->returnValue()->setString(label); } return true; } bool KviKvsObject_tabwidget::functionremovePage(KviKvsObjectFunctionCall *c) { KviKvsObject *ob; kvs_hobject_t hObject; KVSO_PARAMETERS_BEGIN(c) KVSO_PARAMETER("tab_widget",KVS_PT_HOBJECT,0,hObject) KVSO_PARAMETERS_END(c) ob=KviKvsKernel::instance()->objectController()->lookupObject(hObject); if(!widget())return true; if (!ob) { c->warning(__tr2qs("Widget parameter is not an object")); return true; } if (!ob->object()) { c->warning(__tr2qs("Widget parameter is not a valid object")); return true; } if(!ob->object()->isWidgetType()) { c->warning(__tr2qs("Not a widget object")); return true; } int ctrl = ((QTabWidget *)widget())->indexOf (((QWidget *)(ob->object()))); if (ctrl == -1) { c->warning(__tr2qs("Can't find the tab ")); return true; } ((QTabWidget *)widget())->removeTab(ctrl); return true; } bool KviKvsObject_tabwidget::functionchangeTab(KviKvsObjectFunctionCall *c) { KviKvsObject *ob; QString szLabel,szIcon; kvs_hobject_t hObject; KVSO_PARAMETERS_BEGIN(c) KVSO_PARAMETER("tab_widget",KVS_PT_HOBJECT,0,hObject) KVSO_PARAMETER("label",KVS_PT_STRING,0,szLabel) KVSO_PARAMETER("icon_id",KVS_PT_STRING,KVS_PF_OPTIONAL,szIcon) KVSO_PARAMETERS_END(c) ob=KviKvsKernel::instance()->objectController()->lookupObject(hObject); if (!ob) { c->warning(__tr2qs("Widget parameter is not an object")); return true; } if (!ob->object()) { c->warning(__tr2qs("Widget parameter is not a valid object")); return true; } if(!widget())return true; if(!ob->object()->isWidgetType()) { c->warning(__tr2qs("Not a widget object")); return true; } int ctrl = ((QTabWidget *)widget())->indexOf (((QWidget *)(ob->object()))); if (ctrl == -1) { c->warning(__tr2qs("Can't find the tab ")); return true; } QPixmap * pix = g_pIconManager->getImage(szIcon); if(pix)((QTabWidget *)widget())->setTabIcon(ctrl,QIcon(*pix)); ((QTabWidget *)widget())->setTabText(ctrl,szLabel); return true; } bool KviKvsObject_tabwidget::functionsetTabPosition(KviKvsObjectFunctionCall *c) { QString szPos; KVSO_PARAMETERS_BEGIN(c) KVSO_PARAMETER("tab_position",KVS_PT_STRING,0,szPos) KVSO_PARAMETERS_END(c) if(!widget())return true; if(KviQString::equalCI(szPos,"Top")) ((QTabWidget *)widget())->setTabPosition(QTabWidget::North); else if(KviQString::equalCI(szPos,"Bottom")) ((QTabWidget *)widget())->setTabPosition(QTabWidget::South); else c->warning( __tr2qs("Unknown position 'Q%'"),&szPos); return true; } bool KviKvsObject_tabwidget::functionCurrentChangedEvent(KviKvsObjectFunctionCall *c) { emitSignal("valueChanged",c,c->params()); return true; } void KviKvsObject_tabwidget::slotCurrentChanged(int value) { KviKvsVariantList params(new KviKvsVariant((kvs_int_t)value)); callFunction(this,"currentChangedEvent",¶ms); } #ifndef COMPILE_USE_STANDALONE_MOC_SOURCES #include "m_class_tabwidget.moc" #endif //!COMPILE_USE_STANDALONE_MOC_SOURCES