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 "timedisplay.h"
21
22#include <KLocale>
23
24#include <QLabel>
25#include <QBoxLayout>
26
27#include "digitdisplay.h"
28
29namespace
30{
31 const QString FRAME_STYLE = "QFrame {background-clip: content; background: %1; color: %2}";
32}
33
34TimeDisplay::TimeDisplay(QWidget *parent) : QWidget(parent), displayTime(0, 0)
35{
36 displayLayout = new QHBoxLayout(this);
37
38 hourFrame = new QFrame(this);
39 minFrame = new QFrame(this);
40 secFrame = new QFrame(this);
41 fracFrame = new QFrame(this);
42 hourFrame->setFrameShape(QFrame::StyledPanel);
43 hourFrame->setFrameShadow(QFrame::Sunken);
44 minFrame->setFrameShape(QFrame::StyledPanel);
45 minFrame->setFrameShadow(QFrame::Sunken);
46 secFrame->setFrameShape(QFrame::StyledPanel);
47 secFrame->setFrameShadow(QFrame::Sunken);
48 fracFrame->setFrameShape(QFrame::StyledPanel);
49 fracFrame->setFrameShadow(QFrame::Sunken);
50
51 hourLayout = new QVBoxLayout(hourFrame);
52 minLayout = new QVBoxLayout(minFrame);
53 secLayout = new QVBoxLayout(secFrame);
54 fracLayout = new QVBoxLayout(fracFrame);
55
56 hourHeader = new QLabel(hourFrame);
57 minHeader = new QLabel(minFrame);
58 secHeader = new QLabel(secFrame);
59 fracHeader = new QLabel(fracFrame);
60 hourHeader->setAlignment(Qt::AlignCenter);
61 minHeader->setAlignment(Qt::AlignCenter);
62 secHeader->setAlignment(Qt::AlignCenter);
63 fracHeader->setAlignment(Qt::AlignCenter);
64 hourHeader->setText(i18n("Hours"));
65 minHeader->setText(i18n("Minutes"));
66 secHeader->setText(i18n("Seconds"));
67 fracHeader->setText(i18n("Hundredths"));
68 hourHeader->setTextInteractionFlags(Qt::TextSelectableByMouse);
69 minHeader->setTextInteractionFlags(Qt::TextSelectableByMouse);
70 secHeader->setTextInteractionFlags(Qt::TextSelectableByMouse);
71 fracHeader->setTextInteractionFlags(Qt::TextSelectableByMouse);
72
73 hourDisplay = new DigitDisplay(hourFrame);
74 minDisplay = new DigitDisplay(minFrame);
75 secDisplay = new DigitDisplay(secFrame);
76 fracDisplay = new DigitDisplay(fracFrame);
77
78 hourDisplay->showDigits(timeFormat.formatHours(displayTime));
79 minDisplay->showDigits(timeFormat.formatMin(displayTime));
80 secDisplay->showDigits(timeFormat.formatSec(displayTime));
81 fracDisplay->showDigits(timeFormat.formatSecFrac(displayTime));
82
83 hourLayout->addWidget(hourHeader);
84 hourLayout->addWidget(hourDisplay);
85 minLayout->addWidget(minHeader);
86 minLayout->addWidget(minDisplay);
87 secLayout->addWidget(secHeader);
88 secLayout->addWidget(secDisplay);
89 fracLayout->addWidget(fracHeader);
90 fracLayout->addWidget(fracDisplay);
91
92 displayLayout->addWidget(hourFrame);
93 displayLayout->addWidget(minFrame);
94 displayLayout->addWidget(secFrame);
95 displayLayout->addWidget(fracFrame);
96}
97
98void TimeDisplay::setTimeFormat(const TimeFormat &format)
99{
100 timeFormat = format;
101
102 hourFrame->setVisible(timeFormat.isHourEnabled());
103 minFrame->setVisible(timeFormat.isMinEnabled());
104 secFrame->setVisible(timeFormat.isSecEnabled());
105 fracFrame->setVisible(timeFormat.isSecFracEnabled());
106
107 hourDisplay->setDigitCounter(DigitDisplay::TWO_DIGITS);
108 minDisplay->setDigitCounter(DigitDisplay::TWO_DIGITS);
109 secDisplay->setDigitCounter(DigitDisplay::TWO_DIGITS);
110
111 if (timeFormat.isSecFracEnabled()) {
112 if (timeFormat.isMSecEnabled()) {
113 fracHeader->setText(i18n("Milliseconds"));
114 fracDisplay->setDigitCounter(DigitDisplay::THREE_DIGITS);
115 }
116 else if (timeFormat.isHundredthEnabled()) {
117 fracHeader->setText(i18n("Hundredths"));
118 fracDisplay->setDigitCounter(DigitDisplay::TWO_DIGITS);
119 }
120 else if (timeFormat.isTenthEnabled()) {
121 fracHeader->setText(i18n("Tenths"));
122 fracDisplay->setDigitCounter(DigitDisplay::ONE_DIGIT);
123 }
124 }
125
126 updateTimer();
127}
128
129void TimeDisplay::setHourFont(const QFont& font)
130{
131 hourDisplay->setFont(font);
132 updateWidth();
133}
134
135void TimeDisplay::setMinFont(const QFont& font)
136{
137 minDisplay->setFont(font);
138 updateWidth();
139}
140
141void TimeDisplay::setSecFont(const QFont& font)
142{
143 secDisplay->setFont(font);
144 updateWidth();
145}
146
147void TimeDisplay::setFracFont(const QFont& font)
148{
149 fracDisplay->setFont(font);
150 updateWidth();
151}
152
153void TimeDisplay::setBackgroundColor(const QColor& color)
154{
155 backgroundColor = color;
156
157 hourFrame->setStyleSheet(FRAME_STYLE.arg(backgroundColor.name(), textColor.name()));
158 minFrame->setStyleSheet(FRAME_STYLE.arg(backgroundColor.name(), textColor.name()));
159 secFrame->setStyleSheet(FRAME_STYLE.arg(backgroundColor.name(), textColor.name()));
160 fracFrame->setStyleSheet(FRAME_STYLE.arg(backgroundColor.name(), textColor.name()));
161}
162
163void TimeDisplay::setTextColor(const QColor& color)
164{
165 textColor = color;
166
167 hourFrame->setStyleSheet(FRAME_STYLE.arg(backgroundColor.name(), textColor.name()));
168 minFrame->setStyleSheet(FRAME_STYLE.arg(backgroundColor.name(), textColor.name()));
169 secFrame->setStyleSheet(FRAME_STYLE.arg(backgroundColor.name(), textColor.name()));
170 fracFrame->setStyleSheet(FRAME_STYLE.arg(backgroundColor.name(), textColor.name()));
171}
172
173void TimeDisplay::showHeaders(bool show)
174{
175 hourHeader->setVisible(show);
176 minHeader->setVisible(show);
177 secHeader->setVisible(show);
178 fracHeader->setVisible(show);
179}
180
181QString TimeDisplay::currentTime()
182{
183 timeFormat.showDividers(true);
184 QString currentTime = timeFormat.format(displayTime);
185 timeFormat.showDividers(false);
186
187 return currentTime;
188}
189
190
191void TimeDisplay::onTime(const QTime& t)
192{
193 displayTime = t;
194 updateTimer();
195}
196
197void TimeDisplay::updateTimer()
198{
199 if (timeFormat.isHourEnabled()) {
200 hourDisplay->showDigits(timeFormat.formatHours(displayTime));
201 }
202
203 if (timeFormat.isMinEnabled()) {
204 minDisplay->showDigits(timeFormat.formatMin(displayTime));
205 }
206
207 if (timeFormat.isSecEnabled()) {
208 secDisplay->showDigits(timeFormat.formatSec(displayTime));
209 }
210
211 if (timeFormat.isSecFracEnabled()) {
212 fracDisplay->showDigits(timeFormat.formatSecFrac(displayTime));
213 }
214}
215
216void TimeDisplay::updateWidth()
217{
218 int width = qMax(qMax(hourDisplay->width(), minDisplay->width()), qMax(secDisplay->width(), fracDisplay->width()));
219
220 width = width + (width * 20 / 100); // 20% as padding, i.e. 10% as right padding and 10% as left padding
221
222 hourFrame->setMinimumWidth(width);
223 minFrame->setMinimumWidth(width);
224 secFrame->setMinimumWidth(width);
225 fracFrame->setMinimumWidth(width);
226}
227
228