2 * SPDX-FileCopyrightText: 2014 Emmanuel Pescosta <emmanuelpescosta099@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphintabbar.h"
9 #include <KLocalizedString>
11 #include <QDragEnterEvent>
16 DolphinTabBar::DolphinTabBar(QWidget
*parent
)
18 , m_autoActivationIndex(-1)
19 , m_tabToBeClosedOnMiddleMouseButtonRelease(-1)
22 setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab
);
24 setTabsClosable(true);
26 m_autoActivationTimer
= new QTimer(this);
27 m_autoActivationTimer
->setSingleShot(true);
28 m_autoActivationTimer
->setInterval(800);
29 connect(m_autoActivationTimer
, &QTimer::timeout
, this, &DolphinTabBar::slotAutoActivationTimeout
);
32 void DolphinTabBar::dragEnterEvent(QDragEnterEvent
*event
)
34 const QMimeData
*mimeData
= event
->mimeData();
35 const int index
= tabAt(event
->position().toPoint());
37 if (mimeData
->hasUrls()) {
38 event
->acceptProposedAction();
39 updateAutoActivationTimer(index
);
42 QTabBar::dragEnterEvent(event
);
45 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent
*event
)
47 updateAutoActivationTimer(-1);
49 QTabBar::dragLeaveEvent(event
);
52 void DolphinTabBar::dragMoveEvent(QDragMoveEvent
*event
)
54 const QMimeData
*mimeData
= event
->mimeData();
55 const int index
= tabAt(event
->position().toPoint());
57 if (mimeData
->hasUrls()) {
58 updateAutoActivationTimer(index
);
61 QTabBar::dragMoveEvent(event
);
64 void DolphinTabBar::dropEvent(QDropEvent
*event
)
66 // Disable the auto activation timer
67 updateAutoActivationTimer(-1);
69 const QMimeData
*mimeData
= event
->mimeData();
70 const int index
= tabAt(event
->position().toPoint());
72 if (mimeData
->hasUrls()) {
73 Q_EMIT
tabDropEvent(index
, event
);
76 QTabBar::dropEvent(event
);
79 void DolphinTabBar::mousePressEvent(QMouseEvent
*event
)
81 const int index
= tabAt(event
->pos());
83 if (index
>= 0 && event
->button() == Qt::MiddleButton
) {
84 m_tabToBeClosedOnMiddleMouseButtonRelease
= index
;
88 QTabBar::mousePressEvent(event
);
91 void DolphinTabBar::mouseReleaseEvent(QMouseEvent
*event
)
93 const int index
= tabAt(event
->pos());
95 if (index
>= 0 && index
== m_tabToBeClosedOnMiddleMouseButtonRelease
&& event
->button() == Qt::MiddleButton
) {
96 // Mouse middle click on a tab closes this tab.
97 Q_EMIT
tabCloseRequested(index
);
101 QTabBar::mouseReleaseEvent(event
);
104 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent
*event
)
106 const int index
= tabAt(event
->pos());
109 // Double click on the empty tabbar area opens a new activated tab
110 // with the url from the current tab.
111 Q_EMIT
openNewActivatedTab(currentIndex());
115 QTabBar::mouseDoubleClickEvent(event
);
118 void DolphinTabBar::contextMenuEvent(QContextMenuEvent
*event
)
120 const int index
= tabAt(event
->pos());
126 QAction
*newTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
127 QAction
*detachTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
128 QAction
*closeOtherTabsAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
129 QAction
*closeTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
131 QAction
*selectedAction
= menu
.exec(event
->globalPos());
132 if (selectedAction
== newTabAction
) {
133 Q_EMIT
openNewActivatedTab(index
);
134 } else if (selectedAction
== detachTabAction
) {
135 Q_EMIT
tabDetachRequested(index
);
136 } else if (selectedAction
== closeOtherTabsAction
) {
137 const int tabCount
= count();
138 for (int i
= 0; i
< index
; i
++) {
139 Q_EMIT
tabCloseRequested(0);
141 for (int i
= index
+ 1; i
< tabCount
; i
++) {
142 Q_EMIT
tabCloseRequested(1);
144 } else if (selectedAction
== closeTabAction
) {
145 Q_EMIT
tabCloseRequested(index
);
151 QTabBar::contextMenuEvent(event
);
154 void DolphinTabBar::slotAutoActivationTimeout()
156 if (m_autoActivationIndex
>= 0) {
157 setCurrentIndex(m_autoActivationIndex
);
158 updateAutoActivationTimer(-1);
162 void DolphinTabBar::updateAutoActivationTimer(const int index
)
164 if (m_autoActivationIndex
!= index
) {
165 m_autoActivationIndex
= index
;
167 if (m_autoActivationIndex
< 0) {
168 m_autoActivationTimer
->stop();
170 m_autoActivationTimer
->start();