• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • practice
mixedlettersmodewidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Daniel Laidig <d.laidig@gmx.de>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 
18 */
19 
20 #include "mixedlettersmodewidget.h"
21 #include "ui_practice_widget_written.h"
22 
23 #include <krandomsequence.h>
24 #include <kcolorscheme.h>
25 
26 #include <QFontMetrics>
27 #include <QPainter>
28 
29 using namespace Practice;
30 
31 MixedLettersModeWidget::MixedLettersModeWidget(GuiFrontend *frontend, QWidget *parent)
32  : WrittenPracticeWidget(frontend, parent)
33 {
34  m_ui->mixedSolutionLabel->show();
35  connect(m_ui->answerEdit, SIGNAL(textChanged(QString)), this, SLOT(updatePixmap()));
36 }
37 
38 void MixedLettersModeWidget::setSolutionFont(const QFont& font)
39 {
40  m_solutionFont = font;
41  WrittenPracticeWidget::setSolutionFont(font);
42 }
43 
44 void MixedLettersModeWidget::setQuestion(const QVariant& question)
45 {
46  m_question = question.toString();
47  WrittenPracticeWidget::setQuestion(question);
48 }
49 
50 void MixedLettersModeWidget::showQuestion()
51 {
52  WrittenPracticeWidget::showQuestion();
53  updatePixmap();
54 }
55 
56 void MixedLettersModeWidget::updatePixmap()
57 {
58  QFontMetrics fm(m_solutionFont);
59  int charHeight = fm.height();
60  int charWidth = fm.averageCharWidth();
61  m_pixmap = QPixmap(charWidth * m_mixedSolution.length() * 2 + charWidth, charHeight * 3);
62  m_pixmap.fill(QColor(0, 0, 0, 0));
63 
64  QPainter p(&m_pixmap);
65  p.setFont(m_solutionFont);
66  KColorScheme scheme(QPalette::Active);
67  QPen defaultPen = p.pen();
68  defaultPen.setColor(palette().color(QPalette::WindowText));
69  QString enteredChars = m_ui->answerEdit->text();
70  int i = 0;
71  Q_FOREACH(QChar ch, m_mixedSolution) {
72  int pos = enteredChars.indexOf(ch);
73  if (pos != -1) {
74  p.setPen(scheme.foreground(KColorScheme::InactiveText).color());
75  enteredChars.remove(pos, 1);
76  } else {
77  p.setPen(defaultPen);
78  }
79  p.drawText(charWidth + charWidth * i * 2, charHeight + int(m_positions.at(i)*charHeight * 0.25), ch);
80  i++;
81  }
82  m_ui->mixedSolutionLabel->setPixmap(m_pixmap);
83  m_ui->mixedSolutionLabel->setMinimumSize(m_pixmap.size());
84 }
85 
86 void MixedLettersModeWidget::setSolution(const QVariant& solution)
87 {
88  WrittenPracticeWidget::setSolution(solution);
89  m_solution = solution.toString();
90  QList<QChar> chars;
91  Q_FOREACH(QChar ch, solution.toString()) {
92  chars.append(ch);
93  }
94  KRandomSequence random = KRandomSequence();
95  random.randomize(chars);
96  m_mixedSolution.clear();
97  m_positions.clear();
98  Q_FOREACH(QChar ch, chars) {
99  m_mixedSolution.append(ch);
100  m_positions.append(random.getLong(8));
101  }
102 }
103 
104 #include "mixedlettersmodewidget.moc"
QList::clear
void clear()
Practice::WrittenPracticeWidget::setQuestion
virtual void setQuestion(const QVariant &question)
Definition: writtenpracticewidget.cpp:94
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QPixmap::size
QSize size() const
QWidget
QString::append
QString & append(QChar ch)
QWidget::palette
const QPalette & palette() const
Practice::GuiFrontend
Definition: guifrontend.h:35
Practice::WrittenPracticeWidget::setSolutionFont
virtual void setSolutionFont(const QFont &font)
Definition: writtenpracticewidget.cpp:77
QPixmap::fill
void fill(const QColor &color)
QChar
QFont
QList::at
const T & at(int i) const
QFontMetrics
QString::remove
QString & remove(int position, int n)
QString::clear
void clear()
QList::append
void append(const T &value)
QPainter
QWidget::pos
QPoint pos() const
QString
QList
QColor
QPen::setColor
void setColor(const QColor &color)
Practice::MixedLettersModeWidget::setSolution
virtual void setSolution(const QVariant &solution)
Definition: mixedlettersmodewidget.cpp:86
QPixmap
Practice::WrittenPracticeWidget
Definition: writtenpracticewidget.h:29
QWidget::font
const QFont & font() const
Practice::MixedLettersModeWidget::MixedLettersModeWidget
MixedLettersModeWidget(GuiFrontend *frontend, QWidget *parent=0)
Definition: mixedlettersmodewidget.cpp:31
Practice::MixedLettersModeWidget::showQuestion
void showQuestion()
Definition: mixedlettersmodewidget.cpp:50
QString::length
int length() const
QPen
Practice::MixedLettersModeWidget::setSolutionFont
virtual void setSolutionFont(const QFont &font)
Definition: mixedlettersmodewidget.cpp:38
mixedlettersmodewidget.h
Practice::WrittenPracticeWidget::setSolution
virtual void setSolution(const QVariant &solution)
Definition: writtenpracticewidget.cpp:134
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QVariant::toString
QString toString() const
Practice::MixedLettersModeWidget::setQuestion
virtual void setQuestion(const QVariant &question)
Definition: mixedlettersmodewidget.cpp:44
Practice::WrittenPracticeWidget::m_ui
Ui::WrittenPracticeWidget * m_ui
Definition: writtenpracticewidget.h:72
Practice::WrittenPracticeWidget::showQuestion
void showQuestion()
Definition: writtenpracticewidget.cpp:108
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal