Warning, /education/kwordquiz/src/qml/MultipleChoicePage.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 import QtQuick
0005 import QtQuick.Layouts
0006 import QtQuick.Controls as QQC2
0007 import org.kde.kirigami as Kirigami
0008 import org.kde.kirigamiaddons.formcard as FormCard
0009 import org.kde.kwordquiz 1.0
0010 
0011 BasePage {
0012     id: root
0013 
0014     property bool wasCorrect: true
0015 
0016     readonly property int multipleChoiceLimit: 4
0017 
0018     listView.delegate: Rectangle {
0019         id: wordDelegate
0020 
0021         required property string question
0022         required property string questionImage
0023         required property string questionSound
0024         required property string answer
0025         required property string answerImage
0026         required property string answerSound
0027         required property var multipleChoice
0028 
0029         readonly property bool isCurrentItem: ListView.isCurrentItem
0030         readonly property bool hasSelection: multipleChoiceGroup.checkedButton !== null
0031 
0032         width: ListView.view.width
0033         height: ListView.view.height
0034 
0035         color: Kirigami.Theme.backgroundColor
0036 
0037         visible: listView.count >= root.multipleChoiceLimit && !root.finished
0038 
0039         onIsCurrentItemChanged: if (!isCurrentItem) {
0040             multipleChoiceGroup.checkState = Qt.Unchecked;
0041         }
0042 
0043         onHasSelectionChanged: if (Prefs.autoCheck && hasSelection) {
0044             check();
0045             root.showAnswer = true;
0046         }
0047 
0048         function check() {
0049             if (!multipleChoiceGroup.checkedButton || multipleChoiceGroup.checkedButton.text !== wordDelegate.answer) {
0050                 root.wasCorrect = false;
0051                 randomSortModel.markAsError(listView.currentIndex);
0052                 root.errors++;
0053             } else {
0054                 root.wasCorrect = true;
0055                 randomSortModel.unMarkAsError(listView.currentIndex);
0056             }
0057         }
0058 
0059         ColumnLayout {
0060             anchors.centerIn: parent
0061             width: parent.width - Kirigami.Units.gridUnit * 4
0062 
0063             Kirigami.Heading {
0064                 text: wordDelegate.question
0065             }
0066 
0067             QQC2.ButtonGroup {
0068                 id: multipleChoiceGroup
0069             }
0070 
0071             Repeater {
0072                 model: wordDelegate.multipleChoice
0073 
0074                 FormCard.FormRadioDelegate {
0075                     text: modelData
0076                     leftPadding: Kirigami.Units.smallSpacing
0077                     rightPadding: Kirigami.Units.smallSpacing
0078                     topPadding: Kirigami.Units.smallSpacing
0079                     bottomPadding: Kirigami.Units.smallSpacing
0080                     Layout.fillWidth: true
0081                     QQC2.ButtonGroup.group: multipleChoiceGroup
0082                 }
0083             }
0084 
0085             QQC2.Button {
0086                 icon.name: "checkbox"
0087                 text: i18nc("@action:button", "Check")
0088                 enabled: listView.currentItem && listView.currentItem.hasSelection
0089                 onClicked: {
0090                     listView.currentItem.check();
0091                     root.showAnswer = true;
0092                 }
0093                 visible: !root.showAnswer && !root.finished && !Prefs.autoCheck
0094             }
0095 
0096             Kirigami.Heading {
0097                 level: 3
0098                 visible: root.showAnswer
0099                 text: root.wasCorrect ? i18n("This is correct") : i18n("Wrong, the correct answer was \"%1\".", wordDelegate.answer)
0100 
0101                 horizontalAlignment: Text.AlignHCenter
0102 
0103                 Layout.fillWidth: true
0104             }
0105 
0106             Image {
0107                 Layout.fillWidth: true
0108                 Layout.maximumHeight: Kirigami.Units.gridUnit * 8
0109 
0110                 fillMode: Image.PreserveAspectFit
0111                 visible: root.showAnswer
0112                 smooth: true
0113                 source: 'file:' + wordDelegate.answerImage
0114             }
0115 
0116             Loader {
0117                 active: wordDelegate.answerSound && root.showAnswer
0118 
0119                 Layout.fillWidth: true
0120                 Layout.maximumWidth: Kirigami.Units.gridUnit * 10
0121                 Layout.alignment: Qt.AlignHCenter
0122 
0123                 sourceComponent: SoundPlayer {
0124                     source: 'file:' + wordDelegate.answerSound
0125                 }
0126             }
0127 
0128             QQC2.Button {
0129                 icon.name: "go-next"
0130                 text: i18nc("@action:button", "Next")
0131                 visible: root.showAnswer
0132                 onClicked: {
0133                     root.showAnswer = false;
0134                     if (listView.currentIndex + 1 === listView.count) {
0135                         root.finished = true;
0136                     } else {
0137                         listView.incrementCurrentIndex();
0138                     }
0139                 }
0140                 Layout.alignment: Qt.AlignHCenter
0141             }
0142         }
0143     }
0144 
0145     readonly property var _placeHolder: Kirigami.PlaceholderMessage {
0146         parent: root
0147         anchors.centerIn: parent
0148         width: parent.width - Kirigami.Units.gridUnit * 4
0149         visible: listView.count < 4
0150         text: i18n("Not enough words in your deck")
0151         explanation: i18n("You need at least 3 words to enable the multiple choice mode")
0152     }
0153 }