SwitchGroup.qml 2,5 КБ
Newer Older
Alexey Andreev's avatar
Alexey Andreev включено в состав коммита
1
// SPDX-FileCopyrightText: 2020 Open Mobile Platform LLC <community@omp.ru>
Alexey Andreyev's avatar
Alexey Andreyev включено в состав коммита
2
// SPDX-License-Identifier: BSD-3-Clause
Alexey Andreev's avatar
Alexey Andreev включено в состав коммита
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

import QtQuick 2.0
import Sailfish.Silica 1.0

Column {
    id: root

    property alias model: repeater.model
    property bool exclusive: true
    property string selectedRole: "selected"
    property string valueRole: "value"
    property string nameRole: "name"
    property string iconRole: "icon"
    property string descriptionRole: "description"

Alexey Andreev's avatar
Alexey Andreev включено в состав коммита
18
    signal selectedValuesChanged
Alexey Andreev's avatar
Alexey Andreev включено в состав коммита
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

    function selectedValues() {
        var selectedValues = new Array(children.length - 1);
        if (exclusive) {
            if (repeater.selectedIndex >= 0)
                selectedValues[repeater.selectedIndex] = children[repeater.selectedIndex].value;
        } else {
            for (var iChild = 0; iChild < selectedValues.length; ++iChild) {
                var child = children[iChild];
                if (child.checked)
                    selectedValues[iChild] = child.value;
            }
        }
        return selectedValues;
    }

    width: parent ? parent.width : Screen.width

    Repeater {
        id: repeater

        property int selectedIndex: -1

        onModelChanged: selectedIndex = -1
        delegate: switchDelegate
    }

    Component {
        id: switchDelegate

        IconTextSwitch {
            id: switchItem
Alexey Andreev's avatar
Alexey Andreev включено в состав коммита
51

Alexey Andreev's avatar
Alexey Andreev включено в состав коммита
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
            readonly property var m: model.modelData || model
            readonly property var value: m[root.valueRole] === undefined ? m : m[root.valueRole]

            onClicked: {
                if (checked) {
                    if (root.exclusive)
                        return;
                    checked = false;
                    repeater.selectedIndex = -1;
                } else {
                    checked = true;
                    repeater.selectedIndex = model.index;
                }
                root.selectedValuesChanged();
            }
            Component.onCompleted: {
                if (m[root.selectedRole])
                    repeater.selectedIndex = model.index;
            }

            text: m[root.nameRole] === undefined ? value : m[root.nameRole]
            description: m[root.descriptionRole] || ""
            icon.source: m[root.iconRole] || ""
            automaticCheck: false

            Binding {
                id: targetStateWatcher

                when: root.exclusive
                target: switchItem
                property: "checked"
                value: model.index === repeater.selectedIndex
            }
        }
    }
}