Lukáš Tinkl a6c897929a chore: Remove custom rowData method from NIM's models
- Some NIM's models define additional (not part of the
QAbstractModelItem API) method rowData to allow access to model's data
on UI side
- The same job can be done relying fully on public QAbstractModelItem
API using ModelUtils utility. It gives full transparency if we need
nim's model or other like ListModel replacement in tests/storybook
- propagate `tokensStore` to unbreak the Browser's wallet menu

Fixes #14805
2024-05-20 18:02:20 +02:00

53 lines
1.7 KiB
QML

import QtQuick 2.15
import StatusQ.Core.Utils 0.1 as SQUtils
/// Helper item to clone a model and alter its data without affecting the original model
/// \beware this is not a proxy model. It clones the initial state
/// and every time the instance changes and doesn't adapt when the data
/// in the source model \c allNetworksModel changes
/// \beware use it with small models and in temporary views (e.g. popups)
/// \note tried to use SortFilterProxyModel with but it complicates implementation too much
ListModel {
id: root
required property var sourceModel
/// Roles to clone
required property var roles
/// Roles to override or add of the form { role: "roleName", transform: function(modelData) { return newValue } }
property var rolesOverride: []
Component.onCompleted: cloneModel(sourceModel)
onSourceModelChanged: cloneModel(sourceModel)
function findIndexForRole(roleName, value) {
for (let i = 0; i < count; i++) {
if(get(i)[roleName] === value) {
return i
}
}
return -1
}
function cloneModel(model) {
clear()
if (!model) {
console.warn("Missing valid data model to clone. The CloneModel is useless")
return
}
for (let i = 0; i < model.count; i++) {
const clonedItem = new Object()
for (var propName of roles) {
clonedItem[propName] = SQUtils.ModelUtils.get(model, i, propName)
}
for (var newProp of rolesOverride) {
clonedItem[newProp.role] = newProp.transform(clonedItem)
}
append(clonedItem)
}
}
}