Debian Codegraph @

src/activities/graduated_line_read/GraduatedLineRead.qml

raw
1 /* GCompris - GraduatedLineRead.qml
2 *
3 * SPDX-FileCopyrightText: 2023 Bruno ANSELME <[email protected]>
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6 pragma ComponentBehavior: Bound
7
8 import QtQuick
9 import core 1.0
10 import "../../core"
11 import "qrc:/gcompris/src/core/core.js" as Core
12 import "graduated_line_read.js" as Activity
13
14 ActivityBase {
15 id: activity
16 property string activityMode: "tick2number" // Possible values: tick2number, number2tick
17 property string instruction: qsTr("Find the requested number.")
18
19 onStart: focus = true
20 onStop: {}
21
22 pageComponent: Image {
23 id: activityBackground
24 source: "qrc:/gcompris/src/activities/chess/resource/background-wood.svg"
25 anchors.fill: parent
26 fillMode: Image.PreserveAspectCrop
27 sourceSize.height: height
28 signal start
29 signal stop
30
31 Component.onCompleted: {
32 activity.start.connect(start)
33 activity.stop.connect(stop)
34 }
35
36 // Add here the QML items you need to access in javascript
37 QtObject {
38 id: items
39 property Item main: activity.main
40 property alias activityBackground: activityBackground
41 property int currentLevel: activity.currentLevel
42 property int currentSubLevel: 0
43 property int numberOfSubLevel: 0
44 property alias bonus: bonus
45 property alias score: score
46 property alias numPad: numPad
47 property alias padModel: padModel
48 property var levels: activity.datasets
49 property alias goodAnswerSound: goodAnswerSound
50 property alias badAnswerSound: badAnswerSound
51 property alias clickSound: clickSound
52 property alias smudgeSound: smudgeSound
53 property int solutionGrad: 0 // Solution's graduation
54 property int orientation: Qt.LeftToRight // Updated with ApplicationSettings in start() function
55
56 property alias ruler: ruler
57 property alias cursor: cursor
58 property alias rulerModel: rulerModel
59 property alias leftLimit: leftLimit
60 property alias rightLimit: rightLimit
61 property alias boxText: boxText
62 property string answer: ""
63 property alias errorRectangle: errorRectangle
64 property bool buttonsBlocked: false
65 property int segmentThickness: GCStyle.thinBorder
66 property int denominator: 1
67 property alias client: client
68 }
69
70 onStart: { Activity.start(items, activity.activityMode) }
71 onStop: { Activity.stop() }
72
73 GCSoundEffect {
74 id: goodAnswerSound
75 source: "qrc:/gcompris/src/core/resource/sounds/completetask.wav"
76 }
77
78 GCSoundEffect {
79 id: badAnswerSound
80 source: "qrc:/gcompris/src/core/resource/sounds/crash.wav"
81 }
82
83 GCSoundEffect {
84 id: clickSound
85 source: "qrc:/gcompris/src/core/resource/sounds/audioclick.wav"
86 }
87
88 GCSoundEffect {
89 id: smudgeSound
90 source: "qrc:/gcompris/src/core/resource/sounds/smudge.wav"
91 }
92
93 Client { // Client for server version. Prepare data from activity to server
94 id: client
95 getDataCallback: function() {
96 var input
97 switch (activityMode) {
98 case "tick2number":
99 input = items.cursor.children[items.solutionGrad].textValue
100 break
101 case "number2tick":
102 input = items.rulerModel.get(items.solutionGrad).value_.toString()
103 break
104 }
105 var ticks = []
106 for (var i = 0; i < rulerModel.count; i++) {
107 ticks.push(rulerModel.get(i).value_)
108 }
109
110 var data = {
111 "start": Number(rulerModel.get(0).value_),
112 "end": Number(rulerModel.get(rulerModel.count - 1).value_),
113 "ticks": Number(rulerModel.count),
114 "expected": Number(items.answer),
115 "proposal": Number(input)
116 }
117 return data
118 }
119 }
120
121 GCTextPanel {
122 id: instructionPanel
123 panelWidth: parent.width - 2 * GCStyle.baseMargins
124 panelHeight: Math.min(50 * ApplicationInfo.ratio, activityBackground.height * 0.2)
125 fixedHeight: true
126 anchors.horizontalCenter: parent.horizontalCenter
127 anchors.top: parent.top
128 anchors.topMargin: GCStyle.baseMargins
129 textItem.text: activity.instruction
130 }
131
132 Item {
133 id: layoutArea
134 anchors.top: instructionPanel.bottom
135 anchors.bottom: activityBackground.bottom
136 anchors.bottomMargin: 1.5 * bar.height
137 anchors.left: activityBackground.left
138 anchors.right: activityBackground.right
139 anchors.margins: GCStyle.baseMargins
140 }
141
142 ListModel { id: rulerModel }
143
144 Rectangle {
145 id: rulerView
146 anchors.top: layoutArea.top
147 anchors.left: layoutArea.left
148 anchors.right: layoutArea.right
149 height: Math.min(120 * ApplicationInfo.ratio, layoutArea.height * 0.4)
150 color: "#e9e9e9"
151 radius: GCStyle.baseMargins
152 property real rulerWidth: rulerView.width - leftLimit.width
153 property real rulerModelWidth: ruler.width / (rulerModel.count - 1)
154 Column {
155 id: rulerViewColumn
156 width: rulerView.rulerWidth
157 height: parent.height - GCStyle.baseMargins - leftLimit.contentHeight
158 anchors.horizontalCenter: parent.horizontalCenter
159 anchors.horizontalCenterOffset: rulerView.rulerModelWidth * ((items.orientation === Qt.LeftToRight) ? -0.5 : 0.5)
160 anchors.top: parent.top
161 anchors.topMargin: GCStyle.baseMargins
162 Row {
163 id: cursor
164 z: 100
165 width: parent.width
166 height: parent.height * 0.4
167 anchors.horizontalCenter: parent.horizontalCenter
168 layoutDirection: items.orientation
169 Repeater {
170 model: rulerModel
171 delegate : Item {
172 required property int index
173 required property int value_
174 property int value: value_
175 property bool hidden: (index !== items.solutionGrad)
176 property alias textValue: txt.text
177 width: rulerView.rulerModelWidth
178 height: parent.height
179 opacity: hidden ? 0.0 : 1.0
180 Image {
181 visible: activity.activityMode === "number2tick"
182 source: "qrc:/gcompris/src/activities/graduated_line_read/resource/arrow.svg"
183 anchors.centerIn: parent
184 anchors.verticalCenterOffset: GCStyle.halfMargins
185 height: parent.height
186 width: height
187 sourceSize.width: height
188 }
189 Rectangle {
190 id: txtBg
191 visible: activity.activityMode === "tick2number" ? true : txt.text != ""
192 anchors.centerIn: parent
193 height: parent.height
194 width: 90 * ApplicationInfo.ratio
195 radius: GCStyle.tinyMargins
196 color: GCStyle.whiteBg
197 border.color: "#633E0C"
198 border.width: GCStyle.thinBorder
199 }
200 GCText {
201 id: txt
202 anchors.centerIn: txtBg
203 height: txtBg.height - GCStyle.baseMargins
204 width: txtBg.width - GCStyle.baseMargins
205 horizontalAlignment: Text.AlignHCenter
206 verticalAlignment: Text.AlignVCenter
207 text: ""
208 fontSize: mediumSize
209 fontSizeMode: Text.Fit
210 color: GCStyle.darkText
211 }
212 }
213 }
214 }
215
216 Row {
217 id: ruler
218 width: rulerView.rulerWidth
219 height: parent.height * 0.6
220 layoutDirection: items.orientation
221 anchors.horizontalCenter: parent.horizontalCenter
222 Repeater {
223 model: rulerModel
224 delegate : Item {
225 id: rulerElement
226 required property int index
227 required property int value_
228 required property int thickness_
229 property int value: value_
230 property int thickness: thickness_
231 width: rulerView.rulerModelWidth
232 height: parent.height
233 transform: Scale { origin.x: width * 0.5; xScale: (items.orientation === Qt.LeftToRight) ? 1 : -1 }
234
235 Rectangle { // Line between graduations
236 width: (rulerElement.index && (rulerElement.index !== (rulerModel.count - 1))) ? parent.width : parent.width * 0.5
237 height: items.segmentThickness
238 anchors.verticalCenter: parent.verticalCenter
239 anchors.left: (rulerElement.index) ? parent.left : undefined
240 anchors.right: (!rulerElement.index) ? parent.right : undefined
241 color: "#c27a33"
242 }
243 Rectangle { // vertical graduation
244 width: rulerElement.thickness
245 height: ((!rulerElement.index) || (rulerElement.index === (rulerModel.count - 1)) || (rulerElement.index === items.solutionGrad))
246 ? parent.height : (parent.height * 0.5)
247 anchors.centerIn: parent
248 color: (rulerElement.index === items.solutionGrad) ? "#633E0C" : "#c27a33"
249 radius: (rulerElement.index === items.solutionGrad) ? 0 : width * 0.5
250 }
251 }
252 }
253 }
254 }
255 }
256
257 Rectangle {
258 anchors.centerIn: leftLimit
259 width: GCStyle.baseMargins
260 height: leftLimit.contentHeight
261 color: "#e9e9e9"
262 }
263
264 GCText {
265 id: leftLimit
266 width: 80 * ApplicationInfo.ratio
267 anchors.left: layoutArea.left
268 anchors.bottom: rulerView.bottom
269 horizontalAlignment: Text.AlignHCenter
270 verticalAlignment: Text.AlignVCenter
271 fontSize: smallSize
272 fixFontSize: true
273 }
274
275 Rectangle {
276 anchors.centerIn: rightLimit
277 width: GCStyle.baseMargins
278 height: rightLimit.contentHeight
279 color: "#e9e9e9"
280 }
281
282 GCText {
283 id: rightLimit
284 width: leftLimit.width
285 anchors.right: layoutArea.right
286 anchors.bottom: rulerView.bottom
287 horizontalAlignment: Text.AlignHCenter
288 verticalAlignment: Text.AlignVCenter
289 fontSize: smallSize
290 fixFontSize: true
291 }
292
293 ErrorRectangle {
294 id: errorRectangle
295 width: activity.activityMode === "tick2number" ? 90 * ApplicationInfo.ratio : 0
296 height: cursor.height
297 property real cursorOffset: (rulerView.rulerModelWidth - width) * 0.5
298 x: items.orientation === Qt.LeftToRight ? rulerView.x + rulerViewColumn.x + cursor.x + rulerView.rulerModelWidth * items.solutionGrad + cursorOffset :
299 rulerView.x + rulerView.width - rulerViewColumn.x - cursor.x - rulerView.rulerModelWidth * items.solutionGrad + cursorOffset
300 y: rulerView.y + rulerViewColumn.y + cursor.y
301 radius: GCStyle.tinyMargins
302 imageSize: height * 1.2
303 function releaseControls() {
304 items.buttonsBlocked = false;
305 }
306 }
307
308 ListModel {
309 id: padModel
310 ListElement { label: "7"; key: Qt.Key_7 }
311 ListElement { label: "8"; key: Qt.Key_8 }
312 ListElement { label: "9"; key: Qt.Key_9 }
313 ListElement { label: ""; key: Qt.Key_Space } // empty, but set a key to avoid errors or warnings.
314 ListElement { label: "4"; key: Qt.Key_4 }
315 ListElement { label: "5"; key: Qt.Key_5 }
316 ListElement { label: "6"; key: Qt.Key_6 }
317 ListElement { label: ""; key: Qt.Key_Space } // empty
318 ListElement { label: "1"; key: Qt.Key_1 }
319 ListElement { label: "2"; key: Qt.Key_2 }
320 ListElement { label: "3"; key: Qt.Key_3 }
321 ListElement { label: ""; key: Qt.Key_Space } // empty
322 ListElement { label: "0"; key: Qt.Key_0 }
323 ListElement { label: ""; key: Qt.Key_Period } // only displayed for decimal levels
324 ListElement { label: "<<"; key: Qt.Key_Backspace }
325 ListElement { label: "C"; key: Qt.Key_Delete }
326 }
327
328 Row {
329 id: tools
330 width: childrenRect.width
331 height: layoutArea.height - rulerView.height - anchors.topMargin
332 anchors.top: rulerView.bottom
333 anchors.topMargin: GCStyle.baseMargins * 2
334 anchors.horizontalCenter: parent.horizontalCenter
335 spacing: GCStyle.baseMargins
336 Image {
337 id: leftButton
338 source: 'qrc:/gcompris/src/core/resource/arrow_left.svg'
339 smooth: true
340 width: Math.min(GCStyle.bigButtonHeight, layoutArea.width * 0.15)
341 height: width
342 sourceSize.width: width
343 sourceSize.height: width
344 fillMode: Image.PreserveAspectFit
345 visible: (activity.activityMode === "number2tick")
346 MouseArea {
347 id: leftArea
348 anchors.fill: parent
349 hoverEnabled: true
350 enabled: !items.buttonsBlocked
351 onContainsMouseChanged: leftButton.scale = (containsMouse) ? 1.1 : 1.0
352 onClicked: (items.orientation === Qt.LeftToRight) ? Activity.moveLeft() : Activity.moveRight()
353 }
354 Behavior on scale {
355 PropertyAnimation {
356 target: leftButton
357 properties: "scale"
358 duration: 100
359 }
360 }
361 }
362
363 Rectangle {
364 width: 80 * ApplicationInfo.ratio
365 height: 40 * ApplicationInfo.ratio
366 color: "white"
367 radius: GCStyle.halfMargins
368 anchors.verticalCenter: leftButton.verticalCenter
369 visible: (activity.activityMode === "number2tick")
370 GCText {
371 id: boxText
372 anchors.centerIn: parent
373 width: parent.width - GCStyle.baseMargins
374 height: parent.height - GCStyle.baseMargins
375 horizontalAlignment: Text.AlignHCenter
376 verticalAlignment: Text.AlignVCenter
377 fontSizeMode: Text.Fit
378 fontSize: mediumSize
379 text: {
380 if(items.denominator == 1) {
381 return items.answer
382 }
383 else {
384 return Activity.longInt(Number(items.answer) / items.denominator)
385 }
386 }
387 }
388 }
389
390 Image {
391 id: rightButton
392 source: 'qrc:/gcompris/src/core/resource/arrow_right.svg'
393 smooth: true
394 width: leftButton.width
395 height: leftButton.width
396 sourceSize.width: leftButton.width
397 sourceSize.height: leftButton.width
398 fillMode: Image.PreserveAspectFit
399 visible: (activity.activityMode === "number2tick")
400 MouseArea {
401 id: rightArea
402 anchors.fill: parent
403 hoverEnabled: true
404 enabled: !items.buttonsBlocked
405 onContainsMouseChanged: rightButton.scale = (containsMouse) ? 1.1 : 1.0
406 onClicked: (items.orientation === Qt.LeftToRight) ? Activity.moveRight() : Activity.moveLeft()
407 }
408 Behavior on scale {
409 PropertyAnimation {
410 target: rightButton
411 properties: "scale"
412 duration: 100
413 }
414 }
415 }
416
417 GridView {
418 id: numPad
419 width: Math.ceil(4 * cellWidth)
420 height: Math.ceil(4 * cellHeight)
421 cellWidth: Math.min(60 * ApplicationInfo.ratio, maxCellWidth)
422 cellHeight: Math.min(60 * ApplicationInfo.ratio, tools.height * 0.25)
423 interactive: false
424 visible: (activity.activityMode === "tick2number")
425 model: padModel
426
427 property int maxCellWidth: (layoutArea.width - (score.width + GCStyle.baseMargins) * 2) * 0.25
428
429 delegate: Rectangle {
430 id: numKey
431 width: numPad.cellWidth - GCStyle.tinyMargins
432 height: numPad.cellHeight - GCStyle.tinyMargins
433 anchors.margins: border.width
434 color: numArea.containsMouse ? "#C0C0C0" : "#E5E5E5"
435 border.color: GCStyle.grayBorder
436 border.width: GCStyle.thinnestBorder
437 radius: GCStyle.tinyMargins
438 enabled: visible
439 visible: label != ""
440
441 required property var key
442 required property int index
443 required property string label
444
445 GCText {
446 anchors.centerIn: parent
447 width: parent.width - GCStyle.baseMargins
448 height: parent.height - GCStyle.baseMargins
449 horizontalAlignment: Text.AlignHCenter
450 verticalAlignment: Text.AlignVCenter
451 fontSizeMode: Text.Fit
452 fontSize: mediumSize
453 text: numKey.label
454 }
455 MouseArea {
456 id: numArea
457 anchors.fill: parent
458 hoverEnabled: true
459 enabled: !items.buttonsBlocked
460 onClicked: Activity.handleKeys(numKey.key)
461 }
462 states: [
463 State {
464 name: ""
465 PropertyChanges {
466 numKey {
467 color: numArea.containsMouse ? "#C0C0C0" : "#E5E5E5"
468 }
469 }
470 },
471 State {
472 name: "pressed"
473 PropertyChanges {
474 numKey {
475 color: "#999"
476 }
477 }
478 }
479 ]
480 transitions: [
481 Transition {
482 to: "pressed"
483 SequentialAnimation {
484 ScriptAction { script: clickSound.play() }
485 ColorAnimation { duration: 100 }
486 ScriptAction { script: state = "" }
487 }
488 }
489 ]
490 Component.onCompleted: Activity.mapToPad[key] = index
491 }
492 }
493 }
494
495 DialogChooseLevel {
496 id: dialogActivityConfig
497 currentActivity: activity.activityInfo
498
499 onSaveData: {
500 levelFolder = dialogActivityConfig.chosenLevels
501 currentActivity.currentLevels = dialogActivityConfig.chosenLevels
502 }
503 onClose: {
504 activity.home()
505 }
506 onStartActivity: {
507 activityBackground.stop()
508 activityBackground.start()
509 }
510 }
511
512 DialogHelp {
513 id: dialogHelp
514 onClose: activity.home()
515 }
516
517 Score {
518 id: score
519 numberOfSubLevels: items.numberOfSubLevel
520 currentSubLevel: items.currentSubLevel
521 anchors.top: undefined
522 anchors.bottom: layoutArea.bottom
523 anchors.bottomMargin: 0
524 anchors.right: parent.right
525 fixedWidth: true
526 width: GCStyle.bigButtonHeight
527 onStop: Activity.nextSubLevel()
528 }
529
530 Bar {
531 id: bar
532 level: items.currentLevel + 1
533 content: BarEnumContent { value: help | home | level | activityConfig }
534 onHelpClicked: activity.displayDialog(dialogHelp)
535 onActivityConfigClicked: activity.displayDialog(dialogActivityConfig)
536 onPreviousLevelClicked: Activity.previousLevel()
537 onNextLevelClicked: Activity.nextLevel()
538 onHomeClicked: activity.home()
539 }
540
541 BarButton {
542 id: okButton
543 source: "qrc:/gcompris/src/core/resource/bar_ok.svg"
544 width: Math.min(GCStyle.bigButtonHeight, tools.height * 0.4)
545 anchors.bottom: score.top
546 anchors.bottomMargin: GCStyle.baseMargins
547 anchors.horizontalCenter: score.horizontalCenter
548 onClicked: Activity.checkResult()
549 visible: (items.cursor.children[items.solutionGrad].textValue !== "") || (activityMode === "number2tick")
550 mouseArea.enabled: !items.buttonsBlocked
551 }
552
553 Bonus {
554 id: bonus
555 Component.onCompleted: win.connect(Activity.nextLevel)
556 }
557
558 Keys.onPressed: (event) => { Activity.handleEvents(event) }
559 }
560
561 }