Mercurial > hg > svcore
comparison data/model/ModelDataTableModel.cpp @ 1748:91a194e2d80b by-id
Update to ModelId
| author | Chris Cannam |
|---|---|
| date | Wed, 03 Jul 2019 14:20:19 +0100 |
| parents | 70e172e6cc59 |
| children | dffc70996f54 |
comparison
equal
deleted
inserted
replaced
| 1747:498b426191e5 | 1748:91a194e2d80b |
|---|---|
| 20 | 20 |
| 21 #include <map> | 21 #include <map> |
| 22 #include <algorithm> | 22 #include <algorithm> |
| 23 #include <iostream> | 23 #include <iostream> |
| 24 | 24 |
| 25 ModelDataTableModel::ModelDataTableModel(TabularModel *m) : | 25 ModelDataTableModel::ModelDataTableModel(ModelId m) : |
| 26 m_model(m), | 26 m_model(m), |
| 27 m_sortColumn(0), | 27 m_sortColumn(0), |
| 28 m_sortOrdering(Qt::AscendingOrder), | 28 m_sortOrdering(Qt::AscendingOrder), |
| 29 m_currentRow(0) | 29 m_currentRow(0) |
| 30 { | 30 { |
| 31 Model *baseModel = dynamic_cast<Model *>(m); | 31 auto model = ModelById::get(m); |
| 32 | 32 if (model) { |
| 33 connect(baseModel, SIGNAL(modelChanged()), this, SLOT(modelChanged())); | 33 connect(model.get(), SIGNAL(modelChanged()), this, SLOT(modelChanged())); |
| 34 connect(baseModel, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)), | 34 connect(model.get(), SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)), |
| 35 this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t))); | 35 this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t))); |
| 36 connect(baseModel, SIGNAL(aboutToBeDeleted()), | 36 } |
| 37 this, SLOT(modelAboutToBeDeleted())); | |
| 38 } | 37 } |
| 39 | 38 |
| 40 ModelDataTableModel::~ModelDataTableModel() | 39 ModelDataTableModel::~ModelDataTableModel() |
| 41 { | 40 { |
| 42 } | 41 } |
| 43 | 42 |
| 44 QVariant | 43 QVariant |
| 45 ModelDataTableModel::data(const QModelIndex &index, int role) const | 44 ModelDataTableModel::data(const QModelIndex &index, int role) const |
| 46 { | 45 { |
| 47 if (!m_model) return QVariant(); | 46 auto model = getTabularModel(); |
| 47 if (!model) return QVariant(); | |
| 48 if (role != Qt::EditRole && role != Qt::DisplayRole) return QVariant(); | 48 if (role != Qt::EditRole && role != Qt::DisplayRole) return QVariant(); |
| 49 if (!index.isValid()) return QVariant(); | 49 if (!index.isValid()) return QVariant(); |
| 50 QVariant d = m_model->getData(getUnsorted(index.row()), index.column(), role); | 50 QVariant d = model->getData(getUnsorted(index.row()), index.column(), role); |
| 51 return d; | 51 return d; |
| 52 } | 52 } |
| 53 | 53 |
| 54 bool | 54 bool |
| 55 ModelDataTableModel::setData(const QModelIndex &index, const QVariant &value, int role) | 55 ModelDataTableModel::setData(const QModelIndex &index, const QVariant &value, int role) |
| 56 { | 56 { |
| 57 if (!m_model) return false; | 57 auto model = getTabularModel(); |
| 58 if (!model) return false; | |
| 58 if (!index.isValid()) return false; | 59 if (!index.isValid()) return false; |
| 59 Command *command = m_model->getSetDataCommand(getUnsorted(index.row()), | 60 Command *command = model->getSetDataCommand(getUnsorted(index.row()), |
| 60 index.column(), | 61 index.column(), |
| 61 value, role); | 62 value, role); |
| 62 if (command) { | 63 if (command) { |
| 63 emit addCommand(command); | 64 emit addCommand(command); |
| 64 return true; | 65 return true; |
| 68 } | 69 } |
| 69 | 70 |
| 70 bool | 71 bool |
| 71 ModelDataTableModel::insertRow(int row, const QModelIndex &parent) | 72 ModelDataTableModel::insertRow(int row, const QModelIndex &parent) |
| 72 { | 73 { |
| 73 if (!m_model) return false; | 74 auto model = getTabularModel(); |
| 75 if (!model) return false; | |
| 74 if (parent.isValid()) return false; | 76 if (parent.isValid()) return false; |
| 75 | 77 |
| 76 Command *command = m_model->getInsertRowCommand(getUnsorted(row)); | 78 Command *command = model->getInsertRowCommand(getUnsorted(row)); |
| 77 | 79 |
| 78 if (command) { | 80 if (command) { |
| 79 emit addCommand(command); | 81 emit addCommand(command); |
| 80 } | 82 } |
| 81 | 83 |
| 83 } | 85 } |
| 84 | 86 |
| 85 bool | 87 bool |
| 86 ModelDataTableModel::removeRow(int row, const QModelIndex &parent) | 88 ModelDataTableModel::removeRow(int row, const QModelIndex &parent) |
| 87 { | 89 { |
| 88 if (!m_model) return false; | 90 auto model = getTabularModel(); |
| 91 if (!model) return false; | |
| 89 if (parent.isValid()) return false; | 92 if (parent.isValid()) return false; |
| 90 | 93 |
| 91 Command *command = m_model->getRemoveRowCommand(getUnsorted(row)); | 94 Command *command = model->getRemoveRowCommand(getUnsorted(row)); |
| 92 | 95 |
| 93 if (command) { | 96 if (command) { |
| 94 emit addCommand(command); | 97 emit addCommand(command); |
| 95 } | 98 } |
| 96 | 99 |
| 106 } | 109 } |
| 107 | 110 |
| 108 QVariant | 111 QVariant |
| 109 ModelDataTableModel::headerData(int section, Qt::Orientation orientation, int role) const | 112 ModelDataTableModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 110 { | 113 { |
| 111 if (!m_model) return QVariant(); | 114 auto model = getTabularModel(); |
| 115 if (!model) return QVariant(); | |
| 112 | 116 |
| 113 if (orientation == Qt::Vertical && role == Qt::DisplayRole) { | 117 if (orientation == Qt::Vertical && role == Qt::DisplayRole) { |
| 114 return section + 1; | 118 return section + 1; |
| 115 } | 119 } |
| 116 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { | 120 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
| 117 return m_model->getHeading(section); | 121 return model->getHeading(section); |
| 118 } | 122 } |
| 119 return QVariant(); | 123 return QVariant(); |
| 120 } | 124 } |
| 121 | 125 |
| 122 QModelIndex | 126 QModelIndex |
| 132 } | 136 } |
| 133 | 137 |
| 134 int | 138 int |
| 135 ModelDataTableModel::rowCount(const QModelIndex &parent) const | 139 ModelDataTableModel::rowCount(const QModelIndex &parent) const |
| 136 { | 140 { |
| 137 if (!m_model) return 0; | 141 auto model = getTabularModel(); |
| 142 if (!model) return 0; | |
| 138 if (parent.isValid()) return 0; | 143 if (parent.isValid()) return 0; |
| 139 int count = m_model->getRowCount(); | 144 int count = model->getRowCount(); |
| 140 return count; | 145 return count; |
| 141 } | 146 } |
| 142 | 147 |
| 143 int | 148 int |
| 144 ModelDataTableModel::columnCount(const QModelIndex &parent) const | 149 ModelDataTableModel::columnCount(const QModelIndex &parent) const |
| 145 { | 150 { |
| 146 if (!m_model) return 0; | 151 auto model = getTabularModel(); |
| 152 if (!model) return 0; | |
| 147 if (parent.isValid()) return 0; | 153 if (parent.isValid()) return 0; |
| 148 return m_model->getColumnCount(); | 154 return model->getColumnCount(); |
| 149 } | 155 } |
| 150 | 156 |
| 151 QModelIndex | 157 QModelIndex |
| 152 ModelDataTableModel::getModelIndexForFrame(sv_frame_t frame) const | 158 ModelDataTableModel::getModelIndexForFrame(sv_frame_t frame) const |
| 153 { | 159 { |
| 154 if (!m_model) return createIndex(0, 0); | 160 auto model = getTabularModel(); |
| 155 int row = m_model->getRowForFrame(frame); | 161 if (!model) return createIndex(0, 0); |
| 162 int row = model->getRowForFrame(frame); | |
| 156 return createIndex(getSorted(row), 0, (void *)nullptr); | 163 return createIndex(getSorted(row), 0, (void *)nullptr); |
| 157 } | 164 } |
| 158 | 165 |
| 159 sv_frame_t | 166 sv_frame_t |
| 160 ModelDataTableModel::getFrameForModelIndex(const QModelIndex &index) const | 167 ModelDataTableModel::getFrameForModelIndex(const QModelIndex &index) const |
| 161 { | 168 { |
| 162 if (!m_model) return 0; | 169 auto model = getTabularModel(); |
| 163 return m_model->getFrameForRow(getUnsorted(index.row())); | 170 if (!model) return 0; |
| 171 return model->getFrameForRow(getUnsorted(index.row())); | |
| 164 } | 172 } |
| 165 | 173 |
| 166 QModelIndex | 174 QModelIndex |
| 167 ModelDataTableModel::findText(QString text) const | 175 ModelDataTableModel::findText(QString text) const |
| 168 { | 176 { |
| 177 auto model = getTabularModel(); | |
| 178 if (!model) return QModelIndex(); | |
| 169 if (text == "") return QModelIndex(); | 179 if (text == "") return QModelIndex(); |
| 170 int rows = rowCount(); | 180 int rows = rowCount(); |
| 171 int cols = columnCount(); | 181 int cols = columnCount(); |
| 172 int current = getCurrentRow(); | 182 int current = getCurrentRow(); |
| 173 for (int row = 1; row <= rows; ++row) { | 183 for (int row = 1; row <= rows; ++row) { |
| 174 int wrapped = (row + current) % rows; | 184 int wrapped = (row + current) % rows; |
| 175 for (int col = 0; col < cols; ++col) { | 185 for (int col = 0; col < cols; ++col) { |
| 176 if (m_model->getSortType(col) != TabularModel::SortAlphabetical) { | 186 if (model->getSortType(col) != TabularModel::SortAlphabetical) { |
| 177 continue; | 187 continue; |
| 178 } | 188 } |
| 179 QString cell = m_model->getData(getUnsorted(wrapped), col, | 189 QString cell = model->getData(getUnsorted(wrapped), col, |
| 180 Qt::DisplayRole).toString(); | 190 Qt::DisplayRole).toString(); |
| 181 if (cell.contains(text, Qt::CaseInsensitive)) { | 191 if (cell.contains(text, Qt::CaseInsensitive)) { |
| 182 return createIndex(wrapped, col); | 192 return createIndex(wrapped, col); |
| 183 } | 193 } |
| 184 } | 194 } |
| 241 emit dataChanged(ix0, ix1); | 251 emit dataChanged(ix0, ix1); |
| 242 clearSort(); | 252 clearSort(); |
| 243 emit layoutChanged(); | 253 emit layoutChanged(); |
| 244 } | 254 } |
| 245 | 255 |
| 246 void | |
| 247 ModelDataTableModel::modelAboutToBeDeleted() | |
| 248 { | |
| 249 m_model = nullptr; | |
| 250 emit modelRemoved(); | |
| 251 } | |
| 252 | |
| 253 int | 256 int |
| 254 ModelDataTableModel::getSorted(int row) const | 257 ModelDataTableModel::getSorted(int row) const |
| 255 { | 258 { |
| 256 if (!m_model) return row; | 259 auto model = getTabularModel(); |
| 257 | 260 if (!model) return row; |
| 258 if (m_model->isColumnTimeValue(m_sortColumn)) { | 261 |
| 262 if (model->isColumnTimeValue(m_sortColumn)) { | |
| 259 if (m_sortOrdering == Qt::AscendingOrder) { | 263 if (m_sortOrdering == Qt::AscendingOrder) { |
| 260 return row; | 264 return row; |
| 261 } else { | 265 } else { |
| 262 return rowCount() - row - 1; | 266 return rowCount() - row - 1; |
| 263 } | 267 } |
| 278 } | 282 } |
| 279 | 283 |
| 280 int | 284 int |
| 281 ModelDataTableModel::getUnsorted(int row) const | 285 ModelDataTableModel::getUnsorted(int row) const |
| 282 { | 286 { |
| 283 if (!m_model) return row; | 287 auto model = getTabularModel(); |
| 284 | 288 if (!model) return row; |
| 285 if (m_model->isColumnTimeValue(m_sortColumn)) { | 289 |
| 290 if (model->isColumnTimeValue(m_sortColumn)) { | |
| 286 if (m_sortOrdering == Qt::AscendingOrder) { | 291 if (m_sortOrdering == Qt::AscendingOrder) { |
| 287 return row; | 292 return row; |
| 288 } else { | 293 } else { |
| 289 return rowCount() - row - 1; | 294 return rowCount() - row - 1; |
| 290 } | 295 } |
| 307 } | 312 } |
| 308 | 313 |
| 309 void | 314 void |
| 310 ModelDataTableModel::resort() const | 315 ModelDataTableModel::resort() const |
| 311 { | 316 { |
| 312 if (!m_model) return; | 317 auto model = getTabularModel(); |
| 313 | 318 if (!model) return; |
| 314 bool numeric = (m_model->getSortType(m_sortColumn) == | 319 |
| 320 bool numeric = (model->getSortType(m_sortColumn) == | |
| 315 TabularModel::SortNumeric); | 321 TabularModel::SortNumeric); |
| 316 | 322 |
| 317 // cerr << "resort: numeric == " << numeric << endl; | 323 // cerr << "resort: numeric == " << numeric << endl; |
| 318 | 324 |
| 319 m_sort.clear(); | 325 m_sort.clear(); |
| 340 } | 346 } |
| 341 | 347 |
| 342 void | 348 void |
| 343 ModelDataTableModel::resortNumeric() const | 349 ModelDataTableModel::resortNumeric() const |
| 344 { | 350 { |
| 345 if (!m_model) return; | 351 auto model = getTabularModel(); |
| 352 if (!model) return; | |
| 346 | 353 |
| 347 typedef std::multimap<double, int> MapType; | 354 typedef std::multimap<double, int> MapType; |
| 348 | 355 |
| 349 MapType rowMap; | 356 MapType rowMap; |
| 350 int rows = m_model->getRowCount(); | 357 int rows = model->getRowCount(); |
| 351 | 358 |
| 352 for (int i = 0; i < rows; ++i) { | 359 for (int i = 0; i < rows; ++i) { |
| 353 QVariant value = m_model->getData(i, m_sortColumn, TabularModel::SortRole); | 360 QVariant value = model->getData(i, m_sortColumn, TabularModel::SortRole); |
| 354 rowMap.insert(MapType::value_type(value.toDouble(), i)); | 361 rowMap.insert(MapType::value_type(value.toDouble(), i)); |
| 355 } | 362 } |
| 356 | 363 |
| 357 for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) { | 364 for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) { |
| 358 // cerr << "resortNumeric: " << i->second << ": " << i->first << endl; | 365 // cerr << "resortNumeric: " << i->second << ": " << i->first << endl; |
| 363 } | 370 } |
| 364 | 371 |
| 365 void | 372 void |
| 366 ModelDataTableModel::resortAlphabetical() const | 373 ModelDataTableModel::resortAlphabetical() const |
| 367 { | 374 { |
| 368 if (!m_model) return; | 375 auto model = getTabularModel(); |
| 376 if (!model) return; | |
| 369 | 377 |
| 370 typedef std::multimap<QString, int> MapType; | 378 typedef std::multimap<QString, int> MapType; |
| 371 | 379 |
| 372 MapType rowMap; | 380 MapType rowMap; |
| 373 int rows = m_model->getRowCount(); | 381 int rows = model->getRowCount(); |
| 374 | 382 |
| 375 for (int i = 0; i < rows; ++i) { | 383 for (int i = 0; i < rows; ++i) { |
| 376 QVariant value = | 384 QVariant value = |
| 377 m_model->getData(i, m_sortColumn, TabularModel::SortRole); | 385 model->getData(i, m_sortColumn, TabularModel::SortRole); |
| 378 rowMap.insert(MapType::value_type(value.toString(), i)); | 386 rowMap.insert(MapType::value_type(value.toString(), i)); |
| 379 } | 387 } |
| 380 | 388 |
| 381 for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) { | 389 for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) { |
| 382 // cerr << "resortAlphabetical: " << i->second << ": " << i->first << endl; | 390 // cerr << "resortAlphabetical: " << i->second << ": " << i->first << endl; |
