]> cloud.milkyroute.net Git - dolphin.git/commitdiff
- Factored out the Delete/Move To Trash action into own class.
authorDawit Alemayehu <adawit@kde.org>
Sat, 9 Feb 2013 20:56:51 +0000 (15:56 -0500)
committerDawit Alemayehu <adawit@kde.org>
Tue, 14 May 2013 12:37:21 +0000 (08:37 -0400)
- Updated both the Dolphin KPart and context menu to use the new
  DolphinRemoveAction class to manage "Delete/Move to Trash" actions.

See also https://git.reviewboard.kde.org/r/107509/.

REVIEW: 108802

src/CMakeLists.txt
src/dolphincontextmenu.cpp
src/dolphincontextmenu.h
src/dolphinpart.cpp
src/dolphinpart.h
src/dolphinremoveaction.cpp [new file with mode: 0644]
src/dolphinremoveaction.h [new file with mode: 0644]

index ffb232ce286ad6dc646cd17f611606a17eb8ad35..6856991d5ef119d994414edbcfc8cba5c3046114 100644 (file)
@@ -91,6 +91,7 @@ set(dolphinprivate_LIB_SRCS
     views/viewmodecontroller.cpp
     views/viewproperties.cpp
     views/zoomlevelinfo.cpp
+    dolphinremoveaction.cpp
 )
 
 if(HAVE_NEPOMUK)
index 89a169f32d439dfe4b39fee02a79fb6b21356121..af8b61384938c6dd8d2a0d7b37c838dd9aa9c16d 100644 (file)
@@ -24,6 +24,7 @@
 #include "dolphinnewfilemenu.h"
 #include "dolphinviewcontainer.h"
 #include "dolphin_generalsettings.h"
+#include "dolphinremoveaction.h"
 
 #include <KActionCollection>
 #include <KDesktopFile>
@@ -73,17 +74,14 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
     m_context(NoContext),
     m_copyToMenu(parent),
     m_customActions(),
-    m_command(None),
-    m_shiftPressed(qApp->keyboardModifiers() & Qt::ShiftModifier),
-    m_removeAction(0)
+    m_command(None)
 {
     // The context menu either accesses the URLs of the selected items
     // or the items itself. To increase the performance both lists are cached.
     const DolphinView* view = m_mainWindow->activeViewContainer()->view();
     m_selectedItems = view->selectedItems();
 
-    m_removeAction = new QAction(this);
-    connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
+    m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
 }
 
 DolphinContextMenu::~DolphinContextMenu()
@@ -129,8 +127,7 @@ DolphinContextMenu::Command DolphinContextMenu::open()
 void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
 {
     if (ev->key() == Qt::Key_Shift) {
-        m_shiftPressed = true;
-        updateRemoveAction();
+        m_removeAction->update();
     }
     KMenu::keyPressEvent(ev);
 }
@@ -138,23 +135,11 @@ void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
 void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
 {
     if (ev->key() == Qt::Key_Shift) {
-        // not just "m_shiftPressed = false", the user could be playing with both Shift keys...
-        m_shiftPressed = qApp->keyboardModifiers() & Qt::ShiftModifier;
-        updateRemoveAction();
+        m_removeAction->update();
     }
     KMenu::keyReleaseEvent(ev);
 }
 
-void DolphinContextMenu::slotRemoveActionTriggered()
-{
-    const KActionCollection* collection = m_mainWindow->actionCollection();
-    if (moveToTrash()) {
-        collection->action("move_to_trash")->trigger();
-    } else {
-        collection->action("delete")->trigger();
-    }
-}
-
 void DolphinContextMenu::openTrashContextMenu()
 {
     Q_ASSERT(m_context & TrashContext);
@@ -374,7 +359,7 @@ void DolphinContextMenu::insertDefaultItemActions()
         addAction(collection->action("delete"));
     } else {
         addAction(m_removeAction);
-        updateRemoveAction();
+        m_removeAction->update();
     }
 }
 
@@ -509,28 +494,4 @@ void DolphinContextMenu::addCustomActions()
     }
 }
 
