comparison widgets/LayerTree.cpp @ 56:fedaf3ffe80a

* Add menu for re-adding existing layers * Fix layer tree window so that it at least approximates correct * Add bundled operations in command history, for use with things like multiple consecutive changes to a parameter value * Disambiguate plugins that happen to have identical descriptions * Add spectral centroid plugin (could use some parameters!) * Some other fixes
author Chris Cannam
date Fri, 17 Mar 2006 17:38:28 +0000
parents ad214997dddb
children 01ab51f72e84
comparison
equal deleted inserted replaced
55:128ebfeeebee 56:fedaf3ffe80a
11 #include "LayerTree.h" 11 #include "LayerTree.h"
12 #include "PaneStack.h" 12 #include "PaneStack.h"
13 13
14 #include "widgets/Pane.h" 14 #include "widgets/Pane.h"
15 #include "base/Layer.h" 15 #include "base/Layer.h"
16 #include "base/Model.h"
16 17
17 #include <iostream> 18 #include <iostream>
19
20
21 class ViewObjectAssoc : public QObject
22 {
23 public:
24 ViewObjectAssoc(QObject *parent, View *v, QObject *o) :
25 QObject(parent), view(v), object(o) {
26 ++extantCount;
27 }
28
29 virtual ~ViewObjectAssoc() {
30 std::cerr << "~ViewObjectAssoc (now " << --extantCount << " extant)"
31 << std::endl;
32 }
33
34 View *view;
35 QObject *object;
36
37 static int extantCount;
38 };
39
40 int ViewObjectAssoc::extantCount = 0;
18 41
19 42
20 LayerTreeModel::LayerTreeModel(PaneStack *stack, QObject *parent) : 43 LayerTreeModel::LayerTreeModel(PaneStack *stack, QObject *parent) :
21 QAbstractItemModel(parent), 44 QAbstractItemModel(parent),
22 m_stack(stack) 45 m_stack(stack)
28 } 51 }
29 52
30 QVariant 53 QVariant
31 LayerTreeModel::data(const QModelIndex &index, int role) const 54 LayerTreeModel::data(const QModelIndex &index, int role) const
32 { 55 {
33 std::cerr << "LayerTreeModel::data(" << &index << ", role " << role << ")" << std::endl;
34
35 if (!index.isValid()) return QVariant(); 56 if (!index.isValid()) return QVariant();
36 if (role != Qt::DisplayRole) return QVariant(); 57 if (role != Qt::DisplayRole) return QVariant();
58
59 std::cerr << "LayerTreeModel::data(" << &index << ", role " << role << ")" << std::endl;
37 60
38 QObject *obj = static_cast<QObject *>(index.internalPointer()); 61 QObject *obj = static_cast<QObject *>(index.internalPointer());
39 62
40 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj); 63 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
41 if (paneStack) { 64 if (paneStack) {
53 } 76 }
54 } 77 }
55 return QVariant(); 78 return QVariant();
56 } 79 }
57 80
58 Layer *layer = dynamic_cast<Layer *>(obj); 81 ViewObjectAssoc *assoc = dynamic_cast<ViewObjectAssoc *>(obj);
59 if (layer) { 82 if (assoc) {
60 std::cerr << "node is layer" << std::endl; 83 std::cerr << "node is assoc" << std::endl;
61 return QVariant(QString("%1").arg(layer->objectName())); 84 Layer *layer = dynamic_cast<Layer *>(assoc->object);
85 if (layer) {
86 std::cerr << "with layer" << std::endl;
87 return QVariant(layer->objectName());
88 }
89 Model *model = dynamic_cast<Model *>(assoc->object);
90 if (model) {
91 std::cerr << "with model" << std::endl;
92 return QVariant(model->objectName());
93 }
62 } 94 }
63 95
64 return QVariant(); 96 return QVariant();
65 } 97 }
66 98
67 /*
68 Qt::ItemFlags 99 Qt::ItemFlags
69 LayerTreeModel::flags(const QModelIndex &index) const 100 LayerTreeModel::flags(const QModelIndex &index) const
70 { 101 {
102 if (!index.isValid()) return Qt::ItemIsEnabled;
103 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
71 } 104 }
72 105
73 QVariant 106 QVariant
74 LayerTreeModel::headerData(const QModelIndex &index, 107 LayerTreeModel::headerData(int section,
75 Qt::Orientation orientation, 108 Qt::Orientation orientation,
76 int role) const 109 int role) const
77 { 110 {
78 } 111 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
79 */ 112 if (section == 0) return QVariant(tr("Layer"));
113 else if (section == 1) return QVariant(tr("Model"));
114 }
115
116 return QVariant();
117 }
80 118
81 QModelIndex 119 QModelIndex
82 LayerTreeModel::index(int row, int column, const QModelIndex &parent) const 120 LayerTreeModel::index(int row, int column, const QModelIndex &parent) const
83 { 121 {
84 std::cerr << "LayerTreeModel::index(" << row << ", " << column << ", " 122 std::cerr << "LayerTreeModel::index(" << row << ", " << column << ", "
85 << &parent << ")" << std::endl; 123 << &parent << ")" << std::endl;
86 124
87 if (!parent.isValid()) { 125 if (!parent.isValid()) {
88 // this is the pane stack 126 // this is the pane stack
89 std::cerr << "parent invalid, returning pane stack as root" << std::endl; 127 std::cerr << "parent invalid, returning pane stack as root" << std::endl;
128 if (column > 0) return QModelIndex();
90 return createIndex(row, column, m_stack); 129 return createIndex(row, column, m_stack);
91 } 130 }
92 131
93 QObject *obj = static_cast<QObject *>(parent.internalPointer()); 132 QObject *obj = static_cast<QObject *>(parent.internalPointer());
94 133
95 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj); 134 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
96 if (paneStack) { 135 if (paneStack) {
136 if (column > 0) return QModelIndex();
97 if (paneStack == m_stack && row < m_stack->getPaneCount()) { 137 if (paneStack == m_stack && row < m_stack->getPaneCount()) {
98 std::cerr << "parent is pane stack, returning a pane" << std::endl; 138 std::cerr << "parent is pane stack, returning a pane" << std::endl;
99 return createIndex(row, column, m_stack->getPane(row)); 139 return createIndex(row, column, m_stack->getPane(row));
100 } 140 }
101 std::cerr << "parent is wrong pane stack, returning nothing" << std::endl; 141 std::cerr << "parent is wrong pane stack, returning nothing" << std::endl;
102 return QModelIndex(); 142 return QModelIndex();
103 } 143 }
104 144
105 Pane *pane = dynamic_cast<Pane *>(obj); 145 Pane *pane = dynamic_cast<Pane *>(obj);
106 if (pane) { 146 if (pane) {
147 std::cerr << "parent is pane" << std::endl;
107 if (row < pane->getLayerCount()) { 148 if (row < pane->getLayerCount()) {
108 std::cerr << "parent is pane, returning layer" << std::endl; 149 Layer *layer = pane->getLayer(row);
109 return createIndex(row, column, pane->getLayer(row)); 150 if (column == 0) {
151 std::cerr << "parent is pane, returning layer" << std::endl;
152 ViewObjectAssoc *assoc = new ViewObjectAssoc
153 (const_cast<LayerTreeModel *>(this), pane, layer);
154 return createIndex(row, column, assoc);
155 } else {
156 std::cerr << "parent is pane, column != 0, returning model" << std::endl;
157 ViewObjectAssoc *assoc = new ViewObjectAssoc
158 (const_cast<LayerTreeModel *>(this), pane, layer->getModel());
159 return createIndex(row, column, assoc);
160 }
110 } 161 }
111 } 162 }
112 163
113 std::cerr << "unknown parent, returning nothing" << std::endl; 164 std::cerr << "unknown parent, returning nothing" << std::endl;
114 return QModelIndex(); 165 return QModelIndex();
131 if (pane) { 182 if (pane) {
132 std::cerr << "node is pane, returning pane stack as parent" << std::endl; 183 std::cerr << "node is pane, returning pane stack as parent" << std::endl;
133 return createIndex(0, 0, m_stack); 184 return createIndex(0, 0, m_stack);
134 } 185 }
135 186
136 Layer *layer = dynamic_cast<Layer *>(obj); 187 ViewObjectAssoc *assoc = dynamic_cast<ViewObjectAssoc *>(obj);
137 if (layer) { 188 if (assoc) {
138 //!!! const View *view = layer->getView(); 189 View *view = assoc->view;
139 const View *view = 0; 190 Pane *pane = dynamic_cast<Pane *>(view);
140 Pane *pane = const_cast<Pane *>(dynamic_cast<const Pane *>(view));
141 if (pane) { 191 if (pane) {
142 // need index of pane in pane stack 192 // need index of pane in pane stack
143 for (int i = 0; i < m_stack->getPaneCount(); ++i) { 193 for (int i = 0; i < m_stack->getPaneCount(); ++i) {
144 if (pane == m_stack->getPane(i)) { 194 if (pane == m_stack->getPane(i)) {
145 std::cerr << "node is layer, returning pane " << i << " as parent" << std::endl; 195 std::cerr << "node is assoc, returning pane " << i << " as parent" << std::endl;
146 return createIndex(i, 0, pane); 196 return createIndex(i, 0, pane);
147 } 197 }
148 } 198 }
149 } 199 }
150 std::cerr << "node is layer, but no parent found" << std::endl; 200 std::cerr << "node is assoc, but no parent found" << std::endl;
151 return QModelIndex(); 201 return QModelIndex();
152 } 202 }
153
154
155 203
156 std::cerr << "unknown node" << std::endl; 204 std::cerr << "unknown node" << std::endl;
157 return QModelIndex(); 205 return QModelIndex();
158 } 206 }
159 207
192 } 240 }
193 241
194 int 242 int
195 LayerTreeModel::columnCount(const QModelIndex &parent) const 243 LayerTreeModel::columnCount(const QModelIndex &parent) const
196 { 244 {
197 if (!parent.isValid()) return 1; 245 if (!parent.isValid()) {
246 std::cerr << "LayerTreeModel::columnCount: parent invalid, returning 2" << std::endl;
247 return 2;
248 }
198 249
199 QObject *obj = static_cast<QObject *>(parent.internalPointer()); 250 QObject *obj = static_cast<QObject *>(parent.internalPointer());
200 251
201 Pane *pane = dynamic_cast<Pane *>(obj); 252 Pane *pane = dynamic_cast<Pane *>(obj);
202 if (pane) { 253 if (pane) {
203 return 1; // 2; // layer and model 254 std::cerr << "LayerTreeModel::columnCount: pane, returning 2" << std::endl;
204 } 255 return 2; // layer and model
256 }
257
258 std::cerr << "LayerTreeModel::columnCount: returning 1" << std::endl;
205 259
206 return 1; 260 return 1;
207 } 261 }
208 262