Warning, file /education/parley/src/practice/mixedlettersmodewidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2010 Daniel Laidig <d.laidig@gmx.de> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "mixedlettersmodewidget.h" 0007 #include "ui_practice_widget_written.h" 0008 0009 #include <KColorScheme> 0010 #include <KRandom> 0011 #include <QFontMetrics> 0012 #include <QPainter> 0013 #include <QRandomGenerator> 0014 0015 using namespace Practice; 0016 0017 MixedLettersModeWidget::MixedLettersModeWidget(GuiFrontend *frontend, QWidget *parent) 0018 : WrittenPracticeWidget(frontend, parent) 0019 { 0020 m_ui->mixedSolutionLabel->show(); 0021 connect(m_ui->answerEdit, &QLineEdit::textChanged, this, &MixedLettersModeWidget::updatePixmap); 0022 } 0023 0024 void MixedLettersModeWidget::setSolutionFont(const QFont &font) 0025 { 0026 m_solutionFont = font; 0027 WrittenPracticeWidget::setSolutionFont(font); 0028 } 0029 0030 void MixedLettersModeWidget::setQuestion(const QVariant &question) 0031 { 0032 m_question = question.toString(); 0033 WrittenPracticeWidget::setQuestion(question); 0034 } 0035 0036 void MixedLettersModeWidget::showQuestion() 0037 { 0038 WrittenPracticeWidget::showQuestion(); 0039 updatePixmap(); 0040 } 0041 0042 void MixedLettersModeWidget::updatePixmap() 0043 { 0044 QFontMetrics fm(m_solutionFont); 0045 int charHeight = fm.height(); 0046 int charWidth = fm.averageCharWidth(); 0047 m_pixmap = QPixmap(charWidth * m_mixedSolution.length() * 2 + charWidth, charHeight * 3); 0048 m_pixmap.fill(QColor(0, 0, 0, 0)); 0049 0050 QPainter p(&m_pixmap); 0051 p.setFont(m_solutionFont); 0052 KColorScheme scheme(QPalette::Active); 0053 QPen defaultPen = p.pen(); 0054 defaultPen.setColor(palette().color(QPalette::WindowText)); 0055 QString enteredChars = m_ui->answerEdit->text(); 0056 int i = 0; 0057 for (QChar ch : qAsConst(m_mixedSolution)) { 0058 int pos = enteredChars.indexOf(ch); 0059 if (pos != -1) { 0060 p.setPen(scheme.foreground(KColorScheme::InactiveText).color()); 0061 enteredChars.remove(pos, 1); 0062 } else { 0063 p.setPen(defaultPen); 0064 } 0065 p.drawText(charWidth + charWidth * i * 2, charHeight + int(m_positions.at(i) * charHeight * 0.25), ch); 0066 i++; 0067 } 0068 m_ui->mixedSolutionLabel->setPixmap(m_pixmap); 0069 m_ui->mixedSolutionLabel->setMinimumSize(m_pixmap.size()); 0070 } 0071 0072 void MixedLettersModeWidget::setSolution(const QVariant &solution) 0073 { 0074 WrittenPracticeWidget::setSolution(solution); 0075 m_solution = solution.toString(); 0076 QList<QChar> chars; 0077 for (QChar ch : qAsConst(m_solution)) { 0078 chars.append(ch); 0079 } 0080 KRandom::shuffle(chars); 0081 m_mixedSolution.clear(); 0082 m_positions.clear(); 0083 for (QChar ch : qAsConst(chars)) { 0084 m_mixedSolution.append(ch); 0085 m_positions.append(QRandomGenerator::global()->bounded(8)); 0086 } 0087 }