mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-21 07:23:54 +00:00
Inform about a row addition after re-sorting in InfoTableProxyModel
This commit is contained in:
parent
37fd733deb
commit
86b7d2a43d
2 changed files with 50 additions and 18 deletions
|
@ -9,16 +9,17 @@ namespace
|
|||
{
|
||||
QString toLower(const QString &str)
|
||||
{
|
||||
return QString::fromUtf8(Misc::StringUtils::lowerCase(str.toStdString()).c_str());
|
||||
return QString::fromUtf8(Misc::StringUtils::lowerCase(str.toUtf8().constData()).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
CSMWorld::InfoTableProxyModel::InfoTableProxyModel(CSMWorld::UniversalId::Type type, QObject *parent)
|
||||
: IdTableProxyModel(parent),
|
||||
mType(type),
|
||||
mSourceModel(NULL),
|
||||
mInfoColumnId(type == UniversalId::Type_TopicInfos ? Columns::ColumnId_Topic :
|
||||
Columns::ColumnId_Journal)
|
||||
Columns::ColumnId_Journal),
|
||||
mInfoColumnIndex(-1),
|
||||
mLastAddedSourceRow(-1)
|
||||
{
|
||||
Q_ASSERT(type == UniversalId::Type_TopicInfos || type == UniversalId::Type_JournalInfos);
|
||||
}
|
||||
|
@ -26,23 +27,18 @@ CSMWorld::InfoTableProxyModel::InfoTableProxyModel(CSMWorld::UniversalId::Type t
|
|||
void CSMWorld::InfoTableProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
|
||||
{
|
||||
IdTableProxyModel::setSourceModel(sourceModel);
|
||||
mSourceModel = dynamic_cast<IdTableBase *>(sourceModel);
|
||||
|
||||
if (mSourceModel != NULL)
|
||||
{
|
||||
connect(mSourceModel,
|
||||
SIGNAL(rowsInserted(const QModelIndex &, int, int)),
|
||||
this,
|
||||
SLOT(modelRowsChanged(const QModelIndex &, int, int)));
|
||||
connect(mSourceModel,
|
||||
SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
|
||||
this,
|
||||
SLOT(modelRowsChanged(const QModelIndex &, int, int)));
|
||||
mInfoColumnIndex = mSourceModel->findColumnIndex(mInfoColumnId);
|
||||
mFirstRowCache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
bool CSMWorld::InfoTableProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
||||
{
|
||||
Q_ASSERT(mSourceModel != NULL);
|
||||
|
||||
QModelIndex first = mSourceModel->index(getFirstInfoRow(left.row()), left.column());
|
||||
QModelIndex second = mSourceModel->index(getFirstInfoRow(right.row()), right.column());
|
||||
|
||||
|
@ -56,8 +52,10 @@ bool CSMWorld::InfoTableProxyModel::lessThan(const QModelIndex &left, const QMod
|
|||
|
||||
int CSMWorld::InfoTableProxyModel::getFirstInfoRow(int currentRow) const
|
||||
{
|
||||
Q_ASSERT(mSourceModel != NULL);
|
||||
|
||||
int row = currentRow;
|
||||
int column = mSourceModel->findColumnIndex(mInfoColumnId);
|
||||
int column = mInfoColumnIndex;
|
||||
QString info = toLower(mSourceModel->data(mSourceModel->index(row, column)).toString());
|
||||
|
||||
if (mFirstRowCache.contains(info))
|
||||
|
@ -73,7 +71,37 @@ int CSMWorld::InfoTableProxyModel::getFirstInfoRow(int currentRow) const
|
|||
return row;
|
||||
}
|
||||
|
||||
void CSMWorld::InfoTableProxyModel::modelRowsChanged(const QModelIndex &/*parent*/, int /*start*/, int /*end*/)
|
||||
void CSMWorld::InfoTableProxyModel::sourceRowsRemoved(const QModelIndex &/*parent*/, int /*start*/, int /*end*/)
|
||||
{
|
||||
refreshFilter();
|
||||
mFirstRowCache.clear();
|
||||
}
|
||||
|
||||
void CSMWorld::InfoTableProxyModel::sourceRowsInserted(const QModelIndex &/*parent*/, int /*start*/, int end)
|
||||
{
|
||||
refreshFilter();
|
||||
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;
|
||||
}
|
||||
|
||||
void CSMWorld::InfoTableProxyModel::sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
|
||||
{
|
||||
refreshFilter();
|
||||
|
||||
if (mLastAddedSourceRow != -1 &&
|
||||
topLeft.row() <= mLastAddedSourceRow && bottomRight.row() >= mLastAddedSourceRow)
|
||||
{
|
||||
// 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
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,25 +16,29 @@ namespace CSMWorld
|
|||
Q_OBJECT
|
||||
|
||||
UniversalId::Type mType;
|
||||
IdTableBase *mSourceModel;
|
||||
Columns::ColumnId mInfoColumnId;
|
||||
///< Contains ID for Topic or Journal ID
|
||||
int mInfoColumnIndex;
|
||||
int mLastAddedSourceRow;
|
||||
|
||||
mutable QHash<QString, int> mFirstRowCache;
|
||||
|
||||
int getFirstInfoRow(int currentRow) const;
|
||||
///< Finds the first row with the same topic (journal entry) as in \a currentRow
|
||||
///< \a currentRow is a row of the source model.
|
||||
|
||||
public:
|
||||
InfoTableProxyModel(UniversalId::Type type, QObject *parent = 0);
|
||||
|
||||
void setSourceModel(QAbstractItemModel *sourceModel);
|
||||
virtual void setSourceModel(QAbstractItemModel *sourceModel);
|
||||
|
||||
protected:
|
||||
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
|
||||
|
||||
private slots:
|
||||
void modelRowsChanged(const QModelIndex &parent, int start, int end);
|
||||
protected slots:
|
||||
virtual void sourceRowsInserted(const QModelIndex &parent, int start, int end);
|
||||
virtual void sourceRowsRemoved(const QModelIndex &parent, int start, int end);
|
||||
virtual void sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue