#include <kmenubar.h>
#include <kmessagebox.h>
#include <kmimetypetrader.h>
+#include <kmodifierkeyinfo.h>
#include <knewfilemenu.h>
#include <konqmimedata.h>
#include <konq_operations.h>
#include "views/dolphinview.h"
#include "views/viewmodecontroller.h"
+K_GLOBAL_STATIC(KModifierKeyInfo, m_keyInfo)
+
DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
const KFileItem& fileInfo,
const KUrl& baseUrl) :
m_copyToMenu(parent),
m_customActions(),
m_popup(new KMenu(m_mainWindow)),
- m_showDeleteCommand(false),
+ m_command(None),
m_shiftPressed(false),
- m_keyInfo()
+ m_removeAction(0)
{
// The context menu either accesses the URLs of the selected items
// or the items itself. To increase the performance both lists are cached.
m_selectedUrls = view->selectedUrls();
m_selectedItems = view->selectedItems();
- m_showDeleteCommand = KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false);
-
- if (m_keyInfo.isKeyPressed(Qt::Key_Shift) || m_keyInfo.isKeyLatched(Qt::Key_Shift)) {
- m_shiftPressed = true;
+ if (m_keyInfo != 0) {
+ if (m_keyInfo->isKeyPressed(Qt::Key_Shift) || m_keyInfo->isKeyLatched(Qt::Key_Shift)) {
+ m_shiftPressed = true;
+ }
+ connect(m_keyInfo, SIGNAL(keyPressed(Qt::Key, bool)),
+ this, SLOT(slotKeyModifierPressed(Qt::Key, bool)));
}
- connect(&m_keyInfo, SIGNAL(keyPressed(Qt::Key, bool)), this, SLOT(deleteOrTrashMenuEntry(Qt::Key, bool)));
+ m_removeAction = new QAction(this);
+ connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
}
DolphinContextMenu::~DolphinContextMenu()
m_customActions = actions;
}
-void DolphinContextMenu::open()
+DolphinContextMenu::Command DolphinContextMenu::open()
{
// get the context information
- if (m_baseUrl.protocol() == "trash") {
+ if (m_baseUrl.protocol() == QLatin1String("trash")) {
m_context |= TrashContext;
}
Q_ASSERT(m_context == NoContext);
openViewportContextMenu();
}
+
+ return m_command;
+}
+
+void DolphinContextMenu::initializeModifierKeyInfo()
+{
+ // Access m_keyInfo, so that it gets instantiated by
+ // K_GLOBAL_STATIC
+ KModifierKeyInfo* keyInfo = m_keyInfo;
+ Q_UNUSED(keyInfo);
+}
+
+void DolphinContextMenu::slotKeyModifierPressed(Qt::Key key, bool pressed)
+{
+ m_shiftPressed = (key == Qt::Key_Shift) && pressed;
+ updateRemoveAction();
+}
+
+void DolphinContextMenu::slotRemoveActionTriggered()
+{
+ const KActionCollection* collection = m_mainWindow->actionCollection();
+ if (m_shiftPressed) {
+ collection->action("delete")->trigger();
+ } else {
+ collection->action("move_to_trash")->trigger();
+ }
}
void DolphinContextMenu::openTrashContextMenu()
{
Q_ASSERT(!m_fileInfo.isNull());
- if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
- // setup 'Create New' menu
- DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_popup.data(), m_mainWindow);
- const DolphinView* view = m_mainWindow->activeViewContainer()->view();
- newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
- newFileMenu->checkUpToDate();
- newFileMenu->setPopupFiles(m_fileInfo.url());
- newFileMenu->setEnabled(capabilities().supportsWriting());
-
- KMenu* menu = newFileMenu->menu();
- menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
- menu->setIcon(KIcon("document-new"));
- m_popup->addMenu(menu);
- m_popup->addSeparator();
-
- // insert 'Open in new window' and 'Open in new tab' entries
- m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
- m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
- m_popup->addSeparator();
+ QAction* openParentInNewWindowAction = 0;
+ QAction* openParentInNewTabAction = 0;
+ QAction* addToPlacesAction = 0;
+ if (m_selectedUrls.count() == 1) {
+ if (m_fileInfo.isDir()) {
+ // setup 'Create New' menu
+ DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_popup.data(), m_mainWindow);
+ const DolphinView* view = m_mainWindow->activeViewContainer()->view();
+ newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
+ newFileMenu->checkUpToDate();
+ newFileMenu->setPopupFiles(m_fileInfo.url());
+ newFileMenu->setEnabled(capabilities().supportsWriting());
+
+ KMenu* menu = newFileMenu->menu();
+ menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
+ menu->setIcon(KIcon("document-new"));
+ m_popup->addMenu(menu);
+ m_popup->addSeparator();
+
+ // insert 'Open in new window' and 'Open in new tab' entries
+ m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
+ m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
+
+ // insert 'Add to Places' entry
+ if (!placeExists(m_fileInfo.url())) {
+ addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
+ i18nc("@action:inmenu Add selected folder to places",
+ "Add to Places"));
+ }
+
+ m_popup->addSeparator();
+ } else if (m_baseUrl.protocol().contains("search")) {
+ openParentInNewWindowAction = new QAction(KIcon("window-new"),
+ i18nc("@action:inmenu",
+ "Open Path in New Window"),
+ this);
+ m_popup->addAction(openParentInNewWindowAction);
+
+ openParentInNewTabAction = new QAction(KIcon("tab-new"),
+ i18nc("@action:inmenu",
+ "Open Path in New Tab"),
+ this);
+ m_popup->addAction(openParentInNewTabAction);
+
+ m_popup->addSeparator();
+ }
}
+
addShowMenubarAction();
insertDefaultItemActions();
m_copyToMenu.setItems(m_selectedItems);
m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
m_copyToMenu.addActionsTo(m_popup.data());
- m_popup->addSeparator();
- }
-
- // insert 'Add to Places' entry if exactly one item is selected
- QAction* addToPlacesAction = 0;
- if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1) && !placeExists(m_fileInfo.url())) {
- addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
- i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
}
// insert 'Properties...' entry
m_popup->addAction(propertiesAction);
QAction* activatedAction = m_popup->exec(QCursor::pos());
-
- if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
- const KUrl selectedUrl(m_fileInfo.url());
- if (selectedUrl.isValid()) {
- DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
- selectedUrl);
+ if (activatedAction != 0) {
+ if (activatedAction == addToPlacesAction) {
+ const KUrl selectedUrl(m_fileInfo.url());
+ if (selectedUrl.isValid()) {
+ DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
+ selectedUrl);
+ }
+ } else if (activatedAction == openParentInNewWindowAction) {
+ m_command = OpenParentFolderInNewWindow;
+ } else if (activatedAction == openParentInNewTabAction) {
+ m_command = OpenParentFolderInNewTab;
}
}
}
{
const KActionCollection* collection = m_mainWindow->actionCollection();
- // Cut action
+ // Insert 'Cut', 'Copy' and 'Paste'
m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
-
- // Copy action
m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
-
- // Paste action
m_popup->addAction(createPasteAction());
m_popup->addSeparator();
QAction* renameAction = collection->action("rename");
m_popup->addAction(renameAction);
- // Insert move to trash and delete. We need to insert both because both can be visible.
- m_popup->addAction(collection->action("move_to_trash"));
- m_popup->addAction(collection->action("delete"));
-
- const KUrl& url = m_mainWindow->activeViewContainer()->url();
- if (url.isLocalFile()) {
- collection->action("move_to_trash")->setVisible(true);
- collection->action("delete")->setVisible(false);
+ // Insert 'Move to Trash' and/or 'Delete'
+ if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
+ m_popup->addAction(collection->action("move_to_trash"));
+ m_popup->addAction(collection->action("delete"));
} else {
- collection->action("delete")->setVisible(true);
- }
-
- if(m_showDeleteCommand) {
- collection->action("delete")->setVisible(true);
+ m_popup->addAction(m_removeAction);
+ updateRemoveAction();
}
-
- if(m_shiftPressed) {
- deleteOrTrashMenuEntry(Qt::Key_Shift, m_shiftPressed);
- }
-
}
void DolphinContextMenu::addShowMenubarAction()
fileItemActions.addOpenWithActionsTo(m_popup.data(), "DesktopEntryName != 'dolphin'");
// insert 'Actions' sub menu
- if (fileItemActions.addServiceActionsTo(m_popup.data())) {
- m_popup->addSeparator();
- }
+ fileItemActions.addServiceActionsTo(m_popup.data());
}
void DolphinContextMenu::addVersionControlActions()
}
}
-void DolphinContextMenu::deleteOrTrashMenuEntry(Qt::Key key, bool pressed)
+void DolphinContextMenu::updateRemoveAction()
{
- if(m_mainWindow->activeViewContainer()->url().isLocalFile() && !m_showDeleteCommand && key == Qt::Key_Shift) {
-
- // Set the current size as fixed size so that the menu isn't flickering when pressing shift.
- m_popup->setFixedSize(m_popup->size());
- if(pressed) {
- m_mainWindow->actionCollection()->action("delete")->setVisible(true);
- m_mainWindow->actionCollection()->action("move_to_trash")->setVisible(false);
- }
- else {
- m_mainWindow->actionCollection()->action("delete")->setVisible(false);
- m_mainWindow->actionCollection()->action("move_to_trash")->setVisible(true);
- }
-
- // This sets the menu back to a dynamic size followed by a forced resize incase the newly made visible action has bigger text.
- m_popup->setFixedSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
- m_popup->resize(m_popup->sizeHint());
- }
+ const KActionCollection* collection = m_mainWindow->actionCollection();
+ const bool moveToTrash = capabilities().isLocal() && !m_shiftPressed;
+ const QAction* action = moveToTrash ? collection->action("move_to_trash") : collection->action("delete");
+ m_removeAction->setText(action->text());
+ m_removeAction->setIcon(action->icon());
+ m_removeAction->setShortcuts(action->shortcuts());
}
#include "dolphincontextmenu.moc"