Warning, file /utilities/kronometer/src/gui/digitdisplay.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 Copyright (C) 2014 by Elvis Angelaccio <elvis.angelaccio@kde.org> 0003 0004 This file is part of Kronometer. 0005 0006 Kronometer is free software: you can redistribute it and/or modify 0007 it under the terms of the GNU General Public License as published by 0008 the Free Software Foundation, either version 2 of the License, or 0009 (at your option) any later version. 0010 0011 Kronometer is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with Kronometer. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #ifndef DIGITDISPLAY_H 0021 #define DIGITDISPLAY_H 0022 0023 #include <QWidget> 0024 0025 class QLabel; 0026 0027 /** 0028 * @brief A custom widget displaying up to three digits. 0029 * This widget is meant to be used as a QLabel replacement to display the digits 0030 * of a timer component (minutes, seconds, etc.). By default a QLabel displays 0031 * a single text and if the font is not monospace this might look bad when the 0032 * text is refreshed very quickly, as in a stopwatch timer. This widget instead 0033 * displays the text by splitting it into up to three different strings, displayed 0034 * in different QLabels within a horizontal layout. (i.e. simulating a monospace font 0035 * using adequate padding). 0036 */ 0037 class DigitDisplay : public QWidget 0038 { 0039 Q_OBJECT 0040 0041 public: 0042 0043 enum class Digits 0044 { 0045 One, /**< Display one digit. */ 0046 Two, /**< Display two digits. */ 0047 Three, /**< Display three digits. */ 0048 None /**< Display no digit. */ 0049 }; 0050 0051 explicit DigitDisplay(QWidget *parent = nullptr, Digits digits = Digits::None); 0052 0053 /** 0054 * Set the number of digits to be displayed. 0055 * @param digits The number of digits to be displayed. 0056 */ 0057 void setDigits(Digits digits); 0058 0059 /** 0060 * The digits to be displayed. 0061 * The input string shall have a length equal to the internal digit counter. 0062 * Otherwise nothing is displayed. 0063 * @param digits The digits string to be displayed. 0064 */ 0065 void showDigits(const QString& digits) const; 0066 0067 /** 0068 * Set a custom font for the widget labels. 0069 * @param font The custom font to set. 0070 */ 0071 void setFont(const QFont& font); 0072 0073 virtual QSize minimumSizeHint() const override; 0074 0075 private: 0076 0077 QLabel *m_leftmostDigit; 0078 QLabel *m_centerDigit; 0079 QLabel *m_rightmostDigit; 0080 0081 QFont m_displayFont; 0082 Digits m_digits; 0083 0084 /** 0085 * @return Whether the length of @p text is consistent with the display digits. 0086 */ 0087 bool isValid(const QString& text) const; 0088 0089 /** 0090 * Helper function to display a single digit. 0091 * @param digit The digit to be displayed. 0092 */ 0093 void showOneDigit(const QString& digit) const; 0094 0095 /** 0096 * Helper function to display two digits. 0097 * @param digits The digits to be displayed. 0098 */ 0099 void showTwoDigits(const QString& digits) const; 0100 0101 /** 0102 * Helper function to display three digits. 0103 * @param digits The digits to be displayed. 0104 */ 0105 void showThreeDigits(const QString& digits) const; 0106 0107 Q_DISABLE_COPY(DigitDisplay) 0108 }; 0109 0110 #endif