• 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
summarybarwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2010 Daniel Laidig <d.laidig@gmx.de>
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 #include "summarybarwidget.h"
15 
16 #include <kcolorscheme.h>
17 #include <klocale.h>
18 
19 #include <QPainter>
20 #include <QEvent>
21 #include <QHelpEvent>
22 #include <QHBoxLayout>
23 #include <QLabel>
24 
25 using namespace Practice;
26 
27 SummaryBarWidget::SummaryBarWidget(QWidget *parent)
28  : QWidget(parent), m_correct(0), m_wrong(0), m_notAnswered(0), m_total(0)
29 {
30  setMinimumHeight(30);
31  m_layout = new QHBoxLayout(this);
32  setLayout(m_layout);
33  layout()->setContentsMargins(0, layout()->contentsMargins().top() + BAR_HEIGHT, 0, 0);
34  setupCaption();
35 }
36 
37 void SummaryBarWidget::paintEvent(QPaintEvent *event)
38 {
39  QWidget::paintEvent(event);
40 
41  QPainter painter(this);
42  QLinearGradient linearGrad(QPoint(0, 0), QPoint(rect().width(), 0));
43 
44  KColorScheme scheme(QPalette::Active);
45  QColor correctColor = scheme.foreground(KColorScheme::PositiveText).color();
46  QColor wrongColor = scheme.foreground(KColorScheme::NegativeText).color();
47  QColor notAnsweredColor = scheme.foreground(KColorScheme::NormalText).color();
48 
49  if (!m_total) {
50  return;
51  }
52  double correctPos = double(m_correct) / m_total;
53  double wrongPos = correctPos + double(m_wrong) / m_total;
54 
55  double margin = double(2) / rect().width();
56 
57  if (m_correct > 0) {
58  linearGrad.setColorAt(qMax(correctPos - margin, 0.0), correctColor);
59  }
60  if (m_wrong > 0) {
61  linearGrad.setColorAt(qMin(correctPos + margin, 1.0), wrongColor);
62  linearGrad.setColorAt(qMax(wrongPos - margin, 0.0), wrongColor);
63  }
64  if (m_notAnswered > 0) {
65  linearGrad.setColorAt(qMin(wrongPos + margin, 1.0), notAnsweredColor);
66  }
67 
68  QRect r = rect();
69  r.setHeight(BAR_HEIGHT);
70  r.adjust(1, 1, -1, -1);
71 
72  QPainterPath path;
73  path.addRoundedRect(r, 3.0, 3.0);
74  painter.setBrush(QBrush(linearGrad));
75  painter.drawPath(path);
76 }
77 
78 void SummaryBarWidget::setStatistics(int correct, int wrong, int notAnswered)
79 {
80  m_correct = correct;
81  m_wrong = wrong;
82  m_notAnswered = notAnswered;
83  m_total = m_correct + m_wrong + m_notAnswered;
84 
85  m_correctCaption->setText(i18nc("test results", "%1 % correct", qRound(m_correct * 100.0 / m_total)));
86  m_correctCaption->setToolTip(correctText());
87  m_wrongCaption->setText(i18nc("test results", "%1 % wrong", qRound(m_wrong * 100.0 / m_total)));
88  m_wrongCaption->setToolTip(wrongText());
89  m_notAnsweredCaption->setText(i18nc("test results", "%1 % not answered", qRound(m_notAnswered * 100.0 / m_total)));
90  m_notAnsweredCaption->setToolTip(notAnsweredText());
91 
92  update();
93 }
94 
95 bool SummaryBarWidget::event(QEvent *event)
96 {
97  if (event->type() == QEvent::ToolTip) {
98  QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
99  if (!m_total)
100  return QWidget::event(event);
101 
102  if (helpEvent->y() >= BAR_HEIGHT) {
103  return false;
104  }
105 
106  int correctPos = int(rect().width() * double(m_correct) / m_total);
107  int wrongPos = correctPos + int(rect().width() * double(m_wrong) / m_total);
108  if (helpEvent->x() <= correctPos) {
109  setToolTip(correctText());
110  } else if (helpEvent->x() <= wrongPos) {
111  setToolTip(wrongText());
112  } else {
113  setToolTip(notAnsweredText());
114  }
115  }
116  return QWidget::event(event);
117 }
118 
119 void SummaryBarWidget::setupCaption()
120 {
121  QLabel *correctColorLabel = new QLabel(this);
122  m_layout->addWidget(correctColorLabel);
123  m_correctCaption = new QLabel(this);
124  m_layout->addWidget(m_correctCaption);
125  m_layout->addSpacing(10);
126  QLabel *wrongColorLabel = new QLabel(this);
127  m_layout->addWidget(wrongColorLabel);
128  m_wrongCaption = new QLabel(this);
129  m_layout->addWidget(m_wrongCaption);
130  m_layout->addSpacing(10);
131  QLabel *notAnsweredColorLabel = new QLabel(this);
132  m_layout->addWidget(notAnsweredColorLabel);
133  m_notAnsweredCaption = new QLabel(this);
134  m_layout->addWidget(m_notAnsweredCaption);
135  m_layout->addStretch();
136 
137  KColorScheme scheme(QPalette::Active);
138  correctColorLabel->setPixmap(captionPixmap(scheme.foreground(KColorScheme::PositiveText).color()));
139  wrongColorLabel->setPixmap(captionPixmap(scheme.foreground(KColorScheme::NegativeText).color()));
140  notAnsweredColorLabel->setPixmap(captionPixmap(scheme.foreground(KColorScheme::NormalText).color()));
141 
142 }
143 
144 QString SummaryBarWidget::correctText()
145 {
146  if (!m_total)
147  return QString();
148  return i18n("Answered correctly on the first attempt: %1 of %2 (%3 %)", m_correct, m_total, qRound(m_correct * 100.0 / m_total));
149 }
150 
151 QString SummaryBarWidget::wrongText()
152 {
153  if (!m_total)
154  return QString();
155  return i18n("Answered wrong on the first attempt: %1 of %2 (%3 %)", m_wrong, m_total, qRound(m_wrong * 100.0 / m_total));
156 }
157 
158 QString SummaryBarWidget::notAnsweredText()
159 {
160  if (!m_total)
161  return QString();
162  return i18n("Not answered during this practice: %1 of %2 (%3 %)", m_notAnswered, m_total, qRound(m_notAnswered * 100.0 / m_total));
163 }
164 
165 QPixmap SummaryBarWidget::captionPixmap(QColor color)
166 {
167  QImage image(20, 20, QImage::Format_ARGB32_Premultiplied);
168  image.fill(QColor(Qt::transparent).rgba());
169  QPainter painter(&image);
170  painter.setBrush(color);
171  painter.setPen(Qt::black);
172 
173  QPainterPath path;
174  path.addRoundedRect(image.rect().adjusted(0, 0, -1, -1), 3.0, 3.0);
175  painter.drawPath(path);
176  return QPixmap::fromImage(image);
177 }
178 
179 #include "summarybarwidget.moc"
QWidget::layout
QLayout * layout() const
QEvent
QWidget
QEvent::type
Type type() const
QLayout::setContentsMargins
void setContentsMargins(int left, int top, int right, int bottom)
QGradient::setColorAt
void setColorAt(qreal position, const QColor &color)
QLabel::setPixmap
void setPixmap(const QPixmap &)
QPainterPath::addRoundedRect
void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
QHBoxLayout
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QBrush
Practice::SummaryBarWidget::setStatistics
void setStatistics(int correct, int wrong, int notAnswered)
Definition: summarybarwidget.cpp:78
QPoint
QBoxLayout::addSpacing
void addSpacing(int size)
QLinearGradient
Practice::SummaryBarWidget::event
bool event(QEvent *event)
Definition: summarybarwidget.cpp:95
QWidget::contentsMargins
QMargins contentsMargins() const
QWidget::update
void update()
margin
const int margin
Definition: buttondelegate.cpp:25
QWidget::width
int width() const
QRect
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QWidget::paintEvent
virtual void paintEvent(QPaintEvent *event)
Practice::SummaryBarWidget::SummaryBarWidget
SummaryBarWidget(QWidget *parent=0)
Definition: summarybarwidget.cpp:27
QWidget::setLayout
void setLayout(QLayout *layout)
QPainter
QPainter::setBrush
void setBrush(const QBrush &brush)
QLabel::setText
void setText(const QString &)
QString
QColor
QWidget::rect
QRect rect() const
QHelpEvent::x
int x() const
QHelpEvent::y
int y() const
QPixmap
QImage
summarybarwidget.h
QPainterPath
QRect::width
int width() const
QPainter::drawPath
void drawPath(const QPainterPath &path)
QBoxLayout::addStretch
void addStretch(int stretch)
QRect::setHeight
void setHeight(int height)
Practice::SummaryBarWidget::paintEvent
void paintEvent(QPaintEvent *event)
Definition: summarybarwidget.cpp:37
QRect::adjust
void adjust(int dx1, int dy1, int dx2, int dy2)
QWidget::setMinimumHeight
void setMinimumHeight(int minh)
QWidget::setToolTip
void setToolTip(const QString &)
QPaintEvent
QLabel
QHelpEvent
QWidget::event
virtual bool event(QEvent *event)
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