Mercurial > hg > svcore
changeset 1748:91a194e2d80b by-id
Update to ModelId
author | Chris Cannam |
---|---|
date | Wed, 03 Jul 2019 14:20:19 +0100 |
parents | 498b426191e5 |
children | 356be36b2391 |
files | data/model/ModelDataTableModel.cpp data/model/ModelDataTableModel.h |
diffstat | 2 files changed, 64 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- a/data/model/ModelDataTableModel.cpp Tue Jul 02 15:28:51 2019 +0100 +++ b/data/model/ModelDataTableModel.cpp Wed Jul 03 14:20:19 2019 +0100 @@ -22,19 +22,18 @@ #include <algorithm> #include <iostream> -ModelDataTableModel::ModelDataTableModel(TabularModel *m) : +ModelDataTableModel::ModelDataTableModel(ModelId m) : m_model(m), m_sortColumn(0), m_sortOrdering(Qt::AscendingOrder), m_currentRow(0) { - Model *baseModel = dynamic_cast<Model *>(m); - - connect(baseModel, SIGNAL(modelChanged()), this, SLOT(modelChanged())); - connect(baseModel, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)), - this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t))); - connect(baseModel, SIGNAL(aboutToBeDeleted()), - this, SLOT(modelAboutToBeDeleted())); + auto model = ModelById::get(m); + if (model) { + connect(model.get(), SIGNAL(modelChanged()), this, SLOT(modelChanged())); + connect(model.get(), SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)), + this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t))); + } } ModelDataTableModel::~ModelDataTableModel() @@ -44,19 +43,21 @@ QVariant ModelDataTableModel::data(const QModelIndex &index, int role) const { - if (!m_model) return QVariant(); + auto model = getTabularModel(); + if (!model) return QVariant(); if (role != Qt::EditRole && role != Qt::DisplayRole) return QVariant(); if (!index.isValid()) return QVariant(); - QVariant d = m_model->getData(getUnsorted(index.row()), index.column(), role); + QVariant d = model->getData(getUnsorted(index.row()), index.column(), role); return d; } bool ModelDataTableModel::setData(const QModelIndex &index, const QVariant &value, int role) { - if (!m_model) return false; + auto model = getTabularModel(); + if (!model) return false; if (!index.isValid()) return false; - Command *command = m_model->getSetDataCommand(getUnsorted(index.row()), + Command *command = model->getSetDataCommand(getUnsorted(index.row()), index.column(), value, role); if (command) { @@ -70,10 +71,11 @@ bool ModelDataTableModel::insertRow(int row, const QModelIndex &parent) { - if (!m_model) return false; + auto model = getTabularModel(); + if (!model) return false; if (parent.isValid()) return false; - Command *command = m_model->getInsertRowCommand(getUnsorted(row)); + Command *command = model->getInsertRowCommand(getUnsorted(row)); if (command) { emit addCommand(command); @@ -85,10 +87,11 @@ bool ModelDataTableModel::removeRow(int row, const QModelIndex &parent) { - if (!m_model) return false; + auto model = getTabularModel(); + if (!model) return false; if (parent.isValid()) return false; - Command *command = m_model->getRemoveRowCommand(getUnsorted(row)); + Command *command = model->getRemoveRowCommand(getUnsorted(row)); if (command) { emit addCommand(command); @@ -108,13 +111,14 @@ QVariant ModelDataTableModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (!m_model) return QVariant(); + auto model = getTabularModel(); + if (!model) return QVariant(); if (orientation == Qt::Vertical && role == Qt::DisplayRole) { return section + 1; } if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { - return m_model->getHeading(section); + return model->getHeading(section); } return QVariant(); } @@ -134,38 +138,44 @@ int ModelDataTableModel::rowCount(const QModelIndex &parent) const { - if (!m_model) return 0; + auto model = getTabularModel(); + if (!model) return 0; if (parent.isValid()) return 0; - int count = m_model->getRowCount(); + int count = model->getRowCount(); return count; } int ModelDataTableModel::columnCount(const QModelIndex &parent) const { - if (!m_model) return 0; + auto model = getTabularModel(); + if (!model) return 0; if (parent.isValid()) return 0; - return m_model->getColumnCount(); + return model->getColumnCount(); } QModelIndex ModelDataTableModel::getModelIndexForFrame(sv_frame_t frame) const { - if (!m_model) return createIndex(0, 0); - int row = m_model->getRowForFrame(frame); + auto model = getTabularModel(); + if (!model) return createIndex(0, 0); + int row = model->getRowForFrame(frame); return createIndex(getSorted(row), 0, (void *)nullptr); } sv_frame_t ModelDataTableModel::getFrameForModelIndex(const QModelIndex &index) const { - if (!m_model) return 0; - return m_model->getFrameForRow(getUnsorted(index.row())); + auto model = getTabularModel(); + if (!model) return 0; + return model->getFrameForRow(getUnsorted(index.row())); } QModelIndex ModelDataTableModel::findText(QString text) const { + auto model = getTabularModel(); + if (!model) return QModelIndex(); if (text == "") return QModelIndex(); int rows = rowCount(); int cols = columnCount(); @@ -173,10 +183,10 @@ for (int row = 1; row <= rows; ++row) { int wrapped = (row + current) % rows; for (int col = 0; col < cols; ++col) { - if (m_model->getSortType(col) != TabularModel::SortAlphabetical) { + if (model->getSortType(col) != TabularModel::SortAlphabetical) { continue; } - QString cell = m_model->getData(getUnsorted(wrapped), col, + QString cell = model->getData(getUnsorted(wrapped), col, Qt::DisplayRole).toString(); if (cell.contains(text, Qt::CaseInsensitive)) { return createIndex(wrapped, col); @@ -243,19 +253,13 @@ emit layoutChanged(); } -void -ModelDataTableModel::modelAboutToBeDeleted() -{ - m_model = nullptr; - emit modelRemoved(); -} - int ModelDataTableModel::getSorted(int row) const { - if (!m_model) return row; + auto model = getTabularModel(); + if (!model) return row; - if (m_model->isColumnTimeValue(m_sortColumn)) { + if (model->isColumnTimeValue(m_sortColumn)) { if (m_sortOrdering == Qt::AscendingOrder) { return row; } else { @@ -280,9 +284,10 @@ int ModelDataTableModel::getUnsorted(int row) const { - if (!m_model) return row; + auto model = getTabularModel(); + if (!model) return row; - if (m_model->isColumnTimeValue(m_sortColumn)) { + if (model->isColumnTimeValue(m_sortColumn)) { if (m_sortOrdering == Qt::AscendingOrder) { return row; } else { @@ -309,9 +314,10 @@ void ModelDataTableModel::resort() const { - if (!m_model) return; + auto model = getTabularModel(); + if (!model) return; - bool numeric = (m_model->getSortType(m_sortColumn) == + bool numeric = (model->getSortType(m_sortColumn) == TabularModel::SortNumeric); // cerr << "resort: numeric == " << numeric << endl; @@ -342,15 +348,16 @@ void ModelDataTableModel::resortNumeric() const { - if (!m_model) return; + auto model = getTabularModel(); + if (!model) return; typedef std::multimap<double, int> MapType; MapType rowMap; - int rows = m_model->getRowCount(); + int rows = model->getRowCount(); for (int i = 0; i < rows; ++i) { - QVariant value = m_model->getData(i, m_sortColumn, TabularModel::SortRole); + QVariant value = model->getData(i, m_sortColumn, TabularModel::SortRole); rowMap.insert(MapType::value_type(value.toDouble(), i)); } @@ -365,16 +372,17 @@ void ModelDataTableModel::resortAlphabetical() const { - if (!m_model) return; + auto model = getTabularModel(); + if (!model) return; typedef std::multimap<QString, int> MapType; MapType rowMap; - int rows = m_model->getRowCount(); + int rows = model->getRowCount(); for (int i = 0; i < rows; ++i) { QVariant value = - m_model->getData(i, m_sortColumn, TabularModel::SortRole); + model->getData(i, m_sortColumn, TabularModel::SortRole); rowMap.insert(MapType::value_type(value.toString(), i)); }
--- a/data/model/ModelDataTableModel.h Tue Jul 02 15:28:51 2019 +0100 +++ b/data/model/ModelDataTableModel.h Wed Jul 03 14:20:19 2019 +0100 @@ -22,6 +22,9 @@ #include "base/BaseTypes.h" +#include "TabularModel.h" +#include "Model.h" + class TabularModel; class Command; @@ -30,7 +33,7 @@ Q_OBJECT public: - ModelDataTableModel(TabularModel *m); + ModelDataTableModel(ModelId modelId); // a TabularModel virtual ~ModelDataTableModel(); QVariant data(const QModelIndex &index, int role) const override; @@ -75,7 +78,11 @@ void modelAboutToBeDeleted(); protected: - TabularModel *m_model; + std::shared_ptr<TabularModel> getTabularModel() const { + return ModelById::getAs<TabularModel>(m_model); + } + + ModelId m_model; int m_sortColumn; Qt::SortOrder m_sortOrdering; int m_currentRow;