1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup UI_Components
18  * @{
19  *
20  * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file ui_digital_clock.h
28  *
29  * @brief Declares a digital clock.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_UI_DIGITAL_CLOCK_H
36 #define GRAPHIC_LITE_UI_DIGITAL_CLOCK_H
37 
38 #include "gfx_utils/color.h"
39 #include "components/ui_abstract_clock.h"
40 #include "components/ui_label.h"
41 
42 namespace OHOS {
43 /**
44  * @brief Displays time digitally.
45  *
46  * @see UIDigitalClock
47  * @since 1.0
48  * @version 1.0
49  */
50 class UIDigitalClock : public UIAbstractClock {
51 public:
52     /**
53      * @brief A default constructor used to create a <b>UIDigitalClock</b> instance.
54      *
55      * @since 1.0
56      * @version 1.0
57      */
58     UIDigitalClock();
59 
60     /**
61      * @brief A destructor used to delete the <b>UIDigitalClock</b> instance.
62      *
63      * @since 1.0
64      * @version 1.0
65      */
66     virtual ~UIDigitalClock();
67 
68     /**
69      * @brief Obtains the view type.
70      *
71      * @return Returns <b>UI_DIGITAL_CLOCK</b>, as defined in {@link UIViewType}.
72      * @since 1.0
73      * @version 1.0
74      */
GetViewType()75     UIViewType GetViewType() const override
76     {
77         return UI_DIGITAL_CLOCK;
78     }
79 
80     /**
81      * @brief Sets the dynamic font ID for this digital clock.
82      *
83      * @param fontId Indicates the dynamic font ID to set.
84      * @since 1.0
85      * @version 1.0
86      */
87     void SetFontId(uint16_t fontId);
88 
89     /**
90      * @brief Sets the dynamic font for this digital clock.
91      *
92      * @param name Indicates the pointer to the font name.
93      * @param size Indicates the font size to set.
94      * @since 1.0
95      * @version 1.0
96      */
97     void SetFont(const char* name, uint8_t size);
98 
99     /**
100      * @brief Sets the font color for this digital clock.
101      *
102      * @param color Indicates the front color to set.
103      * @since 1.0
104      * @version 1.0
105      */
106     void SetColor(ColorType color);
107 
108     /**
109      * @brief Enumerates the display modes of this digital clock.
110      */
111     enum DisplayMode : uint8_t {
112         /** 12-hour format, without seconds */
113         DISPLAY_12_HOUR_NO_SECONDS,
114         /** 24-hour format, without seconds */
115         DISPLAY_24_HOUR_NO_SECONDS,
116         /** 12-hour format */
117         DISPLAY_12_HOUR,
118         /** 24-hour format */
119         DISPLAY_24_HOUR
120     };
121 
122     /**
123      * @brief Enumerates the time elements of this digital clock.
124      */
125     enum TimeElement : uint8_t {
126         /** Clock */
127         HOUR_ELEMENT = 0,
128         /** Minute */
129         MINUTE_ELEMENT,
130         /** Second */
131         SECOND_ELEMENT
132     };
133 
134     /**
135      * @brief Sets the display mode for this digital clock.
136      *
137      * @param dm Indicates the display mode to set.
138      * @since 1.0
139      * @version 1.0
140      */
SetDisplayMode(DisplayMode dm)141     void SetDisplayMode(DisplayMode dm)
142     {
143         displayMode_ = dm;
144         UpdateClock(false);
145     }
146 
147     /**
148      * @brief Obtains the display mode of this digital clock.
149      *
150      * @return Returns the display mode of this digital clock.
151      * @since 1.0
152      * @version 1.0
153      */
GetDisplayMode()154     DisplayMode GetDisplayMode() const
155     {
156         return displayMode_;
157     }
158 
159     /**
160      * @brief Sets whether to add <b>0</b> before the hour of this digital clock.
161      *
162      * @param displayLeadingZero Specifies whether to add <b>0</b> before the hour of this digital clock.
163      *                           <b>true</b> indicates that <b>0</b> is added, and <b>false</b> indicates
164      *                           the opposite case.
165      * @since 1.0
166      * @version 1.0
167      */
168     void DisplayLeadingZero(bool displayLeadingZero);
169 
170     /**
171      * @brief Sets the opacity for this digital clock.
172      *
173      * @param opacity Indicates the opacity of this digital clock. The value ranges
174      *                from <b>0</b> (transparency) to <b>255</b> (opacity).
175      * @since 1.0
176      * @version 1.0
177      */
178     void SetOpacity(uint8_t opacity);
179 
180     /**
181      * @brief Obtains the opacity of this digital clock.
182      *
183      * @return Returns the opacity of this digital clock, within [0, 255].
184      * @since 1.0
185      * @version 1.0
186      */
187     uint8_t GetOpacity() const;
188 
189     /**
190      * @brief Updates this digital clock.
191      *
192      * @param clockInit Specifies whether the first initialization. <b>true</b> indicates the first initialization,
193      *                  and <b> false</b> indicates the opposite case.
194      * @since 1.0
195      * @version 1.0
196      */
197     void UpdateClock(bool clockInit) override;
198 
199     /**
200      * @brief Obtains the color_.
201      *
202      * @return Returns the color_.
203      */
GetColor()204     ColorType GetColor() const
205     {
206         return color_;
207     }
208 
209     /**
210      * @brief Obtains the leadingZero_.
211      *
212      * @return Returns the leadingZero_.
213      */
GetLeadingZero()214     bool GetLeadingZero() const
215     {
216         return leadingZero_;
217     }
218 protected:
219     static constexpr uint8_t TIME_ELEMENT_COUNT = 3;
220     UILabel* timeLabels_[TIME_ELEMENT_COUNT];
221 
222     virtual void InitTimeLabels();
223 
224 private:
225     static constexpr uint8_t BUFFER_SIZE = 4;
226     void SetHorizontal();
227     void SetVertical();
228     void SetTimeLabels(const char buf[TIME_ELEMENT_COUNT][BUFFER_SIZE]);
229     void SetTimeLabelsPosition();
230     void TimeElementRefresh();
231     void RefreshTime();
232     DisplayMode displayMode_;
233     bool leadingZero_;
234     ColorType color_;
235     uint8_t prevHour_;
236     uint8_t prevMinute_;
237     uint8_t prevSecond_;
238     bool verticalShow_;
239 };
240 } // namespace OHOS
241 #endif // UI_DIGITAL_CLOCK_H
242