Chris@43: Chris@58: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@43: Chris@43: /* Chris@59: Sonic Visualiser Chris@59: An audio file viewer and annotation editor. Chris@59: Centre for Digital Music, Queen Mary, University of London. Chris@59: This file copyright 2006 Chris Cannam. Chris@43: Chris@59: This program is free software; you can redistribute it and/or Chris@59: modify it under the terms of the GNU General Public License as Chris@59: published by the Free Software Foundation; either version 2 of the Chris@59: License, or (at your option) any later version. See the file Chris@59: COPYING included with this distribution for more information. Chris@43: */ Chris@43: Chris@43: #include "LayerTree.h" Chris@43: #include "PaneStack.h" Chris@43: Chris@43: #include "widgets/Pane.h" Chris@43: #include "base/Layer.h" Chris@56: #include "base/Model.h" Chris@43: Chris@43: #include Chris@43: Chris@43: Chris@56: class ViewObjectAssoc : public QObject Chris@56: { Chris@56: public: Chris@56: ViewObjectAssoc(QObject *parent, View *v, QObject *o) : Chris@56: QObject(parent), view(v), object(o) { Chris@56: ++extantCount; Chris@56: } Chris@56: Chris@56: virtual ~ViewObjectAssoc() { Chris@56: std::cerr << "~ViewObjectAssoc (now " << --extantCount << " extant)" Chris@56: << std::endl; Chris@56: } Chris@56: Chris@56: View *view; Chris@56: QObject *object; Chris@56: Chris@56: static int extantCount; Chris@56: }; Chris@56: Chris@56: int ViewObjectAssoc::extantCount = 0; Chris@56: Chris@56: Chris@43: LayerTreeModel::LayerTreeModel(PaneStack *stack, QObject *parent) : Chris@43: QAbstractItemModel(parent), Chris@43: m_stack(stack) Chris@43: { Chris@43: } Chris@43: Chris@43: LayerTreeModel::~LayerTreeModel() Chris@43: { Chris@43: } Chris@43: Chris@43: QVariant Chris@43: LayerTreeModel::data(const QModelIndex &index, int role) const Chris@43: { Chris@43: if (!index.isValid()) return QVariant(); Chris@43: if (role != Qt::DisplayRole) return QVariant(); Chris@43: Chris@56: std::cerr << "LayerTreeModel::data(" << &index << ", role " << role << ")" << std::endl; Chris@56: Chris@43: QObject *obj = static_cast(index.internalPointer()); Chris@43: Chris@43: PaneStack *paneStack = dynamic_cast(obj); Chris@43: if (paneStack) { Chris@43: std::cerr << "node is pane stack" << std::endl; Chris@43: return QVariant("Pane stack"); Chris@43: } Chris@43: Chris@43: Pane *pane = dynamic_cast(obj); Chris@43: if (pane) { Chris@43: // need index of pane in pane stack Chris@43: for (int i = 0; i < m_stack->getPaneCount(); ++i) { Chris@43: if (pane == m_stack->getPane(i)) { Chris@43: std::cerr << "node is pane " << i << std::endl; Chris@43: return QVariant(QString("Pane %1").arg(i + 1)); Chris@43: } Chris@43: } Chris@43: return QVariant(); Chris@43: } Chris@43: Chris@56: ViewObjectAssoc *assoc = dynamic_cast(obj); Chris@56: if (assoc) { Chris@56: std::cerr << "node is assoc" << std::endl; Chris@56: Layer *layer = dynamic_cast(assoc->object); Chris@56: if (layer) { Chris@56: std::cerr << "with layer" << std::endl; Chris@56: return QVariant(layer->objectName()); Chris@56: } Chris@56: Model *model = dynamic_cast(assoc->object); Chris@56: if (model) { Chris@56: std::cerr << "with model" << std::endl; Chris@56: return QVariant(model->objectName()); Chris@56: } Chris@43: } Chris@43: Chris@43: return QVariant(); Chris@43: } Chris@43: Chris@43: Qt::ItemFlags Chris@43: LayerTreeModel::flags(const QModelIndex &index) const Chris@43: { Chris@56: if (!index.isValid()) return Qt::ItemIsEnabled; Chris@56: return Qt::ItemIsEnabled | Qt::ItemIsSelectable; Chris@43: } Chris@43: Chris@43: QVariant Chris@56: LayerTreeModel::headerData(int section, Chris@43: Qt::Orientation orientation, Chris@43: int role) const Chris@43: { Chris@56: if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { Chris@56: if (section == 0) return QVariant(tr("Layer")); Chris@56: else if (section == 1) return QVariant(tr("Model")); Chris@56: } Chris@56: Chris@56: return QVariant(); Chris@43: } Chris@43: Chris@43: QModelIndex Chris@43: LayerTreeModel::index(int row, int column, const QModelIndex &parent) const Chris@43: { Chris@43: std::cerr << "LayerTreeModel::index(" << row << ", " << column << ", " Chris@43: << &parent << ")" << std::endl; Chris@43: Chris@43: if (!parent.isValid()) { Chris@43: // this is the pane stack Chris@43: std::cerr << "parent invalid, returning pane stack as root" << std::endl; Chris@56: if (column > 0) return QModelIndex(); Chris@43: return createIndex(row, column, m_stack); Chris@43: } Chris@43: Chris@43: QObject *obj = static_cast(parent.internalPointer()); Chris@43: Chris@43: PaneStack *paneStack = dynamic_cast(obj); Chris@43: if (paneStack) { Chris@56: if (column > 0) return QModelIndex(); Chris@43: if (paneStack == m_stack && row < m_stack->getPaneCount()) { Chris@43: std::cerr << "parent is pane stack, returning a pane" << std::endl; Chris@43: return createIndex(row, column, m_stack->getPane(row)); Chris@43: } Chris@43: std::cerr << "parent is wrong pane stack, returning nothing" << std::endl; Chris@43: return QModelIndex(); Chris@43: } Chris@43: Chris@43: Pane *pane = dynamic_cast(obj); Chris@43: if (pane) { Chris@56: std::cerr << "parent is pane" << std::endl; Chris@43: if (row < pane->getLayerCount()) { Chris@56: Layer *layer = pane->getLayer(row); Chris@56: if (column == 0) { Chris@56: std::cerr << "parent is pane, returning layer" << std::endl; Chris@56: ViewObjectAssoc *assoc = new ViewObjectAssoc Chris@56: (const_cast(this), pane, layer); Chris@56: return createIndex(row, column, assoc); Chris@56: } else { Chris@56: std::cerr << "parent is pane, column != 0, returning model" << std::endl; Chris@56: ViewObjectAssoc *assoc = new ViewObjectAssoc Chris@56: (const_cast(this), pane, layer->getModel()); Chris@56: return createIndex(row, column, assoc); Chris@56: } Chris@43: } Chris@43: } Chris@43: Chris@43: std::cerr << "unknown parent, returning nothing" << std::endl; Chris@43: return QModelIndex(); Chris@43: } Chris@43: Chris@43: QModelIndex Chris@43: LayerTreeModel::parent(const QModelIndex &index) const Chris@43: { Chris@43: std::cerr << "LayerTreeModel::parent(" << &index << ")" << std::endl; Chris@43: Chris@43: QObject *obj = static_cast(index.internalPointer()); Chris@43: Chris@43: PaneStack *paneStack = dynamic_cast(obj); Chris@43: if (paneStack) { Chris@43: std::cerr << "node is pane stack, returning no parent" << std::endl; Chris@43: return QModelIndex(); Chris@43: } Chris@43: Chris@43: Pane *pane = dynamic_cast(obj); Chris@43: if (pane) { Chris@43: std::cerr << "node is pane, returning pane stack as parent" << std::endl; Chris@43: return createIndex(0, 0, m_stack); Chris@43: } Chris@43: Chris@56: ViewObjectAssoc *assoc = dynamic_cast(obj); Chris@56: if (assoc) { Chris@56: View *view = assoc->view; Chris@56: Pane *pane = dynamic_cast(view); Chris@43: if (pane) { Chris@43: // need index of pane in pane stack Chris@43: for (int i = 0; i < m_stack->getPaneCount(); ++i) { Chris@43: if (pane == m_stack->getPane(i)) { Chris@56: std::cerr << "node is assoc, returning pane " << i << " as parent" << std::endl; Chris@43: return createIndex(i, 0, pane); Chris@43: } Chris@43: } Chris@43: } Chris@56: std::cerr << "node is assoc, but no parent found" << std::endl; Chris@43: return QModelIndex(); Chris@43: } Chris@43: Chris@43: std::cerr << "unknown node" << std::endl; Chris@43: return QModelIndex(); Chris@43: } Chris@43: Chris@43: int Chris@43: LayerTreeModel::rowCount(const QModelIndex &parent) const Chris@43: { Chris@43: std::cerr << "LayerTreeModel::rowCount(" << &parent << ")" << std::endl; Chris@43: Chris@43: if (!parent.isValid()) { Chris@43: std::cerr << "parent invalid, returning 1 for the pane stack" << std::endl; Chris@43: return 1; // the pane stack Chris@43: } Chris@43: Chris@43: QObject *obj = static_cast(parent.internalPointer()); Chris@43: Chris@43: PaneStack *paneStack = dynamic_cast(obj); Chris@43: if (paneStack) { Chris@43: if (paneStack == m_stack) { Chris@43: std::cerr << "parent is pane stack, returning " Chris@43: << m_stack->getPaneCount() << " panes" << std::endl; Chris@43: return m_stack->getPaneCount(); Chris@43: } else { Chris@43: return 0; Chris@43: } Chris@43: } Chris@43: Chris@43: Pane *pane = dynamic_cast(obj); Chris@43: if (pane) { Chris@43: std::cerr << "parent is pane, returning " Chris@43: << pane->getLayerCount() << " layers" << std::endl; Chris@43: return pane->getLayerCount(); Chris@43: } Chris@43: Chris@43: std::cerr << "parent unknown, returning 0" << std::endl; Chris@43: return 0; Chris@43: } Chris@43: Chris@43: int Chris@43: LayerTreeModel::columnCount(const QModelIndex &parent) const Chris@43: { Chris@56: if (!parent.isValid()) { Chris@56: std::cerr << "LayerTreeModel::columnCount: parent invalid, returning 2" << std::endl; Chris@56: return 2; Chris@56: } Chris@43: Chris@43: QObject *obj = static_cast(parent.internalPointer()); Chris@43: Chris@43: Pane *pane = dynamic_cast(obj); Chris@43: if (pane) { Chris@56: std::cerr << "LayerTreeModel::columnCount: pane, returning 2" << std::endl; Chris@56: return 2; // layer and model Chris@43: } Chris@43: Chris@56: std::cerr << "LayerTreeModel::columnCount: returning 1" << std::endl; Chris@56: Chris@43: return 1; Chris@43: } Chris@43: