annotate widgets/LayerTree.cpp @ 271:1a49bd0d8375

* Change a number of keyboard shortcuts -- get rid of all the Alt+ shortcuts and introduce a few more Ctrl+ ones instead, as well as a number of plain single keypresses * Add Playback menu * Add time-ruler support for snap to feature, use it in ffwd/rewind (rewind still needs fixing) * restore layer hierarchy window prior to making it work correctly
author Chris Cannam
date Thu, 28 Jun 2007 14:50:58 +0000
parents 33929e0c3c6b
children 8acd30ed735c
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@128 18 #include "view/PaneStack.h"
Chris@43 19
Chris@128 20 #include "view/Pane.h"
Chris@128 21 #include "layer/Layer.h"
Chris@128 22 #include "data/model/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@271 30 ViewObjectAssoc(View *v, QObject *o) :
Chris@271 31 QObject(0), view(v), object(o) {
Chris@56 32 ++extantCount;
Chris@271 33 std::cerr << "ViewObjectAssoc (now " << extantCount << " extant)"
Chris@271 34 << std::endl;
Chris@56 35 }
Chris@56 36
Chris@56 37 virtual ~ViewObjectAssoc() {
Chris@56 38 std::cerr << "~ViewObjectAssoc (now " << --extantCount << " extant)"
Chris@56 39 << std::endl;
Chris@56 40 }
Chris@56 41
Chris@56 42 View *view;
Chris@56 43 QObject *object;
Chris@56 44
Chris@56 45 static int extantCount;
Chris@56 46 };
Chris@56 47
Chris@56 48 int ViewObjectAssoc::extantCount = 0;
Chris@56 49
Chris@56 50
Chris@43 51 LayerTreeModel::LayerTreeModel(PaneStack *stack, QObject *parent) :
Chris@43 52 QAbstractItemModel(parent),
Chris@43 53 m_stack(stack)
Chris@43 54 {
Chris@271 55 connect(stack, SIGNAL(paneAdded()), this, SIGNAL(layoutChanged()));
Chris@271 56 connect(stack, SIGNAL(paneDeleted()), this, SIGNAL(layoutChanged()));
Chris@43 57 }
Chris@43 58
Chris@43 59 LayerTreeModel::~LayerTreeModel()
Chris@43 60 {
Chris@43 61 }
Chris@43 62
Chris@43 63 QVariant
Chris@43 64 LayerTreeModel::data(const QModelIndex &index, int role) const
Chris@43 65 {
Chris@43 66 if (!index.isValid()) return QVariant();
Chris@43 67 if (role != Qt::DisplayRole) return QVariant();
Chris@43 68
Chris@56 69 std::cerr << "LayerTreeModel::data(" << &index << ", role " << role << ")" << std::endl;
Chris@56 70
Chris@43 71 QObject *obj = static_cast<QObject *>(index.internalPointer());
Chris@43 72
Chris@43 73 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
Chris@43 74 if (paneStack) {
Chris@43 75 std::cerr << "node is pane stack" << std::endl;
Chris@43 76 return QVariant("Pane stack");
Chris@43 77 }
Chris@43 78
Chris@43 79 Pane *pane = dynamic_cast<Pane *>(obj);
Chris@43 80 if (pane) {
Chris@43 81 // need index of pane in pane stack
Chris@43 82 for (int i = 0; i < m_stack->getPaneCount(); ++i) {
Chris@43 83 if (pane == m_stack->getPane(i)) {
Chris@43 84 std::cerr << "node is pane " << i << std::endl;
Chris@43 85 return QVariant(QString("Pane %1").arg(i + 1));
Chris@43 86 }
Chris@43 87 }
Chris@43 88 return QVariant();
Chris@43 89 }
Chris@43 90
Chris@56 91 ViewObjectAssoc *assoc = dynamic_cast<ViewObjectAssoc *>(obj);
Chris@56 92 if (assoc) {
Chris@56 93 std::cerr << "node is assoc" << std::endl;
Chris@56 94 Layer *layer = dynamic_cast<Layer *>(assoc->object);
Chris@56 95 if (layer) {
Chris@56 96 std::cerr << "with layer" << std::endl;
Chris@56 97 return QVariant(layer->objectName());
Chris@56 98 }
Chris@56 99 Model *model = dynamic_cast<Model *>(assoc->object);
Chris@56 100 if (model) {
Chris@56 101 std::cerr << "with model" << std::endl;
Chris@56 102 return QVariant(model->objectName());
Chris@56 103 }
Chris@43 104 }
Chris@43 105
Chris@43 106 return QVariant();
Chris@43 107 }
Chris@43 108
Chris@43 109 Qt::ItemFlags
Chris@43 110 LayerTreeModel::flags(const QModelIndex &index) const
Chris@43 111 {
Chris@56 112 if (!index.isValid()) return Qt::ItemIsEnabled;
Chris@56 113 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
Chris@43 114 }
Chris@43 115
Chris@43 116 QVariant
Chris@56 117 LayerTreeModel::headerData(int section,
Chris@43 118 Qt::Orientation orientation,
Chris@43 119 int role) const
Chris@43 120 {
Chris@56 121 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
Chris@56 122 if (section == 0) return QVariant(tr("Layer"));
Chris@56 123 else if (section == 1) return QVariant(tr("Model"));
Chris@56 124 }
Chris@56 125
Chris@56 126 return QVariant();
Chris@43 127 }
Chris@43 128
Chris@43 129 QModelIndex
Chris@43 130 LayerTreeModel::index(int row, int column, const QModelIndex &parent) const
Chris@43 131 {
Chris@43 132 std::cerr << "LayerTreeModel::index(" << row << ", " << column << ", "
Chris@43 133 << &parent << ")" << std::endl;
Chris@43 134
Chris@43 135 if (!parent.isValid()) {
Chris@43 136 // this is the pane stack
Chris@43 137 std::cerr << "parent invalid, returning pane stack as root" << std::endl;
Chris@56 138 if (column > 0) return QModelIndex();
Chris@43 139 return createIndex(row, column, m_stack);
Chris@43 140 }
Chris@43 141
Chris@43 142 QObject *obj = static_cast<QObject *>(parent.internalPointer());
Chris@43 143
Chris@43 144 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
Chris@43 145 if (paneStack) {
Chris@56 146 if (column > 0) return QModelIndex();
Chris@43 147 if (paneStack == m_stack && row < m_stack->getPaneCount()) {
Chris@43 148 std::cerr << "parent is pane stack, returning a pane" << std::endl;
Chris@43 149 return createIndex(row, column, m_stack->getPane(row));
Chris@43 150 }
Chris@43 151 std::cerr << "parent is wrong pane stack, returning nothing" << std::endl;
Chris@43 152 return QModelIndex();
Chris@43 153 }
Chris@43 154
Chris@43 155 Pane *pane = dynamic_cast<Pane *>(obj);
Chris@43 156 if (pane) {
Chris@56 157 std::cerr << "parent is pane" << std::endl;
Chris@43 158 if (row < pane->getLayerCount()) {
Chris@56 159 Layer *layer = pane->getLayer(row);
Chris@56 160 if (column == 0) {
Chris@56 161 std::cerr << "parent is pane, returning layer" << std::endl;
Chris@56 162 ViewObjectAssoc *assoc = new ViewObjectAssoc
Chris@271 163 // (const_cast<LayerTreeModel *>(this), pane, layer);
Chris@271 164 (pane, layer);
Chris@56 165 return createIndex(row, column, assoc);
Chris@56 166 } else {
Chris@56 167 std::cerr << "parent is pane, column != 0, returning model" << std::endl;
Chris@56 168 ViewObjectAssoc *assoc = new ViewObjectAssoc
Chris@271 169 // (const_cast<LayerTreeModel *>(this), pane, layer->getModel());
Chris@271 170 (pane, layer->getModel());
Chris@56 171 return createIndex(row, column, assoc);
Chris@56 172 }
Chris@43 173 }
Chris@43 174 }
Chris@43 175
Chris@43 176 std::cerr << "unknown parent, returning nothing" << std::endl;
Chris@43 177 return QModelIndex();
Chris@43 178 }
Chris@43 179
Chris@43 180 QModelIndex
Chris@43 181 LayerTreeModel::parent(const QModelIndex &index) const
Chris@43 182 {
Chris@43 183 std::cerr << "LayerTreeModel::parent(" << &index << ")" << std::endl;
Chris@43 184
Chris@43 185 QObject *obj = static_cast<QObject *>(index.internalPointer());
Chris@43 186
Chris@43 187 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
Chris@43 188 if (paneStack) {
Chris@43 189 std::cerr << "node is pane stack, returning no parent" << std::endl;
Chris@43 190 return QModelIndex();
Chris@43 191 }
Chris@43 192
Chris@43 193 Pane *pane = dynamic_cast<Pane *>(obj);
Chris@43 194 if (pane) {
Chris@43 195 std::cerr << "node is pane, returning pane stack as parent" << std::endl;
Chris@43 196 return createIndex(0, 0, m_stack);
Chris@43 197 }
Chris@43 198
Chris@56 199 ViewObjectAssoc *assoc = dynamic_cast<ViewObjectAssoc *>(obj);
Chris@56 200 if (assoc) {
Chris@56 201 View *view = assoc->view;
Chris@56 202 Pane *pane = dynamic_cast<Pane *>(view);
Chris@43 203 if (pane) {
Chris@43 204 // need index of pane in pane stack
Chris@43 205 for (int i = 0; i < m_stack->getPaneCount(); ++i) {
Chris@43 206 if (pane == m_stack->getPane(i)) {
Chris@56 207 std::cerr << "node is assoc, returning pane " << i << " as parent" << std::endl;
Chris@43 208 return createIndex(i, 0, pane);
Chris@43 209 }
Chris@43 210 }
Chris@43 211 }
Chris@56 212 std::cerr << "node is assoc, but no parent found" << std::endl;
Chris@43 213 return QModelIndex();
Chris@43 214 }
Chris@43 215
Chris@43 216 std::cerr << "unknown node" << std::endl;
Chris@43 217 return QModelIndex();
Chris@43 218 }
Chris@43 219
Chris@43 220 int
Chris@43 221 LayerTreeModel::rowCount(const QModelIndex &parent) const
Chris@43 222 {
Chris@43 223 std::cerr << "LayerTreeModel::rowCount(" << &parent << ")" << std::endl;
Chris@43 224
Chris@43 225 if (!parent.isValid()) {
Chris@43 226 std::cerr << "parent invalid, returning 1 for the pane stack" << std::endl;
Chris@43 227 return 1; // the pane stack
Chris@43 228 }
Chris@43 229
Chris@43 230 QObject *obj = static_cast<QObject *>(parent.internalPointer());
Chris@43 231
Chris@43 232 PaneStack *paneStack = dynamic_cast<PaneStack *>(obj);
Chris@43 233 if (paneStack) {
Chris@43 234 if (paneStack == m_stack) {
Chris@43 235 std::cerr << "parent is pane stack, returning "
Chris@43 236 << m_stack->getPaneCount() << " panes" << std::endl;
Chris@43 237 return m_stack->getPaneCount();
Chris@43 238 } else {
Chris@43 239 return 0;
Chris@43 240 }
Chris@43 241 }
Chris@43 242
Chris@43 243 Pane *pane = dynamic_cast<Pane *>(obj);
Chris@43 244 if (pane) {
Chris@43 245 std::cerr << "parent is pane, returning "
Chris@43 246 << pane->getLayerCount() << " layers" << std::endl;
Chris@43 247 return pane->getLayerCount();
Chris@43 248 }
Chris@43 249
Chris@43 250 std::cerr << "parent unknown, returning 0" << std::endl;
Chris@43 251 return 0;
Chris@43 252 }
Chris@43 253
Chris@43 254 int
Chris@43 255 LayerTreeModel::columnCount(const QModelIndex &parent) const
Chris@43 256 {
Chris@56 257 if (!parent.isValid()) {
Chris@56 258 std::cerr << "LayerTreeModel::columnCount: parent invalid, returning 2" << std::endl;
Chris@56 259 return 2;
Chris@56 260 }
Chris@43 261
Chris@43 262 QObject *obj = static_cast<QObject *>(parent.internalPointer());
Chris@43 263
Chris@43 264 Pane *pane = dynamic_cast<Pane *>(obj);
Chris@43 265 if (pane) {
Chris@56 266 std::cerr << "LayerTreeModel::columnCount: pane, returning 2" << std::endl;
Chris@56 267 return 2; // layer and model
Chris@43 268 }
Chris@43 269
Chris@56 270 std::cerr << "LayerTreeModel::columnCount: returning 1" << std::endl;
Chris@56 271
Chris@43 272 return 1;
Chris@43 273 }
Chris@43 274