Debian Codegraph @

src/librssguard/gui/tabbar.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/tabbar.h"
4
5 #include "definitions/definitions.h"
6 #include "definitions/globals.h"
7 #include "gui/reusable/plaintoolbutton.h"
8 #include "miscellaneous/settings.h"
9
10 #include <QMouseEvent>
11 #include <QStyle>
12
13 TabBar::TabBar(QWidget* parent) : QTabBar(parent) {
14 setDocumentMode(false);
15 setUsesScrollButtons(true);
16 setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
17 }
18
19 TabBar::~TabBar() {
20 qDebugNN << LOGSEC_GUI << "Destroying TabBar instance.";
21 }
22
23 void TabBar::setTabType(int index, TabBar::TabType type) {
24 const auto button_position =
25 static_cast<ButtonPosition>(style()->styleHint(QStyle::StyleHint::SH_TabBar_CloseButtonPosition, nullptr, this));
26
27 switch (type) {
28 case TabBar::TabType::DownloadManager:
29 case TabBar::TabType::Closable: {
30 auto* close_button = new PlainToolButton(this);
31
32 close_button->setIcon(qApp->icons()->fromTheme(QSL("application-exit")));
33 close_button->setToolTip(tr("Close this tab."));
34 close_button->setText(tr("Close tab"));
35 close_button->setFixedSize(iconSize());
36
37 // Close underlying tab when button is clicked.
38 connect(close_button, &PlainToolButton::clicked, this, &TabBar::closeTabViaButton);
39 setTabButton(index, button_position, close_button);
40 break;
41 }
42
43 default:
44 setTabButton(index, button_position, nullptr);
45 break;
46 }
47
48 setTabData(index, QVariant(int(type)));
49 }
50
51 void TabBar::closeTabViaButton() {
52 const auto* close_button = qobject_cast<QAbstractButton*>(sender());
53 const auto button_position =
54 static_cast<ButtonPosition>(style()->styleHint(QStyle::StyleHint::SH_TabBar_CloseButtonPosition, nullptr, this));
55
56 if (close_button != nullptr) {
57 // Find index of tab for this close button.
58 for (int i = 0; i < count(); i++) {
59 if (tabButton(i, button_position) == close_button) {
60 emit tabCloseRequested(i);
61
62 return;
63 }
64 }
65 }
66 }
67
68 void TabBar::wheelEvent(QWheelEvent* event) {
69 const int current_index = currentIndex();
70 const int number_of_tabs = count();
71
72 // Make sure rotating works.
73 if (number_of_tabs > 1) {
74 if (event->angleDelta().y() > 0) {
75 // Scroll to the LEFT tab.
76 setCurrentIndex(current_index == 0 ? number_of_tabs - 1 : current_index - 1);
77 }
78 else if (event->angleDelta().y() < 0) {
79 // Scroll to the RIGHT tab.
80 setCurrentIndex(current_index == number_of_tabs - 1 ? 0 : current_index + 1);
81 }
82 }
83 }
84
85 void TabBar::mousePressEvent(QMouseEvent* event) {
86 QTabBar::mousePressEvent(event);
87
88 const int tab_index = tabAt(event->pos());
89
90 // Check if user clicked on some tab or on empty space.
91 if (tab_index >= 0) {
92 // Check if user clicked tab with middle button.
93 // NOTE: This needs to be done here because
94 // destination does not know the original event.
95 if (Globals::hasFlag(event->button(), Qt::MouseButton::MiddleButton) &&
96 qApp->settings()->value(GROUP(GUI), SETTING(GUI::TabCloseMiddleClick)).toBool()) {
97 if (tabType(tab_index) == TabBar::TabType::Closable || tabType(tab_index) == TabBar::TabType::DownloadManager) {
98 // This tab is closable, so we can close it.
99 emit tabCloseRequested(tab_index);
100 }
101 }
102 }
103 }
104
105 void TabBar::mouseDoubleClickEvent(QMouseEvent* event) {
106 QTabBar::mouseDoubleClickEvent(event);
107 const int tab_index = tabAt(event->pos());
108
109 // Check if user clicked on some tab or on empty space.
110 if (tab_index >= 0) {
111 // Check if user clicked tab with middle button.
112 // NOTE: This needs to be done here because
113 // destination does not know the original event.
114 if (Globals::hasFlag(event->button(), Qt::MouseButton::LeftButton) &&
115 qApp->settings()->value(GROUP(GUI), SETTING(GUI::TabCloseDoubleClick)).toBool()) {
116 if (int(tabType(tab_index) & (TabBar::TabType::Closable | TabBar::TabType::DownloadManager)) > 0) {
117 // This tab is closable, so we can close it.
118 emit tabCloseRequested(tab_index);
119 }
120 }
121 }
122 else {
123 emit emptySpaceDoubleClicked();
124 }
125 }
126
127 TabBar::TabType& operator&=(TabBar::TabType& a, TabBar::TabType b) {
128 return (TabBar::TabType&)((int&)a &= (int)b);
129 }
130
131 TabBar::TabType& operator|=(TabBar::TabType& a, TabBar::TabType b) {
132 return (TabBar::TabType&)((int&)a |= (int)b);
133 }
134
135 TabBar::TabType operator&(TabBar::TabType a, TabBar::TabType b) {
136 return (TabBar::TabType)((int)a & (int)b);
137 }
138
139 TabBar::TabType operator|(TabBar::TabType a, TabBar::TabType b) {
140 return (TabBar::TabType)((int)a | (int)b);
141 }