16 #include <kcolorscheme.h>
22 #include <QHBoxLayout>
25 using namespace Practice;
28 :
QWidget(parent), m_correct(0), m_wrong(0), m_notAnswered(0), m_total(0)
31 m_layout =
new QHBoxLayout(
this);
33 layout()->setContentsMargins(0, layout()->contentsMargins().top()+BAR_HEIGHT, 0, 0);
39 QWidget::paintEvent(event);
41 QPainter painter(
this);
42 QLinearGradient linearGrad(QPoint(0, 0), QPoint(rect().width(), 0));
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();
52 double correctPos = double(m_correct)/m_total;
53 double wrongPos = correctPos + double(m_wrong)/m_total;
55 double margin = double(2)/rect().width();
58 linearGrad.setColorAt(qMax(correctPos - margin, 0.0), correctColor);
61 linearGrad.setColorAt(qMin(correctPos + margin, 1.0), wrongColor);
62 linearGrad.setColorAt(qMax(wrongPos - margin, 0.0), wrongColor);
64 if (m_notAnswered > 0) {
65 linearGrad.setColorAt(qMin(wrongPos + margin, 1.0), notAnsweredColor);
69 r.setHeight(BAR_HEIGHT);
70 r.adjust(1, 1, -1, -1);
73 path.addRoundedRect( r, 3.0, 3.0 );
74 painter.setBrush(QBrush(linearGrad));
75 painter.drawPath(path);
82 m_notAnswered = notAnswered;
83 m_total = m_correct + m_wrong + m_notAnswered;
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());
97 if (event->type() == QEvent::ToolTip) {
98 QHelpEvent *helpEvent =
static_cast<QHelpEvent *
>(
event);
100 return QWidget::event(event);
102 if (helpEvent->y() >= BAR_HEIGHT) {
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());
113 setToolTip(notAnsweredText());
116 return QWidget::event(event);
119 void SummaryBarWidget::setupCaption()
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();
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()));
144 QString SummaryBarWidget::correctText()
148 return i18n(
"Answered correctly on the first attempt: %1 of %2 (%3 %)", m_correct, m_total, qRound(m_correct*100.0/m_total));
151 QString SummaryBarWidget::wrongText()
155 return i18n(
"Answered wrong on the first attempt: %1 of %2 (%3 %)", m_wrong, m_total, qRound(m_wrong*100.0/m_total));
158 QString SummaryBarWidget::notAnsweredText()
162 return i18n(
"Not answered during this practice: %1 of %2 (%3 %)", m_notAnswered, m_total, qRound(m_notAnswered*100.0/m_total));
165 QPixmap SummaryBarWidget::captionPixmap(QColor color)
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);
174 path.addRoundedRect(image.rect().adjusted(0, 0, -1, -1), 3.0, 3.0);
175 painter.drawPath(path);
176 return QPixmap::fromImage(image);
179 #include "summarybarwidget.moc"