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 DIGITDISPLAY_H |
21 | #define DIGITDISPLAY_H |
22 | |
23 | #include <QWidget> |
24 | |
25 | class QHBoxLayout; |
26 | class QLabel; |
27 | |
28 | /** |
29 | * @brief A custom widget displaying up to three digits. |
30 | * This widget is meant to be used as a QLabel replacement to display the digits |
31 | * of a timer component (minutes, seconds, etc.). By default a QLabel displays |
32 | * a single text and if the font is not monospace this might look bad when the |
33 | * text is refreshed very quickly, as in a stopwatch timer. This widget instead |
34 | * displays the text by splitting it into up to three different strings, displayed |
35 | * in different QLabels within a horizontal layout. (i.e. simulating a monospace font |
36 | * using adequate padding). |
37 | */ |
38 | class DigitDisplay : public QWidget |
39 | { |
40 | |
41 | public: |
42 | |
43 | enum DigitCounter |
44 | { |
45 | ONE_DIGIT = 1, /**< Display one digit */ |
46 | TWO_DIGITS = 2, /**< Display two digits */ |
47 | THREE_DIGITS = 3, /**< Display three digits */ |
48 | NO_DIGIT /**< Display no digit */ |
49 | }; |
50 | |
51 | explicit DigitDisplay(QWidget *parent = nullptr, DigitCounter counter = NO_DIGIT); |
52 | |
53 | /** |
54 | * Set the number of digits to be displayed. |
55 | * @param counter The number of digits to be displayed. |
56 | */ |
57 | void setDigitCounter(DigitCounter counter); |
58 | |
59 | /** |
60 | * The digits to be displayed. |
61 | * The input string shall have a length equal to the internal digit counter. |
62 | * Otherwise nothing is displayed. |
63 | * @param digits The digits string to be displayed. |
64 | */ |
65 | void showDigits(const QString& digits) const; |
66 | |
67 | /** |
68 | * Set a custom font for the widget labels. |
69 | * @param font The custom font to set. |
70 | */ |
71 | void setFont(const QFont& font); |
72 | |
73 | /** |
74 | * The current widget's width, computed on the current font size and number of digits displayed. |
75 | * @return The current widget's width. |
76 | */ |
77 | int width() const; |
78 | |
79 | private: |
80 | |
81 | QHBoxLayout *layout; |
82 | |
83 | QLabel *leftmostDigit; |
84 | QLabel *centerDigit; |
85 | QLabel *rightmostDigit; |
86 | |
87 | QFont displayFont; |
88 | DigitCounter digitCounter; |
89 | |
90 | /** |
91 | * Helper function to display a single digit. |
92 | * @param digit The digit to be displayed. |
93 | */ |
94 | void showOneDigit(const QString& digit) const; |
95 | |
96 | /** |
97 | * Helper function to display two digits. |
98 | * @param digits The digits to be displayed. |
99 | */ |
100 | void showTwoDigits(const QString& digits) const; |
101 | |
102 | /** |
103 | * Helper function to display three digits. |
104 | * @param digits The digits to be displayed. |
105 | */ |
106 | void showThreeDigits(const QString& digits) const; |
107 | |
108 | }; |
109 | |
110 | #endif |
111 | |