DeviceExplorerModel.hpp
1 #pragma once
2 #include <State/Message.hpp>
3 #include <State/Value.hpp>
4 
5 #include <Device/ItemModels/NodeBasedItemModel.hpp>
6 #include <Device/Node/DeviceNode.hpp>
7 
8 #include <Explorer/Explorer/Column.hpp>
9 
10 #include <score/document/DocumentContext.hpp>
11 
12 #include <QAbstractItemModel>
13 #include <QString>
14 #include <QStringList>
15 #include <QVariant>
16 #include <qnamespace.h>
17 
18 #include <score_plugin_deviceexplorer_export.h>
19 
20 #include <verdigris>
21 class QMimeData;
22 class QObject;
23 namespace score
24 {
25 class CommandStackFacade;
26 }
27 namespace Device
28 {
29 struct DeviceSettings;
30 struct AddressSettings;
31 }
32 
33 namespace Explorer
34 {
35 class ListeningManager;
36 class DeviceDocumentPlugin;
37 class DeviceEditDialog;
38 class DeviceExplorerView;
39 class DeviceExplorerWidget;
40 
52 struct SCORE_PLUGIN_DEVICEEXPLORER_EXPORT SelectedNodes
53 {
57  std::vector<Device::Node*> parents;
58 
62  std::vector<Device::Node*> messages;
63 };
64 
65 class SCORE_PLUGIN_DEVICEEXPLORER_EXPORT DeviceExplorerModel final
67 {
68  W_OBJECT(DeviceExplorerModel)
69 
70 public:
71  using QAbstractItemModel::beginResetModel;
72  using QAbstractItemModel::endResetModel;
73 
74  explicit DeviceExplorerModel(DeviceDocumentPlugin&, QObject* parent);
75 
77 
78  Device::Node& rootNode() override { return m_rootNode; }
79 
80  const Device::Node& rootNode() const override { return m_rootNode; }
81 
82  void setView(DeviceExplorerView* v) { m_view = v; }
83 
84  // The class that does the link with the low-level implementation of the
85  // devices. This is here so that commands don't need to save
86  // at the same time a path to the device explorer, and the device doc plugin.
87  DeviceDocumentPlugin& deviceModel() const;
88  QModelIndexList selectedIndexes() const;
89 
90  const score::CommandStackFacade& commandStack() const { return m_cmdQ; }
91 
92  // Returns the row (useful for undo)
93  int addDevice(Device::Node&& deviceNode);
94  int addDevice(const Device::Node& deviceNode);
95  void updateDevice(const QString& name, const Device::DeviceSettings& dev);
96 
97  Device::Node* addAddress(
98  Device::Node* parentNode, const Device::AddressSettings& addressSettings, int row);
99  void updateAddress(Device::Node* node, const Device::AddressSettings& addressSettings);
100 
101  void addNode(Device::Node* parentNode, Device::Node&& child, int row);
102 
103  void updateValue(
104  Device::Node* n, const State::AddressAccessor& addr, const ossia::value& v);
105 
106  // Checks if the settings can be added; if not,
107  // trigger a dialog to edit them as wanted.
108  // Returns true if the device is to be added, false if
109  // it should not be added.
110  bool checkDeviceInstantiatable(const Device::DeviceSettings& n) const;
111  bool checkDeviceEditable(
112  const QString& originalName, const Device::DeviceSettings& n) const;
113 
114  bool
115  checkAddressInstantiatable(Device::Node& parent, const Device::AddressSettings& addr);
116 
117  bool checkAddressEditable(
118  Device::Node& parent, const Device::AddressSettings& before,
119  const Device::AddressSettings& after);
120 
121  int columnCount() const;
122  QStringList getColumns() const;
123  bool isEmpty() const;
124  bool isDevice(QModelIndex index) const;
125  bool hasCut() const;
126 
127  void debug_printIndexes(const QModelIndexList& indexes);
128 
129  int columnCount(const QModelIndex& parent) const override;
130 
131  QVariant data(const QModelIndex& index, int role) const override;
132  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
133 
134  Qt::ItemFlags flags(const QModelIndex& index) const override;
135 
136  bool setData(const QModelIndex& index, const QVariant& value, int role) override;
137  bool setHeaderData(int, Qt::Orientation, const QVariant&, int = Qt::EditRole) override;
138 
139  void editData(
140  const Device::NodePath& path, Column column, const ossia::value& value, int role);
141  void editData(Device::Node& n, Column column, const ossia::value& value, int role);
142 
143  Qt::DropActions supportedDropActions() const override;
144  Qt::DropActions supportedDragActions() const override;
145  QStringList mimeTypes() const override;
146  QMimeData* mimeData(const QModelIndexList& indexes) const override;
147  bool canDropMimeData(
148  const QMimeData* data, Qt::DropAction action, int row, int column,
149  const QModelIndex& parent) const override;
150  bool dropMimeData(
151  const QMimeData* mimeData, Qt::DropAction action, int row, int column,
152  const QModelIndex& parent) override;
153 
154  SelectedNodes uniqueSelectedNodes(const QModelIndexList& indexes)
155  const; // Note : filters so that only parents are given.
156  void checkAndLoadDevice(Device::Node n);
157  void checkAndLoadDevice(Device::DeviceSettings n);
158 
159 public:
160  void nodeChanged(Device::Node* n)
161  E_SIGNAL(SCORE_PLUGIN_DEVICEEXPLORER_EXPORT, nodeChanged, n)
162 
163 protected:
164  void debug_printPath(const Device::NodePath& path);
165 
166 private:
167  DeviceDocumentPlugin& m_devicePlugin;
168 
169  QModelIndex bottomIndex(const QModelIndex& index) const;
170 
171  Device::Node& m_rootNode;
172 
173  const score::CommandStackFacade& m_cmdQ;
174 
175  DeviceExplorerView* m_view{};
176 };
177 
178 // Will update the tree and return the messages corresponding to the selected
179 // nodes.
180 SCORE_PLUGIN_DEVICEEXPLORER_EXPORT State::MessageList
181 getSelectionSnapshot(DeviceExplorerModel& model);
182 
183 SCORE_PLUGIN_DEVICEEXPLORER_EXPORT DeviceExplorerModel&
184 deviceExplorerFromObject(const QObject&);
185 SCORE_PLUGIN_DEVICEEXPLORER_EXPORT DeviceExplorerModel*
186 try_deviceExplorerFromObject(const QObject&);
187 SCORE_PLUGIN_DEVICEEXPLORER_EXPORT DeviceExplorerModel*
188 try_deviceExplorerFromContext(const score::DocumentContext& ctx);
189 SCORE_PLUGIN_DEVICEEXPLORER_EXPORT DeviceExplorerModel&
190 deviceExplorerFromContext(const score::DocumentContext& ctx);
191 
192 // Will try to find a node and fill the structure with it.
193 SCORE_PLUGIN_DEVICEEXPLORER_EXPORT Device::FullAddressAccessorSettings
194 makeFullAddressAccessorSettings(
195  const State::AddressAccessor& mess, const score::DocumentContext& ctx,
196  ossia::value min, ossia::value max, ossia::value cur);
197 }
Definition: NodeBasedItemModel.hpp:18
Definition: DeviceExplorerModel.hpp:67
Definition: DeviceExplorerView.hpp:18
Path in a tree of QAbstractItemModel objects.
Definition: TreePath.hpp:34
A small abstraction layer over the score::CommandStack.
Definition: CommandStackFacade.hpp:20
Manipulation of Devices from Qt.
Definition: AddressSettings.cpp:14
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: AddressSettings.hpp:49
Definition: DeviceSettings.hpp:20
Definition: AddressSettings.hpp:131
The SelectedNodes struct.
Definition: DeviceExplorerModel.hpp:53
std::vector< Device::Node * > parents
parents The topmost parents of the selected parameters
Definition: DeviceExplorerModel.hpp:57
std::vector< Device::Node * > messages
messages The selected messages
Definition: DeviceExplorerModel.hpp:62
Definition: Address.hpp:108
Definition: DocumentContext.hpp:18