]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.h
Don't delay popup menus of "Create New" and "Recently Closed Tabs" toolbar buttons
[dolphin.git] / src / dolphincontextmenu.h
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #ifndef DOLPHINCONTEXTMENU_H
21 #define DOLPHINCONTEXTMENU_H
22
23 #include <KFileItem>
24 #include <KService>
25 #include <KUrl>
26 #include <konq_copytomenu.h>
27
28 #include <QObject>
29
30 #include <QVector>
31
32 #include <QScopedPointer>
33
34 class KMenu;
35 class KFileItem;
36 class QAction;
37 class DolphinMainWindow;
38 class KFileItemActions;
39 class KFileItemListProperties;
40
41 /**
42 * @brief Represents the context menu which appears when doing a right
43 * click on an item or the viewport of the file manager.
44 *
45 * Beside static menu entries (e. g. 'Paste' or 'Properties') two
46 * dynamic sub menus are shown when opening a context menu above
47 * an item:
48 * - 'Open With': Contains all applications which are registered to
49 * open items of the given MIME type.
50 * - 'Actions': Contains all actions which can be applied to the
51 * given item.
52 */
53 class DolphinContextMenu : public QObject
54 {
55 Q_OBJECT
56
57 public:
58 enum Command
59 {
60 None,
61 OpenParentFolderInNewWindow,
62 OpenParentFolderInNewTab
63 };
64
65 /**
66 * @parent Pointer to the main window the context menu
67 * belongs to.
68 * @pos Position in screen coordinates.
69 * @fileInfo Pointer to the file item the context menu
70 * is applied. If 0 is passed, the context menu
71 * is above the viewport.
72 * @baseUrl Base URL of the viewport where the context menu
73 * should be opened.
74 */
75 DolphinContextMenu(DolphinMainWindow* parent,
76 const QPoint& pos,
77 const KFileItem& fileInfo,
78 const KUrl& baseUrl);
79
80 virtual ~DolphinContextMenu();
81
82 void setCustomActions(const QList<QAction*>& actions);
83
84 /**
85 * Opens the context menu model and returns the requested
86 * command, that should be triggered by the caller. If
87 * Command::None has been returned, either the context-menu
88 * had been closed without executing an action or an
89 * already available action from the main-window has been
90 * executed.
91 */
92 Command open();
93
94 /**
95 * TODO: This method is a workaround for a X11-issue in combination
96 * with KModifierKeyInfo: When constructing KModifierKeyInfo in the
97 * constructor of the context menu, the user interface might freeze.
98 * To bypass this, the KModifierKeyInfo is constructed in DolphinMainWindow
99 * directly after starting the application. Remove this method, if
100 * the X11-issue got fixed (contact the maintainer of KModifierKeyInfo for
101 * more details).
102 */
103 static void initializeModifierKeyInfo();
104
105 private slots:
106 /**
107 * Is invoked if a key modifier has been pressed and updates the context
108 * menu to show the 'Delete' action instead of the 'Move To Trash' action
109 * if the shift-key has been pressed.
110 */
111 void slotKeyModifierPressed(Qt::Key key, bool pressed);
112
113 /**
114 * Triggers the 'Delete'-action if the shift-key has been pressed, otherwise
115 * the 'Move to Trash'-action gets triggered.
116 */
117 void slotRemoveActionTriggered();
118
119 private:
120 void openTrashContextMenu();
121 void openTrashItemContextMenu();
122 void openItemContextMenu();
123 void openViewportContextMenu();
124
125 void insertDefaultItemActions();
126
127 /**
128 * Adds the "Show menubar" action to the menu if the
129 * menubar is hidden.
130 */
131 void addShowMenuBarAction();
132
133 bool placeExists(const KUrl& url) const;
134
135 QAction* createPasteAction();
136
137 KFileItemListProperties& selectedItemsProperties() const;
138
139 /**
140 * Returns the file item for m_baseUrl.
141 */
142 KFileItem baseFileItem();
143
144 /**
145 * Adds actions that have been installed as service-menu.
146 * (see http://techbase.kde.org/index.php?title=Development/Tutorials/Creating_Konqueror_Service_Menus)
147 */
148 void addServiceActions(KFileItemActions& fileItemActions);
149
150 /**
151 * Adds actions that are provided by a KFileItemActionPlugin.
152 */
153 void addFileItemPluginActions();
154
155 /**
156 * Adds actions that are provided by a KVersionControlPlugin.
157 */
158 void addVersionControlPluginActions();
159
160 /**
161 * Adds custom actions e.g. like the "[x] Expandable Folders"-action
162 * provided in the details view.
163 */
164 void addCustomActions();
165
166 /**
167 * Updates m_removeAction to represent the 'Delete'-action if the shift-key
168 * has been pressed or the selection is not local. Otherwise it represents
169 * the 'Move to Trash'-action.
170 */
171 void updateRemoveAction();
172
173 /**
174 * @return True if a moving to the trash should be done instead of
175 * deleting the selected items.
176 * @see updateRemoveAction(), slotRemoveActionTriggered()
177 */
178 bool moveToTrash() const;
179
180 private:
181 struct Entry
182 {
183 int type;
184 QString name;
185 QString filePath; // empty for separator
186 QString templatePath; // same as filePath for template
187 QString icon;
188 QString comment;
189 };
190
191 enum ContextType
192 {
193 NoContext = 0,
194 ItemContext = 1,
195 TrashContext = 2
196 };
197
198 QPoint m_pos;
199 DolphinMainWindow* m_mainWindow;
200
201 KFileItem m_fileInfo;
202
203 KUrl m_baseUrl;
204 KFileItem* m_baseFileItem; /// File item for m_baseUrl
205
206 KFileItemList m_selectedItems;
207 mutable KFileItemListProperties* m_selectedItemsProperties;
208
209 int m_context;
210 KonqCopyToMenu m_copyToMenu;
211 QList<QAction*> m_customActions;
212 KMenu* m_popup;
213
214 Command m_command;
215
216 bool m_shiftPressed;
217 QAction* m_removeAction; // Action that represents either 'Move To Trash' or 'Delete'
218 };
219
220 #endif