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     SPDX-FileCopyrightText: 2014 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef DIGITDISPLAY_H
0008 #define DIGITDISPLAY_H
0009 
0010 #include <QWidget>
0011 
0012 class QLabel;
0013 
0014 /**
0015  * @brief A custom widget displaying up to three digits.
0016  * This widget is meant to be used as a QLabel replacement to display the digits
0017  * of a timer component (minutes, seconds, etc.). By default a QLabel displays
0018  * a single text and if the font is not monospace this might look bad when the
0019  * text is refreshed very quickly, as in a stopwatch timer. This widget instead
0020  * displays the text by splitting it into up to three different strings, displayed
0021  * in different QLabels within a horizontal layout. (i.e. simulating a monospace font
0022  *  using adequate padding).
0023  */
0024 class DigitDisplay : public QWidget
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029 
0030     enum class Digits
0031     {
0032         One,      /**< Display one digit. */
0033         Two,      /**< Display two digits. */
0034         Three,    /**< Display three digits. */
0035         None      /**< Display no digit. */
0036     };
0037 
0038     explicit DigitDisplay(QWidget *parent = nullptr, Digits digits = Digits::None);
0039 
0040     /**
0041      * Set the number of digits to be displayed.
0042      * @param digits The number of digits to be displayed.
0043      */
0044     void setDigits(Digits digits);
0045 
0046     /**
0047      * The digits to be displayed.
0048      * The input string shall have a length equal to the internal digit counter.
0049      * Otherwise nothing is displayed.
0050      * @param digits The digits string to be displayed.
0051      */
0052     void showDigits(const QString& digits) const;
0053 
0054     /**
0055      * Set a custom font for the widget labels.
0056      * @param font The custom font to set.
0057      */
0058     void setFont(const QFont& font);
0059 
0060     virtual QSize minimumSizeHint() const override;
0061 
0062 private:
0063 
0064     QLabel *m_leftmostDigit;
0065     QLabel *m_centerDigit;
0066     QLabel *m_rightmostDigit;
0067 
0068     QFont m_displayFont;
0069     Digits m_digits;
0070 
0071     /**
0072      * @return Whether the length of @p text is consistent with the display digits.
0073      */
0074     bool isValid(const QString& text) const;
0075 
0076     /**
0077      * Helper function to display a single digit.
0078      * @param digit The digit to be displayed.
0079      */
0080     void showOneDigit(const QString& digit) const;
0081 
0082     /**
0083      * Helper function to display two digits.
0084      * @param digits The digits to be displayed.
0085      */
0086     void showTwoDigits(const QString& digits) const;
0087 
0088     /**
0089      * Helper function to display three digits.
0090      * @param digits The digits to be displayed.
0091      */
0092     void showThreeDigits(const QString& digits) const;
0093 
0094     Q_DISABLE_COPY(DigitDisplay)
0095 };
0096 
0097 #endif