fix(showcase): unbreak drag and drop reordering

- unify the signature of the method to `move(from, to, count)` so that
both ListModel and NIM have it the same
- realize the move operation using the proper `begin/endMoveRows`
instead of doing a full model reset
- simplify signaling `dataChanged()` for all model roles (nimqml now
follows the C++ impl)

Fixes #13329
This commit is contained in:
Lukáš Tinkl 2024-02-06 09:16:01 +01:00 committed by Lukáš Tinkl
parent ae16bd8b67
commit 9b85a302fc
5 changed files with 71 additions and 74 deletions

View File

@ -113,14 +113,7 @@ QtObject:
let index = self.createIndex(ind, 0, nil)
defer: index.delete
self.dataChanged(index, index, @[
ModelRole.ShowcaseVisibility.int,
ModelRole.Order.int,
ModelRole.Address.int,
ModelRole.Name.int,
ModelRole.Emoji.int,
ModelRole.ColorId.int,
])
self.dataChanged(index, index)
proc upsertItemJson(self: ProfileShowcaseAccountsModel, itemJson: string) {.slot.} =
self.upsertItemImpl(itemJson.parseJson.toProfileShowcaseAccountItem())
@ -165,16 +158,25 @@ QtObject:
if ind != -1:
self.remove(ind)
proc move*(self: ProfileShowcaseAccountsModel, fromIndex: int, toIndex: int) {.slot.} =
if fromIndex < 0 or fromIndex >= self.items.len:
proc move*(self: ProfileShowcaseAccountsModel, fromRow: int, toRow: int, dummyCount: int = 1) {.slot.} =
if fromRow < 0 or fromRow >= self.items.len:
return
self.beginResetModel()
let item = self.items[fromIndex]
self.items.delete(fromIndex)
self.items.insert(@[item], toIndex)
let sourceIndex = newQModelIndex()
defer: sourceIndex.delete
let destIndex = newQModelIndex()
defer: destIndex.delete
var destRow = toRow
if toRow > fromRow:
inc(destRow)
self.beginMoveRows(sourceIndex, fromRow, fromRow, destIndex, destRow)
let item = self.items[fromRow]
self.items.delete(fromRow)
self.items.insert(@[item], toRow)
self.recalcOrder()
self.endResetModel()
self.endMoveRows()
proc setVisibilityByIndex*(self: ProfileShowcaseAccountsModel, ind: int, visibility: int) {.slot.} =
if (visibility >= ord(low(ProfileShowcaseVisibility)) and

View File

@ -121,16 +121,7 @@ QtObject:
let index = self.createIndex(ind, 0, nil)
defer: index.delete
self.dataChanged(index, index, @[
ModelRole.ShowcaseVisibility.int,
ModelRole.Order.int,
ModelRole.Address.int,
ModelRole.CommunityId.int,
ModelRole.Symbol.int,
ModelRole.Name.int,
ModelRole.EnabledNetworkBalance.int,
ModelRole.Decimals.int,
])
self.dataChanged(index, index)
proc upsertItemJson(self: ProfileShowcaseAssetsModel, itemJson: string) {.slot.} =
self.upsertItemImpl(itemJson.parseJson.toProfileShowcaseAssetItem())
@ -175,16 +166,25 @@ QtObject:
if ind != -1:
self.remove(ind)
proc move*(self: ProfileShowcaseAssetsModel, fromIndex: int, toIndex: int) {.slot.} =
if fromIndex < 0 or fromIndex >= self.items.len:
proc move*(self: ProfileShowcaseAssetsModel, fromRow: int, toRow: int, dummyCount: int = 1) {.slot.} =
if fromRow < 0 or fromRow >= self.items.len:
return
self.beginResetModel()
let item = self.items[fromIndex]
self.items.delete(fromIndex)
self.items.insert(@[item], toIndex)
let sourceIndex = newQModelIndex()
defer: sourceIndex.delete
let destIndex = newQModelIndex()
defer: destIndex.delete
var destRow = toRow
if toRow > fromRow:
inc(destRow)
self.beginMoveRows(sourceIndex, fromRow, fromRow, destIndex, destRow)
let item = self.items[fromRow]
self.items.delete(fromRow)
self.items.insert(@[item], toRow)
self.recalcOrder()
self.endResetModel()
self.endMoveRows()
proc setVisibilityByIndex*(self: ProfileShowcaseAssetsModel, ind: int, visibility: int) {.slot.} =
if (visibility >= ord(low(ProfileShowcaseVisibility)) and

View File

@ -62,7 +62,7 @@ QtObject:
ModelRole.ImageUrl.int: "imageUrl",
ModelRole.BackgroundColor.int: "backgroundColor",
ModelRole.CollectionName.int: "collectionName",
ModelRole.IsLoading.int:"isLoading",
ModelRole.IsLoading.int: "isLoading",
ModelRole.CommunityId.int: "communityId",
ModelRole.ShowcaseVisibility.int: "showcaseVisibility",
@ -138,19 +138,7 @@ QtObject:
let index = self.createIndex(ind, 0, nil)
defer: index.delete
self.dataChanged(index, index, @[
ModelRole.ShowcaseVisibility.int,
ModelRole.Order.int,
ModelRole.ChainId.int,
ModelRole.TokenId.int,
ModelRole.ContractAddress.int,
ModelRole.CommunityId.int,
ModelRole.Name.int,
ModelRole.CollectionName.int,
ModelRole.ImageUrl.int,
ModelRole.BackgroundColor.int,
ModelRole.IsLoading.int
])
self.dataChanged(index, index)
proc upsertItemJson(self: ProfileShowcaseCollectiblesModel, itemJson: string) {.slot.} =
self.upsertItemImpl(itemJson.parseJson.toProfileShowcaseCollectibleItem())
@ -195,16 +183,25 @@ QtObject:
if ind != -1:
self.remove(ind)
proc move*(self: ProfileShowcaseCollectiblesModel, fromIndex: int, toIndex: int) {.slot.} =
if fromIndex < 0 or fromIndex >= self.items.len:
proc move*(self: ProfileShowcaseCollectiblesModel, fromRow: int, toRow: int, dummyCount: int = 1) {.slot.} =
if fromRow < 0 or fromRow >= self.items.len:
return
self.beginResetModel()
let item = self.items[fromIndex]
self.items.delete(fromIndex)
self.items.insert(@[item], toIndex)
let sourceIndex = newQModelIndex()
defer: sourceIndex.delete
let destIndex = newQModelIndex()
defer: destIndex.delete
var destRow = toRow
if toRow > fromRow:
inc(destRow)
self.beginMoveRows(sourceIndex, fromRow, fromRow, destIndex, destRow)
let item = self.items[fromRow]
self.items.delete(fromRow)
self.items.insert(@[item], toRow)
self.recalcOrder()
self.endResetModel()
self.endMoveRows()
proc setVisibilityByIndex*(self: ProfileShowcaseCollectiblesModel, ind: int, visibility: int) {.slot.} =
if (visibility >= ord(low(ProfileShowcaseVisibility)) and
@ -219,4 +216,4 @@ QtObject:
proc setVisibility*(self: ProfileShowcaseCollectiblesModel, uid: string, visibility: int) {.slot.} =
let index = self.findIndexForCollectible(uid)
if index != -1:
self.setVisibilityByIndex(index, visibility)
self.setVisibilityByIndex(index, visibility)

View File

@ -128,18 +128,7 @@ QtObject:
let index = self.createIndex(ind, 0, nil)
defer: index.delete
self.dataChanged(index, index, @[
ModelRole.ShowcaseVisibility.int,
ModelRole.Order.int,
ModelRole.Id.int,
ModelRole.Name.int,
ModelRole.MemberRole.int,
ModelRole.Image.int,
ModelRole.Color.int,
ModelRole.Description.int,
ModelRole.MembersCount.int,
ModelRole.Loading.int,
])
self.dataChanged(index, index)
proc upsertItemJson(self: ProfileShowcaseCommunitiesModel, itemJson: string) {.slot.} =
self.upsertItemImpl(itemJson.parseJson.toProfileShowcaseCommunityItem())
@ -184,16 +173,25 @@ QtObject:
if ind != -1:
self.remove(ind)
proc move*(self: ProfileShowcaseCommunitiesModel, fromIndex: int, toIndex: int) {.slot.} =
if fromIndex < 0 or fromIndex >= self.items.len:
proc move*(self: ProfileShowcaseCommunitiesModel, fromRow: int, toRow: int, dummyCount: int = 1) {.slot.} =
if fromRow < 0 or fromRow >= self.items.len:
return
self.beginResetModel()
let item = self.items[fromIndex]
self.items.delete(fromIndex)
self.items.insert(@[item], toIndex)
let sourceIndex = newQModelIndex()
defer: sourceIndex.delete
let destIndex = newQModelIndex()
defer: destIndex.delete
var destRow = toRow
if toRow > fromRow:
inc(destRow)
self.beginMoveRows(sourceIndex, fromRow, fromRow, destIndex, destRow)
let item = self.items[fromRow]
self.items.delete(fromRow)
self.items.insert(@[item], toRow)
self.recalcOrder()
self.endResetModel()
self.endMoveRows()
proc setVisibilityByIndex*(self: ProfileShowcaseCommunitiesModel, ind: int, visibility: int) {.slot.} =
if (visibility >= ord(low(ProfileShowcaseVisibility)) and

View File

@ -172,7 +172,7 @@ Control {
if (to === from)
return
root.showcaseEntryChanged()
showcaseModel.move(from, to)
showcaseModel.move(from, to, 1)
drag.accept()
}