Chris@413
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@413
|
2
|
Chris@413
|
3 /*
|
Chris@413
|
4 Sonic Visualiser
|
Chris@413
|
5 An audio file viewer and annotation editor.
|
Chris@413
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@413
|
7 This file copyright 2008 QMUL.
|
Chris@413
|
8
|
Chris@413
|
9 This program is free software; you can redistribute it and/or
|
Chris@413
|
10 modify it under the terms of the GNU General Public License as
|
Chris@413
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@413
|
12 License, or (at your option) any later version. See the file
|
Chris@413
|
13 COPYING included with this distribution for more information.
|
Chris@413
|
14 */
|
Chris@413
|
15
|
Chris@413
|
16 #include "ModelDataTableModel.h"
|
Chris@413
|
17
|
Chris@420
|
18 #include "TabularModel.h"
|
Chris@420
|
19 #include "Model.h"
|
Chris@413
|
20
|
Chris@422
|
21 #include <map>
|
Chris@417
|
22 #include <algorithm>
|
Chris@420
|
23 #include <iostream>
|
Chris@417
|
24
|
Chris@420
|
25 ModelDataTableModel::ModelDataTableModel(TabularModel *m) :
|
Chris@421
|
26 m_model(m),
|
Chris@421
|
27 m_sortColumn(0),
|
Chris@421
|
28 m_sortOrdering(Qt::AscendingOrder)
|
Chris@413
|
29 {
|
Chris@420
|
30 Model *baseModel = dynamic_cast<Model *>(m);
|
Chris@420
|
31
|
Chris@420
|
32 connect(baseModel, SIGNAL(modelChanged()), this, SLOT(modelChanged()));
|
Chris@420
|
33 connect(baseModel, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@413
|
34 this, SLOT(modelChanged(size_t, size_t)));
|
Chris@413
|
35 }
|
Chris@413
|
36
|
Chris@413
|
37 ModelDataTableModel::~ModelDataTableModel()
|
Chris@413
|
38 {
|
Chris@413
|
39 }
|
Chris@413
|
40
|
Chris@413
|
41 QVariant
|
Chris@413
|
42 ModelDataTableModel::data(const QModelIndex &index, int role) const
|
Chris@413
|
43 {
|
Chris@413
|
44 if (!index.isValid()) return QVariant();
|
Chris@420
|
45 return m_model->getData(getUnsorted(index.row()), index.column(), role);
|
Chris@413
|
46 }
|
Chris@413
|
47
|
Chris@413
|
48 bool
|
Chris@413
|
49 ModelDataTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
Chris@413
|
50 {
|
Chris@420
|
51 if (!index.isValid()) return false;
|
Chris@421
|
52 std::cerr << "ModelDataTableModel::setData(" << index.row() << ", " << index.column() << ", " << value.toString().toStdString() << ", " << role << ")" << std::endl;
|
Chris@421
|
53 Command *command = m_model->getSetDataCommand(getUnsorted(index.row()),
|
Chris@421
|
54 index.column(),
|
Chris@421
|
55 value, role);
|
Chris@420
|
56 if (command) {
|
Chris@421
|
57 std::cerr << "emitting executeCommand" << std::endl;
|
Chris@420
|
58 emit executeCommand(command);
|
Chris@420
|
59 return true;
|
Chris@420
|
60 } else {
|
Chris@416
|
61 return false;
|
Chris@416
|
62 }
|
Chris@413
|
63 }
|
Chris@413
|
64
|
Chris@413
|
65 Qt::ItemFlags
|
Chris@413
|
66 ModelDataTableModel::flags(const QModelIndex &index) const
|
Chris@413
|
67 {
|
Chris@416
|
68 Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsEditable |
|
Chris@416
|
69 Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsSelectable;
|
Chris@416
|
70 return flags;
|
Chris@413
|
71 }
|
Chris@413
|
72
|
Chris@413
|
73 QVariant
|
Chris@413
|
74 ModelDataTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
Chris@413
|
75 {
|
Chris@413
|
76 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
Chris@420
|
77 return m_model->getHeading(section);
|
Chris@413
|
78 }
|
Chris@413
|
79 return QVariant();
|
Chris@413
|
80 }
|
Chris@413
|
81
|
Chris@413
|
82 QModelIndex
|
Chris@413
|
83 ModelDataTableModel::index(int row, int column, const QModelIndex &parent) const
|
Chris@413
|
84 {
|
Chris@413
|
85 return createIndex(row, column, 0);
|
Chris@413
|
86 }
|
Chris@413
|
87
|
Chris@413
|
88 QModelIndex
|
Chris@413
|
89 ModelDataTableModel::parent(const QModelIndex &index) const
|
Chris@413
|
90 {
|
Chris@413
|
91 return QModelIndex();
|
Chris@413
|
92 }
|
Chris@413
|
93
|
Chris@413
|
94 int
|
Chris@413
|
95 ModelDataTableModel::rowCount(const QModelIndex &parent) const
|
Chris@413
|
96 {
|
Chris@413
|
97 if (parent.isValid()) return 0;
|
Chris@420
|
98 return m_model->getRowCount();
|
Chris@413
|
99 }
|
Chris@413
|
100
|
Chris@413
|
101 int
|
Chris@413
|
102 ModelDataTableModel::columnCount(const QModelIndex &parent) const
|
Chris@413
|
103 {
|
Chris@413
|
104 if (parent.isValid()) return 0;
|
Chris@420
|
105 return m_model->getColumnCount();
|
Chris@416
|
106 }
|
Chris@416
|
107
|
Chris@416
|
108 QModelIndex
|
Chris@416
|
109 ModelDataTableModel::getModelIndexForFrame(size_t frame) const
|
Chris@416
|
110 {
|
Chris@420
|
111 int row = m_model->getRowForFrame(frame);
|
Chris@420
|
112 return createIndex(getSorted(row), 0, 0);
|
Chris@413
|
113 }
|
Chris@413
|
114
|
Chris@419
|
115 size_t
|
Chris@419
|
116 ModelDataTableModel::getFrameForModelIndex(const QModelIndex &index) const
|
Chris@419
|
117 {
|
Chris@420
|
118 return m_model->getFrameForRow(getUnsorted(index.row()));
|
Chris@420
|
119 }
|
Chris@420
|
120
|
Chris@420
|
121 void
|
Chris@420
|
122 ModelDataTableModel::sort(int column, Qt::SortOrder sortOrder)
|
Chris@420
|
123 {
|
Chris@420
|
124 std::cerr << "ModelDataTableModel::sort(" << column << ", " << sortOrder
|
Chris@420
|
125 << ")" << std::endl;
|
Chris@422
|
126 if (m_sortColumn != column) {
|
Chris@422
|
127 m_sort.clear();
|
Chris@422
|
128 }
|
Chris@420
|
129 m_sortColumn = column;
|
Chris@420
|
130 m_sortOrdering = sortOrder;
|
Chris@420
|
131 emit layoutChanged();
|
Chris@419
|
132 }
|
Chris@419
|
133
|
Chris@413
|
134 void
|
Chris@413
|
135 ModelDataTableModel::modelChanged()
|
Chris@413
|
136 {
|
Chris@420
|
137 m_sort.clear();
|
Chris@413
|
138 emit layoutChanged();
|
Chris@413
|
139 }
|
Chris@413
|
140
|
Chris@413
|
141 void
|
Chris@416
|
142 ModelDataTableModel::modelChanged(size_t f0, size_t f1)
|
Chris@416
|
143 {
|
Chris@420
|
144 //!!! inefficient
|
Chris@420
|
145 m_sort.clear();
|
Chris@416
|
146 emit layoutChanged();
|
Chris@416
|
147 }
|
Chris@413
|
148
|
Chris@420
|
149 int
|
Chris@420
|
150 ModelDataTableModel::getSorted(int row)
|
Chris@413
|
151 {
|
Chris@420
|
152 if (m_model->isColumnTimeValue(m_sortColumn)) {
|
Chris@420
|
153 if (m_sortOrdering == Qt::AscendingOrder) {
|
Chris@420
|
154 return row;
|
Chris@420
|
155 } else {
|
Chris@420
|
156 return rowCount() - row - 1;
|
Chris@420
|
157 }
|
Chris@420
|
158 }
|
Chris@413
|
159
|
Chris@420
|
160 if (m_sort.empty()) {
|
Chris@420
|
161 resort();
|
Chris@413
|
162 }
|
Chris@422
|
163 int result = 0;
|
Chris@422
|
164 if (row >= 0 && row < m_sort.size()) {
|
Chris@422
|
165 result = m_sort[row];
|
Chris@422
|
166 }
|
Chris@422
|
167 if (m_sortOrdering == Qt::DescendingOrder) {
|
Chris@422
|
168 result = rowCount() - result - 1;
|
Chris@422
|
169 }
|
Chris@422
|
170
|
Chris@422
|
171 return result;
|
Chris@413
|
172 }
|
Chris@413
|
173
|
Chris@420
|
174 int
|
Chris@420
|
175 ModelDataTableModel::getUnsorted(int row)
|
Chris@413
|
176 {
|
Chris@420
|
177 if (m_model->isColumnTimeValue(m_sortColumn)) {
|
Chris@420
|
178 if (m_sortOrdering == Qt::AscendingOrder) {
|
Chris@420
|
179 return row;
|
Chris@420
|
180 } else {
|
Chris@420
|
181 return rowCount() - row - 1;
|
Chris@420
|
182 }
|
Chris@413
|
183 }
|
Chris@422
|
184
|
Chris@420
|
185 if (m_sort.empty()) {
|
Chris@420
|
186 resort();
|
Chris@420
|
187 }
|
Chris@422
|
188
|
Chris@422
|
189 int result = 0;
|
Chris@422
|
190 if (row >= 0 && row < m_sort.size()) {
|
Chris@422
|
191 if (m_sortOrdering == Qt::AscendingOrder) {
|
Chris@422
|
192 result = m_rsort[row];
|
Chris@422
|
193 } else {
|
Chris@422
|
194 result = m_rsort[rowCount() - row - 1];
|
Chris@422
|
195 }
|
Chris@422
|
196 }
|
Chris@422
|
197
|
Chris@422
|
198 return result;
|
Chris@413
|
199 }
|
Chris@413
|
200
|
Chris@420
|
201 void
|
Chris@420
|
202 ModelDataTableModel::resort()
|
Chris@413
|
203 {
|
Chris@422
|
204 bool numeric = (m_model->getSortType(m_sortColumn) ==
|
Chris@422
|
205 TabularModel::SortNumeric);
|
Chris@422
|
206
|
Chris@422
|
207 m_sort.clear();
|
Chris@422
|
208 m_rsort.clear();
|
Chris@422
|
209
|
Chris@422
|
210 if (numeric) resortNumeric();
|
Chris@422
|
211 else resortAlphabetical();
|
Chris@422
|
212
|
Chris@422
|
213 std::map<int, int> tmp;
|
Chris@422
|
214
|
Chris@422
|
215 // rsort maps from sorted row number to original row number
|
Chris@422
|
216
|
Chris@422
|
217 for (int i = 0; i < m_rsort.size(); ++i) {
|
Chris@422
|
218 tmp[m_rsort[i]] = i;
|
Chris@422
|
219 }
|
Chris@422
|
220
|
Chris@422
|
221 // tmp now maps from original row number to sorted row number
|
Chris@422
|
222
|
Chris@422
|
223 for (std::map<int, int>::const_iterator i = tmp.begin(); i != tmp.end(); ++i) {
|
Chris@422
|
224 m_sort.push_back(i->second);
|
Chris@422
|
225 }
|
Chris@422
|
226
|
Chris@422
|
227 // and sort now maps from original row number to sorted row number
|
Chris@413
|
228 }
|
Chris@413
|
229
|
Chris@422
|
230 void
|
Chris@422
|
231 ModelDataTableModel::resortNumeric()
|
Chris@422
|
232 {
|
Chris@422
|
233 typedef std::multimap<double, int> MapType;
|
Chris@422
|
234
|
Chris@422
|
235 MapType rowMap;
|
Chris@422
|
236 int rows = m_model->getRowCount();
|
Chris@422
|
237
|
Chris@422
|
238 for (int i = 0; i < rows; ++i) {
|
Chris@422
|
239 QVariant value =
|
Chris@422
|
240 m_model->getData(i, m_sortColumn, TabularModel::SortRole);
|
Chris@422
|
241 rowMap.insert(MapType::value_type(value.toDouble(), i));
|
Chris@422
|
242 }
|
Chris@422
|
243
|
Chris@422
|
244 for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) {
|
Chris@422
|
245 m_rsort.push_back(i->second);
|
Chris@422
|
246 }
|
Chris@422
|
247
|
Chris@422
|
248 // rsort now maps from sorted row number to original row number
|
Chris@422
|
249 }
|
Chris@422
|
250
|
Chris@422
|
251 void
|
Chris@422
|
252 ModelDataTableModel::resortAlphabetical()
|
Chris@422
|
253 {
|
Chris@422
|
254 typedef std::multimap<QString, int> MapType;
|
Chris@422
|
255
|
Chris@422
|
256 MapType rowMap;
|
Chris@422
|
257 int rows = m_model->getRowCount();
|
Chris@422
|
258
|
Chris@422
|
259 for (int i = 0; i < rows; ++i) {
|
Chris@422
|
260 QVariant value =
|
Chris@422
|
261 m_model->getData(i, m_sortColumn, TabularModel::SortRole);
|
Chris@422
|
262 rowMap.insert(MapType::value_type(value.toString(), i));
|
Chris@422
|
263 }
|
Chris@422
|
264
|
Chris@422
|
265 for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) {
|
Chris@422
|
266 m_rsort.push_back(i->second);
|
Chris@422
|
267 }
|
Chris@422
|
268
|
Chris@422
|
269 // rsort now maps from sorted row number to original row number
|
Chris@422
|
270 }
|
Chris@422
|
271
|