moment/src/qml/Base/HListModel.qml

142 lines
3.9 KiB
QML
Raw Normal View History

2019-07-08 13:52:41 +10:00
// Copyright 2019 miruka
// This file is part of harmonyqml, licensed under LGPLv3.
import QtQuick 2.12
import SortFilterProxyModel 0.2
SortFilterProxyModel {
// To initialize a HListModel with items,
// use `Component.onCompleted: extend([{"foo": 1, "bar": 2}, ...])`
id: sortFilteredModel
2019-07-07 07:58:59 +10:00
property ListModel model: ListModel {}
sourceModel: model // Can't assign a "ListModel {}" directly here
function append(dict) { return model.append(dict) }
function clear() { return model.clear() }
function insert(index, dict) { return model.inset(index, dict) }
function move(from, to, n=1) { return model.move(from, to, n) }
function remove(index, count=1) { return model.remove(index, count) }
function set(index, dict) { return model.set(index, dict) }
function sync() { return model.sync() }
function setProperty(index, prop, value) {
return model.setProperty(index, prop, value)
}
function extend(newItems) {
2019-07-18 19:18:13 +10:00
for (let item of newItems) { model.append(item) }
}
function getIndices(whereRolesAre, maxResults=null, maxTries=null) {
// maxResults, maxTries: null or int
2019-07-18 19:18:13 +10:00
let results = []
2019-07-18 19:18:13 +10:00
for (let i = 0; i < model.count; i++) {
let item = model.get(i)
let include = true
2019-07-18 19:18:13 +10:00
for (let role in whereRolesAre) {
if (item[role] != whereRolesAre[role]) {
include = false
break
}
}
if (include) {
results.push(i)
if (maxResults && results.length >= maxResults) {
break
}
}
if (maxTries && i >= maxTries) {
break
}
}
return results
}
function getWhere(rolesAre, maxResults=null, maxTries=null) {
2019-07-18 19:18:13 +10:00
let items = []
2019-07-18 19:18:13 +10:00
for (let indice of getIndices(rolesAre, maxResults, maxTries)) {
items.push(model.get(indice))
}
return items
}
function forEachWhere(rolesAre, func, maxResults=null, maxTries=null) {
2019-07-18 19:18:13 +10:00
for (let item of getWhere(rolesAre, maxResults, maxTries)) {
func(item)
}
}
function upsert(
whereRolesAre, newItem, updateIfExist=true, maxTries=null
) {
2019-07-18 19:18:13 +10:00
let indices = getIndices(whereRolesAre, 1, maxTries)
if (indices.length == 0) {
model.append(newItem)
return model.get(model.count)
}
2019-07-18 19:18:13 +10:00
let existing = model.get(indices[0])
if (! updateIfExist) { return existing }
// Really update only if existing and new item have a difference
for (var role in existing) {
if (Boolean(existing[role].getTime)) {
if (existing[role].getTime() != newItem[role].getTime()) {
model.set(indices[0], newItem)
return existing
}
} else {
if (existing[role] != newItem[role]) {
model.set(indices[0], newItem)
return existing
}
}
}
return existing
}
function pop(index) {
2019-07-18 19:18:13 +10:00
let item = model.get(index)
model.remove(index)
return item
}
function popWhere(rolesAre, maxResults=null, maxTries=null) {
2019-07-18 19:18:13 +10:00
let items = []
2019-07-18 19:18:13 +10:00
for (let indice of getIndices(rolesAre, maxResults, maxTries)) {
items.push(model.get(indice))
model.remove(indice)
}
return items
}
function toObject(itemList=sortFilteredModel) {
2019-07-18 19:18:13 +10:00
let objList = []
2019-07-18 19:18:13 +10:00
for (let item of itemList) {
let obj = JSON.parse(JSON.stringify(item))
2019-07-18 19:18:13 +10:00
for (let role in obj) {
if (obj[role]["objectName"] != undefined) {
obj[role] = toObject(item[role])
}
}
objList.push(obj)
}
return objList
}
function toJson() {
return JSON.stringify(toObject(), null, 4)
}
}