2024-02-16 11:31:40 +00:00
|
|
|
|
import QtQml 2.15
|
|
|
|
|
|
|
|
|
|
import StatusQ 0.1
|
|
|
|
|
import StatusQ.Core.Utils 0.1
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Basic building block for storing temporary state in the "Profile Showcase"
|
|
|
|
|
* functionality. Allows to store on the UI side the temporary position and
|
|
|
|
|
* visibility level. Can store temporary state for Communities, Accounts,
|
|
|
|
|
* Collectibles and Assets.
|
|
|
|
|
*/
|
|
|
|
|
WritableProxyModel {
|
|
|
|
|
id: root
|
|
|
|
|
|
2024-02-20 13:26:56 +00:00
|
|
|
|
/**
|
|
|
|
|
* "Hidden" is a special type of visibility that requires special
|
|
|
|
|
* treatment. When the visibility is changed from hidden to other type
|
|
|
|
|
* of visibility, in addition to changing the visibility, the appropriate
|
|
|
|
|
* position is also set.
|
|
|
|
|
*/
|
|
|
|
|
property int visibilityHidden: 0
|
|
|
|
|
|
2024-02-16 11:31:40 +00:00
|
|
|
|
/* Provides the list of objects representing the current state in the
|
|
|
|
|
* in the following format:
|
|
|
|
|
* [ {
|
2024-03-04 22:07:09 +00:00
|
|
|
|
* showcaseKey: <string or integer>
|
|
|
|
|
* showcasePosition: <integer>
|
|
|
|
|
* showcaseVisibility: <integer>
|
2024-02-16 11:31:40 +00:00
|
|
|
|
* }
|
|
|
|
|
* ]
|
|
|
|
|
*
|
|
|
|
|
* The entries with visibility 0 (hidden) are not included in the list.
|
|
|
|
|
*/
|
2024-03-08 09:27:45 +00:00
|
|
|
|
function currentState(roleNames) {
|
|
|
|
|
const visible = d.getVisibleEntries(roleNames)
|
2024-03-04 22:07:09 +00:00
|
|
|
|
const minPos = Math.min(...visible.map(e => e.showcasePosition))
|
2024-02-16 11:31:40 +00:00
|
|
|
|
|
2024-03-04 22:07:09 +00:00
|
|
|
|
return visible.map(e => { e.showcasePosition -= minPos; return e })
|
2024-02-16 11:31:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sets the visibility of the given item. If the element was hidden, it is
|
|
|
|
|
* positioned last.
|
|
|
|
|
*/
|
|
|
|
|
function setVisibility(key, visibility) {
|
|
|
|
|
const sourceIdx = d.indexByKey(key)
|
|
|
|
|
const oldVisibility = d.getVisibility(sourceIdx)
|
|
|
|
|
|
|
|
|
|
if (oldVisibility === visibility)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
// hiding, changing visibility level
|
2024-03-10 20:16:15 +00:00
|
|
|
|
if (visibility === visibilityHidden) {
|
|
|
|
|
set(sourceIdx, { showcaseVisibility: undefined, showcasePosition: undefined})
|
2024-02-16 11:31:40 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 20:16:15 +00:00
|
|
|
|
if (oldVisibility === visibilityHidden || oldVisibility === undefined) {
|
|
|
|
|
// unhiding
|
|
|
|
|
const positions = d.getVisibleEntries().map(e => e.showcasePosition)
|
|
|
|
|
const position = Math.max(-1, ...positions) + 1
|
|
|
|
|
set(sourceIdx, { showcaseVisibility: visibility, showcasePosition: position })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// changing visibility level
|
|
|
|
|
set(sourceIdx, { showcaseVisibility: visibility })
|
2024-02-16 11:31:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 20:16:15 +00:00
|
|
|
|
syncedRemovals: true
|
|
|
|
|
|
2024-02-16 11:31:40 +00:00
|
|
|
|
readonly property QtObject d_: QtObject {
|
|
|
|
|
id: d
|
|
|
|
|
|
|
|
|
|
function indexByKey(key) {
|
2024-03-04 22:07:09 +00:00
|
|
|
|
return ModelUtils.indexOf(root, "showcaseKey", key)
|
2024-02-16 11:31:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 09:27:45 +00:00
|
|
|
|
function getVisibleEntries(roleNames = ["showcaseKey", "showcasePosition", "showcaseVisibility"]) {
|
|
|
|
|
if (roleNames.length === 0)
|
|
|
|
|
roleNames = ["showcaseKey", "showcasePosition", "showcaseVisibility"]
|
|
|
|
|
|
|
|
|
|
if (!roleNames.includes("showcaseVisibility"))
|
|
|
|
|
roleNames.push("showcaseVisibility")
|
|
|
|
|
|
|
|
|
|
const keysAndPos = ModelUtils.modelToArray(root, roleNames)
|
2024-02-16 11:31:40 +00:00
|
|
|
|
|
2024-03-04 22:07:09 +00:00
|
|
|
|
return keysAndPos.filter(p => p.showcaseVisibility
|
|
|
|
|
&& p.showcaseVisibility !== root.visibilityHidden)
|
2024-02-16 11:31:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getVisibility(idx) {
|
2024-03-04 22:07:09 +00:00
|
|
|
|
return ModelUtils.get(root, idx, "showcaseVisibility")
|
2024-02-20 13:26:56 +00:00
|
|
|
|
|| root.visibilityHidden
|
2024-02-16 11:31:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|