aboutsummaryrefslogtreecommitdiffstats
path: root/tests/manual/quicklayouts/layoutItemProxy/ms-showhide.qml
blob: 2e4ca140a159095a3e44778a149a2f55df200a33 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
// This example was created for the blog post about responsive layouts:
// https://www.qt.io/blog/responsive-layouts-in-qt

import QtQuick
import QtQuick.Layouts
import QtQuick.Window

Window {
    id: window

    width: 600
    height: 800

    minimumHeight: 500
    minimumWidth: 160

    title: "Window: (" + width + "x" + height + ")"
    visible: true


    component MyButton : Rectangle {
        implicitWidth: 56
        implicitHeight: 56
        Layout.minimumWidth: 56
        Layout.minimumHeight: 56
        Layout.fillWidth: false
        Layout.fillHeight: false
        Layout.alignment: Qt.AlignHCenter
        property string label: ""
        color: "#eaeaea"
        Text {
            font.pixelSize: 32
            font.bold: true
            anchors.centerIn: parent
            text: label
            color: "#243a5e"
        }
    }

    component ButtonBox : Rectangle {

        id: bb
        property Item elideButton: Item {}
        property Item buttonLayout: Item {}

        height: buttonLayout.height + buttonLayout.anchors.margins * 2
        implicitWidth: buttonLayout.width + buttonLayout.anchors.margins * 2

        color: "#c8c8c8"

        function updateItems() {

            if (width == 0)
                return;

            let butts = buttonLayout.children

            let lastBut = undefined
            let xmax = buttonLayout.anchors.margins
            for (let i = butts.length - 1; i>= 0; --i) {
                if (butts[i] !== elideButton && butts[i].visible) {
                    if (butts[i].x + butts[i].width > xmax) {
                        lastBut = butts[i]
                        xmax = butts[i].x + butts[i].width
                    }
                }
            }

            let buttonsRightX = xmax
                              + buttonLayout.spacing * 2
            let hiddenItems = 0
            for (let i = 0; i < butts.length; i++) {
                if (butts[i] === elideButton)
                    continue;
                if (butts[i].visible === false) {
                    hiddenItems++;
                    let buttonAddedWidth = butts[i].width + buttonLayout.spacing
                    if (buttonsRightX + buttonAddedWidth < width - elideButton.implicitWidth - buttonLayout.anchors.margins - buttonLayout.spacing) {
                        buttonsRightX += buttonAddedWidth
                        butts[i].visible = true
                        hiddenItems--;
                    }
                }
            }

            for (let i = butts.length - 1; i>= 0; --i) {
                if ( butts[i] === elideButton)
                    continue;
                if (butts[i].visible === true) {
                    let buttonRemovedWidth = butts[i].width + buttonLayout.spacing
                    if (buttonsRightX > width - elideButton.implicitWidth - buttonLayout.anchors.margins - buttonLayout.spacing) {
                        buttonsRightX -= buttonRemovedWidth
                        butts[i].visible = false
                        hiddenItems++;
                    }
                }
            }

            if (hiddenItems == 0)
                elideButton.visible = false
            else if (hiddenItems == 1) {
                for (let i = butts.length - 1; i>= 0; --i)
                    butts[i].visible = true
                elideButton.visible = false
            } else
                elideButton.visible = true
        }

        onWidthChanged: {
            updateItems()
        }

        onChildrenChanged: {
            updateItems()
        }
    }

    ColumnLayout {
        height: parent.height
        width: parent.width
        spacing: 0
        ButtonBox {
            Layout.fillHeight: false
            Layout.fillWidth: true

            RowLayout {
                id: buttonLayout
                anchors.margins: 10
                anchors.top: parent.top
                anchors.left: parent.left
                spacing: 8
                MyButton { id: aButton; label: "A"; z: 2 }
                MyButton { id: bButton; label: "B"; z: 2 }
                MyButton { id: cButton; label: "C"; z: 2 }
                MyButton { id: dButton; label: "D"; z: 2 }
                MyButton { id: eButton; label: "E"; z: 2 }
                MyButton { id: fButton; label: "F"; z: 2 }
                MyButton { id: gButton; label: "G"; z: 2 }
                MyButton { id: elideButton; label: "..."; z: 2 }
            }
            elideButton: elideButton
            buttonLayout: buttonLayout
        }

        Rectangle {
            id: rect
            Layout.fillWidth: true
            Layout.fillHeight: true
            color: "#fff"
        }
    }
}