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 class PreventFocusWhileHidden
: public QObject
19 PreventFocusWhileHidden(QObject
*parent
)
23 bool eventFilter(QObject
*obj
, QEvent
*ev
) override
27 static_cast<QWidget
*>(obj
)->setFocusPolicy(Qt::NoFocus
);
30 static_cast<QWidget
*>(obj
)->setFocusPolicy(Qt::TabFocus
);
38 DolphinTabBar::DolphinTabBar(QWidget
*parent
)
40 , m_autoActivationIndex(-1)
41 , m_tabToBeClosedOnMiddleMouseButtonRelease(-1)
44 setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab
);
46 setTabsClosable(true);
48 setFocusPolicy(Qt::NoFocus
);
49 installEventFilter(new PreventFocusWhileHidden(this));
51 m_autoActivationTimer
= new QTimer(this);
52 m_autoActivationTimer
->setSingleShot(true);
53 m_autoActivationTimer
->setInterval(800);
54 connect(m_autoActivationTimer
, &QTimer::timeout
, this, &DolphinTabBar::slotAutoActivationTimeout
);
57 void DolphinTabBar::dragEnterEvent(QDragEnterEvent
*event
)
59 const QMimeData
*mimeData
= event
->mimeData();
60 const int index
= tabAt(event
->position().toPoint());
62 if (mimeData
->hasUrls()) {
63 event
->acceptProposedAction();
64 updateAutoActivationTimer(index
);
67 QTabBar::dragEnterEvent(event
);
70 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent
*event
)
72 updateAutoActivationTimer(-1);
74 QTabBar::dragLeaveEvent(event
);
77 void DolphinTabBar::dragMoveEvent(QDragMoveEvent
*event
)
79 const QMimeData
*mimeData
= event
->mimeData();
80 const int index
= tabAt(event
->position().toPoint());
82 if (mimeData
->hasUrls()) {
83 updateAutoActivationTimer(index
);
86 QTabBar::dragMoveEvent(event
);
89 void DolphinTabBar::dropEvent(QDropEvent
*event
)
91 // Disable the auto activation timer
92 updateAutoActivationTimer(-1);
94 const QMimeData
*mimeData
= event
->mimeData();
95 const int index
= tabAt(event
->position().toPoint());
97 if (mimeData
->hasUrls()) {
98 Q_EMIT
tabDropEvent(index
, event
);
101 QTabBar::dropEvent(event
);
104 void DolphinTabBar::mousePressEvent(QMouseEvent
*event
)
106 const int index
= tabAt(event
->pos());
108 if (index
>= 0 && event
->button() == Qt::MiddleButton
) {
109 m_tabToBeClosedOnMiddleMouseButtonRelease
= index
;
113 QTabBar::mousePressEvent(event
);
116 void DolphinTabBar::mouseReleaseEvent(QMouseEvent
*event
)
118 const int index
= tabAt(event
->pos());
120 if (index
>= 0 && index
== m_tabToBeClosedOnMiddleMouseButtonRelease
&& event
->button() == Qt::MiddleButton
) {
121 // Mouse middle click on a tab closes this tab.
122 Q_EMIT
tabCloseRequested(index
);
126 QTabBar::mouseReleaseEvent(event
);
129 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent
*event
)
131 if (event
->buttons() & Qt::LeftButton
) {
132 int index
= tabAt(event
->pos());
135 // empty tabbar area case
136 index
= currentIndex();
138 // Double left click on the tabbar opens a new activated tab
139 // with the url from the doubleclicked tab or currentTab otherwise.
140 Q_EMIT
openNewActivatedTab(index
);
143 QTabBar::mouseDoubleClickEvent(event
);
146 void DolphinTabBar::contextMenuEvent(QContextMenuEvent
*event
)
148 const int index
= tabAt(event
->pos());
154 QAction
*newTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
155 QAction
*detachTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
156 QAction
*closeOtherTabsAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
157 QAction
*closeTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
159 QAction
*selectedAction
= menu
.exec(event
->globalPos());
160 if (selectedAction
== newTabAction
) {
161 Q_EMIT
openNewActivatedTab(index
);
162 } else if (selectedAction
== detachTabAction
) {
163 Q_EMIT
tabDetachRequested(index
);
164 } else if (selectedAction
== closeOtherTabsAction
) {
165 const int tabCount
= count();
166 for (int i
= 0; i
< index
; i
++) {
167 Q_EMIT
tabCloseRequested(0);
169 for (int i
= index
+ 1; i
< tabCount
; i
++) {
170 Q_EMIT
tabCloseRequested(1);
172 } else if (selectedAction
== closeTabAction
) {
173 Q_EMIT
tabCloseRequested(index
);
179 QTabBar::contextMenuEvent(event
);
182 void DolphinTabBar::slotAutoActivationTimeout()
184 if (m_autoActivationIndex
>= 0) {
185 setCurrentIndex(m_autoActivationIndex
);
186 updateAutoActivationTimer(-1);
190 void DolphinTabBar::updateAutoActivationTimer(const int index
)
192 if (m_autoActivationIndex
!= index
) {
193 m_autoActivationIndex
= index
;
195 if (m_autoActivationIndex
< 0) {
196 m_autoActivationTimer
->stop();
198 m_autoActivationTimer
->start();
203 #include "moc_dolphintabbar.cpp"