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