~saviq/+junk/elisa_qt

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
/*
   This file is part of Elisa.

   Elisa is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Elisa is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Elisa. If not, see <http://www.gnu.org/licenses/>.


   Authors: Florian Boucault <florian@boucault.net>
*/

import Qt 4.7
import "widgets"

Item {
    /* Vertical list visualisation mode.

       Used to display content in a vertical orientation.
       It features a lateral shortcut bar for faster access to content that can
       be activated by setting the property 'with_shortcuts' to true and
       overriding the 'get_shortcut_for_item' function. It assumes that 'model'
       provides a 'get' method that given an integer index returns the associated
       item.
    */
    property alias model: items.model
    property alias delegate: items.delegate
    property bool with_shortcuts: false

    /* Compute the shortcut corresponding to a given item of the model.

       Return a string that will be used to display the shortcut.
    */
    function get_shortcut_for_item(item) {
        return ""
    }

    width: 800
    height: 450

    ListView {
        id: items
        x: with_shortcuts ? shortcuts.x+shortcuts.width+25 : 15
        width: parent.width/2
        anchors.top: top_mask.bottom
        anchors.bottom: bottom_mask.top
        orientation: ListView.Vertical
        // FIXME: spacing should be 1 but computing dynamically the height of subsection_delegate breaks it
        spacing: 2
        highlightMoveDuration: 200
        preferredHighlightBegin: height/3
        preferredHighlightEnd: height*2/3
        highlightRangeMode: ListView.ApplyRange
        focus: true
        // FIXME: workaround a focus bug: focus was lost/stolen to newly created Section components
        cacheBuffer: 10000

        model: 100
        delegate: MenuItemOneLiner { width: ListView.view.width; height: 60; label: "Test"}

        Keys.onPressed: {
            /* FIXME: 10 is hardcoded and duplicated from MenuItem.qml
                      That should be generalised and moved to ListViewCustom */
            var items_per_page = 10
            var index

            if (event.key == Qt.Key_PageDown) {
                index = currentIndex + items_per_page
                if(index >= count)
                    currentIndex = count - 1
                else
                    currentIndex = index
                event.accepted = true;
            }
            else if (event.key == Qt.Key_PageUp) {
                index = currentIndex - items_per_page
                if(index < 0)
                    currentIndex = 0
                else
                    currentIndex = index
                event.accepted = true;
            }
        }

        KeyNavigation.left: if(with_shortcuts) shortcuts

        onCountChanged: if(with_shortcuts) load_shortcuts()
    }


    ListModel {
        id: shortcuts_model
    }

    function load_shortcuts() {
        var index
        var item
        var shortcut
        var existing_shortcuts = []

        for(index = 0; index < items.count; index++) {
            item = items.model.get(index)
            shortcut = get_shortcut_for_item(item)
            if(existing_shortcuts[shortcut] != true) {
                existing_shortcuts[shortcut] = true
                shortcuts_model.append({"shortcut": shortcut, "index": index})
            }
        }
    }

    ListView {
        id: shortcuts
        visible: with_shortcuts
        x: 15
        /* FIXME: 10 is hardcoded and duplicated from MenuItem.qml */
        width: items.height/10-items.spacing

        spacing: items.spacing
        highlightMoveDuration: items.highlightMoveDuration
        preferredHighlightBegin: items.preferredHighlightBegin
        preferredHighlightEnd: items.preferredHighlightEnd
        highlightRangeMode: items.highlightRangeMode
        anchors.top: items.anchors.top
        anchors.bottom: items.anchors.bottom
        orientation: items.orientation

        model: shortcuts_model
        delegate:
                MenuItem {
                    width: ListView.view.width
                    height: width

                    TextNormalized {
                        id: label
                        anchors.fill: parent
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        font_size: "medium"
                        z: 1
                        text: model.shortcut
                        font.bold: true
                    }

                    onClicked: items.currentIndex = model.index
                }

        KeyNavigation.right: items
    }

    Rectangle {
        id: top_mask
        width: parent.width
        height: parent.height/10
        gradient: Gradient {
            GradientStop { position: 0.0; color: "#000000"}
            GradientStop { position: 1.0; color: "transparent"}
        }
    }

    Rectangle {
        id: bottom_mask
        y: parent.height - height
        width: parent.width
        height: parent.height/10
        gradient: Gradient {
            GradientStop { position: 0.0; color: "transparent" }
            GradientStop { position: 1.0; color: "#000000"}
        }
    }
}