File indexing completed on 2024-04-28 04:48:38

0001 /*
0002     SPDX-FileCopyrightText: 2005 Max Howell <max.howell@methylblue.com>
0003     SPDX-FileCopyrightText: 2008 David Edmundson <kde@davidedmundson.co.uk>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "timeLabel.h"
0009 
0010 #include <QFontDatabase>
0011 
0012 #include <KConfigGroup>
0013 #include <KSharedConfig>
0014 
0015 TimeLabel::TimeLabel(QWidget *parent)
0016     : QLabel(QLatin1String(" 0:00:00 "), parent)
0017     , m_currentTime(0)
0018 {
0019     setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
0020     setAlignment(Qt::AlignCenter);
0021     setMinimumSize(sizeHint());
0022     KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("General"));
0023     m_timeFormat = static_cast<TimeFormats>(config.readEntry<int>("TimeFormat", static_cast<int>(SHOW_COMPLETED)));
0024 }
0025 
0026 TimeLabel::~TimeLabel()
0027 {
0028     KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("General"));
0029     config.writeEntry("TimeFormat", static_cast<int>(m_timeFormat));
0030 }
0031 
0032 void TimeLabel::mousePressEvent(QMouseEvent *)
0033 {
0034     if (m_timeFormat == SHOW_REMAINING)
0035         m_timeFormat = SHOW_COMPLETED;
0036     else
0037         m_timeFormat = SHOW_REMAINING;
0038     updateTime();
0039 }
0040 
0041 void TimeLabel::updateTime()
0042 {
0043     qint64 ms;
0044     const auto zeroPad = [](int n) {
0045         return n < 10 ? QString::fromLatin1("0%1").arg(n) : QString::number(n);
0046     };
0047     if (m_timeFormat == SHOW_REMAINING)
0048         ms = m_totalTime - m_currentTime;
0049     else
0050         ms = m_currentTime;
0051     const int s = ms / 1000;
0052     const int m = s / 60;
0053     const int h = m / 60;
0054     QString time = zeroPad(s % 60); // seconds
0055     time.prepend(QLatin1Char(':'));
0056     time.prepend(zeroPad(m % 60)); // minutes
0057     time.prepend(QLatin1Char(':'));
0058     time.prepend(QString::number(h)); // hours
0059     if (m_timeFormat == SHOW_REMAINING)
0060         time.prepend(QLatin1Char('-'));
0061     setText(time);
0062 }
0063 
0064 void TimeLabel::setCurrentTime(qint64 time)
0065 {
0066     m_currentTime = time;
0067     updateTime();
0068 }
0069 
0070 void TimeLabel::setTotalTime(qint64 time)
0071 {
0072     m_totalTime = time;
0073     updateTime();
0074 }
0075 
0076 #include "moc_timeLabel.cpp"