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()) {
40 event
->acceptProposedAction();
42 event
->setDropAction(Qt::IgnoreAction
);
43 // Still need to accept it to receive dragMoveEvent
46 updateAutoActivationTimer(index
);
49 QTabBar::dragEnterEvent(event
);
52 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent
* event
)
54 updateAutoActivationTimer(-1);
56 QTabBar::dragLeaveEvent(event
);
59 void DolphinTabBar::dragMoveEvent(QDragMoveEvent
* event
)
61 const QMimeData
* mimeData
= event
->mimeData();
62 const int index
= tabAt(event
->pos());
64 if (mimeData
->hasUrls()) {
66 event
->acceptProposedAction();
68 event
->setDropAction(Qt::IgnoreAction
);
70 updateAutoActivationTimer(index
);
73 QTabBar::dragMoveEvent(event
);
76 void DolphinTabBar::dropEvent(QDropEvent
* event
)
78 // Disable the auto activation timer
79 updateAutoActivationTimer(-1);
81 const QMimeData
* mimeData
= event
->mimeData();
82 const int index
= tabAt(event
->pos());
84 if (index
>= 0 && mimeData
->hasUrls()) {
85 Q_EMIT
tabDropEvent(index
, event
);
88 QTabBar::dropEvent(event
);
91 void DolphinTabBar::mousePressEvent(QMouseEvent
* event
)
93 const int index
= tabAt(event
->pos());
95 if (index
>= 0 && event
->button() == Qt::MiddleButton
) {
96 m_tabToBeClosedOnMiddleMouseButtonRelease
= index
;
100 QTabBar::mousePressEvent(event
);
103 void DolphinTabBar::mouseReleaseEvent(QMouseEvent
*event
)
105 const int index
= tabAt(event
->pos());
107 if (index
>= 0 && index
== m_tabToBeClosedOnMiddleMouseButtonRelease
108 && event
->button() == Qt::MiddleButton
) {
109 // Mouse middle click on a tab closes this tab.
110 Q_EMIT
tabCloseRequested(index
);
114 QTabBar::mouseReleaseEvent(event
);
117 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent
* event
)
119 const int index
= tabAt(event
->pos());
122 // Double click on the empty tabbar area opens a new activated tab
123 // with the url from the current tab.
124 Q_EMIT
openNewActivatedTab(currentIndex());
128 QTabBar::mouseDoubleClickEvent(event
);
131 void DolphinTabBar::contextMenuEvent(QContextMenuEvent
* event
)
133 const int index
= tabAt(event
->pos());
139 QAction
* newTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
140 QAction
* detachTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
141 QAction
* closeOtherTabsAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
142 QAction
* closeTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
144 QAction
* selectedAction
= menu
.exec(event
->globalPos());
145 if (selectedAction
== newTabAction
) {
146 Q_EMIT
openNewActivatedTab(index
);
147 } else if (selectedAction
== detachTabAction
) {
148 Q_EMIT
tabDetachRequested(index
);
149 } else if (selectedAction
== closeOtherTabsAction
) {
150 const int tabCount
= count();
151 for (int i
= 0; i
< index
; i
++) {
152 Q_EMIT
tabCloseRequested(0);
154 for (int i
= index
+ 1; i
< tabCount
; i
++) {
155 Q_EMIT
tabCloseRequested(1);
157 } else if (selectedAction
== closeTabAction
) {
158 Q_EMIT
tabCloseRequested(index
);
164 QTabBar::contextMenuEvent(event
);
167 void DolphinTabBar::slotAutoActivationTimeout()
169 if (m_autoActivationIndex
>= 0) {
170 setCurrentIndex(m_autoActivationIndex
);
171 updateAutoActivationTimer(-1);
175 void DolphinTabBar::updateAutoActivationTimer(const int index
)
177 if (m_autoActivationIndex
!= index
) {
178 m_autoActivationIndex
= index
;
180 if (m_autoActivationIndex
< 0) {
181 m_autoActivationTimer
->stop();
183 m_autoActivationTimer
->start();