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

119 lines
3.7 KiB
C++
Raw Normal View History

#include "infotableproxymodel.hpp"
2022-10-19 17:02:00 +00:00
#include <QModelIndex>
#include <QObject>
#include <QString>
#include <apps/opencs/model/world/idtableproxymodel.hpp>
#include <apps/opencs/model/world/universalid.hpp>
#include <components/misc/strings/lower.hpp>
2022-10-19 17:02:00 +00:00
#include <string>
#include "columns.hpp"
2022-09-22 18:26:05 +00:00
#include "idtablebase.hpp"
namespace
{
2022-09-22 18:26:05 +00:00
QString toLower(const QString& str)
{
return QString::fromUtf8(Misc::StringUtils::lowerCase(str.toUtf8().constData()).c_str());
}
}
2022-09-22 18:26:05 +00:00
CSMWorld::InfoTableProxyModel::InfoTableProxyModel(CSMWorld::UniversalId::Type type, QObject* parent)
: IdTableProxyModel(parent)
, mType(type)
, mInfoColumnId(type == UniversalId::Type_TopicInfos ? Columns::ColumnId_Topic : Columns::ColumnId_Journal)
, mInfoColumnIndex(-1)
, mLastAddedSourceRow(-1)
{
Q_ASSERT(type == UniversalId::Type_TopicInfos || type == UniversalId::Type_JournalInfos);
}
2022-09-22 18:26:05 +00:00
void CSMWorld::InfoTableProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
{
IdTableProxyModel::setSourceModel(sourceModel);
2018-10-09 06:21:12 +00:00
if (mSourceModel != nullptr)
{
mInfoColumnIndex = mSourceModel->findColumnIndex(mInfoColumnId);
mFirstRowCache.clear();
}
}
2022-09-22 18:26:05 +00:00
bool CSMWorld::InfoTableProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
{
2018-10-09 06:21:12 +00:00
Q_ASSERT(mSourceModel != nullptr);
QModelIndex first = mSourceModel->index(getFirstInfoRow(left.row()), left.column());
QModelIndex second = mSourceModel->index(getFirstInfoRow(right.row()), right.column());
// If both indexes are belonged to the same Topic/Journal, compare their original rows only
if (first.row() == second.row())
{
return sortOrder() == Qt::AscendingOrder ? left.row() < right.row() : right.row() < left.row();
}
return IdTableProxyModel::lessThan(first, second);
}
int CSMWorld::InfoTableProxyModel::getFirstInfoRow(int currentRow) const
{
2018-10-09 06:21:12 +00:00
Q_ASSERT(mSourceModel != nullptr);
int row = currentRow;
int column = mInfoColumnIndex;
QString info = toLower(mSourceModel->data(mSourceModel->index(row, column)).toString());
if (mFirstRowCache.contains(info))
{
return mFirstRowCache[info];
}
2022-09-22 18:26:05 +00:00
while (--row >= 0 && toLower(mSourceModel->data(mSourceModel->index(row, column)).toString()) == info)
;
++row;
mFirstRowCache[info] = row;
return row;
}
2022-09-22 18:26:05 +00:00
void CSMWorld::InfoTableProxyModel::sourceRowsRemoved(const QModelIndex& /*parent*/, int /*start*/, int /*end*/)
{
refreshFilter();
mFirstRowCache.clear();
}
2022-09-22 18:26:05 +00:00
void CSMWorld::InfoTableProxyModel::sourceRowsInserted(const QModelIndex& parent, int /*start*/, int end)
{
refreshFilter();
if (!parent.isValid())
{
mFirstRowCache.clear();
// We can't re-sort the model here, because the topic of the added row isn't set yet.
// Store the row index for using in the first dataChanged() after this row insertion.
mLastAddedSourceRow = end;
}
}
2022-09-22 18:26:05 +00:00
void CSMWorld::InfoTableProxyModel::sourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
refreshFilter();
2022-09-22 18:26:05 +00:00
if (mLastAddedSourceRow != -1 && topLeft.row() <= mLastAddedSourceRow && bottomRight.row() >= mLastAddedSourceRow)
{
2022-09-22 18:26:05 +00:00
// Now the topic of the last added row is set,
// so we can re-sort the model to ensure the corrent position of this row
int column = sortColumn();
Qt::SortOrder order = sortOrder();
sort(mInfoColumnIndex); // Restore the correct position of an added row
2022-09-22 18:26:05 +00:00
sort(column, order); // Restore the original sort order
emit rowAdded(getRecordId(mLastAddedSourceRow).toUtf8().constData());
// Make sure that we perform a re-sorting only in the first dataChanged() after a row insertion
mLastAddedSourceRow = -1;
}
}