]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphintabbar.cpp
GIT_SILENT Update Appstream for new release
[dolphin.git] / src / dolphintabbar.cpp
index 4c1d9e44ae3a954185531e3111ba636d2fd8b562..4df25263f3f52deb07a5555e60feba2453336a2b 100644 (file)
 #include <KLocalizedString>
 
 #include <QDragEnterEvent>
+#include <QInputDialog>
 #include <QMenu>
 #include <QMimeData>
 #include <QTimer>
 
-DolphinTabBar::DolphinTabBar(QWidget* parent) :
-    QTabBar(parent),
-    m_autoActivationIndex(-1),
-    m_tabToBeClosedOnMiddleMouseButtonRelease(-1)
+class PreventFocusWhileHidden : public QObject
+{
+public:
+    PreventFocusWhileHidden(QObject *parent)
+        : QObject(parent){};
+
+protected:
+    bool eventFilter(QObject *obj, QEvent *ev) override
+    {
+        switch (ev->type()) {
+        case QEvent::Hide:
+            static_cast<QWidget *>(obj)->setFocusPolicy(Qt::NoFocus);
+            return false;
+        case QEvent::Show:
+            static_cast<QWidget *>(obj)->setFocusPolicy(Qt::TabFocus);
+            return false;
+        default:
+            return false;
+        }
+    };
+};
+
+DolphinTabBar::DolphinTabBar(QWidget *parent)
+    : QTabBar(parent)
+    , m_autoActivationIndex(-1)
+    , m_tabToBeClosedOnMiddleMouseButtonRelease(-1)
 {
     setAcceptDrops(true);
     setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
     setMovable(true);
     setTabsClosable(true);
 
+    setFocusPolicy(Qt::NoFocus);
+    installEventFilter(new PreventFocusWhileHidden(this));
+
     m_autoActivationTimer = new QTimer(this);
     m_autoActivationTimer->setSingleShot(true);
     m_autoActivationTimer->setInterval(800);
-    connect(m_autoActivationTimer, &QTimer::timeout,
-            this, &DolphinTabBar::slotAutoActivationTimeout);
+    connect(m_autoActivationTimer, &QTimer::timeout, this, &DolphinTabBar::slotAutoActivationTimeout);
 }
 
-void DolphinTabBar::dragEnterEvent(QDragEnterEventevent)
+void DolphinTabBar::dragEnterEvent(QDragEnterEvent *event)
 {
-    const QMimeDatamimeData = event->mimeData();
-    const int index = tabAt(event->pos());
+    const QMimeData *mimeData = event->mimeData();
+    const int index = tabAt(event->position().toPoint());
 
     if (mimeData->hasUrls()) {
-        if (index >= 0) {
-            event->acceptProposedAction();
-        } else {
-            event->setDropAction(Qt::IgnoreAction);
-            // Still need to accept it to receive dragMoveEvent
-            event->accept();
-        }
+        event->acceptProposedAction();
         updateAutoActivationTimer(index);
     }
 
     QTabBar::dragEnterEvent(event);
 }
 
-void DolphinTabBar::dragLeaveEvent(QDragLeaveEventevent)
+void DolphinTabBar::dragLeaveEvent(QDragLeaveEvent *event)
 {
     updateAutoActivationTimer(-1);
 
     QTabBar::dragLeaveEvent(event);
 }
 
-void DolphinTabBar::dragMoveEvent(QDragMoveEventevent)
+void DolphinTabBar::dragMoveEvent(QDragMoveEvent *event)
 {
-    const QMimeDatamimeData = event->mimeData();
-    const int index = tabAt(event->pos());
+    const QMimeData *mimeData = event->mimeData();
+    const int index = tabAt(event->position().toPoint());
 
     if (mimeData->hasUrls()) {
-        if (index >= 0) {
-            event->acceptProposedAction();
-        } else {
-            event->setDropAction(Qt::IgnoreAction);
-        }
+        Q_EMIT tabDragMoveEvent(index, event);
         updateAutoActivationTimer(index);
     }
 
     QTabBar::dragMoveEvent(event);
 }
 
