on d5e1bfa help sign in
unity8/plugins/Unity/Indicators/qml/MenuActionBinding.qml
106 lines 3.0 kB history raw
1/*
2 * Copyright 2013 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Renato Araujo Oliveira Filho <renato@canonical.com>
18 */
20import QtQuick 2.0
22/*!
23 \qmltype MenuActionBinding
24 \inqmlmodule Indicators 0.1
25 \brief Helper class to connect a qmenumodel action state with a qml components property
27 Example:
28 \qml
29 Slider {
30 id: slide
31 }
32 MenuActionBinding {
33 actionGroup: menuItem.actionGroup
34 action: "/ubuntu/sound/volume"
35 target: slider
36 property: "value"
37 }
38 \endqml
39*/
41MenuAction {
42 id: menuAction
44 /*!
45 \preliminary
46 The component which will have the property changed by action
47 */
48 property QtObject target: null
50 /*!
51 \preliminary
52 The property name of \l target component which will be linked with action state
53 */
54 property string property
56 /*!
57 \preliminary
58 Use this to enable or disable the action state propagation to component property.
59 */
60 property bool active: true
62 Component.onCompleted: connectViewChanges()
63 onTargetChanged: connectViewChanges()
64 onPropertyChanged: connectViewChanges()
66 Item {
67 id: d
68 property var currentTarget: undefined
69 property string currentSignal
70 }
72 Binding {
73 id: propertyBinding
74 target: menuAction.target
75 property: menuAction.property
76 value: menuAction.state
77 when: menuAction.actionObject && menuAction.active
78 }
80 // TODO - re-evaluate in c++
81 function connectViewChanges() {
82 if (d.currentTarget != undefined && d.currentSignal !== "") {
83 if (d.currentTarget[d.currentSignal] !== undefined) {
84 d.currentTarget[d.currentSignal].disconnect(onViewValueChanged);
85 }
86 }
88 d.currentTarget = null;
89 d.currentSignal = "";
90 if (target) {
91 var signal = "on%1Changed".arg(property.charAt(0).toUpperCase() + property.slice(1));
92 if (target[signal] !== undefined) {
93 target[signal].connect(onViewValueChanged);
94 d.currentTarget = target;
95 d.currentSignal = signal;
96 }
97 }
98 }
100 function onViewValueChanged() {
101 if (valid) {
102 var propertyValue = target[property];
103 actionObject.updateState(propertyValue);
104 }
105 }