1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2013 - 2018 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 **************************************************************************/
19 
20 #ifndef OTTER_MENU_H
21 #define OTTER_MENU_H
22 
23 #include "../core/ActionExecutor.h"
24 
25 #include <QtCore/QJsonObject>
26 #include <QtWidgets/QMenu>
27 
28 namespace Otter
29 {
30 
31 class Menu : public QMenu
32 {
33 	Q_OBJECT
34 	Q_ENUMS(MenuRole)
35 
36 public:
37 	enum MenuRole
38 	{
39 		UnknownMenu = 0,
40 		BookmarksMenu,
41 		BookmarkSelectorMenu,
42 		NotesMenu,
43 		CharacterEncodingMenu,
44 		ClosedWindowsMenu,
45 		DictionariesMenu,
46 		ImportExportMenu,
47 		OpenInApplicationMenu,
48 		ProxyMenu,
49 		SearchMenu,
50 		SessionsMenu,
51 		StyleSheetsMenu,
52 		ToolBarsMenu,
53 		UserAgentMenu,
54 		ValidateMenu,
55 		WindowsMenu
56 	};
57 
58 	explicit Menu(int role = UnknownMenu, QWidget *parent = nullptr);
59 
60 	void load(const QString &path, const QStringList &includeSections = {}, ActionExecutor::Object executor = ActionExecutor::Object());
61 	void load(const QJsonObject &definition, const QStringList &includeSections = {}, ActionExecutor::Object executor = ActionExecutor::Object());
62 	void load(int option);
63 	void setTitle(const QString &title);
64 	void setExecutor(ActionExecutor::Object executor);
65 	void setActionParameters(const QVariantMap &parameters);
66 	void setMenuOptions(const QVariantMap &options);
67 	int getRole() const;
68 	static int getMenuRoleIdentifier(const QString &name);
69 
70 protected:
71 	void changeEvent(QEvent *event) override;
72 	void hideEvent(QHideEvent *event) override;
73 	void mousePressEvent(QMouseEvent *event) override;
74 	void mouseReleaseEvent(QMouseEvent *event) override;
75 	void contextMenuEvent(QContextMenuEvent *event) override;
76 	void appendAction(const QJsonValue &definition, const QStringList &sections, ActionExecutor::Object executor);
77 	ActionExecutor::Object getExecutor() const;
78 	bool canInclude(const QJsonObject &definition, const QStringList &sections);
79 	bool hasIncludeMatch(const QJsonObject &definition, const QString &key, const QStringList &sections);
80 
81 protected slots:
82 	void hideMenu();
83 	void populateBookmarksMenu();
84 	void populateBookmarkSelectorMenu();
85 	void populateOptionMenu();
86 	void populateCharacterEncodingMenu();
87 	void populateClosedWindowsMenu();
88 	void populateDictionariesMenu();
89 	void populateNotesMenu();
90 	void populateOpenInApplicationMenu();
91 	void populateProxiesMenu();
92 	void populateSearchMenu();
93 	void populateSessionsMenu();
94 	void populateStyleSheetsMenu();
95 	void populateToolBarsMenu();
96 	void populateUserAgentMenu();
97 	void populateWindowsMenu();
98 	void clearBookmarksMenu();
99 	void clearClosedWindows();
100 	void clearNotesMenu();
101 	void selectOption(QAction *action);
102 	void updateClosedWindowsMenu();
103 
104 private:
105 	QActionGroup *m_actionGroup;
106 	QAction *m_clickedAction;
107 	QString m_title;
108 	ActionExecutor::Object m_executor;
109 	QHash<QString, QActionGroup*> m_actionGroups;
110 	QVariantMap m_actionParameters;
111 	QVariantMap m_menuOptions;
112 	int m_role;
113 	int m_option;
114 
115 	static int m_menuRoleIdentifierEnumerator;
116 };
117 
118 }
119 
120 #endif
121