annotate widgets/LayerTree.cpp @ 59:705f05ab42e3

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