/***************************************************************************
* Copyright (C) 2008 by David Faure <faure@kde.org> *
+ * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
* *
* 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 *
#include "dolphinviewactionhandler.h"
+#include <config-baloo.h>
+
#include "settings/viewpropertiesdialog.h"
#include "views/dolphinview.h"
#include "views/zoomlevelinfo.h"
#include <konq_operations.h>
+#include <QPointer>
+
#include <KAction>
#include <KActionCollection>
#include <KActionMenu>
-#include <KFileItemDelegate>
#include <kitemviews/kfileitemmodel.h>
#include <KLocale>
+#include <KMenu>
#include <KNewFileMenu>
#include <KSelectAction>
#include <KToggleAction>
-#include <KRun>
#include <KPropertiesDialog>
+#include <KIcon>
#include <KDebug>
-DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection* collection, QObject* parent)
- : QObject(parent),
- m_actionCollection(collection),
- m_currentView(0)
+#ifdef HAVE_BALOO
+ #include <baloo/indexerconfig.h>
+#endif
+
+DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection* collection, QObject* parent) :
+ QObject(parent),
+ m_actionCollection(collection),
+ m_currentView(0),
+ m_sortByActions(),
+ m_visibleRoles()
{
Q_ASSERT(m_actionCollection);
createActions();
this, SLOT(slotSortRoleChanged(QByteArray)));
connect(view, SIGNAL(zoomLevelChanged(int,int)),
this, SLOT(slotZoomLevelChanged(int,int)));
+ connect(view, SIGNAL(writeStateChanged(bool)),
+ this, SLOT(slotWriteStateChanged(bool)));
}
DolphinView* DolphinViewActionHandler::currentView()
{
// This action doesn't appear in the GUI, it's for the shortcut only.
// KNewFileMenu takes care of the GUI stuff.
- KAction* newDirAction = m_actionCollection->addAction("create_dir");
+ QAction* newDirAction = m_actionCollection->addAction("create_dir");
newDirAction->setText(i18nc("@action", "Create Folder..."));
newDirAction->setShortcut(Qt::Key_F10);
newDirAction->setIcon(KIcon("folder-new"));
+ newDirAction->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
connect(newDirAction, SIGNAL(triggered()), this, SIGNAL(createDirectory()));
// File menu
- KAction* rename = m_actionCollection->addAction("rename");
+ QAction* rename = m_actionCollection->addAction("rename");
rename->setText(i18nc("@action:inmenu File", "Rename..."));
rename->setShortcut(Qt::Key_F2);
rename->setIcon(KIcon("edit-rename"));
connect(rename, SIGNAL(triggered()), this, SLOT(slotRename()));
- KAction* moveToTrash = m_actionCollection->addAction("move_to_trash");
+ QAction* moveToTrash = m_actionCollection->addAction("move_to_trash");
moveToTrash->setText(i18nc("@action:inmenu File", "Move to Trash"));
moveToTrash->setIcon(KIcon("user-trash"));
moveToTrash->setShortcut(QKeySequence::Delete);
connect(moveToTrash, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)),
this, SLOT(slotTrashActivated(Qt::MouseButtons,Qt::KeyboardModifiers)));
- KAction* deleteAction = m_actionCollection->addAction("delete");
+ QAction* deleteAction = m_actionCollection->addAction("delete");
deleteAction->setIcon(KIcon("edit-delete"));
deleteAction->setText(i18nc("@action:inmenu File", "Delete"));
deleteAction->setShortcut(Qt::SHIFT | Qt::Key_Delete);
// disabled and "delete" is enabled (e.g. non-local files), so that Key_Del
// can be used for deleting the file (#76016). It needs to be a separate action
// so that the Edit menu isn't affected.
- KAction* deleteWithTrashShortcut = m_actionCollection->addAction("delete_shortcut");
+ QAction* deleteWithTrashShortcut = m_actionCollection->addAction("delete_shortcut");
// The descriptive text is just for the shortcuts editor.
deleteWithTrashShortcut->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
deleteWithTrashShortcut->setShortcut(QKeySequence::Delete);
deleteWithTrashShortcut->setEnabled(false);
connect(deleteWithTrashShortcut, SIGNAL(triggered()), this, SLOT(slotDeleteItems()));
- KAction *propertiesAction = m_actionCollection->addAction( "properties" );
+ QAction *propertiesAction = m_actionCollection->addAction( "properties" );
// Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
propertiesAction->setText( i18nc("@action:inmenu File", "Properties") );
propertiesAction->setIcon(KIcon("document-properties"));
- propertiesAction->setShortcut(Qt::ALT | Qt::Key_Return);
+ propertiesAction->setShortcuts(QList<QKeySequence>() << Qt::ALT + Qt::Key_Return << Qt::ALT + Qt::Key_Enter);
connect(propertiesAction, SIGNAL(triggered()), SLOT(slotProperties()));
// View menu
connect(sortFoldersFirst, SIGNAL(triggered()), this, SLOT(toggleSortFoldersFirst()));
// View -> Sort By
- QActionGroup* sortByActionGroup = createSortByActionGroup();
- connect(sortByActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(slotSortTriggered(QAction*)));
+ QActionGroup* sortByActionGroup = createFileItemRolesActionGroup("sort_by_");
KActionMenu* sortByActionMenu = m_actionCollection->add<KActionMenu>("sort");
sortByActionMenu->setText(i18nc("@action:inmenu View", "Sort By"));
sortByActionMenu->addAction(sortFoldersFirst);
// View -> Additional Information
- QActionGroup* additionalInfoGroup = createAdditionalInformationActionGroup();
- connect(additionalInfoGroup, SIGNAL(triggered(QAction*)), this, SLOT(toggleAdditionalInfo(QAction*)));
-
- KActionMenu* additionalInfoMenu = m_actionCollection->add<KActionMenu>("additional_info");
- additionalInfoMenu->setText(i18nc("@action:inmenu View", "Additional Information"));
- additionalInfoMenu->setDelayed(false);
- foreach (QAction* action, additionalInfoGroup->actions()) {
- additionalInfoMenu->addAction(action);
+ QActionGroup* visibleRolesGroup = createFileItemRolesActionGroup("show_");
+
+ KActionMenu* visibleRolesMenu = m_actionCollection->add<KActionMenu>("additional_info");
+ visibleRolesMenu->setText(i18nc("@action:inmenu View", "Additional Information"));
+ visibleRolesMenu->setDelayed(false);
+
+ foreach (QAction* action, visibleRolesGroup->actions()) {
+ visibleRolesMenu->addAction(action);
}
KToggleAction* showInGroups = m_actionCollection->add<KToggleAction>("show_in_groups");
showHiddenFiles->setShortcuts(QList<QKeySequence>() << Qt::ALT + Qt::Key_Period << Qt::Key_F8);
connect(showHiddenFiles, SIGNAL(triggered(bool)), this, SLOT(toggleShowHiddenFiles(bool)));
- KAction* adjustViewProps = m_actionCollection->addAction("view_properties");
+ QAction* adjustViewProps = m_actionCollection->addAction("view_properties");
adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
connect(adjustViewProps, SIGNAL(triggered()), this, SLOT(slotAdjustViewProperties()));
}
-QActionGroup* DolphinViewActionHandler::createAdditionalInformationActionGroup()
+QActionGroup* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString& groupPrefix)
{
- QActionGroup* additionalInfoGroup = new QActionGroup(m_actionCollection);
- additionalInfoGroup->setExclusive(false);
+ const bool isSortGroup = (groupPrefix == QLatin1String("sort_by_"));
+ Q_ASSERT(isSortGroup || (!isSortGroup && groupPrefix == QLatin1String("show_")));
+
+ QActionGroup* rolesActionGroup = new QActionGroup(m_actionCollection);
+ rolesActionGroup->setExclusive(isSortGroup);
+ if (isSortGroup) {
+ connect(rolesActionGroup, SIGNAL(triggered(QAction*)),
+ this, SLOT(slotSortTriggered(QAction*)));
+ } else {
+ connect(rolesActionGroup, SIGNAL(triggered(QAction*)),
+ this, SLOT(toggleVisibleRole(QAction*)));
+ }
- KActionMenu* showInformationMenu = m_actionCollection->add<KActionMenu>("additional_info");
- showInformationMenu->setText(i18nc("@action:inmenu View", "Additional Information"));
- showInformationMenu->setDelayed(false);
+ QString groupName;
+ KActionMenu* groupMenu = 0;
+ QActionGroup* groupMenuGroup = 0;
+
+ bool indexingEnabled = false;
+#ifdef HAVE_BALOO
+ Baloo::IndexerConfig config;
+ indexingEnabled = config.fileIndexingEnabled();
+#endif
const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
- if (info.role == "name") {
- // It should not be possible to hide the "name" role
+ if (!isSortGroup && info.role == "text") {
+ // It should not be possible to hide the "text" role
continue;
}
- const QString name = QLatin1String("show_") + info.role;
- KToggleAction* action = m_actionCollection->add<KToggleAction>(name);
+ KToggleAction* action = 0;
+ const QString name = groupPrefix + info.role;
+ if (info.group.isEmpty()) {
+ action = m_actionCollection->add<KToggleAction>(name);
+ action->setActionGroup(rolesActionGroup);
+ } else {
+ if (!groupMenu || info.group != groupName) {
+ groupName = info.group;
+ groupMenu = m_actionCollection->add<KActionMenu>(groupName);
+ groupMenu->setText(groupName);
+ groupMenu->setActionGroup(rolesActionGroup);
+
+ groupMenuGroup = new QActionGroup(groupMenu);
+ groupMenuGroup->setExclusive(isSortGroup);
+ if (isSortGroup) {
+ connect(groupMenuGroup, SIGNAL(triggered(QAction*)),
+ this, SLOT(slotSortTriggered(QAction*)));
+ } else {
+ connect(groupMenuGroup, SIGNAL(triggered(QAction*)),
+ this, SLOT(toggleVisibleRole(QAction*)));
+ }
+ }
+
+ action = new KToggleAction(groupMenu);
+ action->setActionGroup(groupMenuGroup);
+ groupMenu->addAction(action);
+ }
action->setText(info.translation);
action->setData(info.role);
- action->setActionGroup(additionalInfoGroup);
- }
-
- return additionalInfoGroup;
-}
-QActionGroup* DolphinViewActionHandler::createSortByActionGroup()
-{
- QActionGroup* sortByActionGroup = new QActionGroup(m_actionCollection);
- sortByActionGroup->setExclusive(true);
+ const bool enable = (!info.requiresBaloo && !info.requiresIndexer) ||
+ (info.requiresBaloo) ||
+ (info.requiresIndexer && indexingEnabled);
+ action->setEnabled(enable);
- const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
- foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
- const QString name = QLatin1String("sort_by_") + info.role;
- KToggleAction* action = m_actionCollection->add<KToggleAction>(name);
- action->setText(info.translation);
- action->setData(info.role);
- sortByActionGroup->addAction(action);
+ if (isSortGroup) {
+ m_sortByActions.insert(info.role, action);
+ } else {
+ m_visibleRoles.insert(info.role, action);
+ }
}
- return sortByActionGroup;
+ return rolesActionGroup;
}
void DolphinViewActionHandler::slotViewModeActionTriggered(QAction* action)
void DolphinViewActionHandler::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
{
emit actionBeingHandled();
- // Note: kde3's konq_mainwindow.cpp used to check
- // reason == KAction::PopupMenuActivation && ...
- // but this isn't supported anymore
- if (modifiers & Qt::ShiftModifier) {
- m_currentView->deleteSelectedItems();
- } else {
- m_currentView->trashSelectedItems();
- }
+ m_currentView->trashSelectedItems();
}
void DolphinViewActionHandler::slotDeleteItems()
m_actionCollection->action("folders_first")->setChecked(foldersFirst);
}
-void DolphinViewActionHandler::toggleAdditionalInfo(QAction* action)
+void DolphinViewActionHandler::toggleVisibleRole(QAction* action)
{
emit actionBeingHandled();
Q_UNUSED(previous);
const QSet<QByteArray> checkedRoles = current.toSet();
- const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
- foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
- const QString name = QLatin1String("show_") + info.role;
- QAction* action = m_actionCollection->action(name);
- if (action) {
- action->setChecked(checkedRoles.contains(info.role));
- }
+ QHashIterator<QByteArray, KToggleAction*> it(m_visibleRoles);
+ while (it.hasNext()) {
+ it.next();
+ const QByteArray& role = it.key();
+ KToggleAction* action = it.value();
+ action->setChecked(checkedRoles.contains(role));
}
}
showHiddenFilesAction->setChecked(shown);
}
+void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable)
+{
+ m_actionCollection->action("create_dir")->setEnabled(isFolderWritable);
+}
+
KToggleAction* DolphinViewActionHandler::iconsModeAction()
{
KToggleAction* iconsView = m_actionCollection->add<KToggleAction>("icons");
void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray& role)
{
- const QString name = QLatin1String("sort_by_") + role;
- QAction* action = m_actionCollection->action(name);
+ KToggleAction* action = m_sortByActions.value(role);
if (action) {
action->setChecked(true);
- QAction* sortByMenu = m_actionCollection->action("sort");
- sortByMenu->setIcon(KIcon(action->icon()));
+ if (!action->icon().isNull()) {
+ QAction* sortByMenu = m_actionCollection->action("sort");
+ sortByMenu->setIcon(KIcon(action->icon()));
+ }
}
}
void DolphinViewActionHandler::slotSortTriggered(QAction* action)
{
+ // The radiobuttons of the "Sort By"-menu are split between the main-menu
+ // and several sub-menus. Because of this they don't have a common
+ // action-group that assures an exclusive toggle-state between the main-menu
+ // actions and the sub-menu-actions. If an action gets checked, it must
+ // be assured that all other actions get unchecked.
+ QAction* sortByMenu = m_actionCollection->action("sort");
+ foreach (QAction* groupAction, sortByMenu->menu()->actions()) {
+ KActionMenu* actionMenu = qobject_cast<KActionMenu*>(groupAction);
+ if (actionMenu) {
+ foreach (QAction* subAction, actionMenu->menu()->actions()) {
+ subAction->setChecked(false);
+ }
+ } else if (groupAction->actionGroup()) {
+ groupAction->setChecked(false);
+ }
+ }
+ action->setChecked(true);
+
+ // Apply the activated sort-role to the view
const QByteArray role = action->data().toByteArray();
m_currentView->setSortRole(role);
}