Warning, file /utilities/kronometer/src/gui/digitdisplay.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     Copyright (C) 2014 by Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     This file is part of Kronometer.
0005 
0006     Kronometer is free software: you can redistribute it and/or modify
0007     it under the terms of the GNU General Public License as published by
0008     the Free Software Foundation, either version 2 of the License, or
0009     (at your option) any later version.
0010 
0011     Kronometer is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014     GNU General Public License for more details.
0015 
0016     You should have received a copy of the GNU General Public License
0017     along with Kronometer.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #include "digitdisplay.h"
0021 
0022 #include <QHBoxLayout>
0023 #include <QLabel>
0024 
0025 DigitDisplay::DigitDisplay(QWidget *parent, Digits digits) : QWidget(parent)
0026 {
0027     auto layout = new QHBoxLayout {this};
0028     m_leftmostDigit = new QLabel {this};
0029     m_centerDigit = new QLabel {this};
0030     m_rightmostDigit = new QLabel {this};
0031 
0032     m_leftmostDigit->setAlignment(Qt::AlignCenter);
0033     m_centerDigit->setAlignment(Qt::AlignCenter);
0034     m_rightmostDigit->setAlignment(Qt::AlignCenter);
0035 
0036     layout->addWidget(m_leftmostDigit);
0037     layout->addWidget(m_centerDigit);
0038     layout->addWidget(m_rightmostDigit);
0039 
0040     setDigits(digits);
0041 }
0042 
0043 void DigitDisplay::setDigits(Digits digits)
0044 {
0045     m_digits = digits;
0046 
0047     switch (m_digits) {
0048     case Digits::One:
0049         m_leftmostDigit->show();
0050         m_centerDigit->hide();
0051         m_rightmostDigit->hide();
0052         break;
0053     case Digits::Two:
0054         m_leftmostDigit->show();
0055         m_centerDigit->show();
0056         m_rightmostDigit->hide();
0057         break;
0058     case Digits::Three:
0059         m_leftmostDigit->show();
0060         m_centerDigit->show();
0061         m_rightmostDigit->show();
0062         break;
0063     default:
0064         m_leftmostDigit->hide();
0065         m_centerDigit->hide();
0066         m_rightmostDigit->hide();
0067         break;
0068     }
0069 }
0070 
0071 void DigitDisplay::showDigits(const QString& digits) const
0072 {
0073     switch (m_digits) {
0074     case Digits::One:
0075         showOneDigit(digits);
0076         break;
0077     case Digits::Two:
0078         showTwoDigits(digits);
0079         break;
0080     case Digits::Three:
0081         showThreeDigits(digits);
0082         break;
0083     default:
0084         break;
0085     }
0086 }
0087 
0088 void DigitDisplay::setFont(const QFont& font)
0089 {
0090     m_displayFont = font;
0091 
0092     m_leftmostDigit->setFont(m_displayFont);
0093     m_centerDigit->setFont(m_displayFont);
0094     m_rightmostDigit->setFont(m_displayFont);
0095 }
0096 
0097 QSize DigitDisplay::minimumSizeHint() const
0098 {
0099     auto width = 0;
0100     auto fontMetrics = QFontMetrics {m_displayFont};
0101 
0102     switch (m_digits) {
0103     case Digits::One:
0104         width = fontMetrics.width(m_leftmostDigit->text());
0105         break;
0106     case Digits::Two:
0107         width = fontMetrics.width(m_leftmostDigit->text()) + fontMetrics.width(m_centerDigit->text());
0108         break;
0109     case Digits::Three:
0110         width = fontMetrics.width(m_leftmostDigit->text()) + fontMetrics.width(m_centerDigit->text()) + fontMetrics.width(m_rightmostDigit->text());
0111         break;
0112     default:
0113         break;
0114     }
0115 
0116     width += (width * 10 / 100); // 10% used as padding between digits
0117 
0118     return {width, QWidget::minimumSizeHint().height()};
0119 }
0120 
0121 bool DigitDisplay::isValid(const QString &text) const
0122 {
0123     switch (m_digits) {
0124     case Digits::One:
0125         return text.size() == 1;
0126     case Digits::Two:
0127         return text.size() == 2;
0128     case Digits::Three:
0129         return text.size() == 3;
0130     default:
0131         return false;
0132     }
0133 }
0134 
0135 void DigitDisplay::showOneDigit(const QString& digit) const
0136 {
0137     if (not isValid(digit)) {
0138         return;
0139     }
0140 
0141     m_leftmostDigit->setText(digit.at(0));
0142 }
0143 
0144 void DigitDisplay::showTwoDigits(const QString& digits) const
0145 {
0146     if (not isValid(digits)) {
0147         return;
0148     }
0149 
0150     // digits are displayed from right to left
0151     m_centerDigit->setText(digits.at(1));
0152     m_leftmostDigit->setText(digits.at(0));
0153 }
0154 
0155 void DigitDisplay::showThreeDigits(const QString& digits) const
0156 {
0157     if (not isValid(digits)) {
0158         return;
0159     }
0160 
0161     // digits are displayed from right to left
0162     m_rightmostDigit->setText(digits.at(2));
0163     m_centerDigit->setText(digits.at(1));
0164     m_leftmostDigit->setText(digits.at(0));
0165 }