-void DolphinContextMenu::updateRemoveAction()
-{
-    const KActionCollection* collection = m_mainWindow->actionCollection();
-
-    // Using m_removeAction->setText(action->text()) does not apply the &-shortcut.
-    // This is only done until the original action has been shown at least once. To
-    // bypass this issue, the text and &-shortcut is applied manually.
-    const QAction* action = 0;
-    if (moveToTrash()) {
-        action = collection->action("move_to_trash");
-        m_removeAction->setText(i18nc("@action:inmenu", "&Move to Trash"));
-    } else {
-        action = collection->action("delete");
-        m_removeAction->setText(i18nc("@action:inmenu", "&Delete"));
-    }
-    m_removeAction->setIcon(action->icon());
-    m_removeAction->setShortcuts(action->shortcuts());
-}
-
-bool DolphinContextMenu::moveToTrash() const
-{
-    return !m_shiftPressed;
-}
-
 #include "dolphincontextmenu.moc"
index 1c65fab138307dac0c848ee42c858d07e6393690..160f088040190ee3a2db4ebdb5957eee9f12fc6b 100644 (file)
@@ -36,6 +36,7 @@ class QAction;
 class DolphinMainWindow;
 class KFileItemActions;
 class KFileItemListProperties;
+class DolphinRemoveAction;
 
 /**
  * @brief Represents the context menu which appears when doing a right
@@ -94,13 +95,6 @@ protected:
     virtual void keyPressEvent(QKeyEvent *ev);
     virtual void keyReleaseEvent(QKeyEvent *ev);
 
-private slots:
-    /**
-     * Triggers the 'Delete'-action if the shift-key has been pressed, otherwise
-     * the 'Move to Trash'-action gets triggered.
-     */
-    void slotRemoveActionTriggered();
-
 private:
     void openTrashContextMenu();
     void openTrashItemContextMenu();
@@ -148,20 +142,6 @@ private:
      */
     void addCustomActions();
 
-    /**
-     * Updates m_removeAction to represent the 'Delete'-action if the shift-key
-     * has been pressed or the selection is not local. Otherwise it represents
-     * the 'Move to Trash'-action.
-     */
-    void updateRemoveAction();
-
-    /**
-     * @return True if a moving to the trash should be done instead of
-     *         deleting the selected items.
-     * @see updateRemoveAction(), slotRemoveActionTriggered()
-     */
-    bool moveToTrash() const;
-
 private:
     struct Entry
     {
@@ -197,8 +177,7 @@ private:
 
     Command m_command;
 
-    bool m_shiftPressed;
-    QAction* m_removeAction; // Action that represents either 'Move To Trash' or 'Delete'
+    DolphinRemoveAction* m_removeAction; // Action that represents either 'Move To Trash' or 'Delete'
 };
 
 #endif
index 627ba79c56570e87e01d61c40288761aa8610294..642b1501365a1774a75ac25238e485e96efa29d8 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include "dolphinpart.h"
+#include "dolphinremoveaction.h"
 
 #include <KFileItemListProperties>
 #include <konq_operations.h>
@@ -64,6 +65,7 @@ K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantList& args)
     : KParts::ReadOnlyPart(parent)
       ,m_openTerminalAction(0)
+      ,m_removeAction(0)
 {
     Q_UNUSED(args)
     setComponentData(DolphinPartFactory::componentData(), false);
@@ -145,6 +147,10 @@ DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantL
     m_actionHandler->updateViewActions();
     slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
 
+    // Listen to events from the app so we can update the remove key by
+    // checking for a Shift key press.
+    qApp->installEventFilter(this);
+
     // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
     // (sort of spacial navigation)
 
@@ -447,10 +453,18 @@ void DolphinPart::slotOpenContextMenu(const QPoint& pos,
             }
         }
 
-        if (addTrash)
+        if (!addTrash || !addDel) {
+            if (!m_removeAction) {
+                m_removeAction = new DolphinRemoveAction(this, actionCollection());
+            }
+            editActions.append(m_removeAction);
+            m_removeAction->update();
+        } else {
+            delete m_removeAction;
+            m_removeAction = 0;
             editActions.append(actionCollection()->action("move_to_trash"));
-        if (addDel)
             editActions.append(actionCollection()->action("delete"));
+        }
 
         // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
         // since otherwise the created file would not be visible.
@@ -593,6 +607,23 @@ void DolphinPart::setFilesToSelect(const KUrl::List& files)
     m_view->markUrlAsCurrent(files.at(0));
 }
 
+bool DolphinPart::eventFilter(QObject* obj, QEvent* event)
+{
+    const int type = event->type();
+
+    if ((type == QEvent::KeyPress || type == QEvent::KeyRelease) && m_removeAction) {
+        QMenu* menu = qobject_cast<QMenu*>(obj);
+        if (menu && menu->parent() == m_view) {
+            QKeyEvent* ev = static_cast<QKeyEvent*>(event);
+            if (ev->key() == Qt::Key_Shift) {
+                m_removeAction->update();
+            }
+        }
+    }
+
+    return KParts::ReadOnlyPart::eventFilter(obj, event);
+}
+
 ////
 
 void DolphinPartBrowserExtension::restoreState(QDataStream &stream)
