summaryrefstree
path: root/usr/bin/qml/Diag/DiagSwitcher.qml
blob: 5635eb16013b38f3818633a83a2ac7b70ba7545d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
51
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import QtQuick 2.0
import Label 2.0
import "../com/neusoft/auto/widgets"
/*
*   Copyright (C) 2013 by Neusoft
*   Author : Zhaozhicheng <zhaozhch@neusoft.com>
*
*/

/**
 * A Switcher .
 */

import QtQuick 2.0
Rectangle {
    id: toggleBackground
    width: 123
    height: 41
    color: "#08417B"
    border.color: "white"
    /*******************************External Interface************************/
    /**
     * type:bool
     * This property holds the togglePoint's x when the component is completed.
     * if you specify it false,the Switcher's switch will be off.
     * The default is true
     */
    property bool completePos: true
    /**
     * type:bool
     * This property set the FlickButton's switch.
     * For example,when button is clicked,if you specify it false,the Switcher's switch will be off.
     */
    property bool setSwitch
    /**
     * type:bool
     * This property set the diaplay of the Switcher's label.
     * For example,when showlabel si true,the Switcher's label will be always diplayed.
     * The default value is false
     */
    property bool showlabel: false


    /**
     * type:bool
     * This property holds the togglePoint's  switch.
     * Read-Only property
     */
    property bool buttonSwitch/*:togglePointer.x == 0 ?false:true*/
    /**
     * type:string
     * This property holds the source url for the Switcher's background image.
     *
     * The default value is an empty url, which displays no background image.
     */
   // property alias backgroundImg: toggleBackground.source
    /**
     * type:string
     * This property holds the source url for the FlickButton's pointer image.
     *
     * The default value is an empty url, which displays no pointer image.
     */
   // property alias pointerImg: togglePointer.source
    /**
     * type:string
     * This property holds the leftLabel's text of the Switcher.
     */
    property alias leftLabelText: toggleLeftLabel.text
    /**
     * type:string
     * This property holds the leftLabel's color of the Switcher.
     */
    property alias leftLabelColor: toggleLeftLabel.color
    /**
     * type:string
     * This property holds the text's pointSize of the leftLabel of the Switcher.
     */
    property alias leftLabelSize: toggleLeftLabel.font.pixelSize
    /**
     * type:string
     * This property holds the rightLabel's text of the Switcher.
     */
    property alias rightLabelText: toggleRightLabel.text
    /**
     * type:string
     * This property holds the rightLabel's color of the Switcher.
     */
    property alias rightLabelColor: toggleRightLabel.color
    /**
     * type:string
     * This property holds the text's pointSize of the rightLabel of the Switcher.
     */
    property alias rightLabelSize: toggleRightLabel.font.pixelSize

    /*******************************Internal Interface************************/

    /**
     * type:real
     * This property holds the pointer's x of the starting position of the animation.
     */
    property real state1x
    /**
     * type:real
     * This property holds the pointer's x of the target position of the animation.
     */
    property real state2x
    /**
     * type:bool
     * This property holds the leftlabel's text will be diaplayed or not.
     */
    property bool showLText: togglePointer.x>togglePointer.width / 2?true:false
    /**
     * type:bool
     * This property holds the rightlabel's text will be diaplayed or not.
     */
    property bool showRText: togglePointer.x<togglePointer.width / 2?true:false
    focus: false
    Component.onCompleted: {
        togglePointer.x = completePos?(toggleBackground.width - togglePointer.width):0
        buttonSwitch = completePos
    }

    HMILabel{
        id: toggleLeftLabel
        width: toggleBackground.width/2
        height: toggleBackground.height
        visible: showlabel?true:(showLText?true:false)
        font.pixelSize: 24*rate
        text: "开"
        color: "white"
    }
    HMILabel{
        id: toggleRightLabel
        anchors.left: toggleLeftLabel.right
        width: toggleBackground.width/2
        height: toggleBackground.height
        visible: showlabel?true:(showRText?true:false)
        font.pixelSize: 24*rate
        text: "��?"
        color: "white"
    }

    HMIMouseArea{
        id: flyarea
        anchors.fill: parent
        onClicked: {
            toggleBackground.focus = true
            if(mouseX < toggleBackground.width - togglePointer.width)
            {
                numanimation.stop()
                buttonSwitch = false
                if(!dragarea.drag.active)
                {
                    state1x = togglePointer.x
                    state2x = 0
                    numanimation.start()
                }
            }
            else if(mouseX > toggleBackground.width - togglePointer.width)
            {
                buttonSwitch = true
                numanimation.stop()
                if(!dragarea.drag.active)
                {
                    state1x = togglePointer.x
                    state2x = toggleBackground.width-togglePointer.width
                    numanimation.start()
                }
            }
        }
    }

    Rectangle {
        id: togglePointer
        width: 59
        height: 41
        color: "gray"
        border.color: "white"
        //color: "#E0E0E0"
        HMIMouseArea{
            id: dragarea
            anchors.fill: parent
            drag.target: togglePointer
            drag.axis: "XAxis"
            drag.minimumX: 0
            drag.maximumX: toggleBackground.width-togglePointer.width
            onPressed: {
                    numanimation.stop()
            }

            onReleased: {
                toggleBackground.focus = true
                if(togglePointer.x + togglePointer.width/2 < toggleBackground.width - togglePointer.width)
                {
                    buttonSwitch = false
                    numanimation.stop()
                    state1x = togglePointer.x
                    state2x = 0
                    numanimation.start()
                }

                else if(togglePointer.x + togglePointer.width/2 > toggleBackground.width - togglePointer.width)
                {
                    buttonSwitch = true
                    numanimation.stop()
                    state1x = togglePointer.x
                    state2x = toggleBackground.width-togglePointer.width
                    numanimation.start()
                }
            }
        }
    }
    onSetSwitchChanged: {
        toggleBackground.focus = true
        numanimation.stop()
        if(!dragarea.drag.active)
        {
            if(setSwitch == true)
            {
                buttonSwitch = true
                state1x = togglePointer.x
                state2x = toggleBackground.width-togglePointer.width
                numanimation.start()
            }
            else
            {
                buttonSwitch = false
                state1x = togglePointer.x
                state2x = 0
                numanimation.start()
            }
        }
    }

    NumberAnimation {
        id: numanimation
        target: togglePointer
        property: "x"
        from: state1x
        to: state2x
        duration: 400
    }
}