File indexing completed on 2024-04-21 03:50:59

0001 /*
0002     SPDX-FileCopyrightText: 2014 Andreas Xavier
0003     SPDX-FileCopyrightText: 2014 Inge Wallin <inge@lysator.liu.se>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // Own
0008 #include "barwidget.h"
0009 
0010 // Qt
0011 #include <QDebug>
0012 #include <QPainter>
0013 #include <QPainterPath>
0014 #include <QPen>
0015 
0016 // KDE
0017 #include <KLocalizedString>
0018 
0019 // Parley
0020 #include "collectionwidget.h" // for COLLWIDTH, etc
0021 
0022 // FIXME: Find a better home for this variable.
0023 ConfidenceColors globalColors = ConfidenceColors();
0024 
0025 BarWidget::BarWidget(QWidget *parent)
0026     : QWidget(parent)
0027 {
0028 }
0029 
0030 BarWidget::BarWidget(WordCount *dueWords, QWidget *parent)
0031     : QWidget(parent)
0032 {
0033     QPalette palette(BarWidget::palette());
0034     palette.setColor(backgroundRole(), Qt::white);
0035     setPalette(palette);
0036 
0037     for (int i = 0; i <= KV_MAX_GRADE; i++) {
0038         m_dueWords[i] = dueWords->grades[i];
0039     }
0040     m_totalDueWords = dueWords->totalWords;
0041     m_percentageCompleted = dueWords->percentageCompleted();
0042 }
0043 
0044 void BarWidget::setDue(WordCount &wc)
0045 {
0046     for (int i = 0; i <= KV_MAX_GRADE; ++i) {
0047         m_dueWords[i] = wc.grades[i];
0048     }
0049     m_totalDueWords = wc.totalWords;
0050 
0051     update();
0052 }
0053 
0054 void BarWidget::paintEvent(QPaintEvent *)
0055 {
0056     QPainter painter(this);
0057     painter.setRenderHint(QPainter::Antialiasing, true);
0058     const int legendWidth = COLLWIDTH - 10;
0059     const int legendHeight = 45;
0060     const int legendOffsetY = 0;
0061     const int legendOffsetX = 0;
0062     // const int alphaValueIncrement = 35;
0063 
0064     int gradeBarWidth[9];
0065     gradeBarWidth[8] = 0;
0066     int gradeBarOffset[9];
0067     gradeBarOffset[8] = 0;
0068 
0069     // qDebug() << "percentage completed: " << m_percentageCompleted;
0070     // qDebug() << "Total due words: " << m_totalDueWords;
0071 
0072     if (m_percentageCompleted < 100) {
0073         for (int j = 7; j >= 0; j--) {
0074             gradeBarWidth[j] = std::max(0.0, static_cast<double>(m_dueWords[j]) / static_cast<double>(m_totalDueWords) * legendWidth);
0075             gradeBarOffset[j] = gradeBarOffset[j + 1] + gradeBarWidth[j + 1];
0076         }
0077     } else {
0078         for (int j = 6; j >= 0; j--) {
0079             gradeBarWidth[j] = 0;
0080             gradeBarOffset[j] = legendWidth;
0081         }
0082         gradeBarWidth[7] = legendWidth;
0083         gradeBarOffset[7] = 0;
0084     }
0085     if (m_percentageCompleted < 100 && m_totalDueWords == 0) {
0086         for (int j = 6; j >= 0; j--) {
0087             gradeBarWidth[j] = 0;
0088             gradeBarOffset[j] = legendWidth;
0089         }
0090         gradeBarWidth[7] = legendWidth;
0091         gradeBarOffset[7] = 0;
0092     }
0093 
0094     QPen penBar(QColor(255, 255, 255));
0095     painter.setPen(penBar);
0096     QRectF roundedRect(0, 0, legendWidth, legendHeight);
0097     roundedRect.adjust(1, 1, -1, -1);
0098     QPainterPath roundedPath;
0099     roundedPath.addRoundedRect(roundedRect, 8.0, 8.0);
0100 
0101     for (int i = 7; i >= 0; i--) {
0102         QRectF barElement(0 + legendOffsetX + gradeBarOffset[i], 0 + legendOffsetY, gradeBarWidth[i], legendHeight);
0103         QPainterPath barElementPath;
0104         barElementPath.addRect(barElement);
0105         QPainterPath barElementIntersectedPath = roundedPath.intersected(barElementPath);
0106         QColor color;
0107         if (m_totalDueWords == 0 && m_percentageCompleted < 100) {
0108             color = QColor(0, 0, 0, 128);
0109         } else {
0110             color = globalColors.longTermColors[i];
0111         }
0112         painter.setBrush(QBrush(color));
0113         painter.drawPath(barElementIntersectedPath);
0114     }
0115 
0116     QPen pen(QColor(255, 255, 255));
0117     // QPen pen(QColor(0, 0, 0));
0118     painter.setPen(pen);
0119     if (m_percentageCompleted < 100) {
0120         painter.drawText(0, 0, legendWidth, legendHeight, Qt::AlignCenter | Qt::TextWordWrap, i18np("%1 word due", "%1 words due", m_totalDueWords));
0121     } else {
0122         painter.drawText(0, 0, legendWidth, legendHeight, Qt::AlignCenter | Qt::TextWordWrap, i18n("Fully learned"));
0123     }
0124 }