Debian Codegraph @

src/GUI/qml/SidebarButton.qml

raw
1 // SPDX-FileCopyrightText: 2018 James Turner
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 import QtQuick 2.4
5 import FlightGear 1.0
6
7 Item {
8 id: root
9
10 readonly property int __smallMargin: 2
11
12 width: Style.strutSize * 2
13 height: iconImage.height + label.height + Style.margin + (__smallMargin * 2)
14
15 property string icon: ""
16 property alias label: label.text
17
18 signal clicked()
19
20 property bool selected: false
21 property bool enabled: true
22
23 property string disabledText: ""
24
25 onEnabledChanged: {
26 // if we become enabled, ensure we hide the
27 // disabled prompt (can happen if hovering over 'fly!')
28 if (enabled) disabledTextBox.hide();
29 }
30
31 Rectangle {
32 id: baseRect
33 anchors.fill: parent
34 visible: root.enabled & (root.selected | mouse.containsMouse)
35 color: Style.activeColor
36 }
37
38 Image {
39 id: iconImage
40 source: root.icon + "?themeContrast"
41 anchors.horizontalCenter: parent.horizontalCenter
42 opacity: root.enabled ? 1.0 : 0.5
43 y: __smallMargin
44 }
45
46 Text {
47 id: label
48 color: Style.themeContrastTextColor
49 // enabled appearance is done via opacity to match the icon
50 opacity: root.enabled ? 1.0 : 0.5
51 width: parent.width
52 horizontalAlignment: Text.AlignHCenter
53 anchors.top: iconImage.bottom
54 anchors.topMargin: __smallMargin
55 font.pixelSize: Style.baseFontPixelSize
56 }
57
58 MouseArea {
59 id: mouse
60 anchors.fill: parent
61 hoverEnabled: true
62 onClicked: if (root.enabled) root.clicked();
63
64 onEntered: {
65 // disabled tooltip
66 if (!root.enabled) tooltipTimer.restart()
67 }
68
69 onExited: {
70 tooltipTimer.stop()
71 disabledTextBox.hide();
72 }
73 }
74
75 Timer {
76 id: tooltipTimer
77 onTriggered: {
78 disabledTextBox.show()
79 }
80 }
81
82 Rectangle {
83 id: disabledTextBox
84 height:disabledTextContent.implicitHeight + Style.margin * 2
85 width: disabledTextContent.implicitWidth + Style.margin * 2
86 color: Style.themeColor
87 visible: false
88
89 anchors.left: baseRect.right
90 anchors.verticalCenter: baseRect.verticalCenter
91
92 function show() {
93 hideDisabledAnimation.stop();
94 showDisabledAnimation.start();
95 }
96
97 function hide() {
98 if (!visible)
99 return;
100 showDisabledAnimation.stop();
101 hideDisabledAnimation.start();
102 }
103
104 Text {
105 id: disabledTextContent
106 x: Style.margin
107 y: Style.margin
108 color: "white"
109 font.pixelSize: Style.subHeadingFontPixelSize
110 verticalAlignment: Text.AlignVCenter
111 text: root.disabledText
112 }
113
114 SequentialAnimation {
115 id: showDisabledAnimation
116 PropertyAction { target:disabledTextBox; property:"visible"; value: true }
117 NumberAnimation {
118 target:disabledTextBox; property:"opacity"
119 from: 0.0; to: 1.0
120 duration: 500
121 }
122 }
123
124 SequentialAnimation {
125 id: hideDisabledAnimation
126 NumberAnimation {
127 target:disabledTextBox; property:"opacity"
128 from: 1.0; to: 0.0
129 duration: 500
130 }
131 PropertyAction { target:disabledTextBox; property:"visible"; value: false }
132 }
133 } // of disabled text box
134 }