]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabbar.cpp
dolphintabbar: only open tab on double left click
[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 if (event->buttons() & Qt::LeftButton) {
107 int index = tabAt(event->pos());
108
109 if (index < 0) {
110 // empty tabbar area case
111 index = currentIndex();
112 }
113 // Double left click on the tabbar opens a new activated tab
114 // with the url from the doubleclicked tab or currentTab otherwise.
115 Q_EMIT openNewActivatedTab(index);
116 }
117
118 QTabBar::mouseDoubleClickEvent(event);
119 }
120
121 void DolphinTabBar::contextMenuEvent(QContextMenuEvent *event)
122 {
123 const int index = tabAt(event->pos());
124
125 if (index >= 0) {
126 // Tab context menu
127 QMenu menu(this);
128
129 QAction *newTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
130 QAction *detachTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
131 QAction *closeOtherTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
132 QAction *closeTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
133
134 QAction *selectedAction = menu.exec(event->globalPos());
135 if (selectedAction == newTabAction) {
136 Q_EMIT openNewActivatedTab(index);
137 } else if (selectedAction == detachTabAction) {
138 Q_EMIT tabDetachRequested(index);
139 } else if (selectedAction == closeOtherTabsAction) {
140 const int tabCount = count();
141 for (int i = 0; i < index; i++) {
142 Q_EMIT tabCloseRequested(0);
143 }
144 for (int i = index + 1; i < tabCount; i++) {
145 Q_EMIT tabCloseRequested(1);
146 }
147 } else if (selectedAction == closeTabAction) {
148 Q_EMIT tabCloseRequested(index);
149 }
150
151 return;
152 }
153
154 QTabBar::contextMenuEvent(event);
155 }
156
157 void DolphinTabBar::slotAutoActivationTimeout()
158 {
159 if (m_autoActivationIndex >= 0) {
160 setCurrentIndex(m_autoActivationIndex);
161 updateAutoActivationTimer(-1);
162 }
163 }
164
165 void DolphinTabBar::updateAutoActivationTimer(const int index)
166 {
167 if (m_autoActivationIndex != index) {
168 m_autoActivationIndex = index;
169
170 if (m_autoActivationIndex < 0) {
171 m_autoActivationTimer->stop();
172 } else {
173 m_autoActivationTimer->start();
174 }
175 }
176 }
177
178 #include "moc_dolphintabbar.cpp"