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
,
30 this, &DolphinTabBar::slotAutoActivationTimeout
);
33 void DolphinTabBar::dragEnterEvent(QDragEnterEvent
* event
)
35 const QMimeData
* mimeData
= event
->mimeData();
36 const int index
= tabAt(event
->pos());
38 if (mimeData
->hasUrls()) {
39 event
->acceptProposedAction();
40 updateAutoActivationTimer(index
);
43 QTabBar::dragEnterEvent(event
);
46 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent
* event
)
48 updateAutoActivationTimer(-1);
50 QTabBar::dragLeaveEvent(event
);
53 void DolphinTabBar::dragMoveEvent(QDragMoveEvent
* event
)
55 const QMimeData
* mimeData
= event
->mimeData();
56 const int index
= tabAt(event
->pos());
58 if (mimeData
->hasUrls()) {
59 updateAutoActivationTimer(index
);
62 QTabBar::dragMoveEvent(event
);
65 void DolphinTabBar::dropEvent(QDropEvent
* event
)
67 // Disable the auto activation timer
68 updateAutoActivationTimer(-1);
70 const QMimeData
* mimeData
= event
->mimeData();
71 const int index
= tabAt(event
->pos());
73 if (index
>= 0 && mimeData
->hasUrls()) {
74 emit
tabDropEvent(index
, event
);
77 QTabBar::dropEvent(event
);
80 void DolphinTabBar::mousePressEvent(QMouseEvent
* event
)
82 const int index
= tabAt(event
->pos());
84 if (index
>= 0 && event
->button() == Qt::MiddleButton
) {
85 m_tabToBeClosedOnMiddleMouseButtonRelease
= index
;
89 QTabBar::mousePressEvent(event
);
92 void DolphinTabBar::mouseReleaseEvent(QMouseEvent
*event
)
94 const int index
= tabAt(event
->pos());
96 if (index
>= 0 && index
== m_tabToBeClosedOnMiddleMouseButtonRelease
97 && event
->button() == Qt::MiddleButton
) {
98 // Mouse middle click on a tab closes this tab.
99 emit
tabCloseRequested(index
);
103 QTabBar::mouseReleaseEvent(event
);
106 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent
* event
)
108 const int index
= tabAt(event
->pos());
111 // Double click on the empty tabbar area opens a new activated tab
112 // with the url from the current tab.
113 emit
openNewActivatedTab(currentIndex());
117 QTabBar::mouseDoubleClickEvent(event
);
120 void DolphinTabBar::contextMenuEvent(QContextMenuEvent
* event
)
122 const int index
= tabAt(event
->pos());
128 QAction
* newTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
129 QAction
* detachTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
130 QAction
* closeOtherTabsAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
131 QAction
* closeTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
133 QAction
* selectedAction
= menu
.exec(event
->globalPos());
134 if (selectedAction
== newTabAction
) {
135 emit
openNewActivatedTab(index
);
136 } else if (selectedAction
== detachTabAction
) {
137 emit
tabDetachRequested(index
);
138 } else if (selectedAction
== closeOtherTabsAction
) {
139 const int tabCount
= count();
140 for (int i
= 0; i
< index
; i
++) {
141 emit
tabCloseRequested(0);
143 for (int i
= index
+ 1; i
< tabCount
; i
++) {
144 emit
tabCloseRequested(1);
146 } else if (selectedAction
== closeTabAction
) {
147 emit
tabCloseRequested(index
);
153 QTabBar::contextMenuEvent(event
);
156 void DolphinTabBar::slotAutoActivationTimeout()
158 if (m_autoActivationIndex
>= 0) {
159 setCurrentIndex(m_autoActivationIndex
);
160 updateAutoActivationTimer(-1);
164 void DolphinTabBar::updateAutoActivationTimer(const int index
)
166 if (m_autoActivationIndex
!= index
) {
167 m_autoActivationIndex
= index
;
169 if (m_autoActivationIndex
< 0) {
170 m_autoActivationTimer
->stop();
172 m_autoActivationTimer
->start();