comparison widgets/LayerTree.cpp @ 277:8acd30ed735c

* Fix up and simplify the LayerTreeModel, removing a horrible memory leak * Move phase-unwrapped frequency estimation from SpectrogramLayer to FFTDataServer * Make the spectrum show peak phase-unwrapped frequencies as well (still needs work) * Start adding piano keyboard horizontal scale to spectrum * Debug output for id3 tags
author Chris Cannam
date Tue, 03 Jul 2007 12:46:18 +0000
parents 1a49bd0d8375
children 3c402c6052f6
comparison
equal deleted inserted replaced
276:21c7152ddba8 277:8acd30ed735c
22 #include "data/model/Model.h" 22 #include "data/model/Model.h"
23 23
24 #include <iostream> 24 #include <iostream>
25 25
26 26
27 class ViewObjectAssoc : public QObject
28 {
29 public:
30 ViewObjectAssoc(View *v, QObject *o) :
31 QObject(0), view(v), object(o) {
32 ++extantCount;
33 std::cerr << "ViewObjectAssoc (now " << extantCount << " extant)"
34 << std::endl;
35 }
36
37 virtual ~ViewObjectAssoc() {
38 std::cerr << "~ViewObjectAssoc (now " << --extantCount << " extant)"
39 << std::endl;
40 }
41
42 View *view;
43 QObject *object;
44
45 static int extantCount;
46 };
47
48 int ViewObjectAssoc::extantCount = 0;
49
50
51 LayerTreeModel::LayerTreeModel(PaneStack *stack, QObject *parent) : 27 LayerTreeModel::LayerTreeModel(PaneStack *stack, QObject *parent) :
52 QAbstractItemModel(parent), 28 QAbstractItemModel(parent),
53 m_stack(stack) 29 m_stack(stack)
54 { 30 {
55 connect(stack, SIGNAL(paneAdded()), this, SIGNAL(layoutChanged())); 31 connect(stack, SIGNAL(paneAdded()), this, SIGNAL(layoutChanged()));
62 38
63 QVariant 39 QVariant
64 LayerTreeModel::data(const QModelIndex &index, int role) const 40 LayerTreeModel::data(const QModelIndex &index, int role) const
65 { 41 {
66 if (!index.isValid()) return QVariant(); 42 if (!index.isValid()) return QVariant();
67 if (role != Qt::DisplayRole) return QVariant();
68
69 std::cerr << "LayerTreeModel::data(" << &index << ", role " << role << ")" << std::endl;
70 43
71 QObject *obj = static_cast<QObject *>(index.internalPointer()); 44 QObject *obj = static_cast<QObject *>(index.internalPointer());
72 45 int row = index.row(), col = index.column();
73 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj); 46
74 if (paneStack) { 47 Pane *pane = dynamic_cast<Pane *>(obj);
75 std::cerr << "node is pane stack" << std::endl; 48 if (!pane) {
76 return QVariant("Pane stack"); 49 if (col == 0 && row < m_stack->getPaneCount()) {
50 switch (role) {
51 case Qt::DisplayRole:
52 return QVariant(QString("Pane %1").arg(row + 1));
53 case Qt::DecorationRole:
54 return QVariant(QIcon(QString(":/icons/pane.png")));
55 default: break;
56 }
57 }
77 } 58 }
78 59
79 Pane *pane = dynamic_cast<Pane *>(obj); 60 if (pane && pane->getLayerCount() > row) {
80 if (pane) { 61 Layer *layer = pane->getLayer(row);
81 // need index of pane in pane stack 62 if (layer) {
82 for (int i = 0; i < m_stack->getPaneCount(); ++i) { 63 if (col == 0) {
83 if (pane == m_stack->getPane(i)) { 64 switch (role) {
84 std::cerr << "node is pane " << i << std::endl; 65 case Qt::DisplayRole:
85 return QVariant(QString("Pane %1").arg(i + 1)); 66 return QVariant(layer->objectName());
86 } 67 case Qt::DecorationRole:
87 } 68 return QVariant
88 return QVariant(); 69 (QIcon(QString(":/icons/%1.png")
89 } 70 .arg(layer->getPropertyContainerIconName())));
90 71 default: break;
91 ViewObjectAssoc *assoc = dynamic_cast<ViewObjectAssoc *>(obj); 72 }
92 if (assoc) { 73 } else if (col == 1) {
93 std::cerr << "node is assoc" << std::endl; 74 Model *model = layer->getModel();
94 Layer *layer = dynamic_cast<Layer *>(assoc->object); 75 if (model && role == Qt::DisplayRole) {
95 if (layer) { 76 return QVariant(model->objectName());
96 std::cerr << "with layer" << std::endl; 77 }
97 return QVariant(layer->objectName()); 78 }
98 } 79 }
99 Model *model = dynamic_cast<Model *>(assoc->object);
100 if (model) {
101 std::cerr << "with model" << std::endl;
102 return QVariant(model->objectName());
103 }
104 } 80 }
105 81
106 return QVariant(); 82 return QVariant();
107 } 83 }
108 84
127 } 103 }
128 104
129 QModelIndex 105 QModelIndex
130 LayerTreeModel::index(int row, int column, const QModelIndex &parent) const 106 LayerTreeModel::index(int row, int column, const QModelIndex &parent) const
131 { 107 {
132 std::cerr << "LayerTreeModel::index(" << row << ", " << column << ", " 108 // cell for a pane contains row, column, pane stack
133 << &parent << ")" << std::endl; 109 // -> its parent is the invalid cell
110
111 // cell for a layer contains row, column, pane
112 // -> its parent is row, column, pane stack (which identify the pane)
134 113
135 if (!parent.isValid()) { 114 if (!parent.isValid()) {
136 // this is the pane stack 115 if (row >= m_stack->getPaneCount() || column > 0) return QModelIndex();
137 std::cerr << "parent invalid, returning pane stack as root" << std::endl;
138 if (column > 0) return QModelIndex();
139 return createIndex(row, column, m_stack); 116 return createIndex(row, column, m_stack);
140 } 117 }
141 118
142 QObject *obj = static_cast<QObject *>(parent.internalPointer()); 119 QObject *obj = static_cast<QObject *>(parent.internalPointer());
143 120
144 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj); 121 if (obj == m_stack) {
145 if (paneStack) { 122 Pane *pane = m_stack->getPane(parent.row());
146 if (column > 0) return QModelIndex(); 123 if (!pane || parent.column() > 0) return QModelIndex();
147 if (paneStack == m_stack && row < m_stack->getPaneCount()) { 124 return createIndex(row, column, pane);
148 std::cerr << "parent is pane stack, returning a pane" << std::endl;
149 return createIndex(row, column, m_stack->getPane(row));
150 }
151 std::cerr << "parent is wrong pane stack, returning nothing" << std::endl;
152 return QModelIndex();
153 } 125 }
154 126
155 Pane *pane = dynamic_cast<Pane *>(obj);
156 if (pane) {
157 std::cerr << "parent is pane" << std::endl;
158 if (row < pane->getLayerCount()) {
159 Layer *layer = pane->getLayer(row);
160 if (column == 0) {
161 std::cerr << "parent is pane, returning layer" << std::endl;
162 ViewObjectAssoc *assoc = new ViewObjectAssoc
163 // (const_cast<LayerTreeModel *>(this), pane, layer);
164 (pane, layer);
165 return createIndex(row, column, assoc);
166 } else {
167 std::cerr << "parent is pane, column != 0, returning model" << std::endl;
168 ViewObjectAssoc *assoc = new ViewObjectAssoc
169 // (const_cast<LayerTreeModel *>(this), pane, layer->getModel());
170 (pane, layer->getModel());
171 return createIndex(row, column, assoc);
172 }
173 }
174 }
175
176 std::cerr << "unknown parent, returning nothing" << std::endl;
177 return QModelIndex(); 127 return QModelIndex();
178 } 128 }
179 129
180 QModelIndex 130 QModelIndex
181 LayerTreeModel::parent(const QModelIndex &index) const 131 LayerTreeModel::parent(const QModelIndex &index) const
182 { 132 {
183 std::cerr << "LayerTreeModel::parent(" << &index << ")" << std::endl;
184
185 QObject *obj = static_cast<QObject *>(index.internalPointer()); 133 QObject *obj = static_cast<QObject *>(index.internalPointer());
186
187 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
188 if (paneStack) {
189 std::cerr << "node is pane stack, returning no parent" << std::endl;
190 return QModelIndex();
191 }
192 134
193 Pane *pane = dynamic_cast<Pane *>(obj); 135 Pane *pane = dynamic_cast<Pane *>(obj);
194 if (pane) { 136 if (pane) {
195 std::cerr << "node is pane, returning pane stack as parent" << std::endl; 137 int index = m_stack->getPaneIndex(pane);
196 return createIndex(0, 0, m_stack); 138 if (index >= 0) return createIndex(index, 0, m_stack);
197 } 139 }
198 140
199 ViewObjectAssoc *assoc = dynamic_cast<ViewObjectAssoc *>(obj);
200 if (assoc) {
201 View *view = assoc->view;
202 Pane *pane = dynamic_cast<Pane *>(view);
203 if (pane) {
204 // need index of pane in pane stack
205 for (int i = 0; i < m_stack->getPaneCount(); ++i) {
206 if (pane == m_stack->getPane(i)) {
207 std::cerr << "node is assoc, returning pane " << i << " as parent" << std::endl;
208 return createIndex(i, 0, pane);
209 }
210 }
211 }
212 std::cerr << "node is assoc, but no parent found" << std::endl;
213 return QModelIndex();
214 }
215
216 std::cerr << "unknown node" << std::endl;
217 return QModelIndex(); 141 return QModelIndex();
218 } 142 }
219 143
220 int 144 int
221 LayerTreeModel::rowCount(const QModelIndex &parent) const 145 LayerTreeModel::rowCount(const QModelIndex &parent) const
222 { 146 {
223 std::cerr << "LayerTreeModel::rowCount(" << &parent << ")" << std::endl; 147 if (!parent.isValid()) return m_stack->getPaneCount();
224
225 if (!parent.isValid()) {
226 std::cerr << "parent invalid, returning 1 for the pane stack" << std::endl;
227 return 1; // the pane stack
228 }
229 148
230 QObject *obj = static_cast<QObject *>(parent.internalPointer()); 149 QObject *obj = static_cast<QObject *>(parent.internalPointer());
231 150
232 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj); 151 if (obj == m_stack) {
233 if (paneStack) { 152 Pane *pane = m_stack->getPane(parent.row());
234 if (paneStack == m_stack) { 153 if (!pane || parent.column() > 0) return 0;
235 std::cerr << "parent is pane stack, returning " 154 return pane->getLayerCount();
236 << m_stack->getPaneCount() << " panes" << std::endl;
237 return m_stack->getPaneCount();
238 } else {
239 return 0;
240 }
241 }
242
243 Pane *pane = dynamic_cast<Pane *>(obj);
244 if (pane) {
245 std::cerr << "parent is pane, returning "
246 << pane->getLayerCount() << " layers" << std::endl;
247 return pane->getLayerCount();
248 } 155 }
249 156
250 std::cerr << "parent unknown, returning 0" << std::endl;
251 return 0; 157 return 0;
252 } 158 }
253 159
254 int 160 int
255 LayerTreeModel::columnCount(const QModelIndex &parent) const 161 LayerTreeModel::columnCount(const QModelIndex &parent) const
256 { 162 {
257 if (!parent.isValid()) { 163 if (!parent.isValid()) return 2;
258 std::cerr << "LayerTreeModel::columnCount: parent invalid, returning 2" << std::endl;
259 return 2;
260 }
261 164
262 QObject *obj = static_cast<QObject *>(parent.internalPointer()); 165 QObject *obj = static_cast<QObject *>(parent.internalPointer());
263 166 if (obj == m_stack) return 2;
264 Pane *pane = dynamic_cast<Pane *>(obj);
265 if (pane) {
266 std::cerr << "LayerTreeModel::columnCount: pane, returning 2" << std::endl;
267 return 2; // layer and model
268 }
269
270 std::cerr << "LayerTreeModel::columnCount: returning 1" << std::endl;
271 167
272 return 1; 168 return 1;
273 } 169 }
274 170