Files
pay/desktop/widgets/GroupBox.qml
T

134 lines
4.3 KiB
QML
Raw Permalink Normal View History

2021-04-22 22:04:30 +02:00
/*
* This file is part of the Flowee project
* Copyright (C) 2021 Tom Zander <tom@flowee.org>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
2021-05-23 00:27:34 +02:00
import QtQuick 2.11
import QtQuick.Controls 2.11
import QtQuick.Layouts 1.11
2021-04-22 22:04:30 +02:00
Item {
id: root
property bool collapsable: true
2021-10-30 15:23:43 +02:00
property bool collapsed: false
/**
* This is like collapsed, but when the user changes the groupbox collapse-state, this is updated.
* We never overwrite the 'collapsed' property to enable binding it to a model, this is the effective
* collapsed state taking both changes from the `collapsed` property and from user input.
*/
property bool effectiveCollapsed: collapsed
property alias title: titleLabel.text
property alias summary: summaryLabel.text
2021-10-15 20:57:31 +02:00
default property alias content: child.children
2021-04-23 19:59:49 +02:00
property alias columns: child.columns
activeFocusOnTab: collapsable
2021-04-22 22:04:30 +02:00
clip: true
onCollapsedChanged: effectiveCollapsed = collapsed
2021-10-29 18:20:42 +02:00
width: 200 // should be changed by user
implicitHeight: arrowPoint.height + (effectiveCollapsed ? 0 : child.implicitHeight + 25) // 25 = 15 top, 5 bottom of content area
2021-04-22 22:04:30 +02:00
Rectangle { // the outline
width: parent.width
y: titleArea.visible ? titleLabel.height / 2 : arrowPoint.height / 2
height: root.effectiveCollapsed ? 1 : parent.height - y;
2021-04-22 22:04:30 +02:00
color: "#00000000"
border.color: titleLabel.palette.button
2021-04-22 22:04:30 +02:00
border.width: 2
radius: 3
}
MouseArea {
width: parent.width
height: 20
enabled: root.collapsable
2021-11-16 19:12:00 +01:00
visible: enabled // this line is needed to make the cursor shape not be set when not enabled (Qt bug)
onClicked: root.effectiveCollapsed = !root.effectiveCollapsed
2021-10-30 16:29:48 +02:00
cursorShape: Qt.PointingHandCursor
2021-04-22 22:04:30 +02:00
}
Rectangle {
id: titleArea
visible: titleLabel.text !== ""
color: titleLabel.palette.window
width: titleLabel.width + 18 + (summaryLabel.visible ? summaryLabel.width + 20 : 0)
height: titleLabel.height
2021-04-22 22:04:30 +02:00
anchors.left: arrowPoint.right
Label {
id: titleLabel
x: 12
2021-10-15 20:57:31 +02:00
// focus indicator
2021-10-15 20:57:31 +02:00
Rectangle {
anchors.fill: parent
anchors.leftMargin: -2
anchors.rightMargin: -2
color: "#00000000"
border.color: parent.palette.highlight
border.width: 2
visible: root.activeFocus
}
2021-04-22 22:04:30 +02:00
}
Label {
id: summaryLabel
anchors.left: titleLabel.right
anchors.leftMargin: 20
visible: root.effectiveCollapsed && text !== ""
font.italic: true
}
2021-04-22 22:04:30 +02:00
}
Rectangle {
id: arrowPoint
visible: parent.collapsable
color: titleLabel.palette.window
width: point.width + 12
2021-04-22 22:04:30 +02:00
height: point.height + 2
x: 6
y: 2
ArrowPoint {
id: point
x: 10
2021-11-02 19:29:14 +01:00
color: Pay.useDarkSkin ? "white" : "black"
rotation: root.effectiveCollapsed ? 0 : 90
2021-04-22 22:04:30 +02:00
transformOrigin: Item.Center
Behavior on rotation { NumberAnimation {} }
}
}
GridLayout { // user set content
id: child
2021-10-15 20:57:31 +02:00
x: 10
y: titleArea.height + 10
width: root.width - 20
2021-04-22 22:04:30 +02:00
height: implicitHeight
visible: !root.effectiveCollapsed
2021-10-29 18:20:42 +02:00
columns: 1
2021-04-22 22:04:30 +02:00
}
2021-10-30 16:12:41 +02:00
Behavior on implicitHeight { NumberAnimation { } }
2021-10-15 20:57:31 +02:00
Keys.onPressed: {
if (event.key === Qt.Key_Space || event.key === Qt.Key_Enter || event.key === Qt.Key_Return) {
if (root.collapsable) {
root.effectiveCollapsed = !root.effectiveCollapsed
event.accepted = true
2021-10-15 20:57:31 +02:00
}
}
}
2021-04-22 22:04:30 +02:00
}