comparison data/model/ModelDataTableModel.cpp @ 422:4caa28a0a8a2

* sorting arbitrary columns in data editor
author Chris Cannam
date Thu, 12 Jun 2008 09:03:00 +0000
parents 397fe91dc8e0
children eafef13bb0b3
comparison
equal deleted inserted replaced
421:397fe91dc8e0 422:4caa28a0a8a2
16 #include "ModelDataTableModel.h" 16 #include "ModelDataTableModel.h"
17 17
18 #include "TabularModel.h" 18 #include "TabularModel.h"
19 #include "Model.h" 19 #include "Model.h"
20 20
21 #include <map>
21 #include <algorithm> 22 #include <algorithm>
22 #include <iostream> 23 #include <iostream>
23 24
24 ModelDataTableModel::ModelDataTableModel(TabularModel *m) : 25 ModelDataTableModel::ModelDataTableModel(TabularModel *m) :
25 m_model(m), 26 m_model(m),
120 void 121 void
121 ModelDataTableModel::sort(int column, Qt::SortOrder sortOrder) 122 ModelDataTableModel::sort(int column, Qt::SortOrder sortOrder)
122 { 123 {
123 std::cerr << "ModelDataTableModel::sort(" << column << ", " << sortOrder 124 std::cerr << "ModelDataTableModel::sort(" << column << ", " << sortOrder
124 << ")" << std::endl; 125 << ")" << std::endl;
126 if (m_sortColumn != column) {
127 m_sort.clear();
128 }
125 m_sortColumn = column; 129 m_sortColumn = column;
126 m_sortOrdering = sortOrder; 130 m_sortOrdering = sortOrder;
127 m_sort.clear();
128 emit layoutChanged(); 131 emit layoutChanged();
129 } 132 }
130 133
131 void 134 void
132 ModelDataTableModel::modelChanged() 135 ModelDataTableModel::modelChanged()
155 } 158 }
156 159
157 if (m_sort.empty()) { 160 if (m_sort.empty()) {
158 resort(); 161 resort();
159 } 162 }
160 if (row < 0 || row >= m_sort.size()) return 0; 163 int result = 0;
161 return m_sort[row]; 164 if (row >= 0 && row < m_sort.size()) {
165 result = m_sort[row];
166 }
167 if (m_sortOrdering == Qt::DescendingOrder) {
168 result = rowCount() - result - 1;
169 }
170
171 return result;
162 } 172 }
163 173
164 int 174 int
165 ModelDataTableModel::getUnsorted(int row) 175 ModelDataTableModel::getUnsorted(int row)
166 { 176 {
169 return row; 179 return row;
170 } else { 180 } else {
171 return rowCount() - row - 1; 181 return rowCount() - row - 1;
172 } 182 }
173 } 183 }
174 //!!! need the reverse of this 184
175 if (m_sort.empty()) { 185 if (m_sort.empty()) {
176 resort(); 186 resort();
177 } 187 }
178 if (row < 0 || row >= m_sort.size()) return 0; 188
179 return m_sort[row]; 189 int result = 0;
190 if (row >= 0 && row < m_sort.size()) {
191 if (m_sortOrdering == Qt::AscendingOrder) {
192 result = m_rsort[row];
193 } else {
194 result = m_rsort[rowCount() - row - 1];
195 }
196 }
197
198 return result;
180 } 199 }
181 200
182 void 201 void
183 ModelDataTableModel::resort() 202 ModelDataTableModel::resort()
184 { 203 {
185 204 bool numeric = (m_model->getSortType(m_sortColumn) ==
186 } 205 TabularModel::SortNumeric);
187 206
207 m_sort.clear();
208 m_rsort.clear();
209
210 if (numeric) resortNumeric();
211 else resortAlphabetical();
212
213 std::map<int, int> tmp;
214
215 // rsort maps from sorted row number to original row number
216
217 for (int i = 0; i < m_rsort.size(); ++i) {
218 tmp[m_rsort[i]] = i;
219 }
220
221 // tmp now maps from original row number to sorted row number
222
223 for (std::map<int, int>::const_iterator i = tmp.begin(); i != tmp.end(); ++i) {
224 m_sort.push_back(i->second);
225 }
226
227 // and sort now maps from original row number to sorted row number
228 }
229
230 void
231 ModelDataTableModel::resortNumeric()
232 {
233 typedef std::multimap<double, int> MapType;
234
235 MapType rowMap;
236 int rows = m_model->getRowCount();
237
238 for (int i = 0; i < rows; ++i) {
239 QVariant value =
240 m_model->getData(i, m_sortColumn, TabularModel::SortRole);
241 rowMap.insert(MapType::value_type(value.toDouble(), i));
242 }
243
244 for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) {
245 m_rsort.push_back(i->second);
246 }
247
248 // rsort now maps from sorted row number to original row number
249 }
250
251 void
252 ModelDataTableModel::resortAlphabetical()
253 {
254 typedef std::multimap<QString, int> MapType;
255
256 MapType rowMap;
257 int rows = m_model->getRowCount();
258
259 for (int i = 0; i < rows; ++i) {
260 QVariant value =
261 m_model->getData(i, m_sortColumn, TabularModel::SortRole);
262 rowMap.insert(MapType::value_type(value.toString(), i));
263 }
264
265 for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) {
266 m_rsort.push_back(i->second);
267 }
268
269 // rsort now maps from sorted row number to original row number
270 }
271