2014-07-08 17:38:18 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "dolphintabbar.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <KLocalizedString>
|
2014-10-10 21:36:36 +00:00
|
|
|
#include <QMenu>
|
2014-09-12 11:02:43 +00:00
|
|
|
#include <QIcon>
|
2014-10-21 19:18:43 +00:00
|
|
|
#include <QUrl>
|
|
|
|
#include <QMimeData>
|
2014-07-08 17:38:18 +00:00
|
|
|
|
|
|
|
DolphinTabBar::DolphinTabBar(QWidget* parent) :
|
|
|
|
QTabBar(parent),
|
|
|
|
m_autoActivationIndex(-1)
|
|
|
|
{
|
|
|
|
setAcceptDrops(true);
|
|
|
|
setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
|
|
|
|
setMovable(true);
|
|
|
|
setTabsClosable(true);
|
|
|
|
|
|
|
|
m_autoActivationTimer = new QTimer(this);
|
|
|
|
m_autoActivationTimer->setSingleShot(true);
|
|
|
|
m_autoActivationTimer->setInterval(800);
|
2016-03-16 21:17:37 +00:00
|
|
|
connect(m_autoActivationTimer, &QTimer::timeout,
|
|
|
|
this, &DolphinTabBar::slotAutoActivationTimeout);
|
2014-07-08 17:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinTabBar::dragEnterEvent(QDragEnterEvent* event)
|
|
|
|
{
|
|
|
|
const QMimeData* mimeData = event->mimeData();
|
|
|
|
const int index = tabAt(event->pos());
|
|
|
|
|
2014-10-21 19:18:43 +00:00
|
|
|
if (mimeData->hasUrls()) {
|
2014-07-08 17:38:18 +00:00
|
|
|
event->acceptProposedAction();
|
|
|
|
updateAutoActivationTimer(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::dragEnterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent* event)
|
|
|
|
{
|
|
|
|
updateAutoActivationTimer(-1);
|
|
|
|
|
|
|
|
QTabBar::dragLeaveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinTabBar::dragMoveEvent(QDragMoveEvent* event)
|
|
|
|
{
|
|
|
|
const QMimeData* mimeData = event->mimeData();
|
|
|
|
const int index = tabAt(event->pos());
|
|
|
|
|
2014-10-21 19:18:43 +00:00
|
|
|
if (mimeData->hasUrls()) {
|
2014-07-08 17:38:18 +00:00
|
|
|
updateAutoActivationTimer(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::dragMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinTabBar::dropEvent(QDropEvent* event)
|
|
|
|
{
|
|
|
|
// Disable the auto activation timer
|
|
|
|
updateAutoActivationTimer(-1);
|
|
|
|
|
|
|
|
const QMimeData* mimeData = event->mimeData();
|
|
|
|
const int index = tabAt(event->pos());
|
|
|
|
|
2014-10-21 19:18:43 +00:00
|
|
|
if (index >= 0 && mimeData->hasUrls()) {
|
2014-07-08 17:38:18 +00:00
|
|
|
emit tabDropEvent(index, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::dropEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinTabBar::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
const int index = tabAt(event->pos());
|
|
|
|
|
|
|
|
if (index >= 0 && event->button() == Qt::MiddleButton) {
|
|
|
|
// Mouse middle click on a tab closes this tab.
|
|
|
|
emit tabCloseRequested(index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
const int index = tabAt(event->pos());
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
// Double click on the empty tabbar area opens a new activated tab
|
|
|
|
// with the url from the current tab.
|
|
|
|
emit openNewActivatedTab(currentIndex());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::mouseDoubleClickEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinTabBar::contextMenuEvent(QContextMenuEvent* event)
|
|
|
|
{
|
|
|
|
const int index = tabAt(event->pos());
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
// Tab context menu
|
2014-10-10 21:36:36 +00:00
|
|
|
QMenu menu(this);
|
2014-07-08 17:38:18 +00:00
|
|
|
|
2016-03-16 21:17:37 +00:00
|
|
|
QAction* newTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
|
|
|
|
QAction* detachTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
|
|
|
|
QAction* closeOtherTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
|
|
|
|
QAction* closeTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
|
2014-07-08 17:38:18 +00:00
|
|
|
|
|
|
|
QAction* selectedAction = menu.exec(event->globalPos());
|
|
|
|
if (selectedAction == newTabAction) {
|
|
|
|
emit openNewActivatedTab(index);
|
|
|
|
} else if (selectedAction == detachTabAction) {
|
|
|
|
emit tabDetachRequested(index);
|
|
|
|
} else if (selectedAction == closeOtherTabsAction) {
|
|
|
|
const int tabCount = count();
|
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
emit tabCloseRequested(0);
|
|
|
|
}
|
|
|
|
for (int i = index + 1; i < tabCount; i++) {
|
|
|
|
emit tabCloseRequested(1);
|
|
|
|
}
|
|
|
|
} else if (selectedAction == closeTabAction) {
|
|
|
|
emit tabCloseRequested(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::contextMenuEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinTabBar::slotAutoActivationTimeout()
|
|
|
|
{
|
|
|
|
if (m_autoActivationIndex >= 0) {
|
|
|
|
setCurrentIndex(m_autoActivationIndex);
|
|
|
|
updateAutoActivationTimer(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinTabBar::updateAutoActivationTimer(const int index)
|
|
|
|
{
|
|
|
|
if (m_autoActivationIndex != index) {
|
|
|
|
m_autoActivationIndex = index;
|
|
|
|
|
|
|
|
if (m_autoActivationIndex < 0) {
|
|
|
|
m_autoActivationTimer->stop();
|
|
|
|
} else {
|
|
|
|
m_autoActivationTimer->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|