]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabbar.cpp
Merge branch 'stashAction'
[dolphin.git] / src / dolphintabbar.cpp
1 /***************************************************************************
2 * Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphintabbar.h"
21
22 #include <QTimer>
23 #include <QDragEnterEvent>
24 #include <KLocalizedString>
25 #include <QMenu>
26 #include <QIcon>
27 #include <QUrl>
28 #include <QMimeData>
29
30 DolphinTabBar::DolphinTabBar(QWidget* parent) :
31 QTabBar(parent),
32 m_autoActivationIndex(-1),
33 m_tabToBeClosedOnMiddleMouseButtonRelease(-1)
34 {
35 setAcceptDrops(true);
36 setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
37 setMovable(true);
38 setTabsClosable(true);
39
40 m_autoActivationTimer = new QTimer(this);
41 m_autoActivationTimer->setSingleShot(true);
42 m_autoActivationTimer->setInterval(800);
43 connect(m_autoActivationTimer, &QTimer::timeout,
44 this, &DolphinTabBar::slotAutoActivationTimeout);
45 }
46
47 void DolphinTabBar::dragEnterEvent(QDragEnterEvent* event)
48 {
49 const QMimeData* mimeData = event->mimeData();
50 const int index = tabAt(event->pos());
51
52 if (mimeData->hasUrls()) {
53 event->acceptProposedAction();
54 updateAutoActivationTimer(index);
55 }
56
57 QTabBar::dragEnterEvent(event);
58 }
59
60 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent* event)
61 {
62 updateAutoActivationTimer(-1);
63
64 QTabBar::dragLeaveEvent(event);
65 }
66
67 void DolphinTabBar::dragMoveEvent(QDragMoveEvent* event)
68 {
69 const QMimeData* mimeData = event->mimeData();
70 const int index = tabAt(event->pos());
71
72 if (mimeData->hasUrls()) {
73 updateAutoActivationTimer(index);
74 }
75
76 QTabBar::dragMoveEvent(event);
77 }
78
79 void DolphinTabBar::dropEvent(QDropEvent* event)
80 {
81 // Disable the auto activation timer
82 updateAutoActivationTimer(-1);
83
84 const QMimeData* mimeData = event->mimeData();
85 const int index = tabAt(event->pos());
86
87 if (index >= 0 && mimeData->hasUrls()) {
88 emit tabDropEvent(index, event);
89 }
90
91 QTabBar::dropEvent(event);
92 }
93
94 void DolphinTabBar::mousePressEvent(QMouseEvent* event)
95 {
96 const int index = tabAt(event->pos());
97
98 if (index >= 0 && event->button() == Qt::MiddleButton) {
99 m_tabToBeClosedOnMiddleMouseButtonRelease = index;
100 return;
101 }
102
103 QTabBar::mousePressEvent(event);
104 }
105
106 void DolphinTabBar::mouseReleaseEvent(QMouseEvent *event)
107 {
108 const int index = tabAt(event->pos());
109
110 if (index >= 0 && index == m_tabToBeClosedOnMiddleMouseButtonRelease
111 && event->button() == Qt::MiddleButton) {
112 // Mouse middle click on a tab closes this tab.
113 emit tabCloseRequested(index);
114 return;
115 }
116
117 QTabBar::mouseReleaseEvent(event);
118 }
119
120 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent* event)
121 {
122 const int index = tabAt(event->pos());
123
124 if (index < 0) {
125 // Double click on the empty tabbar area opens a new activated tab
126 // with the url from the current tab.
127 emit openNewActivatedTab(currentIndex());
128 return;
129 }
130
131 QTabBar::mouseDoubleClickEvent(event);
132 }
133
134 void DolphinTabBar::contextMenuEvent(QContextMenuEvent* event)
135 {
136 const int index = tabAt(event->pos());
137
138 if (index >= 0) {
139 // Tab context menu
140 QMenu menu(this);
141
142 QAction* newTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
143 QAction* detachTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
144 QAction* closeOtherTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
145 QAction* closeTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
146
147 QAction* selectedAction = menu.exec(event->globalPos());
148 if (selectedAction == newTabAction) {
149 emit openNewActivatedTab(index);
150 } else if (selectedAction == detachTabAction) {
151 emit tabDetachRequested(index);
152 } else if (selectedAction == closeOtherTabsAction) {
153 const int tabCount = count();
154 for (int i = 0; i < index; i++) {
155 emit tabCloseRequested(0);
156 }
157 for (int i = index + 1; i < tabCount; i++) {
158 emit tabCloseRequested(1);
159 }
160 } else if (selectedAction == closeTabAction) {
161 emit tabCloseRequested(index);
162 }
163
164 return;
165 }
166
167 QTabBar::contextMenuEvent(event);
168 }
169
170 void DolphinTabBar::slotAutoActivationTimeout()
171 {
172 if (m_autoActivationIndex >= 0) {
173 setCurrentIndex(m_autoActivationIndex);
174 updateAutoActivationTimer(-1);
175 }
176 }
177
178 void DolphinTabBar::updateAutoActivationTimer(const int index)
179 {
180 if (m_autoActivationIndex != index) {
181 m_autoActivationIndex = index;
182
183 if (m_autoActivationIndex < 0) {
184 m_autoActivationTimer->stop();
185 } else {
186 m_autoActivationTimer->start();
187 }
188 }
189 }