annotate widgets/LayerTree.cpp @ 284:1284955856ab

* threshold, show-peaks properties in spectrum
author Chris Cannam
date Fri, 06 Jul 2007 15:17:35 +0000
parents 3c402c6052f6
children 226cb289bdf4
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@280 24 #include <QIcon>
Chris@43 25 #include <iostream>
Chris@43 26
Chris@43 27
Chris@43 28 LayerTreeModel::LayerTreeModel(PaneStack *stack, QObject *parent) :
Chris@43 29 QAbstractItemModel(parent),
Chris@43 30 m_stack(stack)
Chris@43 31 {
Chris@271 32 connect(stack, SIGNAL(paneAdded()), this, SIGNAL(layoutChanged()));
Chris@271 33 connect(stack, SIGNAL(paneDeleted()), this, SIGNAL(layoutChanged()));
Chris@43 34 }
Chris@43 35
Chris@43 36 LayerTreeModel::~LayerTreeModel()
Chris@43 37 {
Chris@43 38 }
Chris@43 39
Chris@43 40 QVariant
Chris@43 41 LayerTreeModel::data(const QModelIndex &index, int role) const
Chris@43 42 {
Chris@43 43 if (!index.isValid()) return QVariant();
Chris@56 44
Chris@43 45 QObject *obj = static_cast<QObject *>(index.internalPointer());
Chris@277 46 int row = index.row(), col = index.column();
Chris@277 47
Chris@277 48 Pane *pane = dynamic_cast<Pane *>(obj);
Chris@277 49 if (!pane) {
Chris@277 50 if (col == 0 && row < m_stack->getPaneCount()) {
Chris@277 51 switch (role) {
Chris@277 52 case Qt::DisplayRole:
Chris@277 53 return QVariant(QString("Pane %1").arg(row + 1));
Chris@277 54 case Qt::DecorationRole:
Chris@277 55 return QVariant(QIcon(QString(":/icons/pane.png")));
Chris@277 56 default: break;
Chris@277 57 }
Chris@277 58 }
Chris@43 59 }
Chris@43 60
Chris@277 61 if (pane && pane->getLayerCount() > row) {
Chris@277 62 Layer *layer = pane->getLayer(row);
Chris@277 63 if (layer) {
Chris@277 64 if (col == 0) {
Chris@277 65 switch (role) {
Chris@277 66 case Qt::DisplayRole:
Chris@277 67 return QVariant(layer->objectName());
Chris@277 68 case Qt::DecorationRole:
Chris@277 69 return QVariant
Chris@277 70 (QIcon(QString(":/icons/%1.png")
Chris@277 71 .arg(layer->getPropertyContainerIconName())));
Chris@277 72 default: break;
Chris@277 73 }
Chris@277 74 } else if (col == 1) {
Chris@277 75 Model *model = layer->getModel();
Chris@277 76 if (model && role == Qt::DisplayRole) {
Chris@277 77 return QVariant(model->objectName());
Chris@277 78 }
Chris@277 79 }
Chris@277 80 }
Chris@43 81 }
Chris@43 82
Chris@43 83 return QVariant();
Chris@43 84 }
Chris@43 85
Chris@43 86 Qt::ItemFlags
Chris@43 87 LayerTreeModel::flags(const QModelIndex &index) const
Chris@43 88 {
Chris@56 89 if (!index.isValid()) return Qt::ItemIsEnabled;
Chris@56 90 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
Chris@43 91 }
Chris@43 92
Chris@43 93 QVariant
Chris@56 94 LayerTreeModel::headerData(int section,
Chris@43 95 Qt::Orientation orientation,
Chris@43 96 int role) const
Chris@43 97 {
Chris@56 98 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
Chris@56 99 if (section == 0) return QVariant(tr("Layer"));
Chris@56 100 else if (section == 1) return QVariant(tr("Model"));
Chris@56 101 }
Chris@56 102
Chris@56 103 return QVariant();
Chris@43 104 }
Chris@43 105
Chris@43 106 QModelIndex
Chris@43 107 LayerTreeModel::index(int row, int column, const QModelIndex &parent) const
Chris@43 108 {
Chris@277 109 // cell for a pane contains row, column, pane stack
Chris@277 110 // -> its parent is the invalid cell
Chris@277 111
Chris@277 112 // cell for a layer contains row, column, pane
Chris@277 113 // -> its parent is row, column, pane stack (which identify the pane)
Chris@43 114
Chris@43 115 if (!parent.isValid()) {
Chris@277 116 if (row >= m_stack->getPaneCount() || column > 0) return QModelIndex();
Chris@43 117 return createIndex(row, column, m_stack);
Chris@43 118 }
Chris@43 119
Chris@43 120 QObject *obj = static_cast<QObject *>(parent.internalPointer());
Chris@277 121
Chris@277 122 if (obj == m_stack) {
Chris@277 123 Pane *pane = m_stack->getPane(parent.row());
Chris@277 124 if (!pane || parent.column() > 0) return QModelIndex();
Chris@277 125 return createIndex(row, column, pane);
Chris@43 126 }
Chris@43 127
Chris@43 128 return QModelIndex();
Chris@43 129 }
Chris@43 130
Chris@43 131 QModelIndex
Chris@43 132 LayerTreeModel::parent(const QModelIndex &index) const
Chris@43 133 {
Chris@43 134 QObject *obj = static_cast<QObject *>(index.internalPointer());
Chris@43 135
Chris@43 136 Pane *pane = dynamic_cast<Pane *>(obj);
Chris@43 137 if (pane) {
Chris@277 138 int index = m_stack->getPaneIndex(pane);
Chris@277 139 if (index >= 0) return createIndex(index, 0, m_stack);
Chris@43 140 }
Chris@43 141
Chris@43 142 return QModelIndex();
Chris@43 143 }
Chris@43 144
Chris@43 145 int
Chris@43 146 LayerTreeModel::rowCount(const QModelIndex &parent) const
Chris@43 147 {
Chris@277 148 if (!parent.isValid()) return m_stack->getPaneCount();
Chris@43 149
Chris@43 150 QObject *obj = static_cast<QObject *>(parent.internalPointer());
Chris@43 151
Chris@277 152 if (obj == m_stack) {
Chris@277 153 Pane *pane = m_stack->getPane(parent.row());
Chris@277 154 if (!pane || parent.column() > 0) return 0;
Chris@277 155 return pane->getLayerCount();
Chris@43 156 }
Chris@43 157
Chris@43 158 return 0;
Chris@43 159 }
Chris@43 160
Chris@43 161 int
Chris@43 162 LayerTreeModel::columnCount(const QModelIndex &parent) const
Chris@43 163 {
Chris@277 164 if (!parent.isValid()) return 2;
Chris@43 165
Chris@43 166 QObject *obj = static_cast<QObject *>(parent.internalPointer());
Chris@277 167 if (obj == m_stack) return 2;
Chris@56 168
Chris@43 169 return 1;
Chris@43 170 }
Chris@43 171