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