]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabbar.cpp
Add margins to the zoom menu entry
[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 <QInputDialog>
13 #include <QMenu>
14 #include <QMimeData>
15 #include <QTimer>
16
17 class PreventFocusWhileHidden : public QObject
18 {
19 public:
20 PreventFocusWhileHidden(QObject *parent)
21 : QObject(parent){};
22
23 protected:
24 bool eventFilter(QObject *obj, QEvent *ev) override
25 {
26 switch (ev->type()) {
27 case QEvent::Hide:
28 static_cast<QWidget *>(obj)->setFocusPolicy(Qt::NoFocus);
29 return false;
30 case QEvent::Show:
31 static_cast<QWidget *>(obj)->setFocusPolicy(Qt::TabFocus);
32 return false;
33 default:
34 return false;
35 }
36 };
37 };
38
39 DolphinTabBar::DolphinTabBar(QWidget *parent)
40 : QTabBar(parent)
41 , m_autoActivationIndex(-1)
42 , m_tabToBeClosedOnMiddleMouseButtonRelease(-1)
43 {
44 setAcceptDrops(true);
45 setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
46 setMovable(true);
47 setTabsClosable(true);
48
49 setFocusPolicy(Qt::NoFocus);
50 installEventFilter(new PreventFocusWhileHidden(this));
51
52 m_autoActivationTimer = new QTimer(this);
53 m_autoActivationTimer->setSingleShot(true);
54 m_autoActivationTimer->setInterval(800);
55 connect(m_autoActivationTimer, &QTimer::timeout, this, &DolphinTabBar::slotAutoActivationTimeout);
56 }
57
58 void DolphinTabBar::dragEnterEvent(QDragEnterEvent *event)
59 {
60 const QMimeData *mimeData = event->mimeData();
61 const int index = tabAt(event->position().toPoint());
62
63 if (mimeData->hasUrls()) {
64 event->acceptProposedAction();
65 updateAutoActivationTimer(index);
66 }
67
68 QTabBar::dragEnterEvent(event);
69 }
70
71 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent *event)
72 {
73 updateAutoActivationTimer(-1);
74
75 QTabBar::dragLeaveEvent(event);
76 }
77
78 void DolphinTabBar::dragMoveEvent(QDragMoveEvent *event)
79 {
80 const QMimeData *mimeData = event->mimeData();
81 const int index = tabAt(event->position().toPoint());
82
83 if (mimeData->hasUrls()) {
84 Q_EMIT tabDragMoveEvent(index, event);
85 updateAutoActivationTimer(index);
86 }
87
88 QTabBar::dragMoveEvent(event);
89 }
90
91 void DolphinTabBar::dropEvent(QDropEvent *event)
92 {
93 // Disable the auto activation timer
94 updateAutoActivationTimer(-1);
95
96 const QMimeData *mimeData = event->mimeData();
97 const int index = tabAt(event->position().toPoint());
98
99 if (mimeData->hasUrls()) {
100 Q_EMIT tabDropEvent(index, event);
101 }
102
103 QTabBar::dropEvent(event);
104 }
105
106 void DolphinTabBar::mousePressEvent(QMouseEvent *event)
107 {
108 const int index = tabAt(event->pos());
109
110 if (index >= 0 && event->button() == Qt::MiddleButton) {
111 m_tabToBeClosedOnMiddleMouseButtonRelease = index;
112 return;
113 }
114
115 QTabBar::mousePressEvent(event);
116 }
117
118 void DolphinTabBar::mouseReleaseEvent(QMouseEvent *event)
119 {
120 const int index = tabAt(event->pos());
121
122 if (index >= 0 && index == m_tabToBeClosedOnMiddleMouseButtonRelease && event->button() == Qt::MiddleButton) {
123 // Mouse middle click on a tab closes this tab.
124 Q_EMIT tabCloseRequested(index);
125 return;
126 }
127
128 QTabBar::mouseReleaseEvent(event);
129 }
130
131 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent *event)
132 {
133 if (event->buttons() & Qt::LeftButton) {
134 int index = tabAt(event->pos());
135
136 if (index < 0) {
137 // empty tabbar area case
138 index = currentIndex();
139 }
140 // Double left click on the tabbar opens a new activated tab
141 // with the url from the doubleclicked tab or currentTab otherwise.
142 Q_EMIT openNewActivatedTab(index);
143 }
144
145 QTabBar::mouseDoubleClickEvent(event);
146 }
147
148 void DolphinTabBar::contextMenuEvent(QContextMenuEvent *event)
149 {
150 const int index = tabAt(event->pos());
151
152 if (index >= 0) {
153 // Tab context menu
154 QMenu menu(this);
155
156 QAction *newTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
157 QAction *detachTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
158 QAction *closeOtherTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
159 QAction *closeTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
160
161 QAction *renameTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18nc("@action:inmenu", "Rename Tab"));
162
163 QAction *selectedAction = menu.exec(event->globalPos());
164 if (selectedAction == newTabAction) {
165 Q_EMIT openNewActivatedTab(index);
166 } else if (selectedAction == detachTabAction) {
167 Q_EMIT tabDetachRequested(index);
168 } else if (selectedAction == closeOtherTabsAction) {
169 const int tabCount = count();
170 for (int i = 0; i < index; i++) {
171 Q_EMIT tabCloseRequested(0);
172 }
173 for (int i = index + 1; i < tabCount; i++) {
174 Q_EMIT tabCloseRequested(1);
175 }
176 } else if (selectedAction == closeTabAction) {
177 Q_EMIT tabCloseRequested(index);
178 } else if (selectedAction == renameTabAction) {
179 bool renamed = false;
180 const QString tabNewName = QInputDialog::getText(this, i18nc("@title:window for text input", "Rename Tab"), i18n("New tab name:"), QLineEdit::Normal, tabText(index), &renamed);
181
182 if (renamed) {
183 Q_EMIT tabRenamed(index, tabNewName);
184 }
185 }
186
187 return;
188 }
189
190 QTabBar::contextMenuEvent(event);
191 }
192
193 void DolphinTabBar::slotAutoActivationTimeout()
194 {
195 if (m_autoActivationIndex >= 0) {
196 setCurrentIndex(m_autoActivationIndex);
197 updateAutoActivationTimer(-1);
198 }
199 }
200
201 void DolphinTabBar::updateAutoActivationTimer(const int index)
202 {
203 if (m_autoActivationIndex != index) {
204 m_autoActivationIndex = index;
205
206 if (m_autoActivationIndex < 0) {
207 m_autoActivationTimer->stop();
208 } else {
209 m_autoActivationTimer->start();
210 }
211 }
212 }
213
214 #include "moc_dolphintabbar.cpp"