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@128: #include "view/PaneStack.h" Chris@43: Chris@128: #include "view/Pane.h" Chris@128: #include "layer/Layer.h" Chris@128: #include "data/model/Model.h" Chris@43: Chris@280: #include 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@271: connect(stack, SIGNAL(paneAdded()), this, SIGNAL(layoutChanged())); Chris@271: connect(stack, SIGNAL(paneDeleted()), this, SIGNAL(layoutChanged())); 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@56: Chris@43: QObject *obj = static_cast(index.internalPointer()); Chris@277: int row = index.row(), col = index.column(); Chris@277: Chris@277: Pane *pane = dynamic_cast(obj); Chris@277: if (!pane) { Chris@277: if (col == 0 && row < m_stack->getPaneCount()) { Chris@277: switch (role) { Chris@277: case Qt::DisplayRole: Chris@277: return QVariant(QString("Pane %1").arg(row + 1)); Chris@277: case Qt::DecorationRole: Chris@277: return QVariant(QIcon(QString(":/icons/pane.png"))); Chris@277: default: break; Chris@277: } Chris@277: } Chris@43: } Chris@43: Chris@277: if (pane && pane->getLayerCount() > row) { Chris@277: Layer *layer = pane->getLayer(row); Chris@277: if (layer) { Chris@277: if (col == 0) { Chris@277: switch (role) { Chris@277: case Qt::DisplayRole: Chris@277: return QVariant(layer->objectName()); Chris@277: case Qt::DecorationRole: Chris@277: return QVariant Chris@277: (QIcon(QString(":/icons/%1.png") Chris@277: .arg(layer->getPropertyContainerIconName()))); Chris@277: default: break; Chris@277: } Chris@277: } else if (col == 1) { Chris@277: Model *model = layer->getModel(); Chris@277: if (model && role == Qt::DisplayRole) { Chris@277: return QVariant(model->objectName()); Chris@277: } Chris@277: } Chris@277: } 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@277: // cell for a pane contains row, column, pane stack Chris@277: // -> its parent is the invalid cell Chris@277: Chris@277: // cell for a layer contains row, column, pane Chris@277: // -> its parent is row, column, pane stack (which identify the pane) Chris@43: Chris@43: if (!parent.isValid()) { Chris@277: if (row >= m_stack->getPaneCount() || 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@277: Chris@277: if (obj == m_stack) { Chris@277: Pane *pane = m_stack->getPane(parent.row()); Chris@277: if (!pane || parent.column() > 0) return QModelIndex(); Chris@277: return createIndex(row, column, pane); Chris@43: } Chris@43: Chris@43: return QModelIndex(); Chris@43: } Chris@43: Chris@43: QModelIndex Chris@43: LayerTreeModel::parent(const QModelIndex &index) const Chris@43: { Chris@43: QObject *obj = static_cast(index.internalPointer()); Chris@43: Chris@43: Pane *pane = dynamic_cast(obj); Chris@43: if (pane) { Chris@277: int index = m_stack->getPaneIndex(pane); Chris@277: if (index >= 0) return createIndex(index, 0, m_stack); Chris@43: } Chris@43: Chris@43: return QModelIndex(); Chris@43: } Chris@43: Chris@43: int Chris@43: LayerTreeModel::rowCount(const QModelIndex &parent) const Chris@43: { Chris@277: if (!parent.isValid()) return m_stack->getPaneCount(); Chris@43: Chris@43: QObject *obj = static_cast(parent.internalPointer()); Chris@43: Chris@277: if (obj == m_stack) { Chris@277: Pane *pane = m_stack->getPane(parent.row()); Chris@277: if (!pane || parent.column() > 0) return 0; Chris@277: return pane->getLayerCount(); Chris@43: } Chris@43: Chris@43: return 0; Chris@43: } Chris@43: Chris@43: int Chris@43: LayerTreeModel::columnCount(const QModelIndex &parent) const Chris@43: { Chris@277: if (!parent.isValid()) return 2; Chris@43: Chris@43: QObject *obj = static_cast(parent.internalPointer()); Chris@277: if (obj == m_stack) return 2; Chris@56: Chris@43: return 1; Chris@43: } Chris@43: