Skip to content

Commit

Permalink
Properly update cached tags when changing them
Browse files Browse the repository at this point in the history
The cache ended up being left stale, so tag assignments wouldn't
actually end up being reflected in the UI.
  • Loading branch information
askmeaboutlo0m committed Oct 14, 2024
1 parent 34b0e89 commit 019572c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
29 changes: 24 additions & 5 deletions src/libclient/brushes/brushpresetmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct CachedTag {
};

struct CachedPreset : Preset {
QVector<int> tagIds;
QSet<int> tagIds;
};

struct PresetChange {
Expand Down Expand Up @@ -755,7 +755,7 @@ class BrushPresetTagModel::Private {
return s.isNull() ? QStringLiteral("") : s;
}

static void parseGroupedTagIds(const QString &input, QVector<int> &tagIds)
static void parseGroupedTagIds(const QString &input, QSet<int> &tagIds)
{
QStringList tagIdStrings =
input.split(QChar(','), compat::SkipEmptyParts);
Expand All @@ -764,7 +764,7 @@ class BrushPresetTagModel::Private {
bool ok;
int tagId = tagIdString.toInt(&ok);
if(ok && tagId > 0) {
tagIds.append(tagId);
tagIds.insert(tagId);
}
}
}
Expand Down Expand Up @@ -1136,7 +1136,7 @@ class BrushPresetTagModel::Private {
int m_tagIdToFilter = -1;
};

bool Tag::accepts(const QVector<int> &tagIds) const
bool Tag::accepts(const QSet<int> &tagIds) const
{
switch(id) {
case ALL_ID:
Expand Down Expand Up @@ -2209,7 +2209,7 @@ QList<TagAssignment> BrushPresetModel::getTagAssignments(int presetId)
if(i == -1) {
return d->readTagAssignmentsByPresetId(presetId);
} else {
const QVector<int> assignedTagIds = d->getCachedPreset(i).tagIds;
const QSet<int> assignedTagIds = d->getCachedPreset(i).tagIds;
QList<TagAssignment> tagAssignments;
int tagCount = d->tagCacheSize();
for(int j = 0; j < tagCount; ++j) {
Expand Down Expand Up @@ -2255,6 +2255,16 @@ bool BrushPresetModel::changeTagAssignment(
int tagIdToFilter = d->tagIdToFilter();
switch(tagIdToFilter) {
case ALL_ID:
if(int i = d->getCachedPresetIndexById(presetId); i != -1) {
CachedPreset &cp = d->getCachedPreset(i);
if(assigned) {
cp.tagIds.insert(tagId);
} else {
cp.tagIds.remove(tagId);
}
QModelIndex idx = index(i);
emit dataChanged(idx, idx);
}
break;
case UNTAGGED_ID:
if(assigned) {
Expand All @@ -2277,6 +2287,15 @@ bool BrushPresetModel::changeTagAssignment(
beginRemoveRows(QModelIndex(), i, i);
d->removeCachedPreset(i);
endRemoveRows();
} else {
CachedPreset &cp = d->getCachedPreset(i);
if(assigned) {
cp.tagIds.insert(tagId);
} else {
cp.tagIds.remove(tagId);
}
QModelIndex idx = index(i);
emit dataChanged(idx, idx);
}
}
break;
Expand Down
4 changes: 2 additions & 2 deletions src/libclient/brushes/brushpresetmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Tag {

bool isAssignable() const { return id > 0; }
bool isEditable() const { return id > 0; }
bool accepts(const QVector<int> &tagIds) const;
bool accepts(const QSet<int> &tagIds) const;
};

struct TagAssignment {
Expand Down Expand Up @@ -83,7 +83,7 @@ struct ShortcutPreset {
QString name;
QPixmap thumbnail;
QKeySequence shortcut;
QVector<int> tagIds;
QSet<int> tagIds;
};

struct PresetMetadata {
Expand Down
2 changes: 1 addition & 1 deletion src/libclient/utils/brushshortcutmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const QKeySequence &BrushShortcutModel::shortcutAt(int row)
return m_presets[row].shortcut;
}

const QVector<int> &BrushShortcutModel::tagIdsAt(int row)
const QSet<int> &BrushShortcutModel::tagIdsAt(int row)
{
Q_ASSERT(row >= 0);
Q_ASSERT(row < m_presets.size());
Expand Down
2 changes: 1 addition & 1 deletion src/libclient/utils/brushshortcutmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BrushShortcutModel final : public QAbstractTableModel {
Qt::ItemFlags flags(const QModelIndex &index) const override;

const QKeySequence &shortcutAt(int row);
const QVector<int> &tagIdsAt(int row);
const QSet<int> &tagIdsAt(int row);

QModelIndex indexById(int presetId, int column = 0) const;

Expand Down

0 comments on commit 019572c

Please sign in to comment.