1 /***************************************************************************
2 * Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "dolphintabbar.h"
23 #include <QDragEnterEvent>
24 #include <KLocalizedString>
30 DolphinTabBar::DolphinTabBar(QWidget
* parent
) :
32 m_autoActivationIndex(-1),
33 m_tabToBeClosedOnMiddleMouseButtonRelease(-1)
36 setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab
);
38 setTabsClosable(true);
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
);
47 void DolphinTabBar::dragEnterEvent(QDragEnterEvent
* event
)
49 const QMimeData
* mimeData
= event
->mimeData();
50 const int index
= tabAt(event
->pos());
52 if (mimeData
->hasUrls()) {
53 event
->acceptProposedAction();
54 updateAutoActivationTimer(index
);
57 QTabBar::dragEnterEvent(event
);
60 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent
* event
)
62 updateAutoActivationTimer(-1);
64 QTabBar::dragLeaveEvent(event
);
67 void DolphinTabBar::dragMoveEvent(QDragMoveEvent
* event
)
69 const QMimeData
* mimeData
= event
->mimeData();
70 const int index
= tabAt(event
->pos());
72 if (mimeData
->hasUrls()) {
73 updateAutoActivationTimer(index
);
76 QTabBar::dragMoveEvent(event
);
79 void DolphinTabBar::dropEvent(QDropEvent
* event
)
81 // Disable the auto activation timer
82 updateAutoActivationTimer(-1);
84 const QMimeData
* mimeData
= event
->mimeData();
85 const int index
= tabAt(event
->pos());
87 if (index
>= 0 && mimeData
->hasUrls()) {
88 emit
tabDropEvent(index
, event
);
91 QTabBar::dropEvent(event
);
94 void DolphinTabBar::mousePressEvent(QMouseEvent
* event
)
96 const int index
= tabAt(event
->pos());
98 if (index
>= 0 && event
->button() == Qt::MiddleButton
) {
99 m_tabToBeClosedOnMiddleMouseButtonRelease
= index
;
103 QTabBar::mousePressEvent(event
);
106 void DolphinTabBar::mouseReleaseEvent(QMouseEvent
*event
)
108 const int index
= tabAt(event
->pos());
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
);
117 QTabBar::mouseReleaseEvent(event
);
120 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent
* event
)
122 const int index
= tabAt(event
->pos());
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());
131 QTabBar::mouseDoubleClickEvent(event
);
134 void DolphinTabBar::contextMenuEvent(QContextMenuEvent
* event
)
136 const int index
= tabAt(event
->pos());
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"));
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);
157 for (int i
= index
+ 1; i
< tabCount
; i
++) {
158 emit
tabCloseRequested(1);
160 } else if (selectedAction
== closeTabAction
) {
161 emit
tabCloseRequested(index
);
167 QTabBar::contextMenuEvent(event
);
170 void DolphinTabBar::slotAutoActivationTimeout()
172 if (m_autoActivationIndex
>= 0) {
173 setCurrentIndex(m_autoActivationIndex
);
174 updateAutoActivationTimer(-1);
178 void DolphinTabBar::updateAutoActivationTimer(const int index
)
180 if (m_autoActivationIndex
!= index
) {
181 m_autoActivationIndex
= index
;
183 if (m_autoActivationIndex
< 0) {
184 m_autoActivationTimer
->stop();
186 m_autoActivationTimer
->start();