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

View File

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

View File

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

View File

@ -128,18 +128,7 @@ QtObject:
let index = self.createIndex(ind, 0, nil) let index = self.createIndex(ind, 0, nil)
defer: index.delete defer: index.delete
self.dataChanged(index, index, @[ 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,
])
proc upsertItemJson(self: ProfileShowcaseCommunitiesModel, itemJson: string) {.slot.} = proc upsertItemJson(self: ProfileShowcaseCommunitiesModel, itemJson: string) {.slot.} =
self.upsertItemImpl(itemJson.parseJson.toProfileShowcaseCommunityItem()) self.upsertItemImpl(itemJson.parseJson.toProfileShowcaseCommunityItem())
@ -184,16 +173,25 @@ QtObject:
if ind != -1: if ind != -1:
self.remove(ind) self.remove(ind)
proc move*(self: ProfileShowcaseCommunitiesModel, fromIndex: int, toIndex: int) {.slot.} = proc move*(self: ProfileShowcaseCommunitiesModel, fromRow: int, toRow: int, dummyCount: int = 1) {.slot.} =
if fromIndex < 0 or fromIndex >= self.items.len: if fromRow < 0 or fromRow >= self.items.len:
return return
self.beginResetModel() let sourceIndex = newQModelIndex()
let item = self.items[fromIndex] defer: sourceIndex.delete
self.items.delete(fromIndex) let destIndex = newQModelIndex()
self.items.insert(@[item], toIndex) 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.recalcOrder()
self.endResetModel() self.endMoveRows()
proc setVisibilityByIndex*(self: ProfileShowcaseCommunitiesModel, ind: int, visibility: int) {.slot.} = proc setVisibilityByIndex*(self: ProfileShowcaseCommunitiesModel, ind: int, visibility: int) {.slot.} =
if (visibility >= ord(low(ProfileShowcaseVisibility)) and if (visibility >= ord(low(ProfileShowcaseVisibility)) and

View File

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