1/*
2 Copyright (C) 2014 by Elvis Angelaccio <elvis.angelaccio@kdemail.net>
3
4 This file is part of Kronometer.
5
6 Kronometer is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 Kronometer is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Kronometer. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "digitdisplay.h"
21
22#include <QHBoxLayout>
23#include <QLabel>
24
25DigitDisplay::DigitDisplay(QWidget *parent, DigitCounter counter) : QWidget(parent)
26{
27 layout = new QHBoxLayout(this);
28 leftmostDigit = new QLabel(this);
29 centerDigit = new QLabel(this);
30 rightmostDigit = new QLabel(this);
31
32 leftmostDigit->setAlignment(Qt::AlignCenter);
33 centerDigit->setAlignment(Qt::AlignCenter);
34 rightmostDigit->setAlignment(Qt::AlignCenter);
35
36 layout->addWidget(leftmostDigit);
37 layout->addWidget(centerDigit);
38 layout->addWidget(rightmostDigit);
39
40 setDigitCounter(counter);
41}
42
43void DigitDisplay::setDigitCounter(DigitCounter counter)
44{
45 switch (counter) {
46 case ONE_DIGIT:
47 leftmostDigit->show();
48 centerDigit->hide();
49 rightmostDigit->hide();
50 digitCounter = counter;
51 break;
52 case TWO_DIGITS:
53 leftmostDigit->show();
54 centerDigit->show();
55 rightmostDigit->hide();
56 digitCounter = counter;
57 break;
58 case THREE_DIGITS:
59 leftmostDigit->show();
60 centerDigit->show();
61 rightmostDigit->show();
62 digitCounter = counter;
63 break;
64 default:
65 leftmostDigit->hide();
66 centerDigit->hide();
67 rightmostDigit->hide();
68 digitCounter = NO_DIGIT;
69 break;
70 }
71}
72
73void DigitDisplay::showDigits(const QString& digits) const
74{
75 switch (digitCounter) {
76 case ONE_DIGIT:
77 showOneDigit(digits);
78 break;
79 case TWO_DIGITS:
80 showTwoDigits(digits);
81 break;
82 case THREE_DIGITS:
83 showThreeDigits(digits);
84 break;
85 default:
86 break;
87 }
88}
89
90void DigitDisplay::setFont(const QFont& font)
91{
92 displayFont = font;
93
94 leftmostDigit->setFont(displayFont);
95 centerDigit->setFont(displayFont);
96 rightmostDigit->setFont(displayFont);
97}
98
99int DigitDisplay::width() const
100{
101 int width = 0;
102 QFontMetrics fontMetrics(displayFont);
103
104 switch (digitCounter) {
105 case ONE_DIGIT:
106 width = fontMetrics.width(leftmostDigit->text());
107 break;
108 case TWO_DIGITS:
109 width = fontMetrics.width(leftmostDigit->text()) + fontMetrics.width(centerDigit->text());
110 break;
111 case THREE_DIGITS:
112 width = fontMetrics.width(leftmostDigit->text()) + fontMetrics.width(centerDigit->text()) + fontMetrics.width(rightmostDigit->text());
113 break;
114 default:
115 break;
116 }
117
118 return width + (width * 10 / 100); // 10% used as padding between digits
119}
120
121void DigitDisplay::showOneDigit(const QString& digit) const
122{
123 if (digit.size() == digitCounter) {
124 leftmostDigit->setText(digit.at(0));
125 }
126}
127
128void DigitDisplay::showTwoDigits(const QString& digits) const
129{
130 if (digits.size() == digitCounter) {
131 // digits are displayed from right to left
132 centerDigit->setText(digits.at(1));
133 leftmostDigit->setText(digits.at(0));
134 }
135}
136
137void DigitDisplay::showThreeDigits(const QString& digits) const
138{
139 if (digits.size() == digitCounter) {
140 // digits are displayed from right to left
141 rightmostDigit->setText(digits.at(2));
142 centerDigit->setText(digits.at(1));
143 leftmostDigit->setText(digits.at(0));
144 }
145}
146