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"
22 #include <KLocalizedString>
24 #include <QDragEnterEvent>
29 DolphinTabBar::DolphinTabBar(QWidget
* parent
) :
31 m_autoActivationIndex(-1),
32 m_tabToBeClosedOnMiddleMouseButtonRelease(-1)
35 setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab
);
37 setTabsClosable(true);
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
);
46 void DolphinTabBar::dragEnterEvent(QDragEnterEvent
* event
)
48 const QMimeData
* mimeData
= event
->mimeData();
49 const int index
= tabAt(event
->pos());
51 if (mimeData
->hasUrls()) {
52 event
->acceptProposedAction();
53 updateAutoActivationTimer(index
);
56 QTabBar::dragEnterEvent(event
);
59 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent
* event
)
61 updateAutoActivationTimer(-1);
63 QTabBar::dragLeaveEvent(event
);
66 void DolphinTabBar::dragMoveEvent(QDragMoveEvent
* event
)
68 const QMimeData
* mimeData
= event
->mimeData();
69 const int index
= tabAt(event
->pos());
71 if (mimeData
->hasUrls()) {
72 updateAutoActivationTimer(index
);
75 QTabBar::dragMoveEvent(event
);
78 void DolphinTabBar::dropEvent(QDropEvent
* event
)
80 // Disable the auto activation timer
81 updateAutoActivationTimer(-1);
83 const QMimeData
* mimeData
= event
->mimeData();
84 const int index
= tabAt(event
->pos());
86 if (index
>= 0 && mimeData
->hasUrls()) {
87 emit
tabDropEvent(index
, event
);
90 QTabBar::dropEvent(event
);
93 void DolphinTabBar::mousePressEvent(QMouseEvent
* event
)
95 const int index
= tabAt(event
->pos());
97 if (index
>= 0 && event
->button() == Qt::MiddleButton
) {
98 m_tabToBeClosedOnMiddleMouseButtonRelease
= index
;
102 QTabBar::mousePressEvent(event
);
105 void DolphinTabBar::mouseReleaseEvent(QMouseEvent
*event
)
107 const int index
= tabAt(event
->pos());
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
);
116 QTabBar::mouseReleaseEvent(event
);
119 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent
* event
)
121 const int index
= tabAt(event
->pos());
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());
130 QTabBar::mouseDoubleClickEvent(event
);
133 void DolphinTabBar::contextMenuEvent(QContextMenuEvent
* event
)
135 const int index
= tabAt(event
->pos());
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"));
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);
156 for (int i
= index
+ 1; i
< tabCount
; i
++) {
157 emit
tabCloseRequested(1);
159 } else if (selectedAction
== closeTabAction
) {
160 emit
tabCloseRequested(index
);
166 QTabBar::contextMenuEvent(event
);
169 void DolphinTabBar::slotAutoActivationTimeout()
171 if (m_autoActivationIndex
>= 0) {
172 setCurrentIndex(m_autoActivationIndex
);
173 updateAutoActivationTimer(-1);
177 void DolphinTabBar::updateAutoActivationTimer(const int index
)
179 if (m_autoActivationIndex
!= index
) {
180 m_autoActivationIndex
= index
;
182 if (m_autoActivationIndex
< 0) {
183 m_autoActivationTimer
->stop();
185 m_autoActivationTimer
->start();