]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabbar.cpp
port Dolphin from KUrl to QUrl
[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 {
34 setAcceptDrops(true);
35 setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
36 setMovable(true);
37 setTabsClosable(true);
38
39 m_autoActivationTimer = new QTimer(this);
40 m_autoActivationTimer->setSingleShot(true);
41 m_autoActivationTimer->setInterval(800);
42 connect(m_autoActivationTimer, SIGNAL(timeout()),
43 this, SLOT(slotAutoActivationTimeout()));
44 }
45
46 void DolphinTabBar::dragEnterEvent(QDragEnterEvent* event)
47 {
48 const QMimeData* mimeData = event->mimeData();
49 const int index = tabAt(event->pos());
50
51 if (mimeData->hasUrls()) {
52 event->acceptProposedAction();
53 updateAutoActivationTimer(index);
54 }
55
56 QTabBar::dragEnterEvent(event);
57 }
58
59 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent* event)
60 {
61 updateAutoActivationTimer(-1);
62
63 QTabBar::dragLeaveEvent(event);
64 }
65
66 void DolphinTabBar::dragMoveEvent(QDragMoveEvent* event)
67 {
68 const QMimeData* mimeData = event->mimeData();
69 const int index = tabAt(event->pos());
70
71 if (mimeData->hasUrls()) {
72 updateAutoActivationTimer(index);
73 }
74
75 QTabBar::dragMoveEvent(event);
76 }
77
78 void DolphinTabBar::dropEvent(QDropEvent* event)
79 {
80 // Disable the auto activation timer
81 updateAutoActivationTimer(-1);
82
83 const QMimeData* mimeData = event->mimeData();
84 const int index = tabAt(event->pos());
85
86 if (index >= 0 && mimeData->hasUrls()) {
87 emit tabDropEvent(index, event);
88 }
89
90 QTabBar::dropEvent(event);
91 }
92
93 void DolphinTabBar::mousePressEvent(QMouseEvent* event)
94 {
95 const int index = tabAt(event->pos());
96
97 if (index >= 0 && event->button() == Qt::MiddleButton) {
98 // Mouse middle click on a tab closes this tab.
99 emit tabCloseRequested(index);
100 return;
101 }
102
103 QTabBar::mousePressEvent(event);
104 }
105
106 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent* event)
107 {
108 const int index = tabAt(event->pos());
109
110 if (index < 0) {
111 // Double click on the empty tabbar area opens a new activated tab
112 // with the url from the current tab.
113 emit openNewActivatedTab(currentIndex());
114 return;
115 }
116
117 QTabBar::mouseDoubleClickEvent(event);
118 }
119
120 void DolphinTabBar::contextMenuEvent(QContextMenuEvent* event)
121 {
122 const int index = tabAt(event->pos());
123
124 if (index >= 0) {
125 // Tab context menu
126 QMenu menu(this);
127
128 QAction* newTabAction = menu.addAction(QIcon::fromTheme("tab-new"), i18nc("@action:inmenu", "New Tab"));
129 QAction* detachTabAction = menu.addAction(QIcon::fromTheme("tab-detach"), i18nc("@action:inmenu", "Detach Tab"));
130 QAction* closeOtherTabsAction = menu.addAction(QIcon::fromTheme("tab-close-other"), i18nc("@action:inmenu", "Close Other Tabs"));
131 QAction* closeTabAction = menu.addAction(QIcon::fromTheme("tab-close"), i18nc("@action:inmenu", "Close Tab"));
132
133 QAction* selectedAction = menu.exec(event->globalPos());
134 if (selectedAction == newTabAction) {
135 emit openNewActivatedTab(index);
136 } else if (selectedAction == detachTabAction) {
137 emit tabDetachRequested(index);
138 } else if (selectedAction == closeOtherTabsAction) {
139 const int tabCount = count();
140 for (int i = 0; i < index; i++) {
141 emit tabCloseRequested(0);
142 }
143 for (int i = index + 1; i < tabCount; i++) {
144 emit tabCloseRequested(1);
145 }
146 } else if (selectedAction == closeTabAction) {
147 emit tabCloseRequested(index);
148 }
149
150 return;
151 }
152
153 QTabBar::contextMenuEvent(event);
154 }
155
156 void DolphinTabBar::slotAutoActivationTimeout()
157 {
158 if (m_autoActivationIndex >= 0) {
159 setCurrentIndex(m_autoActivationIndex);
160 updateAutoActivationTimer(-1);
161 }
162 }
163
164 void DolphinTabBar::updateAutoActivationTimer(const int index)
165 {
166 if (m_autoActivationIndex != index) {
167 m_autoActivationIndex = index;
168
169 if (m_autoActivationIndex < 0) {
170 m_autoActivationTimer->stop();
171 } else {
172 m_autoActivationTimer->start();
173 }
174 }
175 }