Loading...
Searching...
No Matches
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
24namespace score
25{
26struct GUIApplicationContext;
27}
28
29namespace Library
30{
32{
33 QIcon icon;
34};
35
37SCORE_PLUGIN_LIBRARY_EXPORT
38ProcessNode& addToLibrary(ProcessNode& parent, Library::ProcessData&& data);
39
40class SCORE_PLUGIN_LIBRARY_EXPORT ProcessesItemModel
41 : public TreeNodeBasedItemModel<ProcessNode>
42 , public Nano::Observer
43{
44public:
45 using QAbstractItemModel::beginInsertRows;
46 using QAbstractItemModel::endInsertRows;
47
48 using QAbstractItemModel::beginRemoveRows;
49 using QAbstractItemModel::endRemoveRows;
50
51 ProcessesItemModel(const score::GUIApplicationContext& ctx, QObject* parent);
52
53 void rescan();
54 QModelIndex find(const Process::ProcessModelFactory::ConcreteKey& k);
55
56 ProcessNode& rootNode() override;
57 const ProcessNode& rootNode() const override;
58
59 // Data reading
60 int columnCount(const QModelIndex& parent) const override;
61 QVariant data(const QModelIndex& index, int role) const override;
62
63 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
64 Qt::ItemFlags flags(const QModelIndex& index) const override;
65
66 // Drag, drop, etc.
67 QStringList mimeTypes() const override;
68 QMimeData* mimeData(const QModelIndexList& indexes) const override;
69 Qt::DropActions supportedDragActions() const override;
70
71 void on_newPlugin(const Process::ProcessModelFactory& fact);
72
73private:
74 ProcessNode& addCategory(const QString& cat);
75 const score::GUIApplicationContext& context;
76 ProcessNode m_root;
77};
78
84{
85 Library::ProcessNode* parent{};
86 QDir libraryFolder;
87 std::string libraryFolderPath{};
88 std::string defaultPresetsPath{};
89 ossia::hash_map<QString, Library::ProcessNode*> categories;
90
91 void init(
92 std::string process_name, const QModelIndex& idx,
94 {
95 categories.clear();
96 parent = reinterpret_cast<Library::ProcessNode*>(idx.internalPointer());
97 SCORE_ASSERT(parent);
98
99 // We use the parent folder as category...
100 libraryFolder.setPath(ctx.settings<Library::Settings::Model>().getPackagesPath());
101 libraryFolderPath = libraryFolder.absolutePath().toStdString();
102
103 // Also so that stuff in Presets/<Name of the process> does not needlessly go into a subfolder
104 defaultPresetsPath = "Presets/" + process_name;
105 }
106
107 [[deprecated]] void add(const QFileInfo& file, Library::ProcessData&& pdata)
108 {
109 SCORE_ASSERT(parent);
110 auto parentFolder = file.dir().dirName();
111 if(auto it = categories.find(parentFolder); it != categories.end())
112 {
113 Library::addToLibrary(*it->second, std::move(pdata));
114 }
115 else
116 {
117 if(file.dir() == libraryFolder
118 || file.absolutePath().endsWith(defaultPresetsPath.c_str()))
119 {
120 Library::addToLibrary(*parent, std::move(pdata));
121 }
122 else
123 {
124 auto& category = Library::addToLibrary(
125 *parent, Library::ProcessData{{{}, parentFolder, {}}, {}});
126 Library::addToLibrary(category, std::move(pdata));
127 categories[parentFolder] = &category;
128 }
129 }
130 }
131
132 void add(const score::PathInfo& file, Library::ProcessData&& pdata)
133 {
134 SCORE_ASSERT(parent);
135 auto parentFolder
136 = QString::fromUtf8(file.parentDirName.data(), file.parentDirName.size());
137 if(auto it = categories.find(parentFolder); it != categories.end())
138 {
139 Library::addToLibrary(*it->second, std::move(pdata));
140 }
141 else
142 {
143 if(file.absolutePath == libraryFolderPath
144 || file.absolutePath.ends_with(defaultPresetsPath))
145 {
146 Library::addToLibrary(*parent, std::move(pdata));
147 }
148 else
149 {
150 auto& category = Library::addToLibrary(
151 *parent, Library::ProcessData{{{}, parentFolder, {}}, {}});
152 Library::addToLibrary(category, std::move(pdata));
153 categories[parentFolder] = &category;
154 }
155 }
156 }
157};
158
159}
160
161inline QDataStream& operator<<(QDataStream& i, const Library::ProcessData& sel)
162{
163 return i;
164}
165inline QDataStream& operator>>(QDataStream& i, Library::ProcessData& sel)
166{
167 return i;
168}
169
170W_REGISTER_ARGTYPE(Library::ProcessData)
171Q_DECLARE_METATYPE(Library::ProcessData)
172W_REGISTER_ARGTYPE(std::optional<Library::ProcessData>)
Definition ProcessesItemModel.hpp:43
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:97
Definition ProcessesItemModel.hpp:32
Definition ProcessesItemModel.hpp:84
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