]> cloud.milkyroute.net Git - dolphin.git/commitdiff
- allow the view implementations to attach custom actions to the context menu
authorPeter Penz <peter.penz19@gmail.com>
Wed, 11 Feb 2009 20:45:12 +0000 (20:45 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Wed, 11 Feb 2009 20:45:12 +0000 (20:45 +0000)
- 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

12 files changed:
src/dolphincontextmenu.cpp
src/dolphincontextmenu.h
src/dolphincontroller.cpp
src/dolphincontroller.h
src/dolphindetailsview.cpp
src/dolphindetailsview.h
src/dolphinpart.cpp
src/dolphinpart.h
src/dolphinview.cpp
src/dolphinview.h
src/dolphinviewcontainer.cpp
src/dolphinviewcontainer.h

index 1d99f96373e7dcce4817954c7946dfc9b1eea1cc..f633fb945165c7f54ac6f0923f56b39159b14bea 100644 (file)
@@ -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<QAction*>& 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"
index 4d8fe0c4379b01053f2e7d2c778bbc0e29278ff8..a660f0500934f06587526e44d8400039a7f7b099 100644 (file)
@@ -68,6 +68,8 @@ public:
 
     virtual ~DolphinContextMenu();
 
+    void setCustomActions(const QList<QAction*>& 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<QAction*> m_customActions;
 };
 
 #endif
index 92110643cd66e7eb08062cb9bd6b80f8715e2050..b128c90335e2c89804458b7a0ac88f1b86f9b47d 100644 (file)
@@ -74,10 +74,11 @@ void DolphinController::triggerUrlChangeRequest(const KUrl& url)
     }
 }
 
-void DolphinController::triggerContextMenuRequest(const QPoint& pos)
+void DolphinController::triggerContextMenuRequest(const QPoint& pos,
+                                                  const QList<QAction*>& customActions)
 {
     emit activated();
-    emit requestContextMenu(pos);
+    emit requestContextMenu(pos, customActions);
 }
 
 void DolphinController::requestActivation()
index eb5ef9ce7f214a4f7385f31e49da9886bd359269..29dd7dfb1bfcc56dd7fa9d03226e88914a2a640e 100644 (file)
@@ -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<QAction*>& customActions = QList<QAction*>());
 
     /**
      * 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<QAction*> customActions);
 
     /**
      * Is emitted if the view has been activated by e. g. a mouse click.
index e527a4f79b2f126fd8617d81d698d6a418d1e487..dd58e584248bbf57e73e1f722375bb9fb004783c 100644 (file)
@@ -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<QAction*>() << 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();
index 0cef8374148c4d6c7f385ba6d38209d25d316605..38aea12deef7463f0b9ac286d3a9651bdaea3b0e 100644 (file)
@@ -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;
index 41930221d5499f5499d11e3bae61818ddd07ce34..cd484ed1d7dca0d7fee1673dfbe7e659b84d8d33 100644 (file)
@@ -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<QAction*>)),
+            this, SLOT(slotOpenContextMenu(KFileItem,KUrl,QList<QAction*>)));
     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<QAction*>& 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;
index 3203cb90fdcf73024b648376111182bcdc0764c2..a25bc72f3d55556d128ece02bc57432d7990f321 100644 (file)
@@ -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<QAction*>& customActions);
 
     /**
      * Asks the host to open the URL \a url if the current view has
index fdf93a1dae9db75fe561e6b112f35d9f9c6319ca..4fc3248c81a5085ee2e58224ba10f557ff3f6d5b 100644 (file)
@@ -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<QAction*>&)),
+            this, SLOT(openContextMenu(const QPoint&, const QList<QAction*>&)));
     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<QAction*>& 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;
 }
 
index ea77024a6ce8a81ea6e4c4909d39681b01abe989..5cedeecfa86d3402a4f7a867932907cfc8e9e9ee 100644 (file)
@@ -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<QAction*>& 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<QAction*>& 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);
index e22342ed8b5e055b2270f1c557b47c91716bd952..bc2c44c0738152dc14f4ff73bce173f7c15c543d 100644 (file)
@@ -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<QAction*>&)),
+            this, SLOT(openContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)));
     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<QAction*>& customActions)
 {
     DolphinContextMenu contextMenu(m_mainWindow, item, url);
+    contextMenu.setCustomActions(customActions);
     contextMenu.open();
 }
 
index 3c5270a6e5fe4efef3e98e1a9bb8ecdc8c360f14..da317761b3d8cdb3424260f426d287afd201ae67 100644 (file)
@@ -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<QAction*>& customActions);
 
     /**
      * Saves the position of the contents to the