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 Q_EMIT
tabDragMoveEvent(index
, event
);
84 updateAutoActivationTimer(index
);
87 QTabBar::dragMoveEvent(event
);
90 void DolphinTabBar::dropEvent(QDropEvent
*event
)
92 // Disable the auto activation timer
93 updateAutoActivationTimer(-1);
95 const QMimeData
*mimeData
= event
->mimeData();
96 const int index
= tabAt(event
->position().toPoint());
98 if (mimeData
->hasUrls()) {
99 Q_EMIT
tabDropEvent(index
, event
);
102 QTabBar::dropEvent(event
);
105 void DolphinTabBar::mousePressEvent(QMouseEvent
*event
)
107 const int index
= tabAt(event
->pos());
109 if (index
>= 0 && event
->button() == Qt::MiddleButton
) {
110 m_tabToBeClosedOnMiddleMouseButtonRelease
= index
;
114 QTabBar::mousePressEvent(event
);
117 void DolphinTabBar::mouseReleaseEvent(QMouseEvent
*event
)
119 const int index
= tabAt(event
->pos());
121 if (index
>= 0 && index
== m_tabToBeClosedOnMiddleMouseButtonRelease
&& event
->button() == Qt::MiddleButton
) {
122 // Mouse middle click on a tab closes this tab.
123 Q_EMIT
tabCloseRequested(index
);
127 QTabBar::mouseReleaseEvent(event
);
130 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent
*event
)
132 if (event
->buttons() & Qt::LeftButton
) {
133 int index
= tabAt(event
->pos());
136 // empty tabbar area case
137 index
= currentIndex();
139 // Double left click on the tabbar opens a new activated tab
140 // with the url from the doubleclicked tab or currentTab otherwise.
141 Q_EMIT
openNewActivatedTab(index
);
144 QTabBar::mouseDoubleClickEvent(event
);
147 void DolphinTabBar::contextMenuEvent(QContextMenuEvent
*event
)
149 const int index
= tabAt(event
->pos());
155 QAction
*newTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
156 QAction
*detachTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
157 QAction
*closeOtherTabsAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
158 QAction
*closeTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
160 QAction
*selectedAction
= menu
.exec(event
->globalPos());
161 if (selectedAction
== newTabAction
) {
162 Q_EMIT
openNewActivatedTab(index
);
163 } else if (selectedAction
== detachTabAction
) {
164 Q_EMIT
tabDetachRequested(index
);
165 } else if (selectedAction
== closeOtherTabsAction
) {
166 const int tabCount
= count();
167 for (int i
= 0; i
< index
; i
++) {
168 Q_EMIT
tabCloseRequested(0);
170 for (int i
= index
+ 1; i
< tabCount
; i
++) {
171 Q_EMIT
tabCloseRequested(1);
173 } else if (selectedAction
== closeTabAction
) {
174 Q_EMIT
tabCloseRequested(index
);
180 QTabBar::contextMenuEvent(event
);
183 void DolphinTabBar::slotAutoActivationTimeout()
185 if (m_autoActivationIndex
>= 0) {
186 setCurrentIndex(m_autoActivationIndex
);
187 updateAutoActivationTimer(-1);
191 void DolphinTabBar::updateAutoActivationTimer(const int index
)
193 if (m_autoActivationIndex
!= index
) {
194 m_autoActivationIndex
= index
;
196 if (m_autoActivationIndex
< 0) {
197 m_autoActivationTimer
->stop();
199 m_autoActivationTimer
->start();
204 #include "moc_dolphintabbar.cpp"