stateslist.qml 9.89 KB
Newer Older
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
import Qt 4.6

// Shows list of states
// the model has to expose the path through an iconUrl property
Rectangle {
    id: root
    property int currentStateIndex : 0
    signal createNewState
    signal deleteCurrentState
    signal duplicateCurrentState

    color: "#707070";

    function adjustCurrentStateIndex() {
        if (currentStateIndex >= statesEditorModel.count)
            currentStateIndex = statesEditorModel.count-1;
    }

    Connection {
        sender: statesEditorModel
        signal: "countChanged()"
        script: adjustCurrentStateIndex();
    }

    Connection {
        sender: statesEditorModel
        signal: "changedToState(n)"
        script: {root.currentStateIndex = n}
    }
30
31
	
	Item { id:focusStealer } 
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

    Flickable {
        id: listView

        anchors.fill: root;

        anchors.bottomMargin: 10;
        anchors.topMargin: 2;
        anchors.leftMargin: 29;
        anchors.rightMargin: 40;

        viewportHeight: height
        viewportWidth: listViewRow.width

        Row {
            id: listViewRow
            spacing: 4
            Repeater {
                model: statesEditorModel
                delegate: Delegate
            }
        }

        focus: true;
        clip: true;
        overShoot: false;
        interactive:false;

    }

    Component {
        id: Delegate
        Item {
            id: container
            width: Math.max(img.width, txt.width) + 4
            height: img.height + txt.height + 4

            Rectangle {
                id: Highlight
                anchors.fill: parent
                color: "#0303e0"
                radius: 4
                opacity: (index==root.currentStateIndex);
                onOpacityChanged: if (opacity) {
                    // If the current item becomes selected, check if it is in the viewport
                    if (container.x < listView.viewportX)
                        horizontalScrollbar.viewPosition = container.x;
                    if (container.x+container.width > listView.viewportX + listView.width)
                        horizontalScrollbar.viewPosition = container.x+container.width - listView.width;
                }
            }

            Image {
                id: img
                pixmap: statePixmap
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.top: parent.top
                anchors.topMargin: 2
            }

            MouseRegion {
                id: itemRegion
                anchors.fill: container
                onClicked: {
                    root.currentStateIndex = index
97
					ocusStealer.focus=true; // steal focus from textinput
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
                }
            }

            TextInput {
                anchors.top: img.bottom
                anchors.horizontalCenter: img.horizontalCenter
                id: txt
                text: stateName
                color: "#E1E1E1";
                onAccepted: {
                    // force focus to move to another item, otherwise this one will be reactivated at mode change
                    focus=false;
                    itemRegion.focus=true;
                }
                onFocusChanged: if (!focus) {
                    txtRegion.enabled=true
                    statesEditorModel.renameState(index, text)
                }
                MouseRegion {
                    id: txtRegion
                    anchors.fill:parent
                    property bool wasDoubleClicked:false
                    onClicked: {
                        root.currentStateIndex = index;
                        itemRegion.focus=true;
                    }
                    onDoubleClicked: if (index!=0) { // base state not editable
                        parent.focus=true;
                        enabled=false;
                    }
                }
            }
        }
    }

    // The add button
    Rectangle {
        id: addState

        anchors.left: root.left
        anchors.top: root.top
        anchors.topMargin: 4;
        anchors.leftMargin: 4;

        width: 20
        height: 50

        // Appearance
        gradient: Gradient {
            GradientStop { position: 0.0; color: "#909090" }
            GradientStop { position: 1.0; color: "#AEAEAE" }
        }
        border.width : 1
        border.color : "#4F4F4F"
        radius: 4

        // "clicked" overlay
        Rectangle {
            anchors.fill:parent
            opacity:parent.state=="Pressed"
            gradient: Gradient {
                GradientStop { position: 0.0; color: "#727272" }
                GradientStop { position: 1.0; color: "#909090" }
            }
            border.width : 1
            border.color : "#4F4F4F"
            radius: 4
        }

        states: State{ name: "Pressed"; }

        // "plus" sign
        Rectangle {
            width:12
            height:2
            color:"black"
            anchors.centerIn:parent
            effect: Blur { blurRadius: 1; }
        }
        Rectangle {
            width:2
            height:12
            color:"black"
            anchors.centerIn:parent
            effect: Blur { blurRadius: 1; }
        }

        MouseRegion {
            anchors.fill:parent
            onClicked: {
                if (root.currentStateIndex == 0)
                root.createNewState(); //create new state
                else
                root.duplicateCurrentState(); //duplicate current state

                listView.viewportX = horizontalScrollbar.viewPosition;
                // select the newly created state
                root.currentStateIndex=statesEditorModel.count - 1;
            }
            onPressed: {parent.state="Pressed"}
            onReleased: {parent.state=""}
        }
    }

    // The erase button
    Rectangle {
        id: removeState

        anchors.left: root.left
        anchors.top: addState.bottom
        anchors.topMargin: 4;
        anchors.leftMargin: 4;

        width: 20
        height: 50

        // Appearance
        gradient: Gradient {
            GradientStop { position: 0.0; color: "#909090" }
            GradientStop { position: 1.0; color: "#AEAEAE" }
        }
        border.width : 1
        border.color : "#4F4F4F"
        radius: 4

        // "clicked" overlay
        Rectangle {
            anchors.fill:parent
            opacity:parent.state=="Pressed"
            gradient: Gradient {
                GradientStop { position: 0.0; color: "#727272" }
                GradientStop { position: 1.0; color: "#909090" }
            }
            border.width : 1
            border.color : "#4F4F4F"
            radius: 4
        }

        states: State{ name: "Pressed"; }

        // "minus" sign
        Rectangle {
            width:12
            height:2
            color:"black"
            anchors.centerIn:parent
            effect: Blur { blurRadius: 1; }
        }

        visible: { root.currentStateIndex != 0 }

        MouseRegion {
            anchors.fill:parent
            onClicked: {
                root.deleteCurrentState();
                if (root.currentStateIndex >= statesEditorModel.count)
                    root.currentStateIndex = root.currentStateIndex-1;
                horizontalScrollbar.totalLengthDecreased();
            }
            onPressed: {parent.state="Pressed"}
            onReleased: {parent.state=""}
            enabled: { root.currentStateIndex != 0 }
        }
    }

    Item {
        id: horizontalScrollbar
        // Current listView implementation sometimes has negative width or viewportWidth
        property int viewPosition: 0;
        property int viewLength: ( listView.width>=0 ? listView.width : 0 );
        property int totalLength: ( listView.viewportWidth>=0 ? listView.viewportWidth : 0 );


        onViewPositionChanged: listView.viewportX=viewPosition;
        onViewLengthChanged: {
            if ((totalLength>viewLength) && (viewPosition > totalLength-viewLength))
                viewPosition = totalLength-viewLength;
        }

        function totalLengthDecreased()  {
            if ((totalLength>viewLength) && (viewPosition > totalLength-viewLength))
                viewPosition = totalLength-viewLength;
        }

        opacity: viewLength < totalLength;

        anchors.left : listView.left
        anchors.right : listView.right
        anchors.bottom : root.bottom
        anchors.bottomMargin: 4

        height: 10;

        // the bar itself
        Item {
            id: draggableBar
            width: if (horizontalScrollbar.viewLength>horizontalScrollbar.totalLength) parent.width;
            else horizontalScrollbar.viewLength/horizontalScrollbar.totalLength  * parent.width;
            height: parent.height;
            x: horizontalScrollbar.viewPosition*horizontalScrollbar.width/horizontalScrollbar.totalLength;


            Rectangle {
                height:parent.height-1
                width:parent.width
                y:1

                gradient: Gradient {
                    GradientStop { position: 0.0; color: "#C6C6C6" }
                    GradientStop { position: 1.0; color: "#7E7E7E" }
                }
            }

            MouseRegion {
                anchors.fill:parent
                property int dragging:0;
                property int originalX:0;
                onPressed: { dragging = 1; originalX = mouse.x; }
                onReleased: { dragging = 0; }
                onPositionChanged: if (dragging)
                {
                    var newX = mouse.x - originalX + parent.x;
                    if (newX<0) newX=0;
                    if (newX>horizontalScrollbar.width-draggableBar.width)
                        newX=horizontalScrollbar.width-draggableBar.width;
                    horizontalScrollbar.viewPosition = newX*horizontalScrollbar.totalLength/horizontalScrollbar.width;
                }
            }
        }

        // border
        Rectangle {
            anchors.fill:parent;
            color:"Transparent";
            border.width:1;
            border.color:"#8F8F8F";
        }

    }

}