1 /***************************************************************************
2  *   Copyright (C) 2008-2021 by Ilya Kotov                                 *
3  *   forkotov02@ya.ru                                                      *
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 2 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, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20 #ifndef UIHELPER_H
21 #define UIHELPER_H
22 
23 #include <QObject>
24 #include <QMap>
25 #include <QList>
26 #include <QPointer>
27 #include <QStringList>
28 #include <QApplication>
29 #include <QPointer>
30 #include "playlistmanager.h"
31 #include "playlistmodel.h"
32 #include "qmmpui_export.h"
33 
34 class QAction;
35 class QMenu;
36 class QWidget;
37 class General;
38 class GeneralFactory;
39 class JumpToTrackDialog;
40 
41 /*! @brief The UiHelper class provides simple api to access general plugins and some gui features.
42  * @author Ilya Kotov <forkotov02@ya.ru>
43  */
44 class QMMPUI_EXPORT  UiHelper : public QObject
45 {
46     Q_OBJECT
47 public:
48     /*!
49      * Object constructor,
50      * @param parent Parent object
51      */
52     UiHelper(QObject *parent = nullptr);
53     /*!
54      * Destructor
55      */
56     ~UiHelper();
57     /*!
58      * Enum of menu type
59      */
60     enum MenuType
61     {
62         TOOLS_MENU = 0, /*!< tools menu */
63         PLAYLIST_MENU,  /*!< playlist context menu */
64         ADD_MENU        /*!< "add"/"file" menu */
65     };
66     /*!
67      * Returns \b true if one of the general plugin can change visibility, otherwise returns \b false
68      */
69     bool visibilityControl();
70     /*!
71      * Adds the newly created action to the menu with type \b type.
72      * Menu with type \b type should be created first.
73      * @param action Pointer of action.
74      * @param type Menu type.
75      */
76     void addAction(QAction *action, MenuType type = TOOLS_MENU);
77     /*!
78      * Removes action \b action from created menus.
79      */
80     void removeAction(QAction *action);
81     /*!
82      * Returns a list of actions of the menu type \b type
83      */
84     QList<QAction *> actions(MenuType type = TOOLS_MENU);
85     /*!
86      * Creates menu with type \b type
87      * @param type Menu type.
88      * @param title Menu title.
89      * @param autoHide Hide empty menu.
90      * @param parent Parent widget
91      */
92     QMenu *createMenu(MenuType type, const QString &title = QString(), bool autoHide = true, QWidget *parent = nullptr);
93     /*!
94      * Registers existing menu for access from general plugins.
95      * @param type Menu type.
96      * @param menu Menu pointer.
97      * @param autoHide Hide empty menu.
98      * @param before An action, after which the rest are added.
99      */
100     void registerMenu(MenuType type, QMenu *menu, bool autoHide = false, QAction *before = nullptr);
101     /*!
102      * Opens 'Add Files' dialog
103      * @param parent Parent widget
104      * @param model Destination playlist model
105      */
106     void addFiles(QWidget *parent = qApp->activeWindow(),
107                  PlayListModel *model = PlayListManager::instance()->selectedPlayList());
108     /*!
109      * Opens 'Play Files' dialog
110      * @param parent Parent widget
111      * @param model Destination playlist model
112      */
113     void playFiles(QWidget *parent = qApp->activeWindow(),
114                    PlayListModel *model = PlayListManager::instance()->selectedPlayList());
115     /*!
116      * Opens 'Add Directory' dialog
117      * @param parent Parent widget
118      * @param model Destination playlist model
119      */
120     void addDirectory(QWidget *parent = qApp->activeWindow(),
121                       PlayListModel *model = PlayListManager::instance()->selectedPlayList());
122     /*!
123      * Opens 'Add URL' dialog
124      * @param parent Parent widget
125      * @param model Destination playlist model
126      */
127     void addUrl(QWidget *parent = qApp->activeWindow(),
128                 PlayListModel *model = PlayListManager::instance()->selectedPlayList());
129     /*!
130      * Opens 'Load Playlist' dialog
131      * @param parent Parent widget
132      * @param model Destination playlist model
133      */
134     void loadPlayList(QWidget *parent = qApp->activeWindow(),
135                      PlayListModel *model = PlayListManager::instance()->selectedPlayList());
136     /*!
137      * Opens 'Save Playlist' dialog
138      * @param parent Parent widget
139      * @param model Destination playlist model
140      */
141     void savePlayList(QWidget *parent = qApp->activeWindow(),
142                      PlayListModel *model = PlayListManager::instance()->selectedPlayList());
143     /*!
144      * Opens 'Jump To Track' dialog
145      * @param parent Parent widget
146      * @param model Destination playlist model
147      */
148     void jumpToTrack(QWidget *parent = qApp->activeWindow(),
149                      PlayListModel *model = PlayListManager::instance()->selectedPlayList());
150     /*!
151      * Shows about dialog
152      * @param parent Parent widget
153      */
154     void about(QWidget *parent = nullptr);
155     /*!
156      * Returns a pointer to the object's instance.
157      */
158     static UiHelper* instance();
159 
160 public slots:
161     /*!
162      * Toggles player window visibility.
163      */
164     void toggleVisibility();
165     /*!
166      * Shows the main window of the player.
167      */
168     void showMainWindow();
169     /*!
170      * Tells the player to exit.
171      */
172     void exit();
173 
174 signals:
175     /*!
176      * Emitted when some general plugin requires to toggle visibility.
177      * Use it to show/hide player's window.
178      */
179     void toggleVisibilityCalled();
180     /*!
181      * Emitted when some general plugin requires to show main window.
182      * Use it to show player's window and to raise it to the top.
183      */
184     void showMainWindowCalled();
185     /*!
186      * Emitted when some general plugin adds widget.
187      * User interface should create this widget using the given \b id.
188      */
189     void widgetAdded(const QString &id);
190     /*!
191      * Emitted when some general plugin removes widget \b id.
192      * User interface should remove widget with the given \b id.
193      */
194     void widgetRemoved(const QString &id);
195     /*!
196      * Emitted when some general plugin changes settings of the widget \b id.
197      * User interface should recreate widget with the given \b id.
198      */
199     void widgetUpdated(const QString &id);
200 
201 private slots:
202     void removeAction(QObject *action);
203     void addSelectedFiles(const QStringList &files, bool play);
204     void playSelectedFiles(const QStringList &files);
205     void disconnectPl();
206 
207 private:
208     QMap <GeneralFactory*, General*> m_generals;
209     struct MenuData
210     {
211         QPointer<QMenu> menu;
212         QPointer<QAction> before;
213         QList<QAction*> actions;
214         bool autoHide = false;
215     };
216     QMap<MenuType, MenuData> m_menus;
217     QString m_lastDir;
218     QPointer <JumpToTrackDialog> m_jumpDialog;
219     PlayListModel *m_model = nullptr;
220     static UiHelper* m_instance;
221 };
222 
223 #endif
224