Warning, /education/kwordquiz/src/qml/QuestionAnswerPage.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 2.15 0005 import QtQuick.Layouts 1.15 0006 import QtQuick.Controls 2.15 as QQC2 0007 import org.kde.kirigami 2.20 as Kirigami 0008 import org.kde.kwordquiz 1.0 0009 0010 BasePage { 0011 id: root 0012 0013 property bool wasCorrect: true 0014 property bool hintGiven: false 0015 0016 listView.delegate: Rectangle { 0017 id: wordDelegate 0018 0019 required property string question 0020 required property string questionImage 0021 required property string questionSound 0022 required property string answer 0023 required property string answerImage 0024 required property string answerSound 0025 0026 readonly property bool isCurrentItem: ListView.isCurrentItem 0027 readonly property bool hasSelection: answerField.text.length > 0 0028 0029 onIsCurrentItemChanged: if (!isCurrentItem) { 0030 answerField.text = ''; 0031 } else { 0032 answerField.forceActiveFocus(); 0033 } 0034 0035 function check() { 0036 let correctAnswer = blankAnswer.hasBlank ? blankAnswer.correctAnswer.trim() : answer.trim(); 0037 let givenAnswer = answerField.text.trim(); 0038 0039 if (!Prefs.caseSensitiveAnswers) { 0040 correctAnswer = correctAnswer.toLowerCase(); 0041 givenAnswer = givenAnswer.toLowerCase(); 0042 } 0043 0044 if (correctAnswer === givenAnswer && (!root.hintGiven || !Prefs.hintError)) { 0045 root.wasCorrect = true; 0046 randomSortModel.unMarkAsError(listView.currentIndex); 0047 } else { 0048 root.wasCorrect = correctAnswer === givenAnswer; 0049 randomSortModel.markAsError(listView.currentIndex); 0050 root.errors++; 0051 } 0052 root.hintGiven = false; 0053 } 0054 0055 function giveHint() { 0056 const currentAnswer = answerField.text; 0057 const correctAnswer = blankAnswer.hasBlank ? blankAnswer.correctAnswer.trim() : answer.trim(); 0058 0059 for (let i = 0; i < correctAnswer.length; i++) { 0060 if (i < currentAnswer.length && currentAnswer[i] === correctAnswer[i]) { 0061 continue; 0062 } 0063 0064 answerField.text = correctAnswer.substring(0, i + 1); 0065 break; 0066 } 0067 } 0068 0069 width: ListView.view.width 0070 height: ListView.view.height 0071 0072 color: Kirigami.Theme.backgroundColor 0073 0074 visible: !root.finished 0075 0076 ColumnLayout { 0077 anchors.centerIn: parent 0078 width: parent.width - Kirigami.Units.gridUnit * 4 0079 0080 Image { 0081 Layout.fillWidth: true 0082 Layout.maximumHeight: Kirigami.Units.gridUnit * 8 0083 0084 fillMode: Image.PreserveAspectFit 0085 smooth: true 0086 source: 'file:' + wordDelegate.questionImage 0087 } 0088 0089 Loader { 0090 active: wordDelegate.questionSound 0091 0092 Layout.fillWidth: true 0093 Layout.maximumWidth: Kirigami.Units.gridUnit * 10 0094 Layout.alignment: Qt.AlignHCenter 0095 0096 sourceComponent: SoundPlayer { 0097 source: 'file:' + wordDelegate.questionSound 0098 } 0099 } 0100 0101 Kirigami.Heading { 0102 text: wordDelegate.question.replace(/\[(.*?)\]/, '$1') 0103 wrapMode: Text.Wrap 0104 horizontalAlignment: Text.AlignHCenter 0105 0106 Layout.fillWidth: true 0107 } 0108 0109 Kirigami.Separator { 0110 Layout.fillWidth: true 0111 } 0112 0113 BlankAnswer { 0114 id: blankAnswer 0115 input: wordDelegate.answer 0116 } 0117 0118 Kirigami.Heading { 0119 visible: blankAnswer.hasBlank 0120 text: blankAnswer.blankedAnswer 0121 wrapMode: Text.Wrap 0122 horizontalAlignment: Text.AlignHCenter 0123 0124 Layout.fillWidth: true 0125 } 0126 0127 QQC2.TextField { 0128 id: answerField 0129 0130 Layout.alignment: Qt.AlignHCenter 0131 onAccepted: if (root.showAnswer) { 0132 nextAction.trigger(); 0133 } else { 0134 checkAction.trigger(); 0135 } 0136 } 0137 0138 RowLayout { 0139 Layout.alignment: Qt.AlignHCenter 0140 visible: !root.showAnswer && !root.finished 0141 0142 QQC2.Button { 0143 text: i18nc("@action:button", "Hint") 0144 onClicked: { 0145 root.hintGiven = true; 0146 listView.currentItem.giveHint(); 0147 } 0148 } 0149 0150 QQC2.Button { 0151 text: i18nc("@action:button", "Check") 0152 onClicked: { 0153 listView.currentItem.check(); 0154 root.showAnswer = true; 0155 } 0156 enabled: listView.currentItem && listView.currentItem.hasSelection 0157 } 0158 } 0159 0160 0161 Kirigami.Heading { 0162 level: 3 0163 visible: root.showAnswer 0164 text: root.wasCorrect ? i18n("This is correct") : i18n("Wrong, the correct answer was \"%1\".", wordDelegate.answer) 0165 0166 horizontalAlignment: Text.AlignHCenter 0167 0168 Layout.fillWidth: true 0169 } 0170 0171 Image { 0172 Layout.fillWidth: true 0173 Layout.maximumHeight: Kirigami.Units.gridUnit * 8 0174 0175 fillMode: Image.PreserveAspectFit 0176 visible: root.showAnswer 0177 smooth: true 0178 source: 'file:' + wordDelegate.answerImage 0179 } 0180 0181 Loader { 0182 active: wordDelegate.answerSound && root.showAnswer 0183 0184 Layout.fillWidth: true 0185 Layout.maximumWidth: Kirigami.Units.gridUnit * 10 0186 Layout.alignment: Qt.AlignHCenter 0187 0188 sourceComponent: SoundPlayer { 0189 source: 'file:' + wordDelegate.answerSound 0190 } 0191 } 0192 0193 QQC2.Button { 0194 icon.name: "go-next" 0195 text: i18nc("@action:button", "Next") 0196 visible: root.showAnswer 0197 onClicked: { 0198 root.showAnswer = false; 0199 if (listView.currentIndex + 1 === listView.count) { 0200 root.finished = true; 0201 } else { 0202 listView.incrementCurrentIndex(); 0203 } 0204 } 0205 0206 Layout.alignment: Qt.AlignHCenter 0207 } 0208 } 0209 } 0210 }