Warning, file /system/dolphin/src/dolphintabbar.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Emmanuel Pescosta <emmanuelpescosta099@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "dolphintabbar.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include <QDragEnterEvent>
0012 #include <QMenu>
0013 #include <QMimeData>
0014 #include <QTimer>
0015 
0016 DolphinTabBar::DolphinTabBar(QWidget *parent)
0017     : QTabBar(parent)
0018     , m_autoActivationIndex(-1)
0019     , m_tabToBeClosedOnMiddleMouseButtonRelease(-1)
0020 {
0021     setAcceptDrops(true);
0022     setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
0023     setMovable(true);
0024     setTabsClosable(true);
0025 
0026     m_autoActivationTimer = new QTimer(this);
0027     m_autoActivationTimer->setSingleShot(true);
0028     m_autoActivationTimer->setInterval(800);
0029     connect(m_autoActivationTimer, &QTimer::timeout, this, &DolphinTabBar::slotAutoActivationTimeout);
0030 }
0031 
0032 void DolphinTabBar::dragEnterEvent(QDragEnterEvent *event)
0033 {
0034     const QMimeData *mimeData = event->mimeData();
0035     const int index = tabAt(event->position().toPoint());
0036 
0037     if (mimeData->hasUrls()) {
0038         event->acceptProposedAction();
0039         updateAutoActivationTimer(index);
0040     }
0041 
0042     QTabBar::dragEnterEvent(event);
0043 }
0044 
0045 void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent *event)
0046 {
0047     updateAutoActivationTimer(-1);
0048 
0049     QTabBar::dragLeaveEvent(event);
0050 }
0051 
0052 void DolphinTabBar::dragMoveEvent(QDragMoveEvent *event)
0053 {
0054     const QMimeData *mimeData = event->mimeData();
0055     const int index = tabAt(event->position().toPoint());
0056 
0057     if (mimeData->hasUrls()) {
0058         updateAutoActivationTimer(index);
0059     }
0060 
0061     QTabBar::dragMoveEvent(event);
0062 }
0063 
0064 void DolphinTabBar::dropEvent(QDropEvent *event)
0065 {
0066     // Disable the auto activation timer
0067     updateAutoActivationTimer(-1);
0068 
0069     const QMimeData *mimeData = event->mimeData();
0070     const int index = tabAt(event->position().toPoint());
0071 
0072     if (mimeData->hasUrls()) {
0073         Q_EMIT tabDropEvent(index, event);
0074     }
0075 
0076     QTabBar::dropEvent(event);
0077 }
0078 
0079 void DolphinTabBar::mousePressEvent(QMouseEvent *event)
0080 {
0081     const int index = tabAt(event->pos());
0082 
0083     if (index >= 0 && event->button() == Qt::MiddleButton) {
0084         m_tabToBeClosedOnMiddleMouseButtonRelease = index;
0085         return;
0086     }
0087 
0088     QTabBar::mousePressEvent(event);
0089 }
0090 
0091 void DolphinTabBar::mouseReleaseEvent(QMouseEvent *event)
0092 {
0093     const int index = tabAt(event->pos());
0094 
0095     if (index >= 0 && index == m_tabToBeClosedOnMiddleMouseButtonRelease && event->button() == Qt::MiddleButton) {
0096         // Mouse middle click on a tab closes this tab.
0097         Q_EMIT tabCloseRequested(index);
0098         return;
0099     }
0100 
0101     QTabBar::mouseReleaseEvent(event);
0102 }
0103 
0104 void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent *event)
0105 {
0106     if (event->buttons() & Qt::LeftButton) {
0107         int index = tabAt(event->pos());
0108 
0109         if (index < 0) {
0110             // empty tabbar area case
0111             index = currentIndex();
0112         }
0113         // Double left click on the tabbar opens a new activated tab
0114         // with the url from the doubleclicked tab or currentTab otherwise.
0115         Q_EMIT openNewActivatedTab(index);
0116     }
0117 
0118     QTabBar::mouseDoubleClickEvent(event);
0119 }
0120 
0121 void DolphinTabBar::contextMenuEvent(QContextMenuEvent *event)
0122 {
0123     const int index = tabAt(event->pos());
0124 
0125     if (index >= 0) {
0126         // Tab context menu
0127         QMenu menu(this);
0128 
0129         QAction *newTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
0130         QAction *detachTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
0131         QAction *closeOtherTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
0132         QAction *closeTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
0133 
0134         QAction *selectedAction = menu.exec(event->globalPos());
0135         if (selectedAction == newTabAction) {
0136             Q_EMIT openNewActivatedTab(index);
0137         } else if (selectedAction == detachTabAction) {
0138             Q_EMIT tabDetachRequested(index);
0139         } else if (selectedAction == closeOtherTabsAction) {
0140             const int tabCount = count();
0141             for (int i = 0; i < index; i++) {
0142                 Q_EMIT tabCloseRequested(0);
0143             }
0144             for (int i = index + 1; i < tabCount; i++) {
0145                 Q_EMIT tabCloseRequested(1);
0146             }
0147         } else if (selectedAction == closeTabAction) {
0148             Q_EMIT tabCloseRequested(index);
0149         }
0150 
0151         return;
0152     }
0153 
0154     QTabBar::contextMenuEvent(event);
0155 }
0156 
0157 void DolphinTabBar::slotAutoActivationTimeout()
0158 {
0159     if (m_autoActivationIndex >= 0) {
0160         setCurrentIndex(m_autoActivationIndex);
0161         updateAutoActivationTimer(-1);
0162     }
0163 }
0164 
0165 void DolphinTabBar::updateAutoActivationTimer(const int index)
0166 {
0167     if (m_autoActivationIndex != index) {
0168         m_autoActivationIndex = index;
0169 
0170         if (m_autoActivationIndex < 0) {
0171             m_autoActivationTimer->stop();
0172         } else {
0173             m_autoActivationTimer->start();
0174         }
0175     }
0176 }
0177 
0178 #include "moc_dolphintabbar.cpp"