From: Peter Penz Date: Wed, 11 Feb 2009 20:45:12 +0000 (+0000) Subject: - allow the view implementations to attach custom actions to the context menu X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/2f47863c68bb0ac0417eed76f713ced651a4908a - allow the view implementations to attach custom actions to the context menu - let the "details view" show the "[x] Expandable Folders" action in the context menu CCMAIL: faure@kde.org svn path=/trunk/KDE/kdebase/apps/; revision=924867 --- diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 1d99f9637..f633fb945 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -61,7 +61,8 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, m_fileInfo(fileInfo), m_baseUrl(baseUrl), m_context(NoContext), - m_copyToMenu(parent) + m_copyToMenu(parent), + m_customActions() { // The context menu either accesses the URLs of the selected items // or the items itself. To increase the performance both lists are cached. @@ -76,6 +77,11 @@ DolphinContextMenu::~DolphinContextMenu() m_capabilities = 0; } +void DolphinContextMenu::setCustomActions(const QList& actions) +{ + m_customActions = actions; +} + void DolphinContextMenu::open() { // get the context information @@ -119,6 +125,8 @@ void DolphinContextMenu::openTrashContextMenu() QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"), i18nc("@action:inmenu Add current folder to places", "Add to Places")); + addCustomActions(popup); + QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); popup->addAction(propertiesAction); @@ -210,15 +218,15 @@ void DolphinContextMenu::openItemContextMenu() KonqMenuActions menuActions; menuActions.setPopupMenuInfo(popupInfo); - // Insert 'Open With...' action or sub menu + // insert 'Open With...' action or sub menu menuActions.addOpenWithActionsTo(popup, "DesktopEntryName != 'dolphin'"); - // Insert 'Actions' sub menu + // insert 'Actions' sub menu if (menuActions.addActionsTo(popup)) { popup->addSeparator(); } - // Insert 'Copy To' and 'Move To' sub menus + // insert 'Copy To' and 'Move To' sub menus if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) { m_copyToMenu.setItems(m_selectedItems); m_copyToMenu.setReadOnly(!capabilities().supportsWriting()); @@ -277,7 +285,8 @@ void DolphinContextMenu::openViewportContextMenu() QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"), i18nc("@action:inmenu Add current folder to places", "Add to Places")); - popup->addSeparator(); + + addCustomActions(popup); QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties")); @@ -379,4 +388,11 @@ KonqFileItemCapabilities& DolphinContextMenu::capabilities() return *m_capabilities; } +void DolphinContextMenu::addCustomActions(KMenu* menu) +{ + foreach (QAction* action, m_customActions) { + menu->addAction(action); + } +} + #include "dolphincontextmenu.moc" diff --git a/src/dolphincontextmenu.h b/src/dolphincontextmenu.h index 4d8fe0c43..a660f0500 100644 --- a/src/dolphincontextmenu.h +++ b/src/dolphincontextmenu.h @@ -68,6 +68,8 @@ public: virtual ~DolphinContextMenu(); + void setCustomActions(const QList& actions); + /** Opens the context menu model. */ void open(); @@ -94,6 +96,7 @@ private: private: KonqFileItemCapabilities& capabilities(); + void addCustomActions(KMenu* menu); private: struct Entry @@ -121,6 +124,7 @@ private: KUrl::List m_selectedUrls; int m_context; KonqCopyToMenu m_copyToMenu; + QList m_customActions; }; #endif diff --git a/src/dolphincontroller.cpp b/src/dolphincontroller.cpp index 92110643c..b128c9033 100644 --- a/src/dolphincontroller.cpp +++ b/src/dolphincontroller.cpp @@ -74,10 +74,11 @@ void DolphinController::triggerUrlChangeRequest(const KUrl& url) } } -void DolphinController::triggerContextMenuRequest(const QPoint& pos) +void DolphinController::triggerContextMenuRequest(const QPoint& pos, + const QList& customActions) { emit activated(); - emit requestContextMenu(pos); + emit requestContextMenu(pos, customActions); } void DolphinController::requestActivation() diff --git a/src/dolphincontroller.h b/src/dolphincontroller.h index eb5ef9ce7..29dd7dfb1 100644 --- a/src/dolphincontroller.h +++ b/src/dolphincontroller.h @@ -121,9 +121,11 @@ public: * should be invoked by the view implementation when a context * menu should be opened. The abstract Dolphin view itself * takes care itself to get the selected items depending from - * \a pos. + * \a pos. It is possible to define a custom list of actions for + * the context menu by \a customActions. */ - void triggerContextMenuRequest(const QPoint& pos); + void triggerContextMenuRequest(const QPoint& pos, + const QList& customActions = QList()); /** * Requests an activation of the view and emits the signal @@ -272,12 +274,14 @@ signals: /** * Is emitted if a context menu should be opened (see triggerContextMenuRequest()). * The abstract Dolphin view connects to this signal and will open the context menu. - * @param pos Position relative to the view widget where the - * context menu should be opened. It is recommended - * to get the corresponding model index from - * this position. - */ - void requestContextMenu(const QPoint& pos); + * @param pos Position relative to the view widget where the + * context menu should be opened. It is recommended + * to get the corresponding model index from + * this position. + * @param customActions List of actions that is added to the context menu when + * the menu is opened above the viewport. + */ + void requestContextMenu(const QPoint& pos, QList customActions); /** * Is emitted if the view has been activated by e. g. a mouse click. diff --git a/src/dolphindetailsview.cpp b/src/dolphindetailsview.cpp index e527a4f79..dd58e5842 100644 --- a/src/dolphindetailsview.cpp +++ b/src/dolphindetailsview.cpp @@ -56,6 +56,7 @@ DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* contr m_controller(controller), m_selectionManager(0), m_autoScroller(0), + m_expandableFoldersAction(0), m_font(), m_decorationSize(), m_band() @@ -153,6 +154,11 @@ DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* contr this, SLOT(slotGlobalSettingsChanged(int))); m_useDefaultIndexAt = false; + + m_expandableFoldersAction = new QAction(i18nc("@option:check", "Expandable Folders"), this); + m_expandableFoldersAction->setCheckable(true); + connect(m_expandableFoldersAction, SIGNAL(toggled(bool)), + this, SLOT(setFoldersExpandable(bool))); } DolphinDetailsView::~DolphinDetailsView() @@ -187,7 +193,11 @@ QStyleOptionViewItem DolphinDetailsView::viewOptions() const void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event) { QTreeView::contextMenuEvent(event); - m_controller->triggerContextMenuRequest(event->pos()); + + DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings(); + m_expandableFoldersAction->setChecked(settings->expandableFolders()); + m_controller->triggerContextMenuRequest(event->pos(), + QList() << m_expandableFoldersAction); } void DolphinDetailsView::mousePressEvent(QMouseEvent* event) @@ -572,27 +582,8 @@ void DolphinDetailsView::configureSettings(const QPoint& pos) } popup.addSeparator(); - // add a checkbox item for the "Expandable Folders" setting - DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings(); - QAction* expandableFoldersAction = popup.addAction(i18nc("@option:check", "Expandable Folders")); - expandableFoldersAction->setCheckable(true); - expandableFoldersAction->setChecked(settings->expandableFolders()); - QAction* activatedAction = popup.exec(header()->mapToGlobal(pos)); - if (activatedAction == expandableFoldersAction) { - const bool expand = expandableFoldersAction->isChecked(); - if (!expand) { - // collapse all expanded folders, as QTreeView::setItemsExpandable(false) - // does not do this task - const int rowCount = model()->rowCount(); - for (int row = 0; row < rowCount; ++row) { - setExpanded(model()->index(row, 0), false); - } - } - settings->setExpandableFolders(expand); - setRootIsDecorated(expand); - setItemsExpandable(expand); - } else if (activatedAction != 0) { + if (activatedAction != 0) { const bool show = activatedAction->isChecked(); const int columnIndex = activatedAction->data().toInt(); @@ -859,6 +850,22 @@ void DolphinDetailsView::updateElasticBandSelection() m_band.ignoreOldInfo = false; } +void DolphinDetailsView::setFoldersExpandable(bool expandable) +{ + if (!expandable) { + // collapse all expanded folders, as QTreeView::setItemsExpandable(false) + // does not do this task + const int rowCount = model()->rowCount(); + for (int row = 0; row < rowCount; ++row) { + setExpanded(model()->index(row, 0), false); + } + } + DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings(); + settings->setExpandableFolders(expandable); + setRootIsDecorated(expandable); + setItemsExpandable(expandable); +} + void DolphinDetailsView::updateDecorationSize(bool showPreview) { DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings(); diff --git a/src/dolphindetailsview.h b/src/dolphindetailsview.h index 0cef83741..38aea12de 100644 --- a/src/dolphindetailsview.h +++ b/src/dolphindetailsview.h @@ -153,6 +153,12 @@ private slots: */ void updateElasticBandSelection(); + /** + * If \a expandable is true, the details view acts as tree view. + * The current expandable state is remembered in the settings. + */ + void setFoldersExpandable(bool expandable); + private: /** * Updates the size of the decoration dependent on the @@ -181,6 +187,7 @@ private: DolphinController* m_controller; SelectionManager* m_selectionManager; DolphinViewAutoScroller* m_autoScroller; + QAction* m_expandableFoldersAction; QFont m_font; QSize m_decorationSize; diff --git a/src/dolphinpart.cpp b/src/dolphinpart.cpp index 41930221d..cd484ed1d 100644 --- a/src/dolphinpart.cpp +++ b/src/dolphinpart.cpp @@ -96,8 +96,8 @@ DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantL this, SLOT(slotItemTriggered(KFileItem))); connect(m_view, SIGNAL(tabRequested(KUrl)), this, SLOT(createNewWindow(KUrl))); - connect(m_view, SIGNAL(requestContextMenu(KFileItem,KUrl)), - this, SLOT(slotOpenContextMenu(KFileItem,KUrl))); + connect(m_view, SIGNAL(requestContextMenu(KFileItem,KUrl,QList)), + this, SLOT(slotOpenContextMenu(KFileItem,KUrl,QList))); connect(m_view, SIGNAL(selectionChanged(KFileItemList)), m_extension, SIGNAL(selectionInfo(KFileItemList))); connect(m_view, SIGNAL(selectionChanged(KFileItemList)), @@ -335,8 +335,12 @@ void DolphinPart::createNewWindow(const KUrl& url) emit m_extension->createNewWindow(url, args); } -void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&) +void DolphinPart::slotOpenContextMenu(const KFileItem& _item, + const KUrl&, + const QList& customActions) { + Q_UNUSED(customActions); // TODO: should be added to the context menu + KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems | KParts::BrowserExtension::ShowProperties | KParts::BrowserExtension::ShowUrlOperations; diff --git a/src/dolphinpart.h b/src/dolphinpart.h index 3203cb90f..a25bc72f3 100644 --- a/src/dolphinpart.h +++ b/src/dolphinpart.h @@ -129,11 +129,15 @@ private Q_SLOTS: void createNewWindow(const KUrl& url); /** * Opens the context menu on the current mouse position. - * @item File item context. If item is 0, the context menu - * should be applied to \a url. - * @url URL which contains \a item. - */ - void slotOpenContextMenu(const KFileItem& item, const KUrl& url); + * @item File item context. If item is null, the context menu + * should be applied to \a url. + * @url URL which contains \a item. + * @customActions Actions that should be added to the context menu, + * if the file item is null. + */ + void slotOpenContextMenu(const KFileItem& item, + const KUrl& url, + const QList& customActions); /** * Asks the host to open the URL \a url if the current view has diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index fdf93a1da..4fc3248c8 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -117,8 +117,8 @@ DolphinView::DolphinView(QWidget* parent, connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)), this, SLOT(slotRequestUrlChange(const KUrl&))); - connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)), - this, SLOT(openContextMenu(const QPoint&))); + connect(m_controller, SIGNAL(requestContextMenu(const QPoint&, const QList&)), + this, SLOT(openContextMenu(const QPoint&, const QList&))); connect(m_controller, SIGNAL(urlsDropped(const KFileItem&, const KUrl&, QDropEvent*)), this, SLOT(dropUrls(const KFileItem&, const KUrl&, QDropEvent*))); connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)), @@ -911,7 +911,8 @@ void DolphinView::emitSelectionChangedSignal() emit selectionChanged(DolphinView::selectedItems()); } -void DolphinView::openContextMenu(const QPoint& pos) +void DolphinView::openContextMenu(const QPoint& pos, + const QList& customActions) { KFileItem item; if (isColumnViewActive()) { @@ -929,7 +930,7 @@ void DolphinView::openContextMenu(const QPoint& pos) } m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192 - emit requestContextMenu(item, url()); + emit requestContextMenu(item, url(), customActions); m_isContextMenuOpen = false; } diff --git a/src/dolphinview.h b/src/dolphinview.h index ea77024a6..5cedeecfa 100644 --- a/src/dolphinview.h +++ b/src/dolphinview.h @@ -496,10 +496,13 @@ signals: /** * Is emitted if a context menu is requested for the item \a item, - * which is part of \a url. If the item is 0, the context menu - * for the URL should be shown. + * which is part of \a url. If the item is null, the context menu + * for the URL should be shown and the custom actions \a customActions + * will be added. */ - void requestContextMenu(const KFileItem& item, const KUrl& url); + void requestContextMenu(const KFileItem& item, + const KUrl& url, + const QList& customActions); /** * Is emitted if an information message with the content \a msg @@ -560,7 +563,7 @@ private slots: * is used to check whether the context menu is related to an * item or to the viewport. */ - void openContextMenu(const QPoint& pos); + void openContextMenu(const QPoint& pos, const QList& customActions); /** * Drops dragged URLs to the destination path \a destPath. If @@ -628,8 +631,8 @@ private slots: void restoreCurrentItem(); /** - * If \a view can be positively identified as not being the source for the - * current drag operation, deleteLater() it immediately. Else stores + * If \a view can be positively identified as not being the source for the + * current drag operation, deleteLater() it immediately. Else stores * it for later deletion. */ void deleteWhenNotDragSource(QAbstractItemView* view); diff --git a/src/dolphinviewcontainer.cpp b/src/dolphinviewcontainer.cpp index e22342ed8..bc2c44c07 100644 --- a/src/dolphinviewcontainer.cpp +++ b/src/dolphinviewcontainer.cpp @@ -128,8 +128,8 @@ DolphinViewContainer::DolphinViewContainer(DolphinMainWindow* mainWindow, m_proxyModel); connect(m_view, SIGNAL(urlChanged(const KUrl&)), m_urlNavigator, SLOT(setUrl(const KUrl&))); - connect(m_view, SIGNAL(requestContextMenu(KFileItem, const KUrl&)), - this, SLOT(openContextMenu(KFileItem, const KUrl&))); + connect(m_view, SIGNAL(requestContextMenu(KFileItem, const KUrl&, const QList&)), + this, SLOT(openContextMenu(KFileItem, const KUrl&, const QList&))); connect(m_view, SIGNAL(contentsMoved(int, int)), this, SLOT(saveContentsPos(int, int))); connect(m_view, SIGNAL(requestItemInfo(KFileItem)), @@ -345,9 +345,11 @@ void DolphinViewContainer::setNameFilter(const QString& nameFilter) } void DolphinViewContainer::openContextMenu(const KFileItem& item, - const KUrl& url) + const KUrl& url, + const QList& customActions) { DolphinContextMenu contextMenu(m_mainWindow, item, url); + contextMenu.setCustomActions(customActions); contextMenu.open(); } diff --git a/src/dolphinviewcontainer.h b/src/dolphinviewcontainer.h index 3c5270a6e..da317761b 100644 --- a/src/dolphinviewcontainer.h +++ b/src/dolphinviewcontainer.h @@ -180,11 +180,15 @@ private slots: /** * Opens the context menu on the current mouse position. - * @item File item context. If item is 0, the context menu - * should be applied to \a url. - * @url URL which contains \a item. + * @item File item context. If item is null, the context menu + * should be applied to \a url. + * @url URL which contains \a item. + * @customActions Actions that should be added to the context menu, + * if the file item is null. */ - void openContextMenu(const KFileItem& item, const KUrl& url); + void openContextMenu(const KFileItem& item, + const KUrl& url, + const QList& customActions); /** * Saves the position of the contents to the