NodeBasedItemModel.hpp
1 #pragma once
2 #include <Device/Address/AddressSettings.hpp>
3 #include <Device/Node/DeviceNode.hpp>
4 #include <Device/Protocol/DeviceSettings.hpp>
5 
6 #include <score/model/tree/InvisibleRootNode.hpp>
7 #include <score/model/tree/TreeNode.hpp>
8 #include <score/model/tree/TreeNodeItemModel.hpp>
9 
10 #include <score_lib_device_export.h>
11 
12 #include <vector>
13 
14 namespace Device
15 {
16 class SCORE_LIB_DEVICE_EXPORT NodeBasedItemModel
17  : public TreeNodeBasedItemModel<Device::Node>
18 {
19 public:
21  virtual ~NodeBasedItemModel();
22 
23  QModelIndex modelIndexFromNode(node_type& n, int column) const
24  {
25  if(n.is<InvisibleRootNode>())
26  {
27  return QModelIndex();
28  }
29  else if(n.is<Device::DeviceSettings>())
30  {
31  SCORE_ASSERT(n.parent());
32  return createIndex(n.parent()->indexOfChild(&n), 0, &n);
33  }
34  else
35  {
36  node_type* parent = n.parent();
37  SCORE_ASSERT(parent);
38  SCORE_ASSERT(parent != &rootNode());
39 
40  return createIndex(parent->indexOfChild(&n), column, &n);
41  }
42  }
43 
44  void insertNode(node_type& parentNode, const node_type& other, int row)
45  {
46  QModelIndex parentIndex = modelIndexFromNode(parentNode, 0);
47 
48  beginInsertRows(parentIndex, row, row);
49 
50  auto it = parentNode.begin();
51  std::advance(it, row);
52  parentNode.emplace(it, other, &parentNode);
53 
54  endInsertRows();
55  }
56 
57  auto removeNode(node_type::const_iterator node)
58  {
59  SCORE_ASSERT(!node->is<InvisibleRootNode>());
60 
61  if(node->is<Device::AddressSettings>())
62  {
63  node_type* parent = node->parent();
64  SCORE_ASSERT(parent);
65  SCORE_ASSERT(parent != &rootNode());
66  node_type* grandparent = parent->parent();
67  SCORE_ASSERT(grandparent);
68  int rowParent = grandparent->indexOfChild(parent);
69  QModelIndex parentIndex = createIndex(rowParent, 0, parent);
70 
71  int row = parent->indexOfChild(&*node);
72 
73  beginRemoveRows(parentIndex, row, row);
74  auto it = parent->erase(node);
75  endRemoveRows();
76  return it;
77  }
78  else
79  {
80  SCORE_ASSERT(node->is<Device::DeviceSettings>());
81  int row = rootNode().indexOfChild(&*node);
82 
83  beginRemoveRows(QModelIndex(), row, row);
84  auto it = rootNode().erase(node);
85  endRemoveRows();
86  return it;
87  }
88  }
89 };
90 
91 SCORE_LIB_DEVICE_EXPORT Device::FullAddressAccessorSettings
92 makeFullAddressAccessorSettings(
93  const State::AddressAccessor& mess, const Device::NodeBasedItemModel& ctx,
94  ossia::value min, ossia::value max, ossia::value val);
95 SCORE_LIB_DEVICE_EXPORT Device::FullAddressAccessorSettings
96 makeFullAddressAccessorSettings(const Device::Node& mess);
97 }
Definition: NodeBasedItemModel.hpp:18
Definition: TreeNodeItemModel.hpp:38
Manipulation of Devices from Qt.
Definition: AddressSettings.cpp:14
Definition: AddressSettings.hpp:49
Definition: DeviceSettings.hpp:20
Definition: AddressSettings.hpp:131
The InvisibleRootNodeTag struct.
Definition: InvisibleRootNode.hpp:11
Definition: Address.hpp:108