]>
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>
29 DolphinTabBar::DolphinTabBar(QWidget
* parent
) :
31 m_autoActivationIndex(-1)
34 setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab
);
36 setTabsClosable(true);
38 m_autoActivationTimer
= new QTimer(this);
39 m_autoActivationTimer
->setSingleShot(true);
40 m_autoActivationTimer
->setInterval(800);
41 connect(m_autoActivationTimer
, SIGNAL(timeout()),
42 this, SLOT(slotAutoActivationTimeout()));
45 void DolphinTabBar::dragEnterEvent(QDragEnterEvent
* event
)
47 const QMimeData
* mimeData
= event
->mimeData();
48 const int index
= tabAt(event
->pos());
50 if (KUrl::List::canDecode(mimeData
)) {
51 event
->acceptProposedAction();
52 updateAutoActivationTimer(index
);
55 QTabBar::dragEnterEvent(event
);
58 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent
* event
)
60 updateAutoActivationTimer(-1);
62 QTabBar::dragLeaveEvent(event
);
65 void DolphinTabBar::dragMoveEvent(QDragMoveEvent
* event
)
67 const QMimeData
* mimeData
= event
->mimeData();
68 const int index
= tabAt(event
->pos());
70 if (KUrl::List::canDecode(mimeData
)) {
71 updateAutoActivationTimer(index
);
74 QTabBar::dragMoveEvent(event
);
77 void DolphinTabBar::dropEvent(QDropEvent
* event
)
79 // Disable the auto activation timer
80 updateAutoActivationTimer(-1);
82 const QMimeData
* mimeData
= event
->mimeData();
83 const int index
= tabAt(event
->pos());
85 if (index
>= 0 && KUrl::List::canDecode(mimeData
)) {
86 emit
tabDropEvent(index
, event
);
89 QTabBar::dropEvent(event
);
92 void DolphinTabBar::mousePressEvent(QMouseEvent
* event
)
94 const int index
= tabAt(event
->pos());
96 if (index
>= 0 && event
->button() == Qt::MiddleButton
) {
97 // Mouse middle click on a tab closes this tab.
98 emit
tabCloseRequested(index
);
102 QTabBar::mousePressEvent(event
);
105 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent
* event
)
107 const int index
= tabAt(event
->pos());
110 // Double click on the empty tabbar area opens a new activated tab
111 // with the url from the current tab.
112 emit
openNewActivatedTab(currentIndex());
116 QTabBar::mouseDoubleClickEvent(event
);
119 void DolphinTabBar::contextMenuEvent(QContextMenuEvent
* event
)
121 const int index
= tabAt(event
->pos());
127 QAction
* newTabAction
= menu
.addAction(QIcon::fromTheme("tab-new"), i18nc("@action:inmenu", "New Tab"));
128 QAction
* detachTabAction
= menu
.addAction(QIcon::fromTheme("tab-detach"), i18nc("@action:inmenu", "Detach Tab"));
129 QAction
* closeOtherTabsAction
= menu
.addAction(QIcon::fromTheme("tab-close-other"), i18nc("@action:inmenu", "Close Other Tabs"));
130 QAction
* closeTabAction
= menu
.addAction(QIcon::fromTheme("tab-close"), i18nc("@action:inmenu", "Close Tab"));
132 QAction
* selectedAction
= menu
.exec(event
->globalPos());
133 if (selectedAction
== newTabAction
) {
134 emit
openNewActivatedTab(index
);
135 } else if (selectedAction
== detachTabAction
) {
136 emit
tabDetachRequested(index
);
137 } else if (selectedAction
== closeOtherTabsAction
) {
138 const int tabCount
= count();
139 for (int i
= 0; i
< index
; i
++) {
140 emit
tabCloseRequested(0);
142 for (int i
= index
+ 1; i
< tabCount
; i
++) {
143 emit
tabCloseRequested(1);
145 } else if (selectedAction
== closeTabAction
) {
146 emit
tabCloseRequested(index
);
152 QTabBar::contextMenuEvent(event
);
155 void DolphinTabBar::slotAutoActivationTimeout()
157 if (m_autoActivationIndex
>= 0) {
158 setCurrentIndex(m_autoActivationIndex
);
159 updateAutoActivationTimer(-1);
163 void DolphinTabBar::updateAutoActivationTimer(const int index
)
165 if (m_autoActivationIndex
!= index
) {
166 m_autoActivationIndex
= index
;
168 if (m_autoActivationIndex
< 0) {
169 m_autoActivationTimer
->stop();
171 m_autoActivationTimer
->start();