Chris@43: Chris@43: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@43: Chris@43: /* Chris@43: A waveform viewer and audio annotation editor. Chris@43: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@43: Chris@43: This is experimental software. Not for distribution. 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@43: Chris@43: #include Chris@43: Chris@43: 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: std::cerr << "LayerTreeModel::data(" << &index << ", role " << role << ")" << std::endl; Chris@43: Chris@43: if (!index.isValid()) return QVariant(); Chris@43: if (role != Qt::DisplayRole) return QVariant(); 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" << 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@43: Layer *layer = dynamic_cast(obj); Chris@43: if (layer) { Chris@43: std::cerr << "node is layer" << std::endl; Chris@43: return QVariant(QString("%1").arg(layer->objectName())); Chris@43: } Chris@43: Chris@43: return QVariant(); Chris@43: } Chris@43: Chris@43: /* Chris@43: Qt::ItemFlags Chris@43: LayerTreeModel::flags(const QModelIndex &index) const Chris@43: { Chris@43: } Chris@43: Chris@43: QVariant Chris@43: LayerTreeModel::headerData(const QModelIndex &index, Chris@43: Qt::Orientation orientation, Chris@43: int role) const Chris@43: { Chris@43: } 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@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@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@43: if (row < pane->getLayerCount()) { Chris@43: std::cerr << "parent is pane, returning layer" << std::endl; Chris@43: return createIndex(row, column, pane->getLayer(row)); 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@43: Layer *layer = dynamic_cast(obj); Chris@43: if (layer) { Chris@44: //!!! const View *view = layer->getView(); Chris@44: const View *view = 0; Chris@43: Pane *pane = const_cast(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@43: std::cerr << "node is layer, returning pane " << i << " as parent" << std::endl; Chris@43: return createIndex(i, 0, pane); Chris@43: } Chris@43: } Chris@43: } Chris@43: std::cerr << "node is layer, but no parent found" << std::endl; Chris@43: return QModelIndex(); Chris@43: } Chris@43: 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@43: if (!parent.isValid()) return 1; Chris@43: Chris@43: QObject *obj = static_cast(parent.internalPointer()); Chris@43: Chris@43: Pane *pane = dynamic_cast(obj); Chris@43: if (pane) { Chris@43: return 1; // 2; // layer and model Chris@43: } Chris@43: Chris@43: return 1; Chris@43: } Chris@43: