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@298: #include "base/PlayParameters.h" 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@298: Chris@298: for (int i = 0; i < stack->getPaneCount(); ++i) { Chris@298: Pane *pane = stack->getPane(i); Chris@298: if (!pane) continue; Chris@298: connect(pane, SIGNAL(propertyContainerAdded(PropertyContainer *)), Chris@298: this, SLOT(propertyContainerAdded(PropertyContainer *))); Chris@298: connect(pane, SIGNAL(propertyContainerRemoved(PropertyContainer *)), Chris@298: this, SLOT(propertyContainerRemoved(PropertyContainer *))); Chris@298: connect(pane, SIGNAL(propertyContainerSelected(PropertyContainer *)), Chris@298: this, SLOT(propertyContainerSelected(PropertyContainer *))); Chris@298: connect(pane, SIGNAL(propertyContainerPropertyChanged(PropertyContainer *)), Chris@298: this, SLOT(propertyContainerPropertyChanged(PropertyContainer *))); Chris@298: connect(pane, SIGNAL(propertyContainerNameChanged(PropertyContainer *)), Chris@298: this, SLOT(propertyContainerPropertyChanged(PropertyContainer *))); Chris@298: for (int j = 0; j < pane->getLayerCount(); ++j) { Chris@298: Layer *layer = pane->getLayer(j); Chris@298: if (!layer) continue; Chris@298: PlayParameters *params = layer->getPlayParameters(); Chris@298: if (!params) continue; Chris@298: connect(params, SIGNAL(playAudibleChanged(bool)), Chris@298: this, SLOT(playParametersAudibilityChanged(bool))); Chris@298: } Chris@298: } Chris@43: } Chris@43: Chris@43: LayerTreeModel::~LayerTreeModel() Chris@43: { Chris@43: } Chris@43: Chris@298: void Chris@298: LayerTreeModel::propertyContainerAdded(PropertyContainer *) Chris@298: { Chris@298: emit layoutChanged(); Chris@298: } Chris@298: Chris@298: void Chris@298: LayerTreeModel::propertyContainerRemoved(PropertyContainer *) Chris@298: { Chris@298: emit layoutChanged(); Chris@298: } Chris@298: Chris@298: void Chris@298: LayerTreeModel::propertyContainerSelected(PropertyContainer *) Chris@298: { Chris@298: emit layoutChanged(); Chris@298: } Chris@298: Chris@298: void Chris@298: LayerTreeModel::propertyContainerPropertyChanged(PropertyContainer *pc) Chris@298: { Chris@298: for (int i = 0; i < m_stack->getPaneCount(); ++i) { Chris@298: Pane *pane = m_stack->getPane(i); Chris@298: if (!pane) continue; Chris@298: for (int j = 0; j < pane->getLayerCount(); ++j) { Chris@298: if (pane->getLayer(j) == pc) { Chris@298: emit dataChanged(createIndex(pane->getLayerCount() - j - 1, Chris@298: 0, pane), Chris@298: createIndex(pane->getLayerCount() - j - 1, Chris@298: 3, pane)); Chris@298: } Chris@298: } Chris@298: } Chris@298: } Chris@298: Chris@298: void Chris@298: LayerTreeModel::playParametersAudibilityChanged(bool a) Chris@298: { Chris@298: PlayParameters *params = dynamic_cast(sender()); Chris@298: if (!params) return; Chris@298: Chris@298: std::cerr << "LayerTreeModel::playParametersAudibilityChanged(" Chris@298: << params << "," << a << ")" << std::endl; Chris@298: Chris@298: for (int i = 0; i < m_stack->getPaneCount(); ++i) { Chris@298: Pane *pane = m_stack->getPane(i); Chris@298: if (!pane) continue; Chris@298: for (int j = 0; j < pane->getLayerCount(); ++j) { Chris@298: Layer *layer = pane->getLayer(j); Chris@298: if (!layer) continue; Chris@298: if (layer->getPlayParameters() == params) { Chris@298: std::cerr << "LayerTreeModel::playParametersAudibilityChanged(" Chris@298: << params << "," << a << "): row " << pane->getLayerCount() - j - 1 << ", col " << 2 << std::endl; Chris@298: Chris@298: emit dataChanged(createIndex(pane->getLayerCount() - j - 1, Chris@298: 2, pane), Chris@298: createIndex(pane->getLayerCount() - j - 1, Chris@298: 2, pane)); Chris@298: } Chris@298: } Chris@298: } Chris@298: } Chris@298: 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@298: Layer *layer = pane->getLayer(pane->getLayerCount() - row - 1); 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@298: if (role == Qt::CheckStateRole) { Chris@298: return QVariant(layer->isLayerDormant(pane) ? Chris@298: Qt::Unchecked : Qt::Checked); Chris@298: } else if (role == Qt::TextAlignmentRole) { Chris@298: return QVariant(Qt::AlignHCenter); Chris@298: } Chris@298: } else if (col == 2) { Chris@298: if (role == Qt::CheckStateRole) { Chris@298: PlayParameters *params = layer->getPlayParameters(); Chris@298: if (params) return QVariant(params->isPlayMuted() ? Chris@298: Qt::Unchecked : Qt::Checked); Chris@298: else return QVariant(); Chris@298: } else if (role == Qt::TextAlignmentRole) { Chris@298: return QVariant(Qt::AlignHCenter); Chris@298: } Chris@298: } else if (col == 3) { 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@298: bool Chris@298: LayerTreeModel::setData(const QModelIndex &index, const QVariant &value, int role) Chris@298: { Chris@298: if (!index.isValid()) return false; Chris@298: Chris@298: QObject *obj = static_cast(index.internalPointer()); Chris@298: int row = index.row(), col = index.column(); Chris@298: Chris@298: Pane *pane = dynamic_cast(obj); Chris@298: if (!pane || pane->getLayerCount() <= row) return false; Chris@298: Chris@298: Layer *layer = pane->getLayer(pane->getLayerCount() - row - 1); Chris@298: if (!layer) return false; Chris@298: Chris@298: if (col == 1) { Chris@298: if (role == Qt::CheckStateRole) { Chris@298: layer->showLayer(pane, value.toInt() == Qt::Checked); Chris@298: emit dataChanged(index, index); Chris@298: return true; Chris@298: } Chris@298: } else if (col == 2) { Chris@298: if (role == Qt::CheckStateRole) { Chris@298: PlayParameters *params = layer->getPlayParameters(); Chris@298: if (params) { Chris@298: params->setPlayMuted(value.toInt() == Qt::Unchecked); Chris@298: emit dataChanged(index, index); Chris@298: return true; Chris@298: } Chris@298: } Chris@298: } Chris@298: Chris@298: return false; Chris@298: } Chris@298: Chris@43: Qt::ItemFlags Chris@43: LayerTreeModel::flags(const QModelIndex &index) const Chris@43: { Chris@298: Qt::ItemFlags flags = Qt::ItemIsEnabled; Chris@298: if (!index.isValid()) return flags; Chris@298: Chris@298: if (index.column() == 1 || index.column() == 2) { Chris@298: flags |= Qt::ItemIsUserCheckable; Chris@298: } else if (index.column() == 0) { Chris@298: flags |= Qt::ItemIsSelectable; Chris@298: } Chris@298: Chris@298: return flags; 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@298: else if (section == 1) return QVariant(tr("Shown")); Chris@298: else if (section == 2) return QVariant(tr("Played")); Chris@298: else if (section == 3) 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@298: if (!parent.isValid()) return 4; Chris@43: Chris@43: QObject *obj = static_cast(parent.internalPointer()); Chris@298: if (obj == m_stack) return 4; // row for a layer Chris@56: Chris@43: return 1; Chris@43: } Chris@43: