]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabbar.cpp
Merge branch 'master' into kf6
[dolphin.git] / src / dolphintabbar.cpp
1 /*
2 * SPDX-FileCopyrightText: 2014 Emmanuel Pescosta <emmanuelpescosta099@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "dolphintabbar.h"
8
9 #include <KLocalizedString>
10
11 #include <QDragEnterEvent>
12 #include <QMenu>
13 #include <QMimeData>
14 #include <QTimer>
15
16 DolphinTabBar::DolphinTabBar(QWidget *parent)
17 : QTabBar(parent)
18 , m_autoActivationIndex(-1)
19 , m_tabToBeClosedOnMiddleMouseButtonRelease(-1)
20 {
21 setAcceptDrops(true);
22 setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
23 setMovable(true);
24 setTabsClosable(true);
25
26 m_autoActivationTimer = new QTimer(this);
27 m_autoActivationTimer->setSingleShot(true);
28 m_autoActivationTimer->setInterval(800);
29 connect(m_autoActivationTimer, &QTimer::timeout, this, &DolphinTabBar::slotAutoActivationTimeout);
30 }
31
32 void DolphinTabBar::dragEnterEvent(QDragEnterEvent *event)
33 {
34 const QMimeData *mimeData = event->mimeData();
35 const int index = tabAt(event->position().toPoint());
36
37 if (mimeData->hasUrls()) {
38 event->acceptProposedAction();
39 updateAutoActivationTimer(index);
40 }
41
42 QTabBar::dragEnterEvent(event);
43 }
44
45 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent *event)
46 {
47 updateAutoActivationTimer(-1);
48
49 QTabBar::dragLeaveEvent(event);
50 }
51
52 void DolphinTabBar::dragMoveEvent(QDragMoveEvent *event)
53 {
54 const QMimeData *mimeData = event->mimeData();
55 const int index = tabAt(event->position().toPoint());
56
57 if (mimeData->hasUrls()) {
58 updateAutoActivationTimer(index);
59 }
60
61 QTabBar::dragMoveEvent(event);
62 }
63
64 void DolphinTabBar::dropEvent(QDropEvent *event)
65 {
66 // Disable the auto activation timer
67 updateAutoActivationTimer(-1);
68
69 const QMimeData *mimeData = event->mimeData();
70 const int index = tabAt(event->position().toPoint());
71
72 if (mimeData->hasUrls()) {
73 Q_EMIT tabDropEvent(index, event);
74 }
75
76 QTabBar::dropEvent(event);
77 }
78
79 void DolphinTabBar::mousePressEvent(QMouseEvent *event)
80 {
81 const int index = tabAt(event->pos());
82
83 if (index >= 0 && event->button() == Qt::MiddleButton) {
84 m_tabToBeClosedOnMiddleMouseButtonRelease = index;
85 return;
86 }
87
88 QTabBar::mousePressEvent(event);
89 }
90
91 void DolphinTabBar::mouseReleaseEvent(QMouseEvent *event)
92 {
93 const int index = tabAt(event->pos());
94
95 if (index >= 0 && index == m_tabToBeClosedOnMiddleMouseButtonRelease && event->button() == Qt::MiddleButton) {
96 // Mouse middle click on a tab closes this tab.
97 Q_EMIT tabCloseRequested(index);
98 return;
99 }
100
101 QTabBar::mouseReleaseEvent(event);
102 }
103
104 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent *event)
105 {
106 int index = tabAt(event->pos());
107
108 if (index < 0) {
109 // empty tabbar area case
110 index = currentIndex();
111 }
112 // Double click on the tabbar opens a new activated tab
113 // with the url from the doubleclicked tab or currentTab otherwise.
114 Q_EMIT openNewActivatedTab(index);
115
116 QTabBar::mouseDoubleClickEvent(event);
117 }
118
119 void DolphinTabBar::contextMenuEvent(QContextMenuEvent *event)
120 {
121 const int index = tabAt(event->pos());
122
123 if (index >= 0) {
124 // Tab context menu
125 QMenu menu(this);
126
127 QAction *newTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
128 QAction *detachTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
129 QAction *closeOtherTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
130 QAction *closeTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
131
132 QAction *selectedAction = menu.exec(event->globalPos());
133 if (selectedAction == newTabAction) {
134 Q_EMIT openNewActivatedTab(index);
135 } else if (selectedAction == detachTabAction) {
136 Q_EMIT tabDetachRequested(index);
137 } else if (selectedAction == closeOtherTabsAction) {
138 const int tabCount = count();
139 for (int i = 0; i < index; i++) {
140 Q_EMIT tabCloseRequested(0);
141 }
142 for (int i = index + 1; i < tabCount; i++) {
143 Q_EMIT tabCloseRequested(1);
144 }
145 } else if (selectedAction == closeTabAction) {
146 Q_EMIT tabCloseRequested(index);
147 }
148
149 return;
150 }
151
152 QTabBar::contextMenuEvent(event);
153 }
154
155 void DolphinTabBar::slotAutoActivationTimeout()
156 {
157 if (m_autoActivationIndex >= 0) {
158 setCurrentIndex(m_autoActivationIndex);
159 updateAutoActivationTimer(-1);
160 }
161 }
162
163 void DolphinTabBar::updateAutoActivationTimer(const int index)
164 {
165 if (m_autoActivationIndex != index) {
166 m_autoActivationIndex = index;
167
168 if (m_autoActivationIndex < 0) {
169 m_autoActivationTimer->stop();
170 } else {
171 m_autoActivationTimer->start();
172 }
173 }
174 }
175
176 #include "moc_dolphintabbar.cpp"