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