-void DolphinTabBar::dropEvent(QDropEventevent)
+void DolphinTabBar::dropEvent(QDropEvent *event)
 {
     // Disable the auto activation timer
     updateAutoActivationTimer(-1);
 
-    const QMimeDatamimeData = event->mimeData();
-    const int index = tabAt(event->pos());
+    const QMimeData *mimeData = event->mimeData();
+    const int index = tabAt(event->position().toPoint());
 
-    if (index >= 0 && mimeData->hasUrls()) {
+    if (mimeData->hasUrls()) {
         Q_EMIT tabDropEvent(index, event);
     }
 
     QTabBar::dropEvent(event);
 }
 
-void DolphinTabBar::mousePressEvent(QMouseEventevent)
+void DolphinTabBar::mousePressEvent(QMouseEvent *event)
 {
     const int index = tabAt(event->pos());
 
@@ -104,8 +119,7 @@ void DolphinTabBar::mouseReleaseEvent(QMouseEvent *event)
 {
     const int index = tabAt(event->pos());
 
-    if (index >= 0 && index == m_tabToBeClosedOnMiddleMouseButtonRelease
-        && event->button() == Qt::MiddleButton) {
+    if (index >= 0 && index == m_tabToBeClosedOnMiddleMouseButtonRelease && event->button() == Qt::MiddleButton) {
         // Mouse middle click on a tab closes this tab.
         Q_EMIT tabCloseRequested(index);
         return;
@@ -114,21 +128,24 @@ void DolphinTabBar::mouseReleaseEvent(QMouseEvent *event)
     QTabBar::mouseReleaseEvent(event);
 }
 
-void DolphinTabBar::mouseDoubleClickEvent(QMouseEventevent)
+void DolphinTabBar::mouseDoubleClickEvent(QMouseEvent *event)
 {
-    const int index = tabAt(event->pos());
+    if (event->buttons() & Qt::LeftButton) {
+        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.
-        Q_EMIT openNewActivatedTab(currentIndex());
-        return;
+        if (index < 0) {
+            // empty tabbar area case
+            index = currentIndex();
+        }
+        // Double left click on the tabbar opens a new activated tab
+        // with the url from the doubleclicked tab or currentTab otherwise.
+        Q_EMIT openNewActivatedTab(index);
     }
 
     QTabBar::mouseDoubleClickEvent(event);
 }
 
-void DolphinTabBar::contextMenuEvent(QContextMenuEventevent)
+void DolphinTabBar::contextMenuEvent(QContextMenuEvent *event)
 {
     const int index = tabAt(event->pos());
 
@@ -136,12 +153,14 @@ void DolphinTabBar::contextMenuEvent(QContextMenuEvent* event)
         // Tab context menu
         QMenu menu(this);
 
-        QActionnewTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab"));
-        QActiondetachTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab"));
-        QActioncloseOtherTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs"));
-        QActioncloseTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18nc("@action:inmenu", "Close Tab"));
+        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"));
 
-        QAction* selectedAction = menu.exec(event->globalPos());
+        QAction *renameTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18nc("@action:inmenu", "Rename Tab"));
+
+        QAction *selectedAction = menu.exec(event->globalPos());
         if (selectedAction == newTabAction) {
             Q_EMIT openNewActivatedTab(index);
         } else if (selectedAction == detachTabAction) {
@@ -156,6 +175,13 @@ void DolphinTabBar::contextMenuEvent(QContextMenuEvent* event)
             }
         } else if (selectedAction == closeTabAction) {
             Q_EMIT tabCloseRequested(index);
+        } else if (selectedAction == renameTabAction) {
+            bool renamed = false;
+            const QString tabNewName = QInputDialog::getText(this, i18nc("@title:window for text input", "Rename Tab"), i18n("New tab name:"), QLineEdit::Normal, tabText(index), &renamed);
+
+            if (renamed) {
+                Q_EMIT tabRenamed(index, tabNewName);
+            }
         }
 
         return;
@@ -184,3 +210,5 @@ void DolphinTabBar::updateAutoActivationTimer(const int index)
         }
     }
 }
+
+#include "moc_dolphintabbar.cpp"