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 "timeformat.h"
21
22#include <QTime>
23
24TimeFormat::TimeFormat(bool h, bool mm, bool ss, bool t, bool hundr, bool msec)
25 :
26 hour(h),
27 min(mm),
28 sec(ss),
29 dividers(true)
30{
31 if (msec) {
32 secFraction = SecFraction::MILLISECOND;
33 }
34 else if (hundr) {
35 secFraction = SecFraction::HUNDREDTH;
36 }
37 else if (t) {
38 secFraction = SecFraction::TENTH;
39 }
40
41 setupFormat();
42}
43
44QString TimeFormat::format(const QTime& time) const
45{
46 QString h = formatHours(time);
47 QString m = formatMin(time);
48 QString s = formatSec(time);
49 QString f = formatSecFrac(time);
50
51 return h + m + s + f;
52}
53
54QString TimeFormat::formatHours(const QTime& time) const
55{
56 if (not hour) {
57 return QString();
58 }
59
60 return time.toString(hourFormat);
61}
62
63QString TimeFormat::formatMin(const QTime& time) const
64{
65 if (not min) {
66 return QString();
67 }
68
69 return time.toString(minFormat);
70}
71
72QString TimeFormat::formatSec(const QTime& time) const
73{
74 if (not sec) {
75 return QString();
76 }
77
78 return time.toString(secFormat);
79}
80
81QString TimeFormat::formatSecFrac(const QTime& time) const
82{
83 const QString fractFormat = "zzz";
84
85 if (secFraction == SecFraction::MILLISECOND)
86 return time.toString(fractFormat);
87
88 if (secFraction == SecFraction::HUNDREDTH) {
89 QString temp = time.toString(fractFormat);
90 return temp.left(temp.size() - 1);
91 }
92
93 if (secFraction == SecFraction::TENTH) {
94 QString temp = time.toString(fractFormat);
95 return temp.left(temp.size() - 2);
96 }
97
98 return QString();
99}
100
101bool TimeFormat::isHourEnabled() const
102{
103 return hour;
104}
105
106bool TimeFormat::isMinEnabled() const
107{
108 return min;
109}
110
111bool TimeFormat::isSecEnabled() const
112{
113 return sec;
114}
115
116bool TimeFormat::isSecFracEnabled() const
117{
118 return secFraction != SecFraction::NONE;
119}
120
121bool TimeFormat::isTenthEnabled() const
122{
123 return secFraction == SecFraction::TENTH;
124}
125
126bool TimeFormat::isHundredthEnabled() const
127{
128 return secFraction == SecFraction::HUNDREDTH;
129}
130
131bool TimeFormat::isMSecEnabled() const
132{
133 return secFraction == SecFraction::MILLISECOND;
134}
135
136void TimeFormat::showDividers(bool show)
137{
138 dividers = show;
139 setupFormat();
140}
141
142void TimeFormat::setupFormat()
143{
144 if (hour) {
145 if (dividers and (min or sec or secFraction != SecFraction::NONE)) {
146 hourFormat = "hh:";
147 }
148 else {
149 hourFormat = "hh";
150 }
151 }
152
153 if (min) {
154 if (dividers and (sec or secFraction != SecFraction::NONE)) {
155 minFormat = "mm:";
156 }
157 else {
158 minFormat = "mm";
159 }
160 }
161
162 if (sec) {
163 if (dividers and (secFraction != SecFraction::NONE)) {
164 secFormat = "ss.";
165 }
166 else {
167 secFormat = "ss";
168 }
169 }
170}
171