]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabbar.cpp
1 /***************************************************************************
2 * Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "dolphintabbar.h"
23 #include <QDragEnterEvent>
24 #include <KLocalizedString>
30 DolphinTabBar::DolphinTabBar(QWidget
* parent
) :
32 m_autoActivationIndex(-1)
35 setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab
);
37 setTabsClosable(true);
39 m_autoActivationTimer
= new QTimer(this);
40 m_autoActivationTimer
->setSingleShot(true);
41 m_autoActivationTimer
->setInterval(800);
42 connect(m_autoActivationTimer
, SIGNAL(timeout()),
43 this, SLOT(slotAutoActivationTimeout()));
46 void DolphinTabBar::dragEnterEvent(QDragEnterEvent
* event
)
48 const QMimeData
* mimeData
= event
->mimeData();
49 const int index
= tabAt(event
->pos());
51 if (mimeData
->hasUrls()) {
52 event
->acceptProposedAction();
53 updateAutoActivationTimer(index
);
56 QTabBar::dragEnterEvent(event
);
59 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent
* event
)
61 updateAutoActivationTimer(-1);
63 QTabBar::dragLeaveEvent(event
);
66 void DolphinTabBar::dragMoveEvent(QDragMoveEvent
* event
)
68 const QMimeData
* mimeData
= event
->mimeData();
69 const int index
= tabAt(event
->pos());
71 if (mimeData
->hasUrls()) {
72 updateAutoActivationTimer(index
);
75 QTabBar::dragMoveEvent(event
);
78 void DolphinTabBar::dropEvent(QDropEvent
* event
)
80 // Disable the auto activation timer
81 updateAutoActivationTimer(-1);
83 const QMimeData
* mimeData
= event
->mimeData();
84 const int index
= tabAt(event
->pos());
86 if (index
>= 0 && mimeData
->hasUrls()) {
87 emit
tabDropEvent(index
, event
);
90 QTabBar::dropEvent(event
);
93 void DolphinTabBar::mousePressEvent(QMouseEvent
* event
)
95 const int index
= tabAt(event
->pos());
97 if (index
>= 0 && event
->button() == Qt::MiddleButton
) {
98 // Mouse middle click on a tab closes this tab.
99 emit
tabCloseRequested(index
);
103 QTabBar::mousePressEvent(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("tab-new"), i18nc("@action:inmenu", "New Tab"));
129 QAction
* detachTabAction
= menu
.addAction(QIcon::fromTheme("tab-detach"), i18nc("@action:inmenu", "Detach Tab"));
130 QAction
* closeOtherTabsAction
= menu
.addAction(QIcon::fromTheme("tab-close-other"), i18nc("@action:inmenu", "Close Other Tabs"));
131 QAction
* closeTabAction
= menu
.addAction(QIcon::fromTheme("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();