CategoryLabel.qml 10.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Kai Koehne's avatar
Kai Koehne committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Kai Koehne's avatar
Kai Koehne committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Kai Koehne's avatar
Kai Koehne committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
Kai Koehne's avatar
Kai Koehne committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Kai Koehne's avatar
Kai Koehne committed
24
**
hjk's avatar
hjk committed
25 26
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Kai Koehne's avatar
Kai Koehne committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Kai Koehne's avatar
Kai Koehne committed
30

31
import QtQuick 2.1
32 33
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
Christiaan Janssen's avatar
Christiaan Janssen committed
34 35

Item {
36
    id: labelContainer
37
    property string text: trigger(1) ? qmlProfilerModelProxy.displayName(modelIndex) : ""
38 39 40
    property bool expanded: trigger(qmlProfilerModelProxy.expanded(modelIndex))
    property int modelIndex: index
    property int bindingTrigger: 1
41 42
    property var descriptions: []
    property var extdescriptions: []
43
    property var selectionIds: []
44 45 46 47 48
    property bool dragging
    property Item draggerParent

    signal dragStarted;
    signal dragStopped;
49

50 51
    readonly property int dragHeight: 5

52 53
    function trigger(i) {
        return i * bindingTrigger * bindingTrigger;
54 55
    }

56 57
    property bool reverseSelect: false

58 59
    visible: trigger(qmlProfilerModelProxy.rowCount(modelIndex)) > 0

60
    height: trigger(qmlProfilerModelProxy.models[modelIndex].height)
61
    width: 150
62

63
    function updateDescriptions() {
64
        bindingTrigger = -bindingTrigger;
Christiaan Janssen's avatar
Christiaan Janssen committed
65 66 67
        if (!visible)
            return;

68 69 70
        var desc=[];
        var ids=[];
        var extdesc=[];
71
        var labelList = qmlProfilerModelProxy.labels(modelIndex);
Christiaan Janssen's avatar
Christiaan Janssen committed
72
        for (var i = 0; i < labelList.length; i++ ) {
73
            extdesc[i] = desc[i] = (labelList[i].description || qsTr("<bytecode>"));
Christiaan Janssen's avatar
Christiaan Janssen committed
74
            ids[i] = labelList[i].id;
75 76
            if (labelList[i].displayName !== undefined)
                extdesc[i] += " (" + labelList[i].displayName + ")";
77 78
        }
        descriptions = desc;
79
        selectionIds = ids;
80 81 82
        extdescriptions = extdesc;
    }

83
    Connections {
84
        target: qmlProfilerModelProxy.models[modelIndex]
Ulf Hermann's avatar
Ulf Hermann committed
85
        onExpandedChanged: updateDescriptions()
86
        onRowHeightChanged: updateDescriptions()
87
        onHiddenChanged: updateDescriptions()
88 89 90 91 92
    }

    Connections {
        target: qmlProfilerModelProxy
        onStateChanged: updateDescriptions()
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
        onModelsChanged: updateDescriptions()
    }

    MouseArea {
        id: dragArea
        anchors.fill: txt
        drag.target: dragger
        cursorShape: dragging ? Qt.DragMoveCursor : Qt.OpenHandCursor
    }

    DropArea {
        id: dropArea

        onPositionChanged: {
            if ((drag.source.modelIndex > labelContainer.modelIndex &&
                    drag.source.y < labelContainer.y + drag.source.height) ||
                    (drag.source.modelIndex < labelContainer.modelIndex &&
                    drag.source.y > labelContainer.y + labelContainer.height -
                    drag.source.height)) {
                qmlProfilerModelProxy.swapModels(drag.source.modelIndex,
                                                 labelContainer.modelIndex);
                drag.source.modelIndex = labelContainer.modelIndex;
            }
        }

        anchors.fill: parent
119
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
120 121

    Text {
122
        id: txt
Christiaan Janssen's avatar
Christiaan Janssen committed
123 124
        x: 5
        font.pixelSize: 12
Christiaan Janssen's avatar
Christiaan Janssen committed
125
        text: labelContainer.text
Christiaan Janssen's avatar
Christiaan Janssen committed
126
        color: "#232323"
127
        height: trigger(qmlProfilerModelProxy.rowHeight(modelIndex, 0))
128 129
        width: 140
        verticalAlignment: Text.AlignVCenter
130
        renderType: Text.NativeRendering
Christiaan Janssen's avatar
Christiaan Janssen committed
131 132 133 134 135
    }

    Rectangle {
        height: 1
        width: parent.width
136
        color: "#999999"
Christiaan Janssen's avatar
Christiaan Janssen committed
137
        anchors.bottom: parent.bottom
138
        z: 2
Christiaan Janssen's avatar
Christiaan Janssen committed
139
    }
140 141

    Column {
142
        anchors.top: txt.bottom
143 144 145
        visible: expanded
        Repeater {
            model: descriptions.length
146
            Button {
147
                width: labelContainer.width
148
                height: trigger(qmlProfilerModelProxy.rowHeight(modelIndex, index + 1))
149 150 151
                action: Action {
                    onTriggered: {
                        if (reverseSelect)
152
                            view.selectPrevFromSelectionId(modelIndex,selectionIds[index]);
153
                        else
154
                            view.selectNextFromSelectionId(modelIndex,selectionIds[index]);
155
                    }
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
                    tooltip: extdescriptions[index]
                }

                style: ButtonStyle {
                    background: Rectangle {
                        border.width: 1
                        border.color: "#c8c8c8"
                        color: "#eaeaea"
                    }
                    label: Text {
                        text: descriptions[index]
                        textFormat: Text.PlainText
                        verticalAlignment: Text.AlignVCenter
                        horizontalAlignment: Text.AlignLeft
                        elide: Text.ElideRight
172
                        renderType: Text.NativeRendering
173 174 175 176 177 178 179 180 181 182 183 184 185 186
                    }
                }
                MouseArea {
                    hoverEnabled: true
                    property bool resizing: false
                    onPressed: resizing = true
                    onReleased: resizing = false

                    height: dragHeight
                    anchors.bottom: parent.bottom
                    anchors.left: parent.left
                    anchors.right: parent.right
                    cursorShape: Qt.SizeVerCursor

187 188
                    onMouseYChanged: {
                        if (resizing)
189
                            qmlProfilerModelProxy.setRowHeight(modelIndex, index + 1, y + mouseY);
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
    ToolButton {
        id: notesButton
        anchors.verticalCenter: txt.verticalCenter
        anchors.right: expandButton.left
        implicitWidth: 17
        implicitHeight: txt.height - 1
        property var eventIds: []
        property var texts: []
        property int currentNote: -1
        Connections {
            target: qmlProfilerModelProxy
            onModelsChanged: notesButton.updateNotes()
            onNotesChanged: {
                if (arguments[1] === -1 || arguments[1] === modelIndex)
                    notesButton.updateNotes();
            }
        }

        function updateNotes() {
            var notes = qmlProfilerModelProxy.notesByTimelineModel(modelIndex);
            var newTexts = [];
            var newEventIds = [];
            for (var i in notes) {
                newTexts.push(qmlProfilerModelProxy.noteText(notes[i]))
                newEventIds.push(qmlProfilerModelProxy.noteTimelineIndex(notes[i]));
            }

            // Bindings are only triggered when assigning the whole array.
            eventIds = newEventIds;
            texts = newTexts;
        }

        visible: eventIds.length > 0
        iconSource: "ico_note.png"
        tooltip: texts.join("\n");
        onClicked: {
            if (++currentNote >= eventIds.length)
                currentNote = 0;
            view.selectFromEventIndex(modelIndex, eventIds[currentNote]);
        }
    }

238 239 240 241 242 243 244 245 246 247
    ToolButton {
        id: expandButton
        anchors.verticalCenter: txt.verticalCenter
        anchors.right: parent.right
        implicitWidth: 17
        implicitHeight: txt.height - 1
        enabled: expanded || trigger(qmlProfilerModelProxy.count(modelIndex)) > 0
        iconSource: expanded ? "arrow_down.png" : "arrow_right.png"
        tooltip: qsTr(expanded ? "Collapse category" : "Expand category.")
        onClicked: qmlProfilerModelProxy.setExpanded(modelIndex, !expanded);
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

    Rectangle {
        id: dragger
        property int modelIndex
        width: labelContainer.width
        height: 0
        color: "black"
        opacity: 0.5
        anchors.left: parent.left

        // anchor to top so that it reliably snaps back after dragging
        anchors.top: parent.top

        Drag.active: dragArea.drag.active
        Drag.onActiveChanged: {
            // We don't want height, text, or modelIndex to be changed when reordering occurs, so we
            // don't make them properties.
            draggerText.text = txt.text;
            modelIndex = labelContainer.modelIndex;
            if (Drag.active) {
                height = labelContainer.height;
                labelContainer.dragStarted();
            } else {
                height = 0;
                labelContainer.dragStopped();
            }
        }

        states: [
            State {
                when: dragger.Drag.active
                ParentChange {
                    target: dragger
                    parent: draggerParent
                }
                PropertyChanges {
                    target: dragger
                    anchors.top: undefined
                }
            }
        ]

        Text {
            id: draggerText
            visible: parent.Drag.active
            x: txt.x
            font.pixelSize: txt.font.pixelSize
            color: "white"
            width: txt.width
            height: txt.height
            verticalAlignment: txt.verticalAlignment
            renderType: txt.renderType
        }
    }

    MouseArea {
        anchors.top: dragArea.bottom
        anchors.bottom: labelContainer.dragging ? labelContainer.bottom : dragArea.bottom
        anchors.left: labelContainer.left
        anchors.right: labelContainer.right
        cursorShape: dragArea.cursorShape
    }

Christiaan Janssen's avatar
Christiaan Janssen committed
312
}