From a455d213569903394a6b46f2e85a62c4cdaf284d Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Sun, 19 Jan 2014 12:22:50 +0100 Subject: [PATCH] Added close buttons to "closable" tabs. --- src/gui/tabbar.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/gui/tabbar.h | 15 ++++++++++----- src/gui/tabwidget.cpp | 4 ++-- src/main.cpp | 3 +++ 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/gui/tabbar.cpp b/src/gui/tabbar.cpp index e91de5aff..24de0e670 100644 --- a/src/gui/tabbar.cpp +++ b/src/gui/tabbar.cpp @@ -16,6 +16,49 @@ TabBar::~TabBar() { qDebug("Destroying TabBar instance."); } +void TabBar::setTabType(int index, const TabBar::TabType &type) { + switch (type) { + case TabBar::Closable: { + QToolButton *close_button = new QToolButton(); + + close_button->setIcon(IconThemeFactory::instance()->fromTheme("application-exit")); + close_button->setToolTip(tr("Close this tab.")); + close_button->setText(tr("Close tab")); + close_button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + close_button->setFixedSize(iconSize()); + + connect(close_button, SIGNAL(clicked()), + this, SLOT(closeTabViaButton())); + + setTabButton(index, QTabBar::RightSide, close_button); + break; + } + + case TabBar::NonClosable: + case TabBar::FeedReader: + default: + setTabButton(index, QTabBar::RightSide, 0); + break; + } + + setTabData(index, QVariant(type)); +} + +void TabBar::closeTabViaButton() { + QToolButton *close_button = qobject_cast(sender()); + + if (close_button != NULL) { + // Find index of tab for this close button. + for (int i = 0; i < count(); i++) { + if (tabButton(i, QTabBar::RightSide) == close_button) { + emit tabCloseRequested(i); + + return; + } + } + } +} + void TabBar::wheelEvent(QWheelEvent *event) { int current_index = currentIndex(); int number_of_tabs = count(); diff --git a/src/gui/tabbar.h b/src/gui/tabbar.h index ee7bfde01..beb7176bd 100644 --- a/src/gui/tabbar.h +++ b/src/gui/tabbar.h @@ -1,8 +1,11 @@ #ifndef TABBAR_H #define TABBAR_H +#include "gui/iconthemefactory.h" + #include #include +#include class TabBar : public QTabBar { @@ -20,14 +23,16 @@ class TabBar : public QTabBar { virtual ~TabBar(); // Getter/setter for tab type. - inline void setTabType(int index, const TabBar::TabType &type) { - setTabData(index, QVariant(type)); - } + void setTabType(int index, const TabBar::TabType &type); inline TabBar::TabType tabType(int index) { return static_cast(tabData(index).value()); } - + + protected slots: + // Called when user selects to close tab via close button. + void closeTabViaButton(); + protected: // Reimplementations. void mouseDoubleClickEvent(QMouseEvent *event); @@ -36,7 +41,7 @@ class TabBar : public QTabBar { signals: // Emmited if empty space on tab bar is double clicked. - void emptySpaceDoubleClicked(); + void emptySpaceDoubleClicked(); }; #endif // TABBAR_H diff --git a/src/gui/tabwidget.cpp b/src/gui/tabwidget.cpp index cae2905a4..73b490cc0 100644 --- a/src/gui/tabwidget.cpp +++ b/src/gui/tabwidget.cpp @@ -16,6 +16,8 @@ TabWidget::TabWidget(QWidget *parent) : QTabWidget(parent) { setTabBar(new TabBar(this)); setupCornerButton(); + //setTabsClosable(true); + createConnections(); } @@ -52,8 +54,6 @@ void TabWidget::createConnections() { connect(tabBar(), SIGNAL(currentChanged(int)), this, SLOT(fixContentAfterIndexChange(int))); } - - void TabWidget::initializeTabs() { // Create widget for "Feeds" page and add it. m_feedMessageViewer = new FeedMessageViewer(this); diff --git a/src/main.cpp b/src/main.cpp index d1ca9da62..91129131c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,6 +23,9 @@ #include +// TODO: lepsi paint bez framu balloontip +// http://windowdetective.sourceforge.net/files/Window%20Detective/src/ui/custom_widgets/BalloonTip.cpp + // TODO: Check if extra UNIX signalling is needed. // Use header for it - signal function and catch SIGHUP // void my_terminate (int param) {