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@417
|
21 #include <algorithm>
|
Chris@420
|
22 #include <iostream>
|
Chris@417
|
23
|
Chris@420
|
24 ModelDataTableModel::ModelDataTableModel(TabularModel *m) :
|
Chris@421
|
25 m_model(m),
|
Chris@421
|
26 m_sortColumn(0),
|
Chris@421
|
27 m_sortOrdering(Qt::AscendingOrder)
|
Chris@413
|
28 {
|
Chris@420
|
29 Model *baseModel = dynamic_cast<Model *>(m);
|
Chris@420
|
30
|
Chris@420
|
31 connect(baseModel, SIGNAL(modelChanged()), this, SLOT(modelChanged()));
|
Chris@420
|
32 connect(baseModel, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@413
|
33 this, SLOT(modelChanged(size_t, size_t)));
|
Chris@413
|
34 }
|
Chris@413
|
35
|
Chris@413
|
36 ModelDataTableModel::~ModelDataTableModel()
|
Chris@413
|
37 {
|
Chris@413
|
38 }
|
Chris@413
|
39
|
Chris@413
|
40 QVariant
|
Chris@413
|
41 ModelDataTableModel::data(const QModelIndex &index, int role) const
|
Chris@413
|
42 {
|
Chris@413
|
43 if (!index.isValid()) return QVariant();
|
Chris@420
|
44 return m_model->getData(getUnsorted(index.row()), index.column(), role);
|
Chris@413
|
45 }
|
Chris@413
|
46
|
Chris@413
|
47 bool
|
Chris@413
|
48 ModelDataTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
Chris@413
|
49 {
|
Chris@420
|
50 if (!index.isValid()) return false;
|
Chris@421
|
51 std::cerr << "ModelDataTableModel::setData(" << index.row() << ", " << index.column() << ", " << value.toString().toStdString() << ", " << role << ")" << std::endl;
|
Chris@421
|
52 Command *command = m_model->getSetDataCommand(getUnsorted(index.row()),
|
Chris@421
|
53 index.column(),
|
Chris@421
|
54 value, role);
|
Chris@420
|
55 if (command) {
|
Chris@421
|
56 std::cerr << "emitting executeCommand" << std::endl;
|
Chris@420
|
57 emit executeCommand(command);
|
Chris@420
|
58 return true;
|
Chris@420
|
59 } else {
|
Chris@416
|
60 return false;
|
Chris@416
|
61 }
|
Chris@413
|
62 }
|
Chris@413
|
63
|
Chris@413
|
64 Qt::ItemFlags
|
Chris@413
|
65 ModelDataTableModel::flags(const QModelIndex &index) const
|
Chris@413
|
66 {
|
Chris@416
|
67 Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsEditable |
|
Chris@416
|
68 Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsSelectable;
|
Chris@416
|
69 return flags;
|
Chris@413
|
70 }
|
Chris@413
|
71
|
Chris@413
|
72 QVariant
|
Chris@413
|
73 ModelDataTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
Chris@413
|
74 {
|
Chris@413
|
75 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
Chris@420
|
76 return m_model->getHeading(section);
|
Chris@413
|
77 }
|
Chris@413
|
78 return QVariant();
|
Chris@413
|
79 }
|
Chris@413
|
80
|
Chris@413
|
81 QModelIndex
|
Chris@413
|
82 ModelDataTableModel::index(int row, int column, const QModelIndex &parent) const
|
Chris@413
|
83 {
|
Chris@413
|
84 return createIndex(row, column, 0);
|
Chris@413
|
85 }
|
Chris@413
|
86
|
Chris@413
|
87 QModelIndex
|
Chris@413
|
88 ModelDataTableModel::parent(const QModelIndex &index) const
|
Chris@413
|
89 {
|
Chris@413
|
90 return QModelIndex();
|
Chris@413
|
91 }
|
Chris@413
|
92
|
Chris@413
|
93 int
|
Chris@413
|
94 ModelDataTableModel::rowCount(const QModelIndex &parent) const
|
Chris@413
|
95 {
|
Chris@413
|
96 if (parent.isValid()) return 0;
|
Chris@420
|
97 return m_model->getRowCount();
|
Chris@413
|
98 }
|
Chris@413
|
99
|
Chris@413
|
100 int
|
Chris@413
|
101 ModelDataTableModel::columnCount(const QModelIndex &parent) const
|
Chris@413
|
102 {
|
Chris@413
|
103 if (parent.isValid()) return 0;
|
Chris@420
|
104 return m_model->getColumnCount();
|
Chris@416
|
105 }
|
Chris@416
|
106
|
Chris@416
|
107 QModelIndex
|
Chris@416
|
108 ModelDataTableModel::getModelIndexForFrame(size_t frame) const
|
Chris@416
|
109 {
|
Chris@420
|
110 int row = m_model->getRowForFrame(frame);
|
Chris@420
|
111 return createIndex(getSorted(row), 0, 0);
|
Chris@413
|
112 }
|
Chris@413
|
113
|
Chris@419
|
114 size_t
|
Chris@419
|
115 ModelDataTableModel::getFrameForModelIndex(const QModelIndex &index) const
|
Chris@419
|
116 {
|
Chris@420
|
117 return m_model->getFrameForRow(getUnsorted(index.row()));
|
Chris@420
|
118 }
|
Chris@420
|
119
|
Chris@420
|
120 void
|
Chris@420
|
121 ModelDataTableModel::sort(int column, Qt::SortOrder sortOrder)
|
Chris@420
|
122 {
|
Chris@420
|
123 std::cerr << "ModelDataTableModel::sort(" << column << ", " << sortOrder
|
Chris@420
|
124 << ")" << std::endl;
|
Chris@420
|
125 m_sortColumn = column;
|
Chris@420
|
126 m_sortOrdering = sortOrder;
|
Chris@420
|
127 m_sort.clear();
|
Chris@420
|
128 emit layoutChanged();
|
Chris@419
|
129 }
|
Chris@419
|
130
|
Chris@413
|
131 void
|
Chris@413
|
132 ModelDataTableModel::modelChanged()
|
Chris@413
|
133 {
|
Chris@420
|
134 m_sort.clear();
|
Chris@413
|
135 emit layoutChanged();
|
Chris@413
|
136 }
|
Chris@413
|
137
|
Chris@413
|
138 void
|
Chris@416
|
139 ModelDataTableModel::modelChanged(size_t f0, size_t f1)
|
Chris@416
|
140 {
|
Chris@420
|
141 //!!! inefficient
|
Chris@420
|
142 m_sort.clear();
|
Chris@416
|
143 emit layoutChanged();
|
Chris@416
|
144 }
|
Chris@413
|
145
|
Chris@420
|
146 int
|
Chris@420
|
147 ModelDataTableModel::getSorted(int row)
|
Chris@413
|
148 {
|
Chris@420
|
149 if (m_model->isColumnTimeValue(m_sortColumn)) {
|
Chris@420
|
150 if (m_sortOrdering == Qt::AscendingOrder) {
|
Chris@420
|
151 return row;
|
Chris@420
|
152 } else {
|
Chris@420
|
153 return rowCount() - row - 1;
|
Chris@420
|
154 }
|
Chris@420
|
155 }
|
Chris@413
|
156
|
Chris@420
|
157 if (m_sort.empty()) {
|
Chris@420
|
158 resort();
|
Chris@413
|
159 }
|
Chris@420
|
160 if (row < 0 || row >= m_sort.size()) return 0;
|
Chris@420
|
161 return m_sort[row];
|
Chris@413
|
162 }
|
Chris@413
|
163
|
Chris@420
|
164 int
|
Chris@420
|
165 ModelDataTableModel::getUnsorted(int row)
|
Chris@413
|
166 {
|
Chris@420
|
167 if (m_model->isColumnTimeValue(m_sortColumn)) {
|
Chris@420
|
168 if (m_sortOrdering == Qt::AscendingOrder) {
|
Chris@420
|
169 return row;
|
Chris@420
|
170 } else {
|
Chris@420
|
171 return rowCount() - row - 1;
|
Chris@420
|
172 }
|
Chris@413
|
173 }
|
Chris@420
|
174 //!!! need the reverse of this
|
Chris@420
|
175 if (m_sort.empty()) {
|
Chris@420
|
176 resort();
|
Chris@420
|
177 }
|
Chris@420
|
178 if (row < 0 || row >= m_sort.size()) return 0;
|
Chris@420
|
179 return m_sort[row];
|
Chris@413
|
180 }
|
Chris@413
|
181
|
Chris@420
|
182 void
|
Chris@420
|
183 ModelDataTableModel::resort()
|
Chris@413
|
184 {
|
Chris@421
|
185
|
Chris@413
|
186 }
|
Chris@413
|
187
|