File indexing completed on 2024-04-28 04:58:12

0001 /* This file is part of the KDE project
0002     SPDX-FileCopyrightText: 1998-2016 David Faure <faure@kde.org>
0003     SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@yahoo.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef __konqpopupmenu_h
0009 #define __konqpopupmenu_h
0010 
0011 #include <QMenu>
0012 
0013 #include <libkonq_export.h>
0014 
0015 class KFileItemList;
0016 class KNewFileMenu;
0017 
0018 class KActionCollection;
0019 class KBookmarkManager;
0020 class KonqPopupMenuPrivate;
0021 class QUrl;
0022 
0023 /**
0024  * This class implements the popup menu for URLs in file managers.
0025  *
0026  * The recommended usage is to use QMenu::popup() to avoid modal popupmenus.
0027  *
0028  * Either reuse the instance, or delete it after use with
0029  *   connect(menu, &QMenu::aboutToHide, [menu]() { menu->deleteLater(); });
0030  *
0031  * Users of KonqPopupMenu include: konqueror, kfind, the folderview desktop plasmoid.
0032  */
0033 class LIBKONQ_EXPORT KonqPopupMenu : public QMenu
0034 {
0035     Q_OBJECT
0036 public:
0037 
0038     /**
0039      * Each action group is inserted into the context menu at a specific position.
0040      * "editactions" for actions related text editing,
0041      * "linkactions" for actions related to hyperlinks,
0042      * "partactions" for any other actions provided by the part
0043      */
0044     enum ActionGroup {
0045         TopActions,  ///< actions to be shown at the top of the context menu, like "show menu bar"
0046         TabHandlingActions, ///< actions for tab handling, like "open in new tab"
0047         EditActions, ///< move to trash, delete, etc.
0048         PreviewActions, ///< actions related to viewing the selected file with an embedded viewer
0049         CustomActions, ///< group for more custom actions, such as those provided by KParts components
0050         LinkActions  ///< actions related to handling of hyperlinks, only shown if the IsLink flag is set
0051     };
0052 
0053     typedef QMap<ActionGroup, QList<QAction *> > ActionGroupMap;
0054 
0055     /**
0056      * Set of flags to ask for some items in the popup menu.
0057      */
0058     enum Flag {
0059         DefaultPopupItems = 0x0000, ///< default value, no additional menu item
0060         ShowBookmark = 0x0008, ///< show "add to bookmarks" (usually not done on the local filesystem)
0061         ShowCreateDirectory = 0x0010, ///<  show "create directory" (usually only done on the background of the view, or
0062                                       /// in hierarchical views like directory trees, where the new dir would be visible)
0063         ShowTextSelectionItems = 0x0020, ///< set when selecting text, for a popup that only contains text-related items.
0064         NoDeletion = 0x0040, ///< "cut" not allowed (e.g. parent dir not writeable).
0065                              /// (this is only needed if the protocol itself supports deletion, unlike e.g. HTTP)
0066         IsLink = 0x0080, ///< show "Bookmark This Link" and other link-related actions (LinkActions group)
0067         ShowUrlOperations = 0x0100, ///< show copy, paste, as well as cut if NoDeletion is not set.
0068         ShowProperties = 0x0200,   ///< show "Properties" action (usually done by directory views)
0069         ShowNewWindow = 0x0400, ///< Show "Open in new window" action
0070         NoPlugins = 0x0800      ///< Disable plugins - mostly for the unittest
0071      };
0072     Q_DECLARE_FLAGS(Flags, Flag)
0073 
0074     /**
0075      * Constructor
0076      * @param items the list of file items the popupmenu should be shown for
0077      * @param viewURL the URL shown in the view, to test for RMB click on view background
0078      * @param actions list of actions the caller wants to see in the menu
0079      * @param flags flags which control which items to show in the popupmenu
0080      * @param parentWidget the widget we're showing this popup for. Helps destroying
0081      * the popup if the widget is destroyed before the popup.
0082      *
0083      * The actions to pass in the action collection are :
0084      * reload, cut, copy, paste, pasteto
0085      * The others items are automatically inserted.
0086      */
0087     KonqPopupMenu(const KFileItemList &items,
0088                   const QUrl &viewURL,
0089                   KActionCollection &actions,
0090                   Flags flags,
0091                   QWidget *parentWidget = nullptr);
0092 
0093     /**
0094      * Don't forget to destroy the object
0095      */
0096     ~KonqPopupMenu() override;
0097 
0098     /**
0099      * Sets the "New file" menu instance. This allows to share it with the menubar.
0100      */
0101     void setNewFileMenu(KNewFileMenu *newMenu);
0102 
0103     /**
0104      * Sets the bookmark manager for the "add to bookmark" action.
0105      * Only used if ShowBookmark is set
0106      */
0107     void setBookmarkManager(KBookmarkManager *manager);
0108 
0109     /**
0110      * Sets the additional actions to show in the context menu
0111      */
0112     void setActionGroups(const ActionGroupMap &actionGroups);
0113 
0114     /**
0115      * Set the title of the URL, when the popupmenu is opened for a single URL.
0116      * This is used if the user chooses to add a bookmark for this URL.
0117      */
0118     void setURLTitle(const QString &urlTitle);
0119 
0120 Q_SIGNALS:
0121     /**
0122      * Emitted before the "Open With" dialog is shown
0123      * This is used e.g in folderview to close the folder peek popups on invoking the "Open With" menu action
0124      */
0125     void openWithDialogAboutToBeShown();
0126 
0127 private:
0128     KonqPopupMenuPrivate *d;
0129 };
0130 
0131 #endif