]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabbar.cpp
Fix selection rect after porting from QFontMetrics::width()
[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 <KLocalizedString>
23
24 #include <QDragEnterEvent>
25 #include <QMenu>
26 #include <QMimeData>
27 #include <QTimer>
28
29 DolphinTabBar::DolphinTabBar(QWidget* parent) :
30 QTabBar(parent),
31 m_autoActivationIndex(-1),
32 m_tabToBeClosedOnMiddleMouseButtonRelease(-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, &QTimer::timeout,
43 this, &DolphinTabBar::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 m_tabToBeClosedOnMiddleMouseButtonRelease = index;
99 return;
100 }
101
102 QTabBar::mousePressEvent(event);
103 }
104
105 void DolphinTabBar::mouseReleaseEvent(QMouseEvent *event)
106 {
107 const int index = tabAt(event->pos());
108
109 if (index >= 0 && index == m_tabToBeClosedOnMiddleMouseButtonRelease
110 && event->button() == Qt::MiddleButton) {
111 // Mouse middle click on a tab closes this tab.
112 emit tabCloseRequested(index);
113 return;
114 }
115
116 QTabBar::mouseReleaseEvent(event);
117 }
118
119 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent* event)
120 {
121 const int index = tabAt(event->pos());
122
123 if (index < 0) {
124 // Double click on the empty tabbar area opens a new activated tab
125 // with the url from the current tab.
126 emit openNewActivatedTab(currentIndex());
127 return;
128 }
129
130 QTabBar::mouseDoubleClickEvent(event);
131 }
132
133 void DolphinTabBar::contextMenuEvent(QContextMenuEvent* event)
134 {
135 const int index = tabAt(event->pos());
136
137 if (index >= 0) {
138 // Tab context menu
139 QMenu menu(this);
140
141 QAction* newTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
142 QAction* detachTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
143 QAction* closeOtherTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
144 QAction* closeTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
145
146 QAction* selectedAction = menu.exec(event->globalPos());
147 if (selectedAction == newTabAction) {
148 emit openNewActivatedTab(index);
149 } else if (selectedAction == detachTabAction) {
150 emit tabDetachRequested(index);
151 } else if (selectedAction == closeOtherTabsAction) {
152 const int tabCount = count();
153 for (int i = 0; i < index; i++) {
154 emit tabCloseRequested(0);
155 }
156 for (int i = index + 1; i < tabCount; i++) {
157 emit tabCloseRequested(1);
158 }
159 } else if (selectedAction == closeTabAction) {
160 emit tabCloseRequested(index);
161 }
162
163 return;
164 }
165
166 QTabBar::contextMenuEvent(event);
167 }
168
169 void DolphinTabBar::slotAutoActivationTimeout()
170 {
171 if (m_autoActivationIndex >= 0) {
172 setCurrentIndex(m_autoActivationIndex);
173 updateAutoActivationTimer(-1);
174 }
175 }
176
177 void DolphinTabBar::updateAutoActivationTimer(const int index)
178 {
179 if (m_autoActivationIndex != index) {
180 m_autoActivationIndex = index;
181
182 if (m_autoActivationIndex < 0) {
183 m_autoActivationTimer->stop();
184 } else {
185 m_autoActivationTimer->start();
186 }
187 }
188 }