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