• 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
utils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2015 Inge Wallin
3  ***************************************************************************/
4 
5 /***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 
14 
15 // Own
16 #include "utils.h"
17 
18 // Qt
19 #include <QPainter>
20 #include <QRect>
21 
22 // KEduVocDocument library
23 #include <keduvocdocument.h>
24 #include <keduvocexpression.h>
25 
26 // Parley
27 #include "prefs.h"
28 
29 // ----------------------------------------------------------------
30 // class WordCount
31 
32 
33 WordCount::WordCount()
34 {
35  clear();
36 }
37 
38 
39 void WordCount::clear()
40 {
41  for (int i = 0; i <= KV_MAX_GRADE; ++i) {
42  grades[i] = 0;
43  pregrades[i] = 0;
44  }
45  invalid = 0;
46 
47  initialWords = 0;
48  totalWords = 0;
49 }
50 
51 int WordCount::percentageCompleted() const
52 {
53  // To calculate the percentage done we add:
54  // * 1..KV_MAX_GRADE points for words in the initial phase (grade = 0, pregrade > 0)
55  // * KV_MAX_GRADE * (1..KV_MAX_GRADE) points for words in the long-term phase (grade>0)
56  // So the maximum number of points is KV_MAX_GRADE^2 per word.
57  //
58  // In the final calculation, we exclude all invalid words from the percentage.
59  int points = 0;
60  for (int i = 0; i <= KV_MAX_GRADE + 1; ++i) {
61  points += pregrades[i] * i;
62  points += grades[i] * KV_MAX_GRADE * i;
63  }
64 
65  if (totalWords - invalid == 0) {
66  // Don't divide by 0.
67  return 0;
68  }
69  else {
70  return 100 * points / ((totalWords - invalid) * KV_MAX_GRADE * KV_MAX_GRADE);
71  }
72 }
73 
74 
75 void WordCount::fillFromContainer(KEduVocContainer &container, int translationIndex,
76  KEduVocContainer::EnumEntriesRecursive recursive)
77 {
78  clear();
79 
80  foreach (KEduVocExpression *entry, container.entries(recursive)) {
81  KEduVocTranslation &translation(*entry->translation(translationIndex));
82 
83  ++totalWords;
84  if (translation.isEmpty()) {
85  ++invalid;
86  } else if (translation.preGrade() > 0) {
87  // Initial phase (we assume correctness, i.e. if pregrade>0 then grade = 0)
88  ++initialWords;
89  ++pregrades[translation.preGrade()];
90  } else {
91  // Long term or unpracticed
92  ++grades[translation.grade()];
93  }
94  }
95 }
96 
97 
98 // ----------------------------------------------------------------
99 // class confidenceColors
100 
101 
102 ConfidenceColors::ConfidenceColors(ColorScheme colorScheme)
103 {
104  initColors(colorScheme);
105 }
106 
107 
108 void ConfidenceColors::initColors(ColorScheme colorScheme)
109 {
110  switch (colorScheme) {
111  case MultiColorScheme:
112  default: // Not default at the last line. Hope this works...
113 
114  longTermColors[0] = QColor(25,38,41);
115  //longTermColors[1] = QColor(Qt::yellow);
116  longTermColors[1] = QColor(25,38,41,64);
117  longTermColors[2] = QColor(237,21,21);
118  longTermColors[3] = QColor(246,116,0);
119  longTermColors[4] = QColor(201,206,59);
120  longTermColors[5] = QColor(28,220,154);
121  longTermColors[6] = QColor(17,209,22);
122  longTermColors[7] = QColor(61,174,253);
123 
124  initialTermColor = QColor(25,38,41,64); // Find something else
125 
126  invalidColor = QColor(Qt::red);
127  break;
128 
129  case ProgressiveColorScheme:
130  {
131  static const int AlphaMax = 255;
132  static const int AlphaStep = ((AlphaMax - 10) / KV_MAX_GRADE);
133 
134  QColor color;
135 
136  // Confidence 1..max
137  for (int grade = 1; grade <= KV_MAX_GRADE; ++grade) {
138  color = Prefs::gradeColor();
139  color.setAlpha(AlphaMax - (KV_MAX_GRADE - grade) * AlphaStep);
140 
141  longTermColors[grade] = color;
142  }
143 
144  // Unpracticed (confidence 0)
145  color = QColor("#FFFFFF");
146  color.setAlpha(AlphaMax);
147  longTermColors[0] = color;
148 
149  // Use one color for all initial phase values
150  color = Prefs::preGradeColor();
151  color.setAlpha(AlphaMax);
152  initialTermColor = color;
153 
154  // Invalid
155  invalidColor = Prefs::invalidUnitColor();
156 
157  break;
158  }
159  }
160 
161  // These two are placeholders for the wordcloud background color.
162  frontEndColors[0] = QColor(255,221,217);
163  frontEndColors[1] = QColor(238,232,213);
164 }
165 
166 
167 // ----------------------------------------------------------------
168 // Various utility functions
169 
170 
171 void paintColorBar(QPainter &painter, const QRect &rect,
172  const WordCount &wordCount, const ConfidenceColors &colors)
173 {
174  // The outline of the total bar.
175  QRect roundedRect(rect);
176  roundedRect.adjust(1, 1, -1, -1);
177  QPainterPath roundedPath;
178  roundedPath.addRoundedRect(roundedRect, 2.0, 2.0);
179 
180  int xPosition = 0;
181 
182  // >0: grade, 0: initial, -1: untrained, -2: invalid
183  for (int i = KV_MAX_GRADE; i >= -2; --i) {
184  qreal fraction;
185  QColor color;
186 
187  // Get the fraction and the color
188  if (i > 0) {
189  // long term
190  fraction = qreal(wordCount.grades[i]) / qreal(wordCount.totalWords);
191  color = colors.longTermColors[i];
192  } else if (i == 0) {
193  // initial term
194  fraction = qreal(wordCount.initialWords) / qreal(wordCount.totalWords);
195  color = colors.initialTermColor;
196  } else if (i == -1) {
197  // untrained (stored in longterm[0])
198  fraction = qreal(wordCount.grades[0]) / qreal(wordCount.totalWords);
199  color = colors.longTermColors[0];
200  } else {
201  fraction = qreal(wordCount.invalid) / qreal(wordCount.totalWords);
202  color = colors.invalidColor;
203  }
204 
205  // Create a rect from the current fraction and
206  // adjust the rect to the outer limits.
207  int barElementWidth = fraction * rect.width();
208  //int barElementWidth = fraction * roundedRect.width(); <-- Use this instead?
209  QRectF barElement(rect.x() + xPosition, rect.y(), barElementWidth, rect.height());
210  QPainterPath barElementPath;
211  barElementPath.addRect(barElement);
212 
213  // Intersect the QPainterPath of inner rectangle with outer,
214  // so that the inner rectangle takes the shape of the outer rounded rectangle.
215  QPainterPath barElementIntersectedPath = roundedPath.intersected(barElementPath);
216  xPosition += barElementWidth;
217 
218  // Paint!
219  painter.setBrush(QBrush(color));
220  painter.drawPath(barElementIntersectedPath);
221  }
222 }
Prefs::invalidUnitColor
static QColor invalidUnitColor()
Get Color used to display the confidence levels.
Definition: prefs.h:1058
ConfidenceColors::ProgressiveColorScheme
Definition: utils.h:53
WordCount::invalid
int invalid
Definition: utils.h:41
paintColorBar
void paintColorBar(QPainter &painter, const QRect &rect, const WordCount &wordCount, const ConfidenceColors &colors)
Definition: utils.cpp:171
QPainterPath::addRoundedRect
void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
QColor::setAlpha
void setAlpha(int alpha)
QRect::height
int height() const
QBrush
QRect::x
int x() const
QRect::y
int y() const
prefs.h
ConfidenceColors::longTermColors
QColor longTermColors[KV_MAX_GRADE+1]
Definition: utils.h:60
WordCount
Definition: utils.h:30
ConfidenceColors::invalidColor
QColor invalidColor
Definition: utils.h:62
utils.h
QRect
QPainter
WordCount::initialWords
int initialWords
Definition: utils.h:43
QPainterPath::addRect
void addRect(const QRectF &rectangle)
Prefs::preGradeColor
static QColor preGradeColor()
Get Color used to display the confidence levels.
Definition: prefs.h:1039
QPainter::setBrush
void setBrush(const QBrush &brush)
QColor
WordCount::percentageCompleted
int percentageCompleted() const
Definition: utils.cpp:51
WordCount::totalWords
int totalWords
Definition: utils.h:45
WordCount::pregrades
int pregrades[KV_MAX_GRADE+1]
Definition: utils.h:40
ConfidenceColors::initialTermColor
QColor initialTermColor
Definition: utils.h:61
WordCount::WordCount
WordCount()
Definition: utils.cpp:33
QPainterPath
ConfidenceColors::ConfidenceColors
ConfidenceColors(ColorScheme colorScheme=MultiColorScheme)
Definition: utils.cpp:102
QRect::width
int width() const
QPainter::drawPath
void drawPath(const QPainterPath &path)
QRectF
ConfidenceColors::initColors
void initColors(ColorScheme colorScheme)
Definition: utils.cpp:108
Prefs::gradeColor
static QColor gradeColor()
Get Color used to display the confidence levels.
Definition: prefs.h:1020
QRect::adjust
void adjust(int dx1, int dy1, int dx2, int dy2)
ConfidenceColors
Definition: utils.h:50
ConfidenceColors::MultiColorScheme
Definition: utils.h:52
ConfidenceColors::ColorScheme
ColorScheme
Definition: utils.h:51
WordCount::grades
int grades[KV_MAX_GRADE+1]
Definition: utils.h:39
WordCount::clear
void clear()
Definition: utils.cpp:39
QPainterPath::intersected
QPainterPath intersected(const QPainterPath &p) const
WordCount::fillFromContainer
void fillFromContainer(KEduVocContainer &container, int translationIndex, KEduVocContainer::EnumEntriesRecursive recursive=KEduVocContainer::Recursive)
Definition: utils.cpp:75
ConfidenceColors::frontEndColors
QColor frontEndColors[2]
Definition: utils.h:63
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