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#ifndef TIMEFORMAT_H
21#define TIMEFORMAT_H
22
23#include <QString>
24
25class QTime;
26
27/**
28 * @brief A wrapper for a QTime-like format time string.
29 * A TimeFormat is an abstraction for a QTime-like string used for time formats.
30 * A TimeFormat can be customized using booleans in the constructor.
31 * The QTime-syntax used is the following:
32 * "hh:" whether to show hours (00 to 24)
33 * "mm:" whether to show minutes (00 to 59)
34 * "ss." whether to show seconds (00 to 59)
35 * "zzz" whether to show second fractions (tenths or hundredths or milliseconds)
36 * An example of time formatted with the complete syntax might be the following: 0:05:38.582
37 */
38class TimeFormat
39{
40
41public:
42
43 explicit TimeFormat(bool h = false, bool mm = true, bool ss = true, bool t = true, bool hundr = true, bool msec = false);
44
45 /**
46 * Format the given time with the current time format.
47 * @param time The time to be formatted.
48 * @return The time formatted as string.
49 */
50 QString format(const QTime& time) const;
51
52 /**
53 * Format the given time's hours with the current time format.
54 * @param time The time to be formatted.
55 * @return The time's hours formatted as string, or empty string if hour is not in the format.
56 */
57 QString formatHours(const QTime& time) const;
58
59 /**
60 * Format the given time's minutes with the current time format.
61 * @param time The time to be formatted.
62 * @return The time's minutes formatted as string, or empty string if minute is not in the format.
63 */
64 QString formatMin(const QTime& time) const;
65
66 /**
67 * Format the given time's seconds with the current time format.
68 * @param time The time to be formatted.
69 * @return The time's seconds formatted as string, or empty string if second is not in the format.
70 */
71 QString formatSec(const QTime& time) const;
72
73 /**
74 * Format the given time's second fractions with the current time format.
75 * @param time The time to be formatted.
76 * @return The time's second fractions formatted as string, or empty string if second fraction is not in the format.
77 */
78 QString formatSecFrac(const QTime& time) const;
79
80 /**
81 * Whether the hour is in the time format.
82 * @return true if hour is in the format, false otherwise.
83 */
84 bool isHourEnabled() const;
85
86 /**
87 * Whether the minute is in the time format.
88 * @return true if minute is in the format, false otherwise.
89 */
90 bool isMinEnabled() const;
91
92 /**
93 * Whether the second is in the time format.
94 * @return true if second is in the format, false otherwise.
95 */
96 bool isSecEnabled() const;
97
98 /**
99 * Whether the second fraction is in the time format.
100 * @return true if second fraction is in the format, false otherwise.
101 */
102 bool isSecFracEnabled() const;
103
104 /**
105 * Whether the second fractions in the time format is made by tenths of second.
106 * @return true if second fraction is tenths of second, false otherwise.
107 */
108 bool isTenthEnabled() const;
109
110 /**
111 * Whether the second fractions in the time format is made by hundredths of second.
112 * @return true if second fraction is hundredths of second, false otherwise.
113 */
114 bool isHundredthEnabled() const;
115
116 /**
117 * Whether the second fractions in the time format is made by milliseconds.
118 * @return true if second fraction is milliseconds, false otherwise.
119 */
120 bool isMSecEnabled() const;
121
122 /**
123 * Whether to show the symbols ':' and '.' used as dividers in the time format.
124 * @param show true Whether to show the dividers.
125 */
126 void showDividers(bool show);
127
128private:
129
130 enum class SecFraction
131 {
132 NONE, /**< Null second fraction. */
133 TENTH, /**< Second fraction is tenths of second. */
134 HUNDREDTH, /**< Second fraction is hundrdths of second. */
135 MILLISECOND /**< Second fraction is milliseconds. */
136 };
137
138 bool hour; /** Whether hour is in the internal time format */
139 bool min; /** Whether minute is in the internal time format */
140 bool sec; /** Whether second is in the internal time format */
141 SecFraction secFraction; /** Second fraction internal time format */
142
143 bool dividers; /** Whether to show the symbols used as dividers */
144 QString hourFormat; /** Hour string format */
145 QString minFormat; /** Minute string format */
146 QString secFormat; /** Secondstring format */
147
148 /**
149 * Setup the format strings based on the internal formats
150 */
151 void setupFormat();
152};
153
154#endif
155