ProcessesItemModel.hpp
1 #pragma once
2 #include <Process/ProcessFactory.hpp>
3 #include <Process/ProcessMimeSerialization.hpp>
4 
5 #include <Library/LibrarySettings.hpp>
6 
7 #include <score/application/ApplicationContext.hpp>
8 #include <score/model/tree/TreeNode.hpp>
9 #include <score/model/tree/TreeNodeItemModel.hpp>
10 #include <score/tools/File.hpp>
11 #include <score/tools/std/Optional.hpp>
12 #include <score/tools/std/StringHash.hpp>
13 
14 #include <ossia/detail/hash_map.hpp>
15 
16 #include <QDir>
17 #include <QIcon>
18 
19 #include <nano_observer.hpp>
20 #include <score_plugin_library_export.h>
21 
22 #include <verdigris>
23 
24 namespace score
25 {
26 struct GUIApplicationContext;
27 }
28 
29 namespace Library
30 {
32 {
33  QIcon icon;
34  QString author;
35  QString description;
36 };
37 
39 SCORE_PLUGIN_LIBRARY_EXPORT
40 ProcessNode& addToLibrary(ProcessNode& parent, Library::ProcessData&& data);
41 
42 class SCORE_PLUGIN_LIBRARY_EXPORT ProcessesItemModel
43  : public TreeNodeBasedItemModel<ProcessNode>
44  , public Nano::Observer
45 {
46 public:
47  using QAbstractItemModel::beginInsertRows;
48  using QAbstractItemModel::endInsertRows;
49 
50  using QAbstractItemModel::beginRemoveRows;
51  using QAbstractItemModel::endRemoveRows;
52 
53  ProcessesItemModel(const score::GUIApplicationContext& ctx, QObject* parent);
54 
55  void rescan();
56  QModelIndex find(const Process::ProcessModelFactory::ConcreteKey& k);
57 
58  ProcessNode& rootNode() override;
59  const ProcessNode& rootNode() const override;
60 
61  // Data reading
62  int columnCount(const QModelIndex& parent) const override;
63  QVariant data(const QModelIndex& index, int role) const override;
64 
65  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
66  Qt::ItemFlags flags(const QModelIndex& index) const override;
67 
68  // Drag, drop, etc.
69  QStringList mimeTypes() const override;
70  QMimeData* mimeData(const QModelIndexList& indexes) const override;
71  Qt::DropActions supportedDragActions() const override;
72 
73  void on_newPlugin(const Process::ProcessModelFactory& fact);
74 
75 private:
76  ProcessNode& addCategory(const QString& cat);
77  const score::GUIApplicationContext& context;
78  ProcessNode m_root;
79 };
80 
86 {
87  Library::ProcessNode* parent{};
88  QDir libraryFolder;
89  std::string libraryFolderPath{};
90  ossia::hash_map<QString, Library::ProcessNode*> categories;
91 
92  void init(const QModelIndex& idx, const score::ApplicationContext& ctx)
93  {
94  categories.clear();
95  parent = reinterpret_cast<Library::ProcessNode*>(idx.internalPointer());
96 
97  // We use the parent folder as category...
98  libraryFolder.setPath(ctx.settings<Library::Settings::Model>().getPackagesPath());
99  libraryFolderPath = libraryFolder.absolutePath().toStdString();
100  }
101 
102  [[deprecated]] void add(const QFileInfo& file, Library::ProcessData&& pdata)
103  {
104  auto parentFolder = file.dir().dirName();
105  if(auto it = categories.find(parentFolder); it != categories.end())
106  {
107  Library::addToLibrary(*it->second, std::move(pdata));
108  }
109  else
110  {
111  if(file.dir() == libraryFolder)
112  {
113  Library::addToLibrary(*parent, std::move(pdata));
114  }
115  else
116  {
117  auto& category = Library::addToLibrary(
118  *parent, Library::ProcessData{{{}, parentFolder, {}}, {}, {}, {}});
119  Library::addToLibrary(category, std::move(pdata));
120  categories[parentFolder] = &category;
121  }
122  }
123  }
124 
125  void add(const score::PathInfo& file, Library::ProcessData&& pdata)
126  {
127  auto parentFolder
128  = QString::fromUtf8(file.parentDirName.data(), file.parentDirName.size());
129  if(auto it = categories.find(parentFolder); it != categories.end())
130  {
131  Library::addToLibrary(*it->second, std::move(pdata));
132  }
133  else
134  {
135  if(file.absolutePath == libraryFolderPath)
136  {
137  Library::addToLibrary(*parent, std::move(pdata));
138  }
139  else
140  {
141  auto& category = Library::addToLibrary(
142  *parent, Library::ProcessData{{{}, parentFolder, {}}, {}, {}, {}});
143  Library::addToLibrary(category, std::move(pdata));
144  categories[parentFolder] = &category;
145  }
146  }
147  }
148 };
149 
150 }
151 
152 inline QDataStream& operator<<(QDataStream& i, const Library::ProcessData& sel)
153 {
154  return i;
155 }
156 inline QDataStream& operator>>(QDataStream& i, Library::ProcessData& sel)
157 {
158  return i;
159 }
160 
161 W_REGISTER_ARGTYPE(Library::ProcessData)
162 Q_DECLARE_METATYPE(Library::ProcessData)
163 W_REGISTER_ARGTYPE(std::optional<Library::ProcessData>)
Definition: ProcessesItemModel.hpp:45
Definition: LibrarySettings.hpp:46
The ProcessFactory class.
Definition: score-lib-process/Process/ProcessFactory.hpp:35
Definition: TreeNodeItemModel.hpp:38
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: ProcessesItemModel.hpp:32
Definition: ProcessesItemModel.hpp:86
Definition: ProcessMimeSerialization.hpp:38
Used to access all the application-wide state and structures.
Definition: ApplicationContext.hpp:24
T & settings() const
Access a specific Settings model instance.
Definition: ApplicationContext.hpp:40
Specializes ApplicationContext with the QMainWindow.
Definition: GUIApplicationContext.hpp:15
Definition: lib/score/tools/File.hpp:20