Chris@43
|
1
|
Chris@58
|
2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@43
|
3
|
Chris@43
|
4 /*
|
Chris@43
|
5 A waveform viewer and audio annotation editor.
|
Chris@43
|
6 Chris Cannam, Queen Mary University of London, 2005-2006
|
Chris@43
|
7
|
Chris@43
|
8 This is experimental software. Not for distribution.
|
Chris@43
|
9 */
|
Chris@43
|
10
|
Chris@43
|
11 #include "LayerTree.h"
|
Chris@43
|
12 #include "PaneStack.h"
|
Chris@43
|
13
|
Chris@43
|
14 #include "widgets/Pane.h"
|
Chris@43
|
15 #include "base/Layer.h"
|
Chris@56
|
16 #include "base/Model.h"
|
Chris@43
|
17
|
Chris@43
|
18 #include <iostream>
|
Chris@43
|
19
|
Chris@43
|
20
|
Chris@56
|
21 class ViewObjectAssoc : public QObject
|
Chris@56
|
22 {
|
Chris@56
|
23 public:
|
Chris@56
|
24 ViewObjectAssoc(QObject *parent, View *v, QObject *o) :
|
Chris@56
|
25 QObject(parent), view(v), object(o) {
|
Chris@56
|
26 ++extantCount;
|
Chris@56
|
27 }
|
Chris@56
|
28
|
Chris@56
|
29 virtual ~ViewObjectAssoc() {
|
Chris@56
|
30 std::cerr << "~ViewObjectAssoc (now " << --extantCount << " extant)"
|
Chris@56
|
31 << std::endl;
|
Chris@56
|
32 }
|
Chris@56
|
33
|
Chris@56
|
34 View *view;
|
Chris@56
|
35 QObject *object;
|
Chris@56
|
36
|
Chris@56
|
37 static int extantCount;
|
Chris@56
|
38 };
|
Chris@56
|
39
|
Chris@56
|
40 int ViewObjectAssoc::extantCount = 0;
|
Chris@56
|
41
|
Chris@56
|
42
|
Chris@43
|
43 LayerTreeModel::LayerTreeModel(PaneStack *stack, QObject *parent) :
|
Chris@43
|
44 QAbstractItemModel(parent),
|
Chris@43
|
45 m_stack(stack)
|
Chris@43
|
46 {
|
Chris@43
|
47 }
|
Chris@43
|
48
|
Chris@43
|
49 LayerTreeModel::~LayerTreeModel()
|
Chris@43
|
50 {
|
Chris@43
|
51 }
|
Chris@43
|
52
|
Chris@43
|
53 QVariant
|
Chris@43
|
54 LayerTreeModel::data(const QModelIndex &index, int role) const
|
Chris@43
|
55 {
|
Chris@43
|
56 if (!index.isValid()) return QVariant();
|
Chris@43
|
57 if (role != Qt::DisplayRole) return QVariant();
|
Chris@43
|
58
|
Chris@56
|
59 std::cerr << "LayerTreeModel::data(" << &index << ", role " << role << ")" << std::endl;
|
Chris@56
|
60
|
Chris@43
|
61 QObject *obj = static_cast<QObject *>(index.internalPointer());
|
Chris@43
|
62
|
Chris@43
|
63 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
|
Chris@43
|
64 if (paneStack) {
|
Chris@43
|
65 std::cerr << "node is pane stack" << std::endl;
|
Chris@43
|
66 return QVariant("Pane stack");
|
Chris@43
|
67 }
|
Chris@43
|
68
|
Chris@43
|
69 Pane *pane = dynamic_cast<Pane *>(obj);
|
Chris@43
|
70 if (pane) {
|
Chris@43
|
71 // need index of pane in pane stack
|
Chris@43
|
72 for (int i = 0; i < m_stack->getPaneCount(); ++i) {
|
Chris@43
|
73 if (pane == m_stack->getPane(i)) {
|
Chris@43
|
74 std::cerr << "node is pane " << i << std::endl;
|
Chris@43
|
75 return QVariant(QString("Pane %1").arg(i + 1));
|
Chris@43
|
76 }
|
Chris@43
|
77 }
|
Chris@43
|
78 return QVariant();
|
Chris@43
|
79 }
|
Chris@43
|
80
|
Chris@56
|
81 ViewObjectAssoc *assoc = dynamic_cast<ViewObjectAssoc *>(obj);
|
Chris@56
|
82 if (assoc) {
|
Chris@56
|
83 std::cerr << "node is assoc" << std::endl;
|
Chris@56
|
84 Layer *layer = dynamic_cast<Layer *>(assoc->object);
|
Chris@56
|
85 if (layer) {
|
Chris@56
|
86 std::cerr << "with layer" << std::endl;
|
Chris@56
|
87 return QVariant(layer->objectName());
|
Chris@56
|
88 }
|
Chris@56
|
89 Model *model = dynamic_cast<Model *>(assoc->object);
|
Chris@56
|
90 if (model) {
|
Chris@56
|
91 std::cerr << "with model" << std::endl;
|
Chris@56
|
92 return QVariant(model->objectName());
|
Chris@56
|
93 }
|
Chris@43
|
94 }
|
Chris@43
|
95
|
Chris@43
|
96 return QVariant();
|
Chris@43
|
97 }
|
Chris@43
|
98
|
Chris@43
|
99 Qt::ItemFlags
|
Chris@43
|
100 LayerTreeModel::flags(const QModelIndex &index) const
|
Chris@43
|
101 {
|
Chris@56
|
102 if (!index.isValid()) return Qt::ItemIsEnabled;
|
Chris@56
|
103 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
Chris@43
|
104 }
|
Chris@43
|
105
|
Chris@43
|
106 QVariant
|
Chris@56
|
107 LayerTreeModel::headerData(int section,
|
Chris@43
|
108 Qt::Orientation orientation,
|
Chris@43
|
109 int role) const
|
Chris@43
|
110 {
|
Chris@56
|
111 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
Chris@56
|
112 if (section == 0) return QVariant(tr("Layer"));
|
Chris@56
|
113 else if (section == 1) return QVariant(tr("Model"));
|
Chris@56
|
114 }
|
Chris@56
|
115
|
Chris@56
|
116 return QVariant();
|
Chris@43
|
117 }
|
Chris@43
|
118
|
Chris@43
|
119 QModelIndex
|
Chris@43
|
120 LayerTreeModel::index(int row, int column, const QModelIndex &parent) const
|
Chris@43
|
121 {
|
Chris@43
|
122 std::cerr << "LayerTreeModel::index(" << row << ", " << column << ", "
|
Chris@43
|
123 << &parent << ")" << std::endl;
|
Chris@43
|
124
|
Chris@43
|
125 if (!parent.isValid()) {
|
Chris@43
|
126 // this is the pane stack
|
Chris@43
|
127 std::cerr << "parent invalid, returning pane stack as root" << std::endl;
|
Chris@56
|
128 if (column > 0) return QModelIndex();
|
Chris@43
|
129 return createIndex(row, column, m_stack);
|
Chris@43
|
130 }
|
Chris@43
|
131
|
Chris@43
|
132 QObject *obj = static_cast<QObject *>(parent.internalPointer());
|
Chris@43
|
133
|
Chris@43
|
134 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
|
Chris@43
|
135 if (paneStack) {
|
Chris@56
|
136 if (column > 0) return QModelIndex();
|
Chris@43
|
137 if (paneStack == m_stack && row < m_stack->getPaneCount()) {
|
Chris@43
|
138 std::cerr << "parent is pane stack, returning a pane" << std::endl;
|
Chris@43
|
139 return createIndex(row, column, m_stack->getPane(row));
|
Chris@43
|
140 }
|
Chris@43
|
141 std::cerr << "parent is wrong pane stack, returning nothing" << std::endl;
|
Chris@43
|
142 return QModelIndex();
|
Chris@43
|
143 }
|
Chris@43
|
144
|
Chris@43
|
145 Pane *pane = dynamic_cast<Pane *>(obj);
|
Chris@43
|
146 if (pane) {
|
Chris@56
|
147 std::cerr << "parent is pane" << std::endl;
|
Chris@43
|
148 if (row < pane->getLayerCount()) {
|
Chris@56
|
149 Layer *layer = pane->getLayer(row);
|
Chris@56
|
150 if (column == 0) {
|
Chris@56
|
151 std::cerr << "parent is pane, returning layer" << std::endl;
|
Chris@56
|
152 ViewObjectAssoc *assoc = new ViewObjectAssoc
|
Chris@56
|
153 (const_cast<LayerTreeModel *>(this), pane, layer);
|
Chris@56
|
154 return createIndex(row, column, assoc);
|
Chris@56
|
155 } else {
|
Chris@56
|
156 std::cerr << "parent is pane, column != 0, returning model" << std::endl;
|
Chris@56
|
157 ViewObjectAssoc *assoc = new ViewObjectAssoc
|
Chris@56
|
158 (const_cast<LayerTreeModel *>(this), pane, layer->getModel());
|
Chris@56
|
159 return createIndex(row, column, assoc);
|
Chris@56
|
160 }
|
Chris@43
|
161 }
|
Chris@43
|
162 }
|
Chris@43
|
163
|
Chris@43
|
164 std::cerr << "unknown parent, returning nothing" << std::endl;
|
Chris@43
|
165 return QModelIndex();
|
Chris@43
|
166 }
|
Chris@43
|
167
|
Chris@43
|
168 QModelIndex
|
Chris@43
|
169 LayerTreeModel::parent(const QModelIndex &index) const
|
Chris@43
|
170 {
|
Chris@43
|
171 std::cerr << "LayerTreeModel::parent(" << &index << ")" << std::endl;
|
Chris@43
|
172
|
Chris@43
|
173 QObject *obj = static_cast<QObject *>(index.internalPointer());
|
Chris@43
|
174
|
Chris@43
|
175 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
|
Chris@43
|
176 if (paneStack) {
|
Chris@43
|
177 std::cerr << "node is pane stack, returning no parent" << std::endl;
|
Chris@43
|
178 return QModelIndex();
|
Chris@43
|
179 }
|
Chris@43
|
180
|
Chris@43
|
181 Pane *pane = dynamic_cast<Pane *>(obj);
|
Chris@43
|
182 if (pane) {
|
Chris@43
|
183 std::cerr << "node is pane, returning pane stack as parent" << std::endl;
|
Chris@43
|
184 return createIndex(0, 0, m_stack);
|
Chris@43
|
185 }
|
Chris@43
|
186
|
Chris@56
|
187 ViewObjectAssoc *assoc = dynamic_cast<ViewObjectAssoc *>(obj);
|
Chris@56
|
188 if (assoc) {
|
Chris@56
|
189 View *view = assoc->view;
|
Chris@56
|
190 Pane *pane = dynamic_cast<Pane *>(view);
|
Chris@43
|
191 if (pane) {
|
Chris@43
|
192 // need index of pane in pane stack
|
Chris@43
|
193 for (int i = 0; i < m_stack->getPaneCount(); ++i) {
|
Chris@43
|
194 if (pane == m_stack->getPane(i)) {
|
Chris@56
|
195 std::cerr << "node is assoc, returning pane " << i << " as parent" << std::endl;
|
Chris@43
|
196 return createIndex(i, 0, pane);
|
Chris@43
|
197 }
|
Chris@43
|
198 }
|
Chris@43
|
199 }
|
Chris@56
|
200 std::cerr << "node is assoc, but no parent found" << std::endl;
|
Chris@43
|
201 return QModelIndex();
|
Chris@43
|
202 }
|
Chris@43
|
203
|
Chris@43
|
204 std::cerr << "unknown node" << std::endl;
|
Chris@43
|
205 return QModelIndex();
|
Chris@43
|
206 }
|
Chris@43
|
207
|
Chris@43
|
208 int
|
Chris@43
|
209 LayerTreeModel::rowCount(const QModelIndex &parent) const
|
Chris@43
|
210 {
|
Chris@43
|
211 std::cerr << "LayerTreeModel::rowCount(" << &parent << ")" << std::endl;
|
Chris@43
|
212
|
Chris@43
|
213 if (!parent.isValid()) {
|
Chris@43
|
214 std::cerr << "parent invalid, returning 1 for the pane stack" << std::endl;
|
Chris@43
|
215 return 1; // the pane stack
|
Chris@43
|
216 }
|
Chris@43
|
217
|
Chris@43
|
218 QObject *obj = static_cast<QObject *>(parent.internalPointer());
|
Chris@43
|
219
|
Chris@43
|
220 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
|
Chris@43
|
221 if (paneStack) {
|
Chris@43
|
222 if (paneStack == m_stack) {
|
Chris@43
|
223 std::cerr << "parent is pane stack, returning "
|
Chris@43
|
224 << m_stack->getPaneCount() << " panes" << std::endl;
|
Chris@43
|
225 return m_stack->getPaneCount();
|
Chris@43
|
226 } else {
|
Chris@43
|
227 return 0;
|
Chris@43
|
228 }
|
Chris@43
|
229 }
|
Chris@43
|
230
|
Chris@43
|
231 Pane *pane = dynamic_cast<Pane *>(obj);
|
Chris@43
|
232 if (pane) {
|
Chris@43
|
233 std::cerr << "parent is pane, returning "
|
Chris@43
|
234 << pane->getLayerCount() << " layers" << std::endl;
|
Chris@43
|
235 return pane->getLayerCount();
|
Chris@43
|
236 }
|
Chris@43
|
237
|
Chris@43
|
238 std::cerr << "parent unknown, returning 0" << std::endl;
|
Chris@43
|
239 return 0;
|
Chris@43
|
240 }
|
Chris@43
|
241
|
Chris@43
|
242 int
|
Chris@43
|
243 LayerTreeModel::columnCount(const QModelIndex &parent) const
|
Chris@43
|
244 {
|
Chris@56
|
245 if (!parent.isValid()) {
|
Chris@56
|
246 std::cerr << "LayerTreeModel::columnCount: parent invalid, returning 2" << std::endl;
|
Chris@56
|
247 return 2;
|
Chris@56
|
248 }
|
Chris@43
|
249
|
Chris@43
|
250 QObject *obj = static_cast<QObject *>(parent.internalPointer());
|
Chris@43
|
251
|
Chris@43
|
252 Pane *pane = dynamic_cast<Pane *>(obj);
|
Chris@43
|
253 if (pane) {
|
Chris@56
|
254 std::cerr << "LayerTreeModel::columnCount: pane, returning 2" << std::endl;
|
Chris@56
|
255 return 2; // layer and model
|
Chris@43
|
256 }
|
Chris@43
|
257
|
Chris@56
|
258 std::cerr << "LayerTreeModel::columnCount: returning 1" << std::endl;
|
Chris@56
|
259
|
Chris@43
|
260 return 1;
|
Chris@43
|
261 }
|
Chris@43
|
262
|