File indexing completed on 2024-04-14 05:46:47

0001 /*
0002     SPDX-FileCopyrightText: 2006 Bram Schoenmakers <bramschoenmakers@kde.nl>
0003     SPDX-FileCopyrightText: 2007 Tom Albers <toma@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "rsistatwidget.h"
0009 #include "rsistats.h"
0010 
0011 #include <QGridLayout>
0012 #include <QGroupBox>
0013 #include <QLabel>
0014 #include <QLocale>
0015 #include <QTime>
0016 
0017 #include <KLocalizedString>
0018 #include <QFontDatabase>
0019 
0020 RSIStatWidget::RSIStatWidget(QWidget *parent)
0021     : QWidget(parent)
0022 {
0023     mGrid = new QGridLayout(this);
0024 
0025     QGroupBox *gb = new QGroupBox(i18n("Time"), this);
0026     QGridLayout *subgrid = new QGridLayout(gb);
0027     addStat(TOTAL_TIME, subgrid, 0);
0028     addStat(ACTIVITY, subgrid, 1);
0029     addStat(IDLENESS, subgrid, 2);
0030     addStat(CURRENT_IDLE_TIME, subgrid, 3);
0031     addStat(MAX_IDLENESS, subgrid, 4);
0032     mGrid->addWidget(gb, 0, 0);
0033 
0034     gb = new QGroupBox(i18n("Short Breaks"), this);
0035     subgrid = new QGridLayout(gb);
0036     addStat(TINY_BREAKS, subgrid, 0);
0037     addStat(LAST_TINY_BREAK, subgrid, 1);
0038     addStat(TINY_BREAKS_SKIPPED, subgrid, 2);
0039     addStat(TINY_BREAKS_POSTPONED, subgrid, 3);
0040     addStat(IDLENESS_CAUSED_SKIP_TINY, subgrid, 4);
0041     mGrid->addWidget(gb, 0, 1);
0042 
0043     gb = new QGroupBox(i18n("Pause"), this);
0044     subgrid = new QGridLayout(gb);
0045     addStat(ACTIVITY_PERC, subgrid, 0);
0046     addStat(ACTIVITY_PERC_MINUTE, subgrid, 1);
0047     addStat(ACTIVITY_PERC_HOUR, subgrid, 2);
0048     addStat(ACTIVITY_PERC_6HOUR, subgrid, 3);
0049     addStat(PAUSE_SCORE, subgrid, 4);
0050     mGrid->addWidget(gb, 1, 0);
0051 
0052     gb = new QGroupBox(i18n("Long Breaks"), this);
0053     subgrid = new QGridLayout(gb);
0054     addStat(BIG_BREAKS, subgrid, 0);
0055     addStat(LAST_BIG_BREAK, subgrid, 1);
0056     addStat(BIG_BREAKS_SKIPPED, subgrid, 2);
0057     addStat(BIG_BREAKS_POSTPONED, subgrid, 3);
0058     addStat(IDLENESS_CAUSED_SKIP_BIG, subgrid, 4);
0059     mGrid->addWidget(gb, 1, 1);
0060 }
0061 
0062 RSIStatWidget::~RSIStatWidget()
0063 {
0064 }
0065 
0066 void RSIStatWidget::addStat(RSIStat stat, QGridLayout *grid, int row)
0067 {
0068     QLabel *l = RSIGlobals::instance()->stats()->getDescription(stat);
0069     l->setParent(this);
0070 
0071     QLabel *m = RSIGlobals::instance()->stats()->getLabel(stat);
0072     m->setParent(grid->parentWidget());
0073     m->setAlignment(Qt::AlignRight);
0074 
0075     grid->addWidget(l, row, 0);
0076     grid->addWidget(m, row, 1);
0077 
0078     // measure minimal width with current font settings
0079     QFontMetrics fm(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
0080     int width = 0;
0081     switch (stat) {
0082     case TOTAL_TIME:
0083     case ACTIVITY:
0084     case IDLENESS:
0085     case MAX_IDLENESS:
0086         width = fm.boundingRect("One one and "
0087                                 + i18nc("Translate this as the longest plural form. This is used to "
0088                                         "calculate the width of window",
0089                                         "minutes")
0090                                 + i18nc("Translate this as the longest plural form. This is used to "
0091                                         "calculate the width of window",
0092                                         "seconds"))
0093                     .width();
0094         break;
0095     case LAST_TINY_BREAK:
0096     case LAST_BIG_BREAK: {
0097         QTime dt(QTime::currentTime());
0098         width = (int)(fm.boundingRect(QLocale().toString(dt)).width() * 1.25);
0099         break;
0100     }
0101     default:;
0102     }
0103 
0104     if (width > 0)
0105         m->setMinimumWidth(width);
0106 }
0107 
0108 void RSIStatWidget::showEvent(QShowEvent *)
0109 {
0110     RSIGlobals::instance()->stats()->doUpdates(true);
0111 }
0112 
0113 void RSIStatWidget::hideEvent(QHideEvent *)
0114 {
0115     RSIGlobals::instance()->stats()->doUpdates(false);
0116 }