2019-12-03 07:29:29 +11:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
|
|
|
pragma Singleton
|
|
|
|
import QtQuick 2.12
|
|
|
|
import "PythonBridge"
|
|
|
|
|
|
|
|
QtObject {
|
|
|
|
property QtObject privates: QtObject {
|
|
|
|
readonly property var store: ({})
|
|
|
|
|
|
|
|
readonly property PythonBridge py: PythonBridge {}
|
|
|
|
|
|
|
|
readonly property Component model: Component {
|
|
|
|
ListModel {
|
2020-07-12 14:25:57 +10:00
|
|
|
property var modelId
|
2020-09-03 03:38:11 +10:00
|
|
|
property var idToItems: ({})
|
2020-07-12 14:25:57 +10:00
|
|
|
|
2020-05-01 14:30:26 +10:00
|
|
|
// Used by HFilterModel
|
|
|
|
signal fieldsChanged(int index, var changes)
|
|
|
|
|
2020-04-30 04:00:02 +10:00
|
|
|
function findIndex(id, default_=null) {
|
2020-03-10 20:38:28 +11:00
|
|
|
for (let i = 0; i < count; i++)
|
|
|
|
if (get(i).id === id) return i
|
|
|
|
|
2020-04-30 04:00:02 +10:00
|
|
|
return default_
|
2020-03-10 20:38:28 +11:00
|
|
|
}
|
|
|
|
|
2020-04-30 04:00:02 +10:00
|
|
|
function find(id, default_=null) {
|
2020-09-03 03:38:11 +10:00
|
|
|
return idToItems[id] || default_
|
2019-12-03 07:29:29 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 14:25:57 +10:00
|
|
|
|
|
|
|
signal ensureModelExists(var modelId)
|
|
|
|
|
|
|
|
onEnsureModelExists:
|
|
|
|
py.callCoro("models.ensure_exists_from_qml", [modelId])
|
2019-12-03 07:29:29 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get(...modelId) {
|
|
|
|
if (modelId.length === 1) modelId = modelId[0]
|
|
|
|
|
2020-05-11 04:58:59 +10:00
|
|
|
if (! privates.store[modelId]) {
|
2020-05-21 15:26:12 +10:00
|
|
|
// Using a signal somehow avoids a binding loop
|
|
|
|
privates.ensureModelExists(modelId)
|
2020-05-11 04:58:59 +10:00
|
|
|
|
2019-12-03 07:29:29 +11:00
|
|
|
privates.store[modelId] =
|
|
|
|
privates.model.createObject(this, {modelId})
|
2020-05-11 04:58:59 +10:00
|
|
|
}
|
2019-12-03 07:29:29 +11:00
|
|
|
|
|
|
|
return privates.store[modelId]
|
|
|
|
}
|
|
|
|
}
|