Changeset View
Changeset View
Standalone View
Standalone View
src/appchooserdialog.h
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright © 2016-2018 Red Hat, Inc | 2 | * Copyright © 2016-2019 Red Hat, Inc | ||
3 | * | 3 | * | ||
4 | * This program is free software; you can redistribute it and/or | 4 | * This program is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | 5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | 6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2 of the License, or (at your option) any later version. | 7 | * version 2 of the License, or (at your option) any later version. | ||
8 | * | 8 | * | ||
9 | * This library is distributed in the hope that it will be useful, | 9 | * This library is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Lesser General Public License for more details. | 12 | * Lesser General Public License for more details. | ||
13 | * | 13 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public | 14 | * You should have received a copy of the GNU Lesser General Public | ||
15 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | 15 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
16 | * | 16 | * | ||
17 | * Authors: | 17 | * Authors: | ||
18 | * Jan Grulich <jgrulich@redhat.com> | 18 | * Jan Grulich <jgrulich@redhat.com> | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #ifndef XDG_DESKTOP_PORTAL_KDE_APPCHOOSER_DIALOG_H | 21 | #ifndef XDG_DESKTOP_PORTAL_KDE_APPCHOOSER_DIALOG_H | ||
22 | #define XDG_DESKTOP_PORTAL_KDE_APPCHOOSER_DIALOG_H | 22 | #define XDG_DESKTOP_PORTAL_KDE_APPCHOOSER_DIALOG_H | ||
23 | 23 | | |||
24 | #include <QDialog> | 24 | #include <QDialog> | ||
25 | 25 | | |||
26 | class QGridLayout; | 26 | #include <QAbstractListModel> | ||
27 | #include <QSortFilterProxyModel> | ||||
28 | | ||||
29 | namespace Ui | ||||
30 | { | ||||
31 | class AppChooserDialog; | ||||
32 | } | ||||
33 | | ||||
34 | class ApplicationItem | ||||
35 | { | ||||
36 | public: | ||||
37 | enum ApplicationCategory { | ||||
38 | PreferredApplication, | ||||
39 | AllApplications | ||||
40 | }; | ||||
41 | | ||||
42 | explicit ApplicationItem(const QString &name, const QString &icon, const QString &desktopFileName); | ||||
43 | | ||||
44 | QString applicationName() const; | ||||
45 | QString applicationIcon() const; | ||||
46 | QString applicationDesktopFile() const; | ||||
47 | | ||||
48 | void setApplicationCategory(ApplicationCategory category); | ||||
49 | ApplicationCategory applicationCategory() const; | ||||
50 | | ||||
51 | bool operator==(const ApplicationItem &item) const; | ||||
52 | private: | ||||
53 | QString m_applicationName; | ||||
54 | QString m_applicationIcon; | ||||
55 | QString m_applicationDesktopFile; | ||||
56 | ApplicationCategory m_applicationCategory; | ||||
57 | }; | ||||
58 | | ||||
59 | class AppFilterModel : public QSortFilterProxyModel | ||||
60 | { | ||||
61 | Q_OBJECT | ||||
62 | Q_PROPERTY(bool showOnlyPreferredApps READ showOnlyPreferredApps WRITE setShowOnlyPrefferedApps) | ||||
63 | Q_PROPERTY(QString filter READ filter WRITE setFilter) | ||||
64 | public: | ||||
65 | explicit AppFilterModel(QObject *parent = nullptr); | ||||
66 | ~AppFilterModel() override; | ||||
67 | | ||||
68 | void setShowOnlyPrefferedApps(bool show); | ||||
69 | bool showOnlyPreferredApps() const; | ||||
70 | | ||||
71 | void setFilter(const QString &text); | ||||
72 | QString filter() const; | ||||
73 | | ||||
74 | protected: | ||||
75 | bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; | ||||
76 | bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; | ||||
77 | | ||||
78 | private: | ||||
79 | bool m_showOnlyPreferredApps = true; | ||||
80 | QString m_filter; | ||||
81 | }; | ||||
82 | | ||||
83 | class AppModel : public QAbstractListModel | ||||
84 | { | ||||
85 | Q_OBJECT | ||||
86 | public: | ||||
87 | enum ItemRoles { | ||||
88 | ApplicationNameRole = Qt::UserRole + 1, | ||||
89 | ApplicationIconRole, | ||||
90 | ApplicationDesktopFileRole, | ||||
91 | ApplicationCategoryRole | ||||
92 | }; | ||||
93 | | ||||
94 | explicit AppModel(QObject *parent = nullptr); | ||||
95 | ~AppModel() override; | ||||
96 | | ||||
97 | void setPreferredApps(const QStringList &list); | ||||
98 | | ||||
99 | QVariant data(const QModelIndex &index, int role) const override; | ||||
100 | int rowCount(const QModelIndex &parent) const override; | ||||
101 | QHash<int, QByteArray> roleNames() const override; | ||||
102 | | ||||
103 | private: | ||||
104 | void loadApplications(); | ||||
105 | | ||||
106 | QList<ApplicationItem> m_list; | ||||
107 | }; | ||||
27 | 108 | | |||
28 | class AppChooserDialog : public QDialog | 109 | class AppChooserDialog : public QDialog | ||
29 | { | 110 | { | ||
30 | Q_OBJECT | 111 | Q_OBJECT | ||
31 | public: | 112 | public: | ||
32 | AppChooserDialog(const QStringList &choices, const QString &defaultApp, const QString &fileName, QDialog *parent = nullptr, Qt::WindowFlags flags = {}); | 113 | explicit AppChooserDialog(const QStringList &choices, const QString &defaultApp, const QString &fileName, QDialog *parent = nullptr, Qt::WindowFlags flags = {}); | ||
33 | ~AppChooserDialog(); | 114 | ~AppChooserDialog(); | ||
34 | 115 | | |||
35 | void updateChoices(const QStringList &choices); | 116 | void updateChoices(const QStringList &choices); | ||
36 | 117 | | |||
37 | QString selectedApplication() const; | 118 | QString selectedApplication() const; | ||
38 | | ||||
39 | private Q_SLOTS: | 119 | private Q_SLOTS: | ||
40 | void addDialogItems(); | 120 | void onApplicationSelected(const QString &desktopFile); | ||
41 | 121 | void onOpenDiscover(); | |||
42 | private: | 122 | private: | ||
123 | Ui::AppChooserDialog *m_dialog; | ||||
43 | 124 | | |||
44 | QStringList m_choices; | 125 | AppModel *m_model; | ||
126 | QStringList m_defaultChoices; | ||||
45 | QString m_defaultApp; | 127 | QString m_defaultApp; | ||
46 | QString m_selectedApplication; | 128 | QString m_selectedApplication; | ||
47 | QGridLayout *m_gridLayout; | | |||
48 | }; | 129 | }; | ||
49 | 130 | | |||
50 | #endif // XDG_DESKTOP_PORTAL_KDE_APPCHOOSER_DIALOG_H | 131 | #endif // XDG_DESKTOP_PORTAL_KDE_APPCHOOSER_DIALOG_H | ||
51 | | ||||
52 | |