]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.h
Merge branch 'release/20.12'
[dolphin.git] / src / dolphincontextmenu.h
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #ifndef DOLPHINCONTEXTMENU_H
8 #define DOLPHINCONTEXTMENU_H
9
10 #include <KFileCopyToMenu>
11 #include <KFileItem>
12
13 #include <QMenu>
14 #include <QUrl>
15
16 class QAction;
17 class DolphinMainWindow;
18 class KFileItemActions;
19 class KFileItemListProperties;
20 class DolphinRemoveAction;
21
22 /**
23 * @brief Represents the context menu which appears when doing a right
24 * click on an item or the viewport of the file manager.
25 *
26 * Beside static menu entries (e. g. 'Paste' or 'Properties') two
27 * dynamic sub menus are shown when opening a context menu above
28 * an item:
29 * - 'Open With': Contains all applications which are registered to
30 * open items of the given MIME type.
31 * - 'Actions': Contains all actions which can be applied to the
32 * given item.
33 */
34 class DolphinContextMenu : public QMenu
35 {
36 Q_OBJECT
37
38 public:
39 enum Command
40 {
41 None,
42 OpenParentFolder,
43 OpenParentFolderInNewWindow,
44 OpenParentFolderInNewTab
45 };
46
47 /**
48 * @parent Pointer to the main window the context menu
49 * belongs to.
50 * @pos Position in screen coordinates.
51 * @fileInfo Pointer to the file item the context menu
52 * is applied. If 0 is passed, the context menu
53 * is above the viewport.
54 * @baseUrl Base URL of the viewport where the context menu
55 * should be opened.
56 */
57 DolphinContextMenu(DolphinMainWindow* parent,
58 const QPoint& pos,
59 const KFileItem& fileInfo,
60 const QUrl& baseUrl);
61
62 ~DolphinContextMenu() override;
63
64 void setCustomActions(const QList<QAction*>& actions);
65
66 /**
67 * Opens the context menu model and returns the requested
68 * command, that should be triggered by the caller. If
69 * Command::None has been returned, either the context-menu
70 * had been closed without executing an action or an
71 * already available action from the main-window has been
72 * executed.
73 */
74 Command open();
75
76 protected:
77 void childEvent(QChildEvent* event) override;
78 bool eventFilter(QObject* dest, QEvent* event) override;
79
80 private:
81 void openTrashContextMenu();
82 void openTrashItemContextMenu();
83 void openItemContextMenu();
84 void openViewportContextMenu();
85
86 void insertDefaultItemActions(const KFileItemListProperties&);
87
88 /**
89 * Adds the "Show menubar" action to the menu if the
90 * menubar is hidden.
91 */
92 void addShowMenuBarAction();
93
94 bool placeExists(const QUrl& url) const;
95
96 QAction* createPasteAction();
97
98 KFileItemListProperties& selectedItemsProperties() const;
99
100 /**
101 * Returns the file item for m_baseUrl.
102 */
103 KFileItem baseFileItem();
104
105 /**
106 * Adds "Open With" actions
107 */
108 void addOpenWithActions(KFileItemActions& fileItemActions);
109
110 /**
111 * Adds custom actions e.g. like the "[x] Expandable Folders"-action
112 * provided in the details view.
113 */
114 void addCustomActions();
115
116 private:
117 /**
118 * Add services, custom actions, plugins and version control items to the menu
119 */
120 void addAdditionalActions(KFileItemActions &fileItemActions, const KFileItemListProperties &props);
121
122 struct Entry
123 {
124 int type;
125 QString name;
126 QString filePath; // empty for separator
127 QString templatePath; // same as filePath for template
128 QString icon;
129 QString comment;
130 };
131
132 enum ContextType
133 {
134 NoContext = 0,
135 ItemContext = 1,
136 TrashContext = 2,
137 TimelineContext = 4,
138 SearchContext = 8,
139 };
140
141 QPoint m_pos;
142 DolphinMainWindow* m_mainWindow;
143
144 KFileItem m_fileInfo;
145
146 QUrl m_baseUrl;
147 KFileItem* m_baseFileItem; /// File item for m_baseUrl
148
149 KFileItemList m_selectedItems;
150 mutable KFileItemListProperties* m_selectedItemsProperties;
151
152 int m_context;
153 KFileCopyToMenu m_copyToMenu;
154 QList<QAction*> m_customActions;
155
156 Command m_command;
157
158 DolphinRemoveAction* m_removeAction; // Action that represents either 'Move To Trash' or 'Delete'
159 void addDirectoryItemContextMenu(KFileItemActions &fileItemActions);
160
161 };
162
163 #endif