1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 16:53:54 +00:00
openmw/apps/opencs/model/world/nestedtableproxymodel.cpp

188 lines
5.8 KiB
C++
Raw Normal View History

2015-03-30 05:41:55 +00:00
#include "nestedtableproxymodel.hpp"
#include "idtree.hpp"
2022-10-19 17:02:00 +00:00
2022-09-22 18:26:05 +00:00
#include <cassert>
2022-10-19 17:02:00 +00:00
#include <apps/opencs/model/world/columnbase.hpp>
2022-09-22 18:26:05 +00:00
CSMWorld::NestedTableProxyModel::NestedTableProxyModel(
const QModelIndex& parent, ColumnBase::Display columnId, CSMWorld::IdTree* parentModel)
: mParentColumn(parent.column())
, mMainModel(parentModel)
{
const int parentRow = parent.row();
mId = std::string(parentModel->index(parentRow, 0).data().toString().toUtf8());
QAbstractProxyModel::setSourceModel(parentModel);
2015-03-30 05:41:55 +00:00
2022-09-22 18:26:05 +00:00
connect(mMainModel, &IdTree::rowsAboutToBeInserted, this, &NestedTableProxyModel::forwardRowsAboutToInserted);
2015-03-30 05:41:55 +00:00
2022-09-22 18:26:05 +00:00
connect(mMainModel, &IdTree::rowsInserted, this, &NestedTableProxyModel::forwardRowsInserted);
2014-07-02 11:13:03 +00:00
2022-09-22 18:26:05 +00:00
connect(mMainModel, &IdTree::rowsAboutToBeRemoved, this, &NestedTableProxyModel::forwardRowsAboutToRemoved);
2014-07-02 11:29:25 +00:00
2022-09-22 18:26:05 +00:00
connect(mMainModel, &IdTree::rowsRemoved, this, &NestedTableProxyModel::forwardRowsRemoved);
2015-03-30 05:41:55 +00:00
2022-09-22 18:26:05 +00:00
connect(mMainModel, &IdTree::resetStart, this, &NestedTableProxyModel::forwardResetStart);
2014-07-20 20:39:39 +00:00
2022-09-22 18:26:05 +00:00
connect(mMainModel, &IdTree::resetEnd, this, &NestedTableProxyModel::forwardResetEnd);
2022-09-22 18:26:05 +00:00
connect(mMainModel, &IdTree::dataChanged, this, &NestedTableProxyModel::forwardDataChanged);
}
2015-03-30 05:41:55 +00:00
QModelIndex CSMWorld::NestedTableProxyModel::mapFromSource(const QModelIndex& sourceIndex) const
{
const QModelIndex& testedParent = mMainModel->parent(sourceIndex);
2022-09-22 18:26:05 +00:00
const QModelIndex& parent = mMainModel->getNestedModelIndex(mId, mParentColumn);
if (testedParent == parent)
{
return createIndex(sourceIndex.row(), sourceIndex.column());
}
2014-06-18 14:53:46 +00:00
else
{
return QModelIndex();
}
}
2015-03-30 05:41:55 +00:00
QModelIndex CSMWorld::NestedTableProxyModel::mapToSource(const QModelIndex& proxyIndex) const
{
2022-09-22 18:26:05 +00:00
const QModelIndex& parent = mMainModel->getNestedModelIndex(mId, mParentColumn);
return mMainModel->index(proxyIndex.row(), proxyIndex.column(), parent);
}
2014-06-18 14:53:46 +00:00
2015-03-30 05:41:55 +00:00
int CSMWorld::NestedTableProxyModel::rowCount(const QModelIndex& index) const
2014-06-18 14:53:46 +00:00
{
2022-09-22 18:26:05 +00:00
assert(!index.isValid());
2014-06-18 14:53:46 +00:00
return mMainModel->rowCount(mMainModel->getModelIndex(mId, mParentColumn));
2014-06-18 14:53:46 +00:00
}
2015-03-30 05:41:55 +00:00
int CSMWorld::NestedTableProxyModel::columnCount(const QModelIndex& parent) const
2014-06-18 14:53:46 +00:00
{
2022-09-22 18:26:05 +00:00
assert(!parent.isValid());
2014-06-18 14:53:46 +00:00
return mMainModel->columnCount(mMainModel->getModelIndex(mId, mParentColumn));
2014-06-18 14:53:46 +00:00
}
2015-03-30 05:41:55 +00:00
QModelIndex CSMWorld::NestedTableProxyModel::index(int row, int column, const QModelIndex& parent) const
2014-06-18 14:53:46 +00:00
{
2022-09-22 18:26:05 +00:00
assert(!parent.isValid());
2014-06-18 14:53:46 +00:00
2015-07-25 14:57:40 +00:00
int numRows = rowCount(parent);
int numColumns = columnCount(parent);
2014-06-18 14:53:46 +00:00
2015-07-25 14:57:40 +00:00
if (row < 0 || row >= numRows || column < 0 || column >= numColumns)
2014-06-18 14:53:46 +00:00
return QModelIndex();
return createIndex(row, column);
}
2015-03-30 05:41:55 +00:00
QModelIndex CSMWorld::NestedTableProxyModel::parent(const QModelIndex& index) const
2014-06-18 14:53:46 +00:00
{
return QModelIndex();
}
2022-09-22 18:26:05 +00:00
QVariant CSMWorld::NestedTableProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return mMainModel->nestedHeaderData(mParentColumn, section, orientation, role);
}
2014-06-30 12:12:57 +00:00
QVariant CSMWorld::NestedTableProxyModel::data(const QModelIndex& index, int role) const
{
return mMainModel->data(mapToSource(index), role);
}
2014-06-30 12:12:57 +00:00
// NOTE: Due to mapToSouce(index) the dataChanged() signal resulting from setData() will have the
// source model's index values. The indicies need to be converted to the proxy space values.
// See forwardDataChanged()
2022-09-22 18:26:05 +00:00
bool CSMWorld::NestedTableProxyModel::setData(const QModelIndex& index, const QVariant& value, int role)
2014-06-30 12:12:57 +00:00
{
return mMainModel->setData(mapToSource(index), value, role);
}
2015-03-30 05:41:55 +00:00
Qt::ItemFlags CSMWorld::NestedTableProxyModel::flags(const QModelIndex& index) const
2014-06-30 12:12:57 +00:00
{
return mMainModel->flags(mapToSource(index));
2014-06-30 12:12:57 +00:00
}
2014-07-01 18:52:27 +00:00
2015-03-30 05:41:55 +00:00
std::string CSMWorld::NestedTableProxyModel::getParentId() const
2014-07-01 18:52:27 +00:00
{
return mId;
}
2015-03-30 05:41:55 +00:00
int CSMWorld::NestedTableProxyModel::getParentColumn() const
2014-07-01 18:52:27 +00:00
{
return mParentColumn;
}
CSMWorld::IdTree* CSMWorld::NestedTableProxyModel::model() const
2014-07-01 18:52:27 +00:00
{
return mMainModel;
}
2014-07-02 11:13:03 +00:00
2022-09-22 18:26:05 +00:00
void CSMWorld::NestedTableProxyModel::forwardRowsAboutToInserted(const QModelIndex& parent, int first, int last)
2014-07-02 11:13:03 +00:00
{
if (indexIsParent(parent))
{
beginInsertRows(QModelIndex(), first, last);
}
}
2015-03-30 05:41:55 +00:00
void CSMWorld::NestedTableProxyModel::forwardRowsInserted(const QModelIndex& parent, int first, int last)
2014-07-02 11:13:03 +00:00
{
if (indexIsParent(parent))
{
endInsertRows();
}
}
2015-03-30 05:41:55 +00:00
bool CSMWorld::NestedTableProxyModel::indexIsParent(const QModelIndex& index)
2014-07-02 11:13:03 +00:00
{
2022-09-22 18:26:05 +00:00
return (index.isValid() && index.column() == mParentColumn
&& mMainModel->data(mMainModel->index(index.row(), 0)).toString().toUtf8().constData() == mId);
2014-07-02 11:13:03 +00:00
}
2014-07-02 11:29:25 +00:00
2022-09-22 18:26:05 +00:00
void CSMWorld::NestedTableProxyModel::forwardRowsAboutToRemoved(const QModelIndex& parent, int first, int last)
2014-07-02 11:29:25 +00:00
{
if (indexIsParent(parent))
{
beginRemoveRows(QModelIndex(), first, last);
}
}
2015-03-30 05:41:55 +00:00
void CSMWorld::NestedTableProxyModel::forwardRowsRemoved(const QModelIndex& parent, int first, int last)
2014-07-02 11:29:25 +00:00
{
if (indexIsParent(parent))
{
endRemoveRows();
}
}
2014-07-20 20:39:39 +00:00
2015-03-30 05:41:55 +00:00
void CSMWorld::NestedTableProxyModel::forwardResetStart(const QString& id)
2014-07-20 20:39:39 +00:00
{
if (id.toUtf8() == mId.c_str())
beginResetModel();
}
2015-03-30 05:41:55 +00:00
void CSMWorld::NestedTableProxyModel::forwardResetEnd(const QString& id)
2014-07-20 20:39:39 +00:00
{
if (id.toUtf8() == mId.c_str())
endResetModel();
}
2022-09-22 18:26:05 +00:00
void CSMWorld::NestedTableProxyModel::forwardDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
2022-09-22 18:26:05 +00:00
const QModelIndex& parent = mMainModel->getNestedModelIndex(mId, mParentColumn);
if (topLeft.column() <= parent.column() && bottomRight.column() >= parent.column())
{
2022-09-22 18:26:05 +00:00
emit dataChanged(index(0, 0), index(mMainModel->rowCount(parent) - 1, mMainModel->columnCount(parent) - 1));
}
else if (topLeft.parent() == parent && bottomRight.parent() == parent)
{
emit dataChanged(index(topLeft.row(), topLeft.column()), index(bottomRight.row(), bottomRight.column()));
}
}