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_time_picker.h
28  *
29  * @brief Defines the attributes and functions of the <b>UITimePicker</b> class.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_UI_TIME_PICKER_H
36 #define GRAPHIC_LITE_UI_TIME_PICKER_H
37 
38 #include "components/ui_picker.h"
39 
40 namespace OHOS {
41 /**
42  * @brief Defines the time picker. The time is in the format of "hour:minute" or "hour:minute:second".
43  *        The selected time is highlighted.
44  *
45  * @since 1.0
46  * @version 1.0
47  */
48 class UITimePicker : public UIViewGroup {
49 public:
50 #if ENABLE_ROTATE_INPUT
51     static constexpr const char* HOUR_LIST_NAME = "hour";
52     static constexpr const char* MIN_LIST_NAME = "minute";
53     static constexpr const char* SEC_LIST_NAME = "second";
54 #endif
55     /**
56      * 枚举Picker类型
57      */
58     enum PickerType : uint8_t {
59         /* 时 */
60         PICKER_HOUR = 0,
61         /* 分 */
62         PICKER_MIN,
63         /* 秒 */
64         PICKER_SEC,
65         /* 最大值 */
66         PICKER_MAX,
67     };
68     /**
69      * @brief A constructor used to create a <b>UITimePicker</b> instance.
70      *
71      * @since 1.0
72      * @version 1.0
73      */
74     UITimePicker();
75 
76     /**
77      * @brief A destructor used to delete the <b>UITimePicker</b> instance.
78      *
79      * @since 1.0
80      * @version 1.0
81      */
82     virtual ~UITimePicker();
83 
84     /**
85      * @brief Obtains the view type.
86      *
87      * @return Returns the view type. For details, see {@link UIViewType}.
88      * @since 1.0
89      * @version 1.0
90      */
GetViewType()91     UIViewType GetViewType() const override
92     {
93         return UI_TIME_PICKER;
94     }
95 
96     /**
97      * @brief Sets the time currently selected in the time picker.
98      *
99      * @param value Indicates the pointer to the selected time, which is in the format of "hour:minute:second",
100      *              for example, 10:12:50. The second is optional and depends on the setting of {@link EnableSecond}.
101      *              The time must be a valid value. For example, the hour value must range from 0 to 23.
102      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
103      * @since 1.0
104      * @version 1.0
105      */
106     bool SetSelected(const char* value);
107 
108     /**
109      * @brief Obtains the time currently selected in the time picker.
110      *
111      * @return Returns a string representing the selected time. The time is in the format of "hour:minute:second",
112      *         for example, 10:12:50. The second is optional and depends on the setting of {@link EnableSecond}.
113      * @since 1.0
114      * @version 1.0
115      */
GetSelectValue()116     const char* GetSelectValue()
117     {
118         return selectedValue_;
119     }
120 
121     /**
122      * @brief Obtains the hour currently selected in the time picker.
123      *
124      * @return Returns a string representing the selected hour, for example, 10.
125      * @since 1.0
126      * @version 1.0
127      */
GetSelectHour()128     const char* GetSelectHour() const
129     {
130         return selectedHour_;
131     }
132 
133     /**
134      * @brief Obtains the minute currently selected in the time picker.
135      *
136      * @return Returns a string representing the selected minute, for example, 12.
137      * @since 1.0
138      * @version 1.0
139      */
GetSelectMinute()140     const char* GetSelectMinute() const
141     {
142         return selectedMinute_;
143     }
144 
145     /**
146      * @brief Obtains the second currently selected in the time picker.
147      *
148      * @return Returns a string representing the selected second, for example, 50.
149      * @since 1.0
150      * @version 1.0
151      */
GetSelectSecond()152     const char* GetSelectSecond() const
153     {
154         return selectedSecond_;
155     }
156 
157     /**
158      * @brief Sets the height of each item in the time picker.
159      *
160      * @param height Indicates the height of each item.
161      * @since 1.0
162      * @version 1.0
163      */
164     void SetItemHeight(int16_t height);
165 
166     /**
167      * @brief Sets whether to enable seconds in the time picker.
168      *
169      * @param state Specifies whether to enable seconds in the time picker. Value <b>true</b> means to enable seconds,
170      *              and value <b>false</b> means to disable seconds. The default value is <b>false</b>.
171      * @since 1.0
172      * @version 1.0
173      */
174     void EnableSecond(bool state);
175 
176     /**
177      * @brief Sets the text format in the time picker, including the font ID and color.
178      *
179      * @param backgroundFontId Indicates the font ID of the background text.
180      * @param highlightFontId  Indicates the font ID of the highlighted text.
181      * @param backgroundColor  Indicates the color of the background text.
182      * @param highlightColor   Indicates the color of the highlighted text.
183      * @since 1.0
184      * @version 1.0
185      */
186     void SetTextStyle(uint16_t backgroundFontId,
187                       uint16_t highlightFontId,
188                       ColorType backgroundColor,
189                       ColorType highlightColor);
190 
191     /**
192      * @brief Sets the text color in the time picker.
193      *
194      * @param backgroundColor Indicates the color of the background text.
195      * @param highlightColor Indicates the color of the highlighted text.
196      * @since 1.0
197      * @version 1.0
198      */
199     void SetTextColor(ColorType backgroundColor, ColorType highlightColor);
200 
201     /**
202      * @brief Sets the font name and size for the background text.
203      *
204      * @param name Indicates the pointer to the font name to set.
205      * @param size Indicates the font size to set.
206      * @since 1.0
207      * @version 1.0
208      */
209     void SetBackgroundFont(const char* name, uint8_t size);
210 
211     /**
212      * @brief Sets the font name and size for the highlighted text.
213      *
214      * @param name Indicates the pointer to the font name to set.
215      * @param size Indicates the font size to set.
216      * @since 1.0
217      * @version 1.0
218      */
219     void SetHighlightFont(const char* name, uint8_t size);
220 
221     /**
222      * @brief Sets the width for the time picker.
223      *
224      * @param width Indicates the width to set.
225      * @since 1.0
226      * @version 1.0
227      */
228     void SetWidth(int16_t width) override;
229 
230     /**
231      * @brief Sets the height for the time picker.
232      *
233      * @param height Indicates the height to set.
234      * @since 1.0
235      * @version 1.0
236      */
237     void SetHeight(int16_t height) override;
238 
239     /**
240      * @brief 设置是否开启循环
241      *
242      * @param pickerType Picker类型
243      * @param state 状态
244      *
245      * @since 3.0
246      * @version 5.0
247      */
248     void SetLoopState(const uint8_t pickerType, bool state);
249 
250     /**
251      * @brief Defines the listener used by the time picker. This listener is triggered when an item is selected
252      *        after sliding stops.
253      *
254      * @since 1.0
255      * @version 1.0
256      */
257     class SelectedListener : public HeapBase {
258     public:
259         /**
260          * @brief A constructor used to create a <b>SelectedListener</b> instance.
261          *
262          * @since 1.0
263          * @version 1.0
264          */
SelectedListener()265         SelectedListener() {}
266 
267         /**
268          * @brief A destructor used to delete the <b>SelectedListener</b> instance.
269          *
270          * @since 1.0
271          * @version 1.0
272          */
~SelectedListener()273         virtual ~SelectedListener() {}
274 
275         /**
276          * @brief Called when an item is selected after sliding stops. This function is implemented by applications.
277          *
278          * @param picker Indicates the time picker instance.
279          * @since 1.0
280          * @version 1.0
281          */
OnTimePickerStoped(UITimePicker & picker)282         virtual void OnTimePickerStoped(UITimePicker& picker) {}
283     };
284 
285     /**
286      * @brief Registers a listener for a selected event.
287      *
288      * @param timePickerListener Indicates the listener to register. For details, see {@link SelectedListener}.
289      *
290      * @since 1.0
291      * @version 1.0
292      */
RegisterSelectedListener(SelectedListener * timePickerListener)293     void RegisterSelectedListener(SelectedListener* timePickerListener)
294     {
295         timePickerListener_ = timePickerListener;
296     }
297 
298     bool OnPressEvent(const PressEvent& event) override;
299 private:
300     class UIPickerListener : public UIPicker::SelectedListener {
301     public:
UIPickerListener(UITimePicker * timePicker)302         explicit UIPickerListener(UITimePicker* timePicker)
303         {
304             timePicker_ = timePicker;
305         }
306 
~UIPickerListener()307         ~UIPickerListener() {}
308 
OnPickerStoped(UIPicker & picker)309         void OnPickerStoped(UIPicker& picker) override
310         {
311             if (timePicker_ != nullptr) {
312                 timePicker_->TimeSelectedCallback();
313             }
314         }
315 
316     private:
317         UITimePicker* timePicker_;
318     };
319     static constexpr uint8_t TIME_START = 0;
320     static constexpr uint8_t HOUR_END = 23;
321     static constexpr uint8_t MIN_END = 59;
322     static constexpr uint8_t SEC_END = 59;
323     static constexpr uint8_t BUF_SIZE = 3;
324     static constexpr uint8_t SELECTED_VALUE_SIZE = 9;
325     static constexpr uint8_t SEC_VISIBLE_COUNT = 3;
326     static constexpr uint8_t SEC_INVISIBLE_COUNT = 2;
327     void TimeSelectedCallback();
328     void InitTimePicker();
329     void DeInitTimePicker();
330     void RefreshTimePicker();
331     bool RefreshSelected(const char* value);
332     void InitPicker(UIPicker*& picker, int16_t start, int16_t end);
333     void DeInitPicker(UIPicker*& picker);
334     void GetValueByIndex(char* value, uint8_t len, uint16_t index, int16_t start, int16_t end);
335     UIPicker* hourPicker_;
336     UIPicker* minutePicker_;
337     UIPicker* secondPicker_;
338     char selectedValue_[SELECTED_VALUE_SIZE];
339     char selectedHour_[BUF_SIZE];
340     char selectedMinute_[BUF_SIZE];
341     char selectedSecond_[BUF_SIZE];
342     bool secVisible_;
343     bool loopState_[PICKER_MAX];
344     uint16_t pickerWidth_;
345     uint16_t itemsHeight_;
346     int16_t xPos_;
347     uint16_t highlightFontId_;
348     uint16_t backgroundFontId_;
349     uint8_t backgroundFontSize_;
350     uint8_t highlightFontSize_;
351     char* backgroundFontName_;
352     char* highlightFontName_;
353     ColorType highlightColor_;
354     ColorType backgroundColor_;
355     UIPickerListener pickerListener_;
356     SelectedListener* timePickerListener_;
357 };
358 } // namespace OHOS
359 #endif // GRAPHIC_LITE_UI_TIME_PICKER_H
360