index 7881ded43c15a1c1d9e4adeee11fe9427c81f272..172bfafc6d14b0dac5c7a5bc3b327902d9b43bbd 100644 (file)
@@ -39,6 +39,7 @@ class DolphinModel;
 class KDirLister;
 class DolphinView;
 class KAboutData;
+class DolphinRemoveAction;
 
 class DolphinPart : public KParts::ReadOnlyPart
 {
@@ -227,6 +228,8 @@ private Q_SLOTS:
     void setFilesToSelect(const KUrl::List& files);
     KUrl::List filesToSelect() const { return KUrl::List(); } // silence moc
 
+    virtual bool eventFilter(QObject*, QEvent*);
+
 private:
     void createActions();
     void createGoAction(const char* name, const char* iconName,
@@ -245,6 +248,7 @@ private:
     KAction* m_findFileAction;
     KAction* m_openTerminalAction;
     QString m_nameFilter;
+    DolphinRemoveAction* m_removeAction;
     Q_DISABLE_COPY(DolphinPart)
 };
 
diff --git a/src/dolphinremoveaction.cpp b/src/dolphinremoveaction.cpp
new file mode 100644 (file)
index 0000000..200fc40
--- /dev/null
@@ -0,0 +1,60 @@
+/***************************************************************************
+ *   Copyright (C) 2013 by Dawit Alemayehu <adawit@kde.org                 *
+ *                                                                         *
+ *   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 "dolphinremoveaction.h"
+
+#include <QApplication>
+
+#include <KLocalizedString>
+
+
+DolphinRemoveAction::DolphinRemoveAction(QObject* parent, KActionCollection* collection) :
+    QAction(parent),
+    m_collection(collection)
+{
+    update();
+    connect(this, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
+}
+
+void DolphinRemoveAction::slotRemoveActionTriggered()
+{
+    if (m_action) {
+        m_action->trigger();
+    }
+}
+
+void DolphinRemoveAction::update()
+{
+    Q_ASSERT(m_collection);
+    // Using setText(action->text()) does not apply the &-shortcut.
+    // This is only done until the original action has been shown at least once. To
+    // bypass this issue, the text and &-shortcut is applied manually.
+    if (qApp->keyboardModifiers() & Qt::ShiftModifier) {
+        m_action = m_collection ? m_collection->action("delete") : 0;
+        setText(i18nc("@action:inmenu", "&Delete"));
+    } else {
+        m_action = m_collection ? m_collection->action("move_to_trash") : 0;
+        setText(i18nc("@action:inmenu", "&Move to Trash"));
+    }
+
+   if (m_action) {
+        setIcon(m_action->icon());
+        setShortcuts(m_action->shortcuts());
+    }
+}
diff --git a/src/dolphinremoveaction.h b/src/dolphinremoveaction.h
new file mode 100644 (file)
index 0000000..1a123ac
--- /dev/null
@@ -0,0 +1,55 @@
+/***************************************************************************
+ *   Copyright (C) 2013 by Dawit Alemayehu <adawit@kde.org                 *
+ *                                                                         *
+ *   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            *
+ ***************************************************************************/
+
+#ifndef DOLPHINREMOVEACTION_H
+#define DOLPHINREMOVEACTION_H
+
+#include "libdolphin_export.h"
+
+#include <QAction>
+#include <QPointer>
+
+#include <KActionCollection>
+
+/**
+ * A QAction that manages the delete based on the current state of
+ * the Shift key or the parameter passed to update.
+ *
+ * This class expects the presence of both the "move_to_trash" and "delete"
+ * actions in @ref collection.
+ */
+class LIBDOLPHINPRIVATE_EXPORT DolphinRemoveAction : public QAction
+{
+  Q_OBJECT
+public:
+    DolphinRemoveAction(QObject* parent, KActionCollection* collection);
+    /**
+     * Updates this action key based on the state of the Shift key.
+     */
+    void update();
+
+private Q_SLOTS:
+    void slotRemoveActionTriggered();
+
+private:
+    QPointer<KActionCollection> m_collection;
+    QPointer<QAction> m_action;
+};
+
+#endif