- Factored out the Delete/Move To Trash action into own class.

- 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
This commit is contained in:
Dawit Alemayehu 2013-02-09 15:56:51 -05:00
parent 799641859e
commit 01761798a9
7 changed files with 161 additions and 70 deletions

View file

@ -91,6 +91,7 @@ set(dolphinprivate_LIB_SRCS
views/viewmodecontroller.cpp views/viewmodecontroller.cpp
views/viewproperties.cpp views/viewproperties.cpp
views/zoomlevelinfo.cpp views/zoomlevelinfo.cpp
dolphinremoveaction.cpp
) )
if(HAVE_NEPOMUK) if(HAVE_NEPOMUK)

View file

@ -24,6 +24,7 @@
#include "dolphinnewfilemenu.h" #include "dolphinnewfilemenu.h"
#include "dolphinviewcontainer.h" #include "dolphinviewcontainer.h"
#include "dolphin_generalsettings.h" #include "dolphin_generalsettings.h"
#include "dolphinremoveaction.h"
#include <KActionCollection> #include <KActionCollection>
#include <KDesktopFile> #include <KDesktopFile>
@ -73,17 +74,14 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
m_context(NoContext), m_context(NoContext),
m_copyToMenu(parent), m_copyToMenu(parent),
m_customActions(), m_customActions(),
m_command(None), m_command(None)
m_shiftPressed(qApp->keyboardModifiers() & Qt::ShiftModifier),
m_removeAction(0)
{ {
// The context menu either accesses the URLs of the selected items // The context menu either accesses the URLs of the selected items
// or the items itself. To increase the performance both lists are cached. // or the items itself. To increase the performance both lists are cached.
const DolphinView* view = m_mainWindow->activeViewContainer()->view(); const DolphinView* view = m_mainWindow->activeViewContainer()->view();
m_selectedItems = view->selectedItems(); m_selectedItems = view->selectedItems();
m_removeAction = new QAction(this); m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
} }
DolphinContextMenu::~DolphinContextMenu() DolphinContextMenu::~DolphinContextMenu()
@ -129,8 +127,7 @@ DolphinContextMenu::Command DolphinContextMenu::open()
void DolphinContextMenu::keyPressEvent(QKeyEvent *ev) void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
{ {
if (ev->key() == Qt::Key_Shift) { if (ev->key() == Qt::Key_Shift) {
m_shiftPressed = true; m_removeAction->update();
updateRemoveAction();
} }
KMenu::keyPressEvent(ev); KMenu::keyPressEvent(ev);
} }
@ -138,23 +135,11 @@ void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev) void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
{ {
if (ev->key() == Qt::Key_Shift) { if (ev->key() == Qt::Key_Shift) {
// not just "m_shiftPressed = false", the user could be playing with both Shift keys... m_removeAction->update();
m_shiftPressed = qApp->keyboardModifiers() & Qt::ShiftModifier;
updateRemoveAction();
} }
KMenu::keyReleaseEvent(ev); 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() void DolphinContextMenu::openTrashContextMenu()
{ {
Q_ASSERT(m_context & TrashContext); Q_ASSERT(m_context & TrashContext);
@ -374,7 +359,7 @@ void DolphinContextMenu::insertDefaultItemActions()
addAction(collection->action("delete")); addAction(collection->action("delete"));
} else { } else {
addAction(m_removeAction); 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" #include "dolphincontextmenu.moc"

View file

@ -36,6 +36,7 @@ class QAction;
class DolphinMainWindow; class DolphinMainWindow;
class KFileItemActions; class KFileItemActions;
class KFileItemListProperties; class KFileItemListProperties;
class DolphinRemoveAction;
/** /**
* @brief Represents the context menu which appears when doing a right * @brief Represents the context menu which appears when doing a right
@ -94,13 +95,6 @@ protected:
virtual void keyPressEvent(QKeyEvent *ev); virtual void keyPressEvent(QKeyEvent *ev);
virtual void keyReleaseEvent(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: private:
void openTrashContextMenu(); void openTrashContextMenu();
void openTrashItemContextMenu(); void openTrashItemContextMenu();
@ -148,20 +142,6 @@ private:
*/ */
void addCustomActions(); 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: private:
struct Entry struct Entry
{ {
@ -197,8 +177,7 @@ private:
Command m_command; Command m_command;
bool m_shiftPressed; DolphinRemoveAction* m_removeAction; // Action that represents either 'Move To Trash' or 'Delete'
QAction* m_removeAction; // Action that represents either 'Move To Trash' or 'Delete'
}; };
#endif #endif

View file

@ -18,6 +18,7 @@
*/ */
#include "dolphinpart.h" #include "dolphinpart.h"
#include "dolphinremoveaction.h"
#include <KFileItemListProperties> #include <KFileItemListProperties>
#include <konq_operations.h> #include <konq_operations.h>
@ -64,6 +65,7 @@ K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantList& args) DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantList& args)
: KParts::ReadOnlyPart(parent) : KParts::ReadOnlyPart(parent)
,m_openTerminalAction(0) ,m_openTerminalAction(0)
,m_removeAction(0)
{ {
Q_UNUSED(args) Q_UNUSED(args)
setComponentData(DolphinPartFactory::componentData(), false); setComponentData(DolphinPartFactory::componentData(), false);
@ -145,6 +147,10 @@ DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantL
m_actionHandler->updateViewActions(); m_actionHandler->updateViewActions();
slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions 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 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
// (sort of spacial navigation) // (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")); editActions.append(actionCollection()->action("move_to_trash"));
if (addDel)
editActions.append(actionCollection()->action("delete")); editActions.append(actionCollection()->action("delete"));
}
// Normally KonqPopupMenu only shows the "Create new" submenu in the current view // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
// since otherwise the created file would not be visible. // 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)); 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) void DolphinPartBrowserExtension::restoreState(QDataStream &stream)

View file

@ -39,6 +39,7 @@ class DolphinModel;
class KDirLister; class KDirLister;
class DolphinView; class DolphinView;
class KAboutData; class KAboutData;
class DolphinRemoveAction;
class DolphinPart : public KParts::ReadOnlyPart class DolphinPart : public KParts::ReadOnlyPart
{ {
@ -227,6 +228,8 @@ private Q_SLOTS:
void setFilesToSelect(const KUrl::List& files); void setFilesToSelect(const KUrl::List& files);
KUrl::List filesToSelect() const { return KUrl::List(); } // silence moc KUrl::List filesToSelect() const { return KUrl::List(); } // silence moc
virtual bool eventFilter(QObject*, QEvent*);
private: private:
void createActions(); void createActions();
void createGoAction(const char* name, const char* iconName, void createGoAction(const char* name, const char* iconName,
@ -245,6 +248,7 @@ private:
KAction* m_findFileAction; KAction* m_findFileAction;
KAction* m_openTerminalAction; KAction* m_openTerminalAction;
QString m_nameFilter; QString m_nameFilter;
DolphinRemoveAction* m_removeAction;
Q_DISABLE_COPY(DolphinPart) Q_DISABLE_COPY(DolphinPart)
}; };

View file

@ -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());
}
}

55
src/dolphinremoveaction.h Normal file
View file

@ -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