PluginItemModel.hpp
1 #pragma once
2 #include <score/plugins/Addon.hpp>
3 
4 #include <ossia/detail/algorithms.hpp>
5 #include <ossia/detail/flat_map.hpp>
6 #include <ossia/detail/optional.hpp>
7 
8 #include <boost/iterator/filter_iterator.hpp>
9 
10 #include <QAbstractItemModel>
11 #include <QFileSystemWatcher>
12 #include <QImage>
13 #include <QUrl>
14 
15 #include <verdigris>
16 
17 namespace score
18 {
19 struct ApplicationContext;
20 }
21 namespace PM
22 {
23 struct Package
24 {
25  static std::optional<Package> fromJson(const QJsonObject& obj) noexcept;
26 
27  UuidKey<score::Addon> key; // Can be the same as plug-in's
28 
29  QString raw_name;
30  QString name;
31  int version{}; // version of the add-on
32  QString target; // version of score targeted by this version of the add-on
33  QString kind; // what kind of package it is (for now: "addon", "sdk", "library")
34  QUrl file; // URL to a file containing the current version.
35  QString url; // Link to the homepage of the package if any
36 
37  QString shortDescription;
38  QString longDescription;
39  QString smallImagePath;
40  QString largeImagePath;
41  QImage smallImage;
42  QImage largeImage;
43  QString size;
44  bool enabled = true;
45  bool corePlugin = false; // For plug-ins shipped with score
46 };
47 
48 class PackagesModel : public QAbstractItemModel
49 {
50 public:
51  std::vector<Package> m_vec;
52 
53  void addAddon(Package e);
54  void removeAddon(Package e);
55 
56  auto& addons() { return m_vec; }
57  auto& addons() const { return m_vec; }
58 
59  void clear();
60 
61 private:
62  enum class Column
63  {
64  Name,
65  Version,
66  Size,
67  ShortDesc
68  };
69  static constexpr const int ColumnCount = 4;
70 
71  QModelIndex index(int row, int column, const QModelIndex& parent) const override;
72  QModelIndex parent(const QModelIndex& child) const override;
73  int rowCount(const QModelIndex& parent) const override;
74  int columnCount(const QModelIndex& parent) const override;
75  QVariant data(const QModelIndex& index, int role) const override;
76  Qt::ItemFlags flags(const QModelIndex& index) const override;
77 
78  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
79 };
80 
82 {
83  explicit LocalPackagesModel(const score::ApplicationContext& ctx);
84  void registerAddon(const QString& p);
85  QFileSystemWatcher m_addonsWatch;
86 };
87 
89 {
90 public:
91  template <typename Fun>
92  void updateAddon(UuidKey<score::Addon> k, Fun f)
93  {
94  if(auto add = addon(k))
95  {
96  beginResetModel();
97  f(*add);
98  endResetModel();
99  }
100  }
101 
102 private:
104  {
105  auto it = ossia::find_if(m_vec, [&](auto& add) { return add.key == k; });
106 
107  if(it != m_vec.end())
108  return &*it;
109 
110  return nullptr;
111  }
112 };
113 }
114 
115 Q_DECLARE_METATYPE(PM::Package)
116 W_REGISTER_ARGTYPE(PM::Package)
Definition: PluginItemModel.hpp:49
Definition: PluginItemModel.hpp:89
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: PluginItemModel.hpp:82
Definition: PluginItemModel.hpp:24
Used to access all the application-wide state and structures.
Definition: ApplicationContext.hpp:24