File indexing completed on 2024-04-21 03:51:07
0001 /* 0002 SPDX-FileCopyrightText: 2010 Daniel Laidig <d.laidig@gmx.de> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "summarybarwidget.h" 0007 0008 #include <KColorScheme> 0009 #include <KLocalizedString> 0010 0011 #include <QEvent> 0012 #include <QHBoxLayout> 0013 #include <QHelpEvent> 0014 #include <QLabel> 0015 #include <QPainter> 0016 #include <QPainterPath> 0017 0018 using namespace Practice; 0019 0020 SummaryBarWidget::SummaryBarWidget(QWidget *parent) 0021 : QWidget(parent) 0022 { 0023 setMinimumHeight(30); 0024 m_layout = new QHBoxLayout(this); 0025 setLayout(m_layout); 0026 layout()->setContentsMargins(0, layout()->contentsMargins().top() + BAR_HEIGHT, 0, 0); 0027 setupCaption(); 0028 } 0029 0030 void SummaryBarWidget::paintEvent(QPaintEvent *event) 0031 { 0032 QWidget::paintEvent(event); 0033 0034 QPainter painter(this); 0035 QLinearGradient linearGrad(QPoint(0, 0), QPoint(rect().width(), 0)); 0036 0037 KColorScheme scheme(QPalette::Active); 0038 QColor correctColor = scheme.foreground(KColorScheme::PositiveText).color(); 0039 QColor wrongColor = scheme.foreground(KColorScheme::NegativeText).color(); 0040 QColor notAnsweredColor = scheme.foreground(KColorScheme::NormalText).color(); 0041 0042 if (!m_total) { 0043 return; 0044 } 0045 double correctPos = double(m_correct) / m_total; 0046 double wrongPos = correctPos + double(m_wrong) / m_total; 0047 0048 double margin = double(2) / rect().width(); 0049 0050 if (m_correct > 0) { 0051 linearGrad.setColorAt(qMax(correctPos - margin, 0.0), correctColor); 0052 } 0053 if (m_wrong > 0) { 0054 linearGrad.setColorAt(qMin(correctPos + margin, 1.0), wrongColor); 0055 linearGrad.setColorAt(qMax(wrongPos - margin, 0.0), wrongColor); 0056 } 0057 if (m_notAnswered > 0) { 0058 linearGrad.setColorAt(qMin(wrongPos + margin, 1.0), notAnsweredColor); 0059 } 0060 0061 QRect r = rect(); 0062 r.setHeight(BAR_HEIGHT); 0063 r.adjust(1, 1, -1, -1); 0064 0065 QPainterPath path; 0066 path.addRoundedRect(r, 3.0, 3.0); 0067 painter.setBrush(QBrush(linearGrad)); 0068 painter.drawPath(path); 0069 } 0070 0071 void SummaryBarWidget::setStatistics(int correct, int wrong, int notAnswered) 0072 { 0073 m_correct = correct; 0074 m_wrong = wrong; 0075 m_notAnswered = notAnswered; 0076 m_total = m_correct + m_wrong + m_notAnswered; 0077 0078 m_correctCaption->setText(i18nc("test results", "%1 % correct", qRound(m_correct * 100.0 / m_total))); 0079 m_correctCaption->setToolTip(correctText()); 0080 m_wrongCaption->setText(i18nc("test results", "%1 % wrong", qRound(m_wrong * 100.0 / m_total))); 0081 m_wrongCaption->setToolTip(wrongText()); 0082 m_notAnsweredCaption->setText(i18nc("test results", "%1 % not answered", qRound(m_notAnswered * 100.0 / m_total))); 0083 m_notAnsweredCaption->setToolTip(notAnsweredText()); 0084 0085 update(); 0086 } 0087 0088 bool SummaryBarWidget::event(QEvent *event) 0089 { 0090 if (event->type() == QEvent::ToolTip) { 0091 QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event); 0092 if (!m_total) 0093 return QWidget::event(event); 0094 0095 if (helpEvent->y() >= BAR_HEIGHT) { 0096 return false; 0097 } 0098 0099 int correctPos = int(rect().width() * double(m_correct) / m_total); 0100 int wrongPos = correctPos + int(rect().width() * double(m_wrong) / m_total); 0101 if (helpEvent->x() <= correctPos) { 0102 setToolTip(correctText()); 0103 } else if (helpEvent->x() <= wrongPos) { 0104 setToolTip(wrongText()); 0105 } else { 0106 setToolTip(notAnsweredText()); 0107 } 0108 } 0109 return QWidget::event(event); 0110 } 0111 0112 void SummaryBarWidget::setupCaption() 0113 { 0114 QLabel *correctColorLabel = new QLabel(this); 0115 m_layout->addWidget(correctColorLabel); 0116 m_correctCaption = new QLabel(this); 0117 m_layout->addWidget(m_correctCaption); 0118 m_layout->addSpacing(10); 0119 QLabel *wrongColorLabel = new QLabel(this); 0120 m_layout->addWidget(wrongColorLabel); 0121 m_wrongCaption = new QLabel(this); 0122 m_layout->addWidget(m_wrongCaption); 0123 m_layout->addSpacing(10); 0124 QLabel *notAnsweredColorLabel = new QLabel(this); 0125 m_layout->addWidget(notAnsweredColorLabel); 0126 m_notAnsweredCaption = new QLabel(this); 0127 m_layout->addWidget(m_notAnsweredCaption); 0128 m_layout->addStretch(); 0129 0130 KColorScheme scheme(QPalette::Active); 0131 correctColorLabel->setPixmap(captionPixmap(scheme.foreground(KColorScheme::PositiveText).color())); 0132 wrongColorLabel->setPixmap(captionPixmap(scheme.foreground(KColorScheme::NegativeText).color())); 0133 notAnsweredColorLabel->setPixmap(captionPixmap(scheme.foreground(KColorScheme::NormalText).color())); 0134 } 0135 0136 QString SummaryBarWidget::correctText() 0137 { 0138 if (!m_total) 0139 return QString(); 0140 return i18n("Answered correctly on the first attempt: %1 of %2 (%3 %)", m_correct, m_total, qRound(m_correct * 100.0 / m_total)); 0141 } 0142 0143 QString SummaryBarWidget::wrongText() 0144 { 0145 if (!m_total) 0146 return QString(); 0147 return i18n("Answered wrong on the first attempt: %1 of %2 (%3 %)", m_wrong, m_total, qRound(m_wrong * 100.0 / m_total)); 0148 } 0149 0150 QString SummaryBarWidget::notAnsweredText() 0151 { 0152 if (!m_total) 0153 return QString(); 0154 return i18n("Not answered during this practice: %1 of %2 (%3 %)", m_notAnswered, m_total, qRound(m_notAnswered * 100.0 / m_total)); 0155 } 0156 0157 QPixmap SummaryBarWidget::captionPixmap(QColor color) 0158 { 0159 QImage image(20, 20, QImage::Format_ARGB32_Premultiplied); 0160 image.fill(QColor(Qt::transparent).rgba()); 0161 QPainter painter(&image); 0162 painter.setBrush(color); 0163 painter.setPen(Qt::black); 0164 0165 QPainterPath path; 0166 path.addRoundedRect(image.rect().adjusted(0, 0, -1, -1), 3.0, 3.0); 0167 painter.drawPath(path); 0168 return QPixmap::fromImage(image); 0169 } 0170 0171 #include "moc_summarybarwidget.cpp"