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