Warning, /education/gcompris/src/activities/fractions_create/ChartDisplay.qml is written in an unsupported language. File is not indexed.

0001 /* GCompris - ChartDisplay.qml
0002  *
0003  * SPDX-FileCopyrightText: 2022 Johnny Jazeix <jazeix@gmail.com>
0004  * SPDX-FileCopyrightText: 2022 Timothée Giet <animtim@gmail.com>
0005  * SPDX-License-Identifier: GPL-3.0-or-later
0006  */
0007 import QtQuick 2.12
0008 
0009 import "fractions_create.js" as Activity
0010 
0011 Flow {
0012     id: gridContainer
0013     // color needs to be in hex format with lowercase for comparison checks to work
0014     readonly property string selectedColor: "#80ffffff"
0015     readonly property string unselectedColor: "#00000000"
0016     property double layoutWidth: 10
0017     property double layoutHeight: 10
0018     property double gridItemHeight: items.horizontalLayout ?
0019                         Math.min(layoutWidth / repeater.model, layoutHeight) :
0020                         Math.min(layoutWidth, layoutHeight / repeater.model)
0021     property double gridItemWidth: items.horizontalLayout ? gridItemHeight :
0022                         layoutWidth
0023     width: items.horizontalLayout ? gridItemWidth * repeater.model : gridItemWidth
0024     height: items.horizontalLayout ? gridItemHeight : gridItemHeight * repeater.model
0025     flow: items.horizontalLayout ? Flow.LeftToRight : Flow.TopToBottom
0026     spacing: 0
0027     anchors.centerIn: parent
0028     Repeater {
0029         id: repeater
0030         model: Math.ceil(items.numeratorToFind / items.denominatorToFind)
0031         Loader {
0032             id: graphLoader
0033             width: gridContainer.gridItemWidth
0034             height: gridContainer.gridItemHeight
0035             asynchronous: false
0036             source: items.chartType === "pie" ? "PieChart.qml" : "RectangleChart.qml"
0037             property bool horizontalLayout: items.horizontalLayout
0038             property int numberOfCharts: repeater.model
0039         }
0040     }
0041     function initLevel() {
0042         for(var pieIndex = 0; pieIndex < repeater.count; ++ pieIndex) {
0043             repeater.itemAt(pieIndex).item.initLevel(pieIndex);
0044         }
0045     }
0046 
0047     function checkAnswer() {
0048         var goodAnswer = false;
0049         if(activity.mode === "selectPie") {
0050             // count how many selected
0051             var selected = 0;
0052             for(var pieIndex = 0; pieIndex < repeater.count; ++ pieIndex) {
0053                 selected += repeater.itemAt(pieIndex).item.countSelectedParts();
0054             }
0055             goodAnswer = (selected == items.numeratorToFind);
0056         }
0057         else {
0058             // We also accept multiples of the actual solution (it is used in the case you can choose both numerator and denominator).
0059             // For example, if we want 2/4, we also accept 1/2 or 3/6 as good answer.
0060             // We force the check on the denominator not null because 1) it's not possible, 2) if both numerator and denominator to find are 0, it is not a correct answer
0061             goodAnswer = Number(items.denominatorValue) != 0 && (Number(items.numeratorValue) * items.denominatorToFind == items.numeratorToFind * Number(items.denominatorValue));
0062         }
0063         if(goodAnswer) {
0064             Activity.goodAnswer();
0065         }
0066         else {
0067             Activity.badAnswer();
0068         }
0069     }
0070 }