comparison widgets/LayerTree.cpp @ 43:78515b1e29eb

* Rejig project file a bit to do pkg-config detection &c and change some HAVE_* symbol names accordingly * Add selection move/resize/delete * First stubs for add layer / pane commands
author Chris Cannam
date Wed, 01 Mar 2006 18:13:01 +0000
parents
children ad214997dddb
comparison
equal deleted inserted replaced
42:1bdf285c4eac 43:78515b1e29eb
1
2 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
3
4 /*
5 A waveform viewer and audio annotation editor.
6 Chris Cannam, Queen Mary University of London, 2005-2006
7
8 This is experimental software. Not for distribution.
9 */
10
11 #include "LayerTree.h"
12 #include "PaneStack.h"
13
14 #include "widgets/Pane.h"
15 #include "base/Layer.h"
16
17 #include <iostream>
18
19
20 LayerTreeModel::LayerTreeModel(PaneStack *stack, QObject *parent) :
21 QAbstractItemModel(parent),
22 m_stack(stack)
23 {
24 }
25
26 LayerTreeModel::~LayerTreeModel()
27 {
28 }
29
30 QVariant
31 LayerTreeModel::data(const QModelIndex &index, int role) const
32 {
33 std::cerr << "LayerTreeModel::data(" << &index << ", role " << role << ")" << std::endl;
34
35 if (!index.isValid()) return QVariant();
36 if (role != Qt::DisplayRole) return QVariant();
37
38 QObject *obj = static_cast<QObject *>(index.internalPointer());
39
40 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
41 if (paneStack) {
42 std::cerr << "node is pane stack" << std::endl;
43 return QVariant("Pane stack");
44 }
45
46 Pane *pane = dynamic_cast<Pane *>(obj);
47 if (pane) {
48 // need index of pane in pane stack
49 for (int i = 0; i < m_stack->getPaneCount(); ++i) {
50 if (pane == m_stack->getPane(i)) {
51 std::cerr << "node is pane " << i << std::endl;
52 return QVariant(QString("Pane %1").arg(i + 1));
53 }
54 }
55 return QVariant();
56 }
57
58 Layer *layer = dynamic_cast<Layer *>(obj);
59 if (layer) {
60 std::cerr << "node is layer" << std::endl;
61 return QVariant(QString("%1").arg(layer->objectName()));
62 }
63
64 return QVariant();
65 }
66
67 /*
68 Qt::ItemFlags
69 LayerTreeModel::flags(const QModelIndex &index) const
70 {
71 }
72
73 QVariant
74 LayerTreeModel::headerData(const QModelIndex &index,
75 Qt::Orientation orientation,
76 int role) const
77 {
78 }
79 */
80
81 QModelIndex
82 LayerTreeModel::index(int row, int column, const QModelIndex &parent) const
83 {
84 std::cerr << "LayerTreeModel::index(" << row << ", " << column << ", "
85 << &parent << ")" << std::endl;
86
87 if (!parent.isValid()) {
88 // this is the pane stack
89 std::cerr << "parent invalid, returning pane stack as root" << std::endl;
90 return createIndex(row, column, m_stack);
91 }
92
93 QObject *obj = static_cast<QObject *>(parent.internalPointer());
94
95 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
96 if (paneStack) {
97 if (paneStack == m_stack && row < m_stack->getPaneCount()) {
98 std::cerr << "parent is pane stack, returning a pane" << std::endl;
99 return createIndex(row, column, m_stack->getPane(row));
100 }
101 std::cerr << "parent is wrong pane stack, returning nothing" << std::endl;
102 return QModelIndex();
103 }
104
105 Pane *pane = dynamic_cast<Pane *>(obj);
106 if (pane) {
107 if (row < pane->getLayerCount()) {
108 std::cerr << "parent is pane, returning layer" << std::endl;
109 return createIndex(row, column, pane->getLayer(row));
110 }
111 }
112
113 std::cerr << "unknown parent, returning nothing" << std::endl;
114 return QModelIndex();
115 }
116
117 QModelIndex
118 LayerTreeModel::parent(const QModelIndex &index) const
119 {
120 std::cerr << "LayerTreeModel::parent(" << &index << ")" << std::endl;
121
122 QObject *obj = static_cast<QObject *>(index.internalPointer());
123
124 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
125 if (paneStack) {
126 std::cerr << "node is pane stack, returning no parent" << std::endl;
127 return QModelIndex();
128 }
129
130 Pane *pane = dynamic_cast<Pane *>(obj);
131 if (pane) {
132 std::cerr << "node is pane, returning pane stack as parent" << std::endl;
133 return createIndex(0, 0, m_stack);
134 }
135
136 Layer *layer = dynamic_cast<Layer *>(obj);
137 if (layer) {
138 const View *view = layer->getView();
139 Pane *pane = const_cast<Pane *>(dynamic_cast<const Pane *>(view));
140 if (pane) {
141 // need index of pane in pane stack
142 for (int i = 0; i < m_stack->getPaneCount(); ++i) {
143 if (pane == m_stack->getPane(i)) {
144 std::cerr << "node is layer, returning pane " << i << " as parent" << std::endl;
145 return createIndex(i, 0, pane);
146 }
147 }
148 }
149 std::cerr << "node is layer, but no parent found" << std::endl;
150 return QModelIndex();
151 }
152
153
154
155 std::cerr << "unknown node" << std::endl;
156 return QModelIndex();
157 }
158
159 int
160 LayerTreeModel::rowCount(const QModelIndex &parent) const
161 {
162 std::cerr << "LayerTreeModel::rowCount(" << &parent << ")" << std::endl;
163
164 if (!parent.isValid()) {
165 std::cerr << "parent invalid, returning 1 for the pane stack" << std::endl;
166 return 1; // the pane stack
167 }
168
169 QObject *obj = static_cast<QObject *>(parent.internalPointer());
170
171 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
172 if (paneStack) {
173 if (paneStack == m_stack) {
174 std::cerr << "parent is pane stack, returning "
175 << m_stack->getPaneCount() << " panes" << std::endl;
176 return m_stack->getPaneCount();
177 } else {
178 return 0;
179 }
180 }
181
182 Pane *pane = dynamic_cast<Pane *>(obj);
183 if (pane) {
184 std::cerr << "parent is pane, returning "
185 << pane->getLayerCount() << " layers" << std::endl;
186 return pane->getLayerCount();
187 }
188
189 std::cerr << "parent unknown, returning 0" << std::endl;
190 return 0;
191 }
192
193 int
194 LayerTreeModel::columnCount(const QModelIndex &parent) const
195 {
196 if (!parent.isValid()) return 1;
197
198 QObject *obj = static_cast<QObject *>(parent.internalPointer());
199
200 Pane *pane = dynamic_cast<Pane *>(obj);
201 if (pane) {
202 return 1; // 2; // layer and model
203 }
204
205 return 1;
206 }
207