go back to old model with SortProxyModel

This commit is contained in:
Jonathan Rainville 2023-05-23 12:55:52 -04:00
parent c3b1a5f2ab
commit 500ee972ac
3 changed files with 34 additions and 31 deletions

View File

@ -174,17 +174,9 @@ QtObject:
proc getItemIdxById*(self: Model, id: string): int = proc getItemIdxById*(self: Model, id: string): int =
return getItemIdxById(self.items, id) return getItemIdxById(self.items, id)
proc cmpChatsAndCats*(x, y: Item): int =
# Sort proc to compare chats and categories
# Compares first by categoryPosition, then by position
result = cmp(x.categoryPosition, y.categoryPosition)
if result == 0:
result = cmp(x.position, y.position)
proc setData*(self: Model, items: seq[Item]) = proc setData*(self: Model, items: seq[Item]) =
self.beginResetModel() self.beginResetModel()
self.items = items self.items = items
self.items.sort(cmpChatsAndCats)
self.endResetModel() self.endResetModel()
self.countChanged() self.countChanged()
@ -376,8 +368,6 @@ QtObject:
categoryId: string, categoryId: string,
newCategoryPosition: int, newCategoryPosition: int,
) = ) =
self.beginResetModel()
for i in 0 ..< self.items.len: for i in 0 ..< self.items.len:
var item = self.items[i] var item = self.items[i]
if item.`type` == CATEGORY_TYPE: if item.`type` == CATEGORY_TYPE:
@ -392,13 +382,18 @@ QtObject:
item.position = chat.position item.position = chat.position
item.categoryId = chat.categoryId item.categoryId = chat.categoryId
item.categoryPosition = if nowHasCategory: newCategoryPosition else: -1 item.categoryPosition = if nowHasCategory: newCategoryPosition else: -1
let modelIndex = self.createIndex(i, 0, nil)
var roleChanges = @[
ModelRole.Position.int,
ModelRole.CategoryId.int,
ModelRole.CategoryPosition.int,
]
if not nowHasCategory: if not nowHasCategory:
item.categoryOpened = true item.categoryOpened = true
roleChanges.add(ModelRole.CategoryOpened.int)
self.dataChanged(modelIndex, modelIndex, roleChanges)
break break
self.items.sort(cmpChatsAndCats)
self.endResetModel()
proc removeCategory*( proc removeCategory*(
self: Model, self: Model,
categoryId: string, categoryId: string,
@ -406,7 +401,6 @@ QtObject:
) = ) =
self.removeItemById(categoryId) self.removeItemById(categoryId)
self.beginResetModel()
for i in 0 ..< self.items.len: for i in 0 ..< self.items.len:
var item = self.items[i] var item = self.items[i]
if item.categoryId != categoryId: if item.categoryId != categoryId:
@ -429,9 +423,6 @@ QtObject:
]) ])
break break
self.items.sort(cmpChatsAndCats)
self.endResetModel()
proc renameCategory*(self: Model, categoryId, newName: string) = proc renameCategory*(self: Model, categoryId, newName: string) =
let index = self.getItemIdxById(categoryId) let index = self.getItemIdxById(categoryId)
if index == -1: if index == -1:
@ -493,13 +484,12 @@ QtObject:
self: Model, self: Model,
updatedChats: seq[ChatDto], updatedChats: seq[ChatDto],
) = ) =
self.beginResetModel()
for updatedChat in updatedChats: for updatedChat in updatedChats:
let index = self.getItemIdxById(updatedChat.id) let index = self.getItemIdxById(updatedChat.id)
if index == -1: if index == -1:
continue continue
var roles = @[ModelRole.Position.int]
if(self.items[index].categoryId != updatedChat.categoryId): if(self.items[index].categoryId != updatedChat.categoryId):
if updatedChat.categoryId == "": if updatedChat.categoryId == "":
# Moved out of a category # Moved out of a category
@ -511,20 +501,20 @@ QtObject:
continue continue
self.items[index].categoryId = category.id self.items[index].categoryId = category.id
self.items[index].categoryPosition = category.categoryPosition self.items[index].categoryPosition = category.categoryPosition
roles = roles.concat(@[
ModelRole.CategoryId.int,
ModelRole.CategoryPosition.int,
])
self.items[index].position = updatedChat.position self.items[index].position = updatedChat.position
let modelIndex = self.createIndex(index, 0, nil)
self.items.sort(cmpChatsAndCats) self.dataChanged(modelIndex, modelIndex, roles)
self.endResetModel()
proc reorderCategoryById*( proc reorderCategoryById*(
self: Model, self: Model,
categoryId: string, categoryId: string,
position: int, position: int,
) = ) =
self.beginResetModel()
for i in 0 ..< self.items.len: for i in 0 ..< self.items.len:
var item = self.items[i] var item = self.items[i]
if item.categoryId != categoryId: if item.categoryId != categoryId:
@ -532,9 +522,8 @@ QtObject:
if item.categoryPosition == position: if item.categoryPosition == position:
continue continue
item.categoryPosition = position item.categoryPosition = position
let modelIndex = self.createIndex(i, 0, nil)
self.items.sort(cmpChatsAndCats) self.dataChanged(modelIndex, modelIndex, @[ModelRole.CategoryPosition.int])
self.endResetModel()
proc clearItems*(self: Model) = proc clearItems*(self: Model) =
self.beginResetModel() self.beginResetModel()

View File

@ -142,10 +142,10 @@ Item {
highlighted = true; highlighted = true;
categoryPopupMenuSlot.item.popup() categoryPopupMenuSlot.item.popup()
} else if (mouse.button === Qt.LeftButton) { } else if (mouse.button === Qt.LeftButton) {
root.model.changeCategoryOpened(model.categoryId, !statusChatListCategoryItem.opened) root.model.sourceModel.changeCategoryOpened(model.categoryId, !statusChatListCategoryItem.opened)
} }
} }
onToggleButtonClicked: root.model.changeCategoryOpened(model.categoryId, !statusChatListCategoryItem.opened) onToggleButtonClicked: root.model.sourceModel.changeCategoryOpened(model.categoryId, !statusChatListCategoryItem.opened)
onMenuButtonClicked: { onMenuButtonClicked: {
statusChatListCategoryItem.setupPopup() statusChatListCategoryItem.setupPopup()
highlighted = true highlighted = true

View File

@ -7,6 +7,8 @@ import StatusQ.Components 0.1
import StatusQ.Popups 0.1 import StatusQ.Popups 0.1
import StatusQ.Core 0.1 import StatusQ.Core 0.1
import SortFilterProxyModel 0.2
Item { Item {
id: root id: root
@ -68,7 +70,19 @@ Item {
root.categoryAddButtonClicked(id) root.categoryAddButtonClicked(id)
} }
model: root.model model: SortFilterProxyModel {
sourceModel: root.model
sorters: [
RoleSorter {
roleName: "categoryPosition"
priority: 2 // Higher number === higher priority
},
RoleSorter {
roleName: "position"
priority: 1
}
]
}
popupMenu: root.chatListPopupMenu popupMenu: root.chatListPopupMenu
categoryPopupMenu: root.categoryPopupMenu categoryPopupMenu: root.categoryPopupMenu