Finding out whether shift is pressed initially can be done
with qApp->keyboardModifiers(), and finding out that the user is pressing
or release shift can be done with keyPressEvent/keyReleaseEvent.
This required to inherit from KMenu rather than having the KMenu
as a member.
KModifierKeyInfo is only implemented on X11, so this makes the code
more portable. If similar solutions can be found for other users of
KModifierKeyInfo, it will be deprecated in KF5.
REVIEW: 110303
#include <KMenuBar>
#include <KMessageBox>
#include <KMimeTypeTrader>
#include <KMenuBar>
#include <KMessageBox>
#include <KMimeTypeTrader>
-#include <KModifierKeyInfo>
#include <KNewFileMenu>
#include <konqmimedata.h>
#include <konq_operations.h>
#include <KNewFileMenu>
#include <konqmimedata.h>
#include <konq_operations.h>
#include "views/dolphinview.h"
#include "views/viewmodecontroller.h"
#include "views/dolphinview.h"
#include "views/viewmodecontroller.h"
-K_GLOBAL_STATIC(KModifierKeyInfo, m_keyInfo)
-
DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
const QPoint& pos,
const KFileItem& fileInfo,
const KUrl& baseUrl) :
DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
const QPoint& pos,
const KFileItem& fileInfo,
const KUrl& baseUrl) :
m_pos(pos),
m_mainWindow(parent),
m_fileInfo(fileInfo),
m_pos(pos),
m_mainWindow(parent),
m_fileInfo(fileInfo),
m_context(NoContext),
m_copyToMenu(parent),
m_customActions(),
m_context(NoContext),
m_copyToMenu(parent),
m_customActions(),
+ m_shiftPressed(qApp->keyboardModifiers() & Qt::ShiftModifier),
m_removeAction(0)
{
// The context menu either accesses the URLs of the selected items
m_removeAction(0)
{
// The context menu either accesses the URLs of the selected items
const DolphinView* view = m_mainWindow->activeViewContainer()->view();
m_selectedItems = view->selectedItems();
const DolphinView* view = m_mainWindow->activeViewContainer()->view();
m_selectedItems = view->selectedItems();
- if (m_keyInfo) {
- 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)));
- }
-
m_removeAction = new QAction(this);
connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
m_removeAction = new QAction(this);
connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
-
- m_popup = new KMenu(m_mainWindow);
}
DolphinContextMenu::~DolphinContextMenu()
{
delete m_selectedItemsProperties;
m_selectedItemsProperties = 0;
}
DolphinContextMenu::~DolphinContextMenu()
{
delete m_selectedItemsProperties;
m_selectedItemsProperties = 0;
-
- delete m_popup;
- m_popup = 0;
}
void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
}
void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
-void DolphinContextMenu::initializeModifierKeyInfo()
+void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
- // Access m_keyInfo, so that it gets instantiated by
- // K_GLOBAL_STATIC
- KModifierKeyInfo* keyInfo = m_keyInfo;
- Q_UNUSED(keyInfo);
+ if (ev->key() == Qt::Key_Shift) {
+ m_shiftPressed = true;
+ updateRemoveAction();
+ }
+ KMenu::keyPressEvent(ev);
-void DolphinContextMenu::slotKeyModifierPressed(Qt::Key key, bool pressed)
+void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
- m_shiftPressed = (key == Qt::Key_Shift) && pressed;
- updateRemoveAction();
+ 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();
+ }
+ KMenu::keyReleaseEvent(ev);
}
void DolphinContextMenu::slotRemoveActionTriggered()
}
void DolphinContextMenu::slotRemoveActionTriggered()
{
Q_ASSERT(m_context & TrashContext);
{
Q_ASSERT(m_context & TrashContext);
- QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup);
+ QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
KConfig trashConfig("trashrc", KConfig::SimpleConfig);
emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
KConfig trashConfig("trashrc", KConfig::SimpleConfig);
emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
- m_popup->addAction(emptyTrashAction);
+ addAction(emptyTrashAction);
addCustomActions();
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
addCustomActions();
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
- m_popup->addAction(propertiesAction);
+ addAction(propertiesAction);
- if (m_popup->exec(m_pos) == emptyTrashAction) {
+ if (exec(m_pos) == emptyTrashAction) {
KonqOperations::emptyTrash(m_mainWindow);
}
}
KonqOperations::emptyTrash(m_mainWindow);
}
}
Q_ASSERT(m_context & ItemContext);
QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
Q_ASSERT(m_context & ItemContext);
QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
- m_popup->addAction(restoreAction);
+ addAction(restoreAction);
QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
- m_popup->addAction(deleteAction);
+ addAction(deleteAction);
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
- m_popup->addAction(propertiesAction);
+ addAction(propertiesAction);
- if (m_popup->exec(m_pos) == restoreAction) {
+ if (exec(m_pos) == restoreAction) {
KUrl::List selectedUrls;
foreach (const KFileItem &item, m_selectedItems) {
selectedUrls.append(item.url());
KUrl::List selectedUrls;
foreach (const KFileItem &item, m_selectedItems) {
selectedUrls.append(item.url());
KMenu* menu = newFileMenu->menu();
menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
menu->setIcon(KIcon("document-new"));
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();
+ addMenu(menu);
+ addSeparator();
// insert 'Open in new window' and 'Open in new tab' entries
// 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"));
+ addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
+ addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
// insert 'Add to Places' entry
if (!placeExists(m_fileInfo.url())) {
// insert 'Add to Places' entry
if (!placeExists(m_fileInfo.url())) {
- addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
+ addToPlacesAction = addAction(KIcon("bookmark-new"),
i18nc("@action:inmenu Add selected folder to places",
"Add to Places"));
}
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);
} 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);
+ addAction(openParentInNewWindowAction);
openParentInNewTabAction = new QAction(KIcon("tab-new"),
i18nc("@action:inmenu",
"Open Path in New Tab"),
this);
openParentInNewTabAction = new QAction(KIcon("tab-new"),
i18nc("@action:inmenu",
"Open Path in New Tab"),
this);
- m_popup->addAction(openParentInNewTabAction);
+ addAction(openParentInNewTabAction);
- m_popup->addSeparator();
}
}
insertDefaultItemActions();
}
}
insertDefaultItemActions();
- m_popup->addSeparator();
KFileItemActions fileItemActions;
fileItemActions.setItemListProperties(selectedItemsProperties());
KFileItemActions fileItemActions;
fileItemActions.setItemListProperties(selectedItemsProperties());
if (GeneralSettings::showCopyMoveMenu()) {
m_copyToMenu.setItems(m_selectedItems);
m_copyToMenu.setReadOnly(!selectedItemsProperties().supportsWriting());
if (GeneralSettings::showCopyMoveMenu()) {
m_copyToMenu.setItems(m_selectedItems);
m_copyToMenu.setReadOnly(!selectedItemsProperties().supportsWriting());
- m_copyToMenu.addActionsTo(m_popup);
+ m_copyToMenu.addActionsTo(this);
}
// insert 'Properties...' entry
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
}
// insert 'Properties...' entry
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
- m_popup->addAction(propertiesAction);
+ addAction(propertiesAction);
- QAction* activatedAction = m_popup->exec(m_pos);
+ QAction* activatedAction = exec(m_pos);
if (activatedAction) {
if (activatedAction == addToPlacesAction) {
const KUrl selectedUrl(m_fileInfo.url());
if (activatedAction) {
if (activatedAction == addToPlacesAction) {
const KUrl selectedUrl(m_fileInfo.url());
newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
newFileMenu->checkUpToDate();
newFileMenu->setPopupFiles(m_baseUrl);
newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
newFileMenu->checkUpToDate();
newFileMenu->setPopupFiles(m_baseUrl);
- m_popup->addMenu(newFileMenu->menu());
- m_popup->addSeparator();
+ addMenu(newFileMenu->menu());
+ addSeparator();
// Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
// "open_in_new_tab" here, as the current selection should get ignored.
// Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
// "open_in_new_tab" here, as the current selection should get ignored.
- m_popup->addAction(m_mainWindow->actionCollection()->action("new_window"));
- m_popup->addAction(m_mainWindow->actionCollection()->action("new_tab"));
+ addAction(m_mainWindow->actionCollection()->action("new_window"));
+ addAction(m_mainWindow->actionCollection()->action("new_tab"));
// Insert 'Add to Places' entry if exactly one item is selected
QAction* addToPlacesAction = 0;
if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
// Insert 'Add to Places' entry if exactly one item is selected
QAction* addToPlacesAction = 0;
if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
- addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
+ addToPlacesAction = addAction(KIcon("bookmark-new"),
i18nc("@action:inmenu Add current folder to places", "Add to Places"));
}
i18nc("@action:inmenu Add current folder to places", "Add to Places"));
}
- m_popup->addSeparator();
QAction* pasteAction = createPasteAction();
QAction* pasteAction = createPasteAction();
- m_popup->addAction(pasteAction);
- m_popup->addSeparator();
+ addAction(pasteAction);
+ addSeparator();
// Insert service actions
const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
// Insert service actions
const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
addCustomActions();
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
addCustomActions();
QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
- m_popup->addAction(propertiesAction);
+ addAction(propertiesAction);
- QAction* action = m_popup->exec(m_pos);
+ QAction* action = exec(m_pos);
if (addToPlacesAction && (action == addToPlacesAction)) {
const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
if (container->url().isValid()) {
if (addToPlacesAction && (action == addToPlacesAction)) {
const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
if (container->url().isValid()) {
const KActionCollection* collection = m_mainWindow->actionCollection();
// Insert 'Cut', 'Copy' and 'Paste'
const KActionCollection* collection = m_mainWindow->actionCollection();
// Insert 'Cut', 'Copy' and 'Paste'
- m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
- m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
- m_popup->addAction(createPasteAction());
+ addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
+ addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
+ addAction(createPasteAction());
- m_popup->addSeparator();
// Insert 'Rename'
QAction* renameAction = collection->action("rename");
// Insert 'Rename'
QAction* renameAction = collection->action("rename");
- m_popup->addAction(renameAction);
+ addAction(renameAction);
// Insert 'Move to Trash' and/or 'Delete'
if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", 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"));
+ addAction(collection->action("move_to_trash"));
+ addAction(collection->action("delete"));
- m_popup->addAction(m_removeAction);
+ addAction(m_removeAction);
updateRemoveAction();
}
}
updateRemoveAction();
}
}
const KActionCollection* ac = m_mainWindow->actionCollection();
QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
const KActionCollection* ac = m_mainWindow->actionCollection();
QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
- m_popup->addSeparator();
- m_popup->addAction(showMenuBar);
+ addSeparator();
+ addAction(showMenuBar);
fileItemActions.setParentWidget(m_mainWindow);
// insert 'Open With...' action or sub menu
fileItemActions.setParentWidget(m_mainWindow);
// insert 'Open With...' action or sub menu
- fileItemActions.addOpenWithActionsTo(m_popup, "DesktopEntryName != 'dolphin'");
+ fileItemActions.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
// insert 'Actions' sub menu
// insert 'Actions' sub menu
- fileItemActions.addServiceActionsTo(m_popup);
+ fileItemActions.addServiceActionsTo(this);
}
void DolphinContextMenu::addFileItemPluginActions()
}
void DolphinContextMenu::addFileItemPluginActions()
// Old API (kdelibs-4.6.0 only)
KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
if (plugin) {
// Old API (kdelibs-4.6.0 only)
KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
if (plugin) {
- plugin->setParent(m_popup);
- m_popup->addActions(plugin->actions(props, m_mainWindow));
+ plugin->setParent(this);
+ addActions(plugin->actions(props, m_mainWindow));
}
// New API (kdelibs >= 4.6.1)
KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
if (abstractPlugin) {
}
// New API (kdelibs >= 4.6.1)
KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
if (abstractPlugin) {
- abstractPlugin->setParent(m_popup);
- m_popup->addActions(abstractPlugin->actions(props, m_mainWindow));
+ abstractPlugin->setParent(this);
+ addActions(abstractPlugin->actions(props, m_mainWindow));
const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
if (!versionControlActions.isEmpty()) {
foreach (QAction* action, versionControlActions) {
const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
if (!versionControlActions.isEmpty()) {
foreach (QAction* action, versionControlActions) {
- m_popup->addAction(action);
- m_popup->addSeparator();
}
}
void DolphinContextMenu::addCustomActions()
{
foreach (QAction* action, m_customActions) {
}
}
void DolphinContextMenu::addCustomActions()
{
foreach (QAction* action, m_customActions) {
- m_popup->addAction(action);
#include <KService>
#include <KUrl>
#include <konq_copytomenu.h>
#include <KService>
#include <KUrl>
#include <konq_copytomenu.h>
#include <QScopedPointer>
#include <QScopedPointer>
-class KMenu;
-class KFileItem;
class QAction;
class DolphinMainWindow;
class KFileItemActions;
class QAction;
class DolphinMainWindow;
class KFileItemActions;
* - 'Actions': Contains all actions which can be applied to the
* given item.
*/
* - 'Actions': Contains all actions which can be applied to the
* given item.
*/
-class DolphinContextMenu : public QObject
+class DolphinContextMenu : public KMenu
- /**
- * TODO: This method is a workaround for a X11-issue in combination
- * with KModifierKeyInfo: When constructing KModifierKeyInfo in the
- * constructor of the context menu, the user interface might freeze.
- * To bypass this, the KModifierKeyInfo is constructed in DolphinMainWindow
- * directly after starting the application. Remove this method, if
- * the X11-issue got fixed (contact the maintainer of KModifierKeyInfo for
- * more details).
- */
- static void initializeModifierKeyInfo();
+protected:
+ virtual void keyPressEvent(QKeyEvent *ev);
+ virtual void keyReleaseEvent(QKeyEvent *ev);
- /**
- * Is invoked if a key modifier has been pressed and updates the context
- * menu to show the 'Delete' action instead of the 'Move To Trash' action
- * if the shift-key has been pressed.
- */
- void slotKeyModifierPressed(Qt::Key key, bool pressed);
-
/**
* Triggers the 'Delete'-action if the shift-key has been pressed, otherwise
* the 'Move to Trash'-action gets triggered.
/**
* Triggers the 'Delete'-action if the shift-key has been pressed, otherwise
* the 'Move to Trash'-action gets triggered.
int m_context;
KonqCopyToMenu m_copyToMenu;
QList<QAction*> m_customActions;
int m_context;
KonqCopyToMenu m_copyToMenu;
QList<QAction*> m_customActions;
m_updateToolBarTimer(0),
m_lastHandleUrlStatJob(0)
{
m_updateToolBarTimer(0),
m_lastHandleUrlStatJob(0)
{
- // Workaround for a X11-issue in combination with KModifierInfo
- // (see DolphinContextMenu::initializeModifierKeyInfo() for
- // more information):
- DolphinContextMenu::initializeModifierKeyInfo();
-
setObjectName("Dolphin#");
m_viewTab.append(ViewTab());
setObjectName("Dolphin#");
m_viewTab.append(ViewTab());