rssguard/src/gui/tabbar.cpp

138 lines
4.5 KiB
C++
Raw Normal View History

2013-12-21 21:08:52 +01:00
#include "gui/tabbar.h"
2013-07-30 18:54:36 +02:00
#include "core/defs.h"
#include "core/settings.h"
#include "gui/closebutton.h"
2013-12-21 21:08:52 +01:00
#include <QMouseEvent>
#include <QStyle>
2013-07-30 18:54:36 +02:00
TabBar::TabBar(QWidget *parent) : QTabBar(parent) {
setDocumentMode(true);
setUsesScrollButtons(true);
setContextMenuPolicy(Qt::CustomContextMenu);
}
TabBar::~TabBar() {
2013-10-15 21:10:54 +02:00
qDebug("Destroying TabBar instance.");
2013-07-30 18:54:36 +02:00
}
void TabBar::setTabType(int index, const TabBar::TabType &type) {
switch (type) {
case TabBar::Closable: {
CloseButton *close_button = new CloseButton(this);
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());
2014-01-19 12:51:32 +01:00
// Close underlying tab when button is clicked.
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() {
CloseButton *close_button = qobject_cast<CloseButton*>(sender());
2014-01-19 12:51:32 +01:00
QTabBar::ButtonPosition button_position = static_cast<ButtonPosition>(style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition,
0,
this));
if (close_button != NULL) {
// Find index of tab for this close button.
for (int i = 0; i < count(); i++) {
if (tabButton(i, button_position) == close_button) {
emit tabCloseRequested(i);
return;
}
}
}
}
2013-12-11 14:07:18 +01:00
void TabBar::wheelEvent(QWheelEvent *event) {
int current_index = currentIndex();
int number_of_tabs = count();
// Make sure rotating works.
if (number_of_tabs > 1) {
2013-12-11 18:54:09 +01:00
if (event->delta() > 0) {
// Scroll to the LEFT tab.
setCurrentIndex(current_index == 0 ?
number_of_tabs - 1 :
current_index - 1);
2013-12-11 14:07:18 +01:00
}
2013-12-11 18:54:09 +01:00
else if (event->delta() < 0) {
// Scroll to the RIGHT tab.
setCurrentIndex(current_index == number_of_tabs - 1 ?
0 :
current_index + 1);
2013-12-11 14:07:18 +01:00
}
}
}
2013-07-30 18:54:36 +02:00
void TabBar::mousePressEvent(QMouseEvent *event) {
QTabBar::mousePressEvent(event);
int tab_index = tabAt(event->pos());
// Check if user clicked on some tab or on empty space.
if (tab_index >= 0) {
// Check if user clicked tab with middle button.
// NOTE: This needs to be done here because
// destination does not know the original event.
2014-01-10 09:18:29 +01:00
if (event->button() & Qt::MiddleButton && Settings::instance()->value(APP_CFG_GUI,
"tab_close_mid_button",
true).toBool()) {
2013-07-30 18:54:36 +02:00
if (tabType(tab_index) == TabBar::Closable) {
// This tab is closable, so we can close it.
emit tabCloseRequested(tab_index);
}
}
}
}
void TabBar::mouseDoubleClickEvent(QMouseEvent *event) {
QTabBar::mouseDoubleClickEvent(event);
int tab_index = tabAt(event->pos());
// Check if user clicked on some tab or on empty space.
if (tab_index >= 0) {
// Check if user clicked tab with middle button.
// NOTE: This needs to be done here because
// destination does not know the original event.
2014-01-10 09:18:29 +01:00
if (event->button() & Qt::LeftButton && Settings::instance()->value(APP_CFG_GUI,
"tab_close_double_button",
true).toBool()) {
2013-07-30 18:54:36 +02:00
if (tabType(tab_index) == TabBar::Closable) {
// This tab is closable, so we can close it.
emit tabCloseRequested(tab_index);
}
}
}
// Check if new tab should be opened with initial web browser.
// NOTE: This check could be unnecesary here and should be done in
// destination object but we keep it here for consistency.
2014-01-10 09:18:29 +01:00
else if (Settings::instance()->value(APP_CFG_GUI,
"tab_new_double_button",
true).toBool()) {
2013-07-30 18:54:36 +02:00
emit emptySpaceDoubleClicked();
}
}