Debian Codegraph @

src/GUI/qml/AircraftDownloadPanel.qml

raw
1
2 // SPDX-FileCopyrightText: 2017 James Turner
3 // SPDX-License-Identifier: GPL-2.0-or-later
4
5 import QtQuick 2.4
6 import FlightGear.Launcher 1.0
7 import FlightGear 1.0
8
9 Item {
10 id: root
11
12 property url uri
13 property int installStatus: LocalAircraftCache.PackageNotInstalled
14 property int packageSize: 0
15
16 property int downloadedBytes: 0
17
18 readonly property bool active: (installStatus == LocalAircraftCache.PackageQueued) ||
19 (installStatus == LocalAircraftCache.PackageDownloading)
20
21 readonly property int compactWidth: button.width + sizeText.width
22
23 property bool compact: false
24
25 implicitWidth: childrenRect.width
26 implicitHeight: Math.max(button.height, sizeText.height, progressColumn.height)
27
28 state: "not-installed"
29
30 function updateState()
31 {
32 if (!_launcher.isNetworkAvailable) {
33 if (installStatus == LocalAircraftCache.PackageInstalled) {
34 state = "installed";
35 } else {
36 state = "offline";
37 }
38 return;
39 }
40
41 if (installStatus == LocalAircraftCache.PackageInstalled) {
42 state = "installed";
43 } else if (installStatus == LocalAircraftCache.PackageNotInstalled) {
44 state = "not-installed"
45 } else if (installStatus == LocalAircraftCache.PackageUpdateAvailable) {
46 state = "has-update"
47 } else if (installStatus == LocalAircraftCache.PackageQueued) {
48 state = "queued"
49 } else if (installStatus == LocalAircraftCache.PackageDownloading) {
50 state = "downloading"
51 }
52 }
53
54 onInstallStatusChanged: updateState()
55
56 states: [
57 State {
58 name: "not-installed"
59
60 PropertyChanges {
61 target: button
62 text: qsTr("Install")
63 hoverText: ""
64 visible: true
65 }
66
67 PropertyChanges { target: sizeText; visible: true }
68 PropertyChanges { target: confirmUninstallPanel; visible: false }
69 PropertyChanges { target: uninstallButton; visible: false }
70 },
71
72 State {
73 name: "installed"
74
75 PropertyChanges { target: uninstallButton; visible: true }
76 PropertyChanges { target: sizeText; visible: true }
77 PropertyChanges { target: confirmUninstallPanel; visible: false }
78 PropertyChanges { target: button; visible: false }
79 },
80
81 State {
82 name: "has-update"
83
84 PropertyChanges {
85 target: button
86 text: qsTr("Update")
87 hoverText: ""
88 visible: true
89 }
90
91 PropertyChanges { target: uninstallButton; visible: true }
92 PropertyChanges { target: sizeText; visible: true }
93 PropertyChanges { target: confirmUninstallPanel; visible: false }
94 },
95
96 State {
97 name: "queued"
98
99 PropertyChanges {
100 target: sizeText
101 visible: true
102 }
103
104 PropertyChanges {
105 target: button
106 text: qsTr("Queued")
107 hoverText: qsTr("Cancel")
108 visible: true
109 }
110
111 PropertyChanges { target: uninstallButton; visible: false }
112 PropertyChanges { target: confirmUninstallPanel; visible: false }
113 },
114
115 State {
116 name: "downloading"
117 PropertyChanges { target: progressFrame; visible: true }
118 PropertyChanges { target: statusText; visible: true }
119 PropertyChanges { target: sizeText; visible: false }
120 PropertyChanges { target: confirmUninstallPanel; visible: false }
121 PropertyChanges { target: uninstallButton; visible: false }
122
123 PropertyChanges {
124 target: button
125 text: qsTr("Downloading")
126 hoverText: qsTr("Cancel")
127 visible: true
128 }
129 },
130
131 State {
132 name: "confirm-uninstall"
133 PropertyChanges { target: button; visible: false }
134 PropertyChanges { target: uninstallButton; visible: false }
135 PropertyChanges { target: progressFrame; visible: false }
136 PropertyChanges { target: statusText; visible: false }
137 PropertyChanges { target: sizeText; visible: false }
138 PropertyChanges { target: confirmUninstallPanel; visible: true }
139 },
140
141 State {
142 name: "offline"
143
144 PropertyChanges { target: uninstallButton; visible: false }
145 PropertyChanges { target: sizeText; visible: true }
146 PropertyChanges { target: confirmUninstallPanel; visible: false }
147 PropertyChanges { target: button; visible: false }
148 }
149 ]
150
151 Row {
152 id: buttonRow
153 spacing: Style.margin
154 Button {
155 id: button
156 onClicked: {
157 if ((root.state == "has-update") || (root.state == "not-installed")) {
158 _launcher.requestInstallUpdate(root.uri);
159 } else {
160 _launcher.requestInstallCancel(root.uri)
161 }
162 }
163 }
164
165 Button {
166 id: uninstallButton
167 text: qsTr("Uninstall")
168 anchors.verticalCenter: button.verticalCenter
169 onClicked: root.state = "confirm-uninstall"
170 }
171 }
172
173 StyledText {
174 id: sizeText
175 anchors.left: buttonRow.right
176 anchors.leftMargin: Style.margin
177 anchors.right: parent.right
178 anchors.verticalCenter: buttonRow.verticalCenter
179 text: qsTr("Size: ") + (root.packageSize / 0x100000).toFixed(1) + qsTr(" MB")
180 }
181
182 Column {
183 id: progressColumn
184
185 anchors.verticalCenter: buttonRow.verticalCenter
186 anchors.left: buttonRow.right
187 anchors.leftMargin: Style.margin
188 anchors.right: parent.right
189
190 Rectangle {
191 id: progressFrame
192 radius: 6
193 height: 12
194
195 width: parent.width - (Style.margin * 2)
196 visible: false // hidden by default
197
198 border.color: Style.minorFrameColor
199 border.width: 2
200
201 Rectangle {
202 id: progressBar
203
204 radius:3
205 height: 6
206 anchors.verticalCenter: parent.verticalCenter
207
208 color: Style.themeColor
209
210 readonly property real fraction: root.downloadedBytes / root.packageSize
211 readonly property real maxWidth: parent.width - 10
212
213 x: 5
214 width: maxWidth * fraction
215 }
216 }
217
218 // show download progress textually, or error message if a problem occurs
219 StyledText {
220 id: statusText
221 visible: false
222 text: (compact ? "" : qsTr("Downloaded ")) + (root.downloadedBytes / 0x100000).toFixed(1) +
223 qsTr(" MB of ") + (root.packageSize / 0x100000).toFixed(1) + qsTr(" MB");
224 }
225 } // item container for progress bar and text
226
227
228
229 YesNoPanel {
230 id: confirmUninstallPanel
231 anchors.fill: parent
232 promptText: qsTr("Are you sure you want to uninstall this aircraft?")
233 yesIsDestructive: true
234 yesText: qsTr("Uninstall")
235 noText: qsTr("Cancel")
236
237 // revert to the current underlying state
238 onRejected: updateState()
239 onAccepted: _launcher.requestUninstall(root.uri)
240 }
241 }