| 1 | // CatalogListModel.hxx - part of GUI launcher using Qt5 |
| 2 | // |
| 3 | // Written by James Turner, started March 2015. |
| 4 | // |
| 5 | // Copyright (C) 2015 James Turner <[email protected]> |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | #ifndef FG_GUI_CATALOG_LIST_MODEL |
| 22 | #define FG_GUI_CATALOG_LIST_MODEL |
| 23 | |
| 24 | #include <QAbstractListModel> |
| 25 | #include <QDateTime> |
| 26 | #include <QDir> |
| 27 | #include <QPixmap> |
| 28 | #include <QStringList> |
| 29 | #include <QUrl> |
| 30 | |
| 31 | #include <simgear/package/Root.hxx> |
| 32 | #include <simgear/package/Catalog.hxx> |
| 33 | |
| 34 | const int CatalogUrlRole = Qt::UserRole + 1; |
| 35 | const int CatalogIdRole = Qt::UserRole + 2; |
| 36 | const int CatalogPackageCountRole = Qt::UserRole + 3; |
| 37 | const int CatalogInstallCountRole = Qt::UserRole + 4; |
| 38 | const int CatalogStatusRole = Qt::UserRole + 5; |
| 39 | const int CatalogDescriptionRole = Qt::UserRole + 6; |
| 40 | const int CatalogNameRole = Qt::UserRole + 7; |
| 41 | const int CatalogIsNewlyAdded = Qt::UserRole + 8; |
| 42 | const int CatalogEnabled = Qt::UserRole + 9; |
| 43 | |
| 44 | class CatalogDelegate; |
| 45 | |
| 46 | class CatalogListModel : public QAbstractListModel |
| 47 | { |
| 48 | Q_OBJECT |
| 49 | |
| 50 | Q_PROPERTY(bool isAddingCatalog READ isAddingCatalog NOTIFY isAddingCatalogChanged) |
| 51 | Q_PROPERTY(CatalogStatus statusOfAddingCatalog READ statusOfAddingCatalog NOTIFY statusOfAddingCatalogChanged) |
| 52 | public: |
| 53 | CatalogListModel(QObject* pr, const simgear::pkg::RootRef& root); |
| 54 | |
| 55 | ~CatalogListModel(); |
| 56 | |
| 57 | int rowCount(const QModelIndex& parent) const override; |
| 58 | |
| 59 | QVariant data(const QModelIndex& index, int role) const override; |
| 60 | |
| 61 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
| 62 | |
| 63 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 64 | |
| 65 | QHash<int, QByteArray> roleNames() const override; |
| 66 | |
| 67 | Q_INVOKABLE void removeCatalog(int index); |
| 68 | |
| 69 | Q_INVOKABLE void refreshCatalog(int index); |
| 70 | |
| 71 | Q_INVOKABLE void installDefaultCatalog(bool showAddFeedback); |
| 72 | |
| 73 | // returns the index of the new catalog |
| 74 | Q_INVOKABLE void addCatalogByUrl(QUrl url); |
| 75 | |
| 76 | Q_INVOKABLE void finalizeAddCatalog(); |
| 77 | |
| 78 | Q_INVOKABLE void abandonAddCatalog(); |
| 79 | |
| 80 | bool isAddingCatalog() const; |
| 81 | |
| 82 | enum CatalogStatus |
| 83 | { |
| 84 | Ok = 0, |
| 85 | Refreshing, |
| 86 | NetworkError, |
| 87 | NotFoundOnServer, |
| 88 | IncompatibleVersion, |
| 89 | HTTPForbidden, |
| 90 | InvalidData, |
| 91 | UnknownError, |
| 92 | NoAddInProgress |
| 93 | }; |
| 94 | |
| 95 | Q_ENUMS(CatalogStatus) |
| 96 | |
| 97 | void onCatalogStatusChanged(simgear::pkg::Catalog *cat); |
| 98 | |
| 99 | CatalogStatus statusOfAddingCatalog() const; |
| 100 | |
| 101 | signals: |
| 102 | void isAddingCatalogChanged(); |
| 103 | void statusOfAddingCatalogChanged(); |
| 104 | |
| 105 | void catalogsChanged(); |
| 106 | |
| 107 | public slots: |
| 108 | void resetData(); |
| 109 | |
| 110 | private slots: |
| 111 | |
| 112 | private: |
| 113 | simgear::pkg::RootRef m_packageRoot; |
| 114 | simgear::pkg::CatalogList m_catalogs; |
| 115 | |
| 116 | simgear::pkg::CatalogRef m_newlyAddedCatalog; |
| 117 | |
| 118 | int indexOf(QUrl url); |
| 119 | |
| 120 | CatalogDelegate* m_delegate; |
| 121 | |
| 122 | CatalogStatus translateStatusForCatalog(simgear::pkg::CatalogRef cat) const; |
| 123 | }; |
| 124 | |
| 125 | #endif // of FG_GUI_CATALOG_LIST_MODEL |