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_circle_progress.h
28  *
29  * @brief Defines the attributes and common functions of a circular progress bar.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_UI_CIRCLE_PROGRESS_H
36 #define GRAPHIC_LITE_UI_CIRCLE_PROGRESS_H
37 
38 #include "components/ui_abstract_progress.h"
39 
40 namespace OHOS {
41 /**
42  * @brief Represents a circular progress bar.
43  *
44  * This class is used to set the start and end angles, range, and current value to display the circular progress bar.
45  *
46  * @see UIAbstractProgress
47  * @since 1.0
48  * @version 1.0
49  */
50 class UICircleProgress : public UIAbstractProgress {
51 public:
52     /**
53      * @brief A constructor used to create a <b>UICircleProgress</b> instance.
54      *
55      * @since 1.0
56      * @version 1.0
57      */
58     UICircleProgress();
59 
60     /**
61      * @brief A destructor used to delete the <b>UICircleProgress</b> instance.
62      *
63      * @since 1.0
64      * @version 1.0
65      */
~UICircleProgress()66     virtual ~UICircleProgress() {}
67 
68     /**
69      * @brief Obtains the view type.
70      *
71      * @return Returns the view type, as defined in {@link UIViewType}.
72      * @since 1.0
73      * @version 1.0
74      */
GetViewType()75     UIViewType GetViewType() const override
76     {
77         return UI_CIRCLE_PROGRESS;
78     }
79 
OnPreDraw(Rect & invalidatedArea)80     bool OnPreDraw(Rect& invalidatedArea) const override
81     {
82         return false;
83     }
84 
85     void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
86 
87     /**
88      * @brief Sets the coordinates of the center point for this circular progress bar.
89      *
90      * The coordinates of the center point refer to the position relative to the view.
91      *
92      * @param x Indicates the x-coordinate to set. The default value is 0.
93      * @param y Indicates the y-coordinate to set. The default value is 0.
94      * @see GetCenterPosition
95      * @since 1.0
96      * @version 1.0
97      */
98     void SetCenterPosition(int16_t x, int16_t y);
99 
100     /**
101      * @brief Obtains the coordinates of the center point for this circular progress bar.
102      *
103      * The coordinates of the center point refer to the position relative to the view.
104      *
105      * @return Returns the coordinates of the center point.
106      * @see SetCenterPosition
107      * @since 1.0
108      * @version 1.0
109      */
GetCenterPosition()110     Point GetCenterPosition() const
111     {
112         return center_;
113     }
114 
115     /**
116      * @brief Sets the outer radius for this circular progress bar.
117      *
118      * @param radius Indicates the outer radius to set.
119      * @see GetRadius
120      * @since 1.0
121      * @version 1.0
122      */
SetRadius(uint16_t radius)123     void SetRadius(uint16_t radius)
124     {
125         radius_ = radius;
126     }
127 
128     /**
129      * @brief Obtains the outer radius of this circular progress bar.
130      *
131      * @return Returns the outer radius.
132      * @see SetRadius
133      * @since 1.0
134      * @version 1.0
135      */
GetRadius()136     uint16_t GetRadius() const
137     {
138         return radius_;
139     }
140 
141     /**
142      * @brief Sets the start angle.
143      *
144      * The 12-clock direction is 0 degrees, and the 3-clock direction is 90 degrees. \n
145      * If the start angle is smaller than the end angle, the progress bar is clockwise.
146      * Otherwise, the progress bar is anticlockwise. \n
147      * The maximum range of a progress bar is a circle. If the difference between the start angle and end angle
148      * exceeds 360 degrees, a circular progress bar is created. \n
149      *
150      * @param startAngle Indicates the start angle to set, which can be any value represented by <b>int16_t</b>.
151      *                   The default value is 0.
152      * @see GetStartAngle | SetEndAngle
153      * @since 1.0
154      * @version 1.0
155      */
156     void SetStartAngle(int16_t startAngle);
157 
158     /**
159      * @brief Obtains the start angle.
160      *
161      * @return Returns the start angle.
162      * @see SetStartAngle
163      * @since 1.0
164      * @version 1.0
165      */
GetStartAngle()166     int16_t GetStartAngle() const
167     {
168         return startAngle_;
169     }
170 
171     /**
172      * @brief Sets the end angle.
173      *
174      * The 12-clock direction is 0 degrees, and the 3-clock direction is 90 degrees. \n
175      * If the start angle is smaller than the end angle, the progress bar is clockwise.
176      * Otherwise, the progress bar is anticlockwise. \n
177      * The maximum range of a progress bar is a circle. If the difference between the start angle and end angle
178      * exceeds 360 degrees, a circular progress bar is created. \n
179      *
180      * @param endAngle Indicates the end angle to set, which can be any value represented by <b>int16_t</b>.
181      *                 The default value is 360.
182      * @see GetEndAngle | SetStartAngle
183      * @since 1.0
184      * @version 1.0
185      */
186     void SetEndAngle(int16_t endAngle);
187 
188     /**
189      * @brief Obtains the end angle.
190      *
191      * @return Returns the end angle.
192      * @see SetEndAngle
193      * @since 1.0
194      * @version 1.0
195      */
GetEndAngle()196     int16_t GetEndAngle() const
197     {
198         return endAngle_;
199     }
200 
201     /**
202      * @brief Sets the coordinates of the foreground image for this progress bar relative to the view.
203      *
204      * @param x Indicates the x-coordinate to set. The default value is 0.
205      * @param y Indicates the y-coordinate to set. The default value is 0.
206      * @see SetBackgroundImagePosition
207      * @since 1.0
208      * @version 1.0
209      */
SetProgressImagePosition(int16_t x,int16_t y)210     void SetProgressImagePosition(int16_t x, int16_t y)
211     {
212         progressImagePos_.x = x + style_->paddingLeft_ + style_->borderWidth_;
213         progressImagePos_.y = y + style_->paddingTop_ + style_->borderWidth_;
214     }
215 
216     /**
217      * @brief Sets the coordinates of the background image for this progress bar relative to the view.
218      *
219      * @param x Indicates the x-coordinate to set. The default value is 0.
220      * @param y Indicates the y-coordinate to set. The default value is 0.
221      * @see SetProgressImagePosition
222      * @since 1.0
223      * @version 1.0
224      */
SetBackgroundImagePosition(int16_t x,int16_t y)225     void SetBackgroundImagePosition(int16_t x, int16_t y)
226     {
227         backgroundImagePos_.x = x + style_->paddingLeft_ + style_->borderWidth_;
228         backgroundImagePos_.y = y + style_->paddingTop_ + style_->borderWidth_;
229     }
230 
231     /**
232      * @brief Sets the foreground color for this progress bar.
233      *
234      * @param color Indicates the foreground color to set. For details, see {@link ColorType}.
235      * @since 1.0
236      * @version 1.0
237      */
SetLineColor(ColorType color)238     void SetLineColor(ColorType color)
239     {
240         SetForegroundStyle(STYLE_LINE_COLOR, color.full);
241     }
242 
243 private:
244     static constexpr uint16_t MAX_ANGLE_VALUE = CIRCLE_IN_DEGREE;
245     static constexpr uint16_t MIN_ANGLE_VALUE = 0;
246 
247     void GetStartEndAngle(int16_t& start, int16_t& end) const;
248     void GetAngleRange(int16_t& start, int16_t& end) const;
249     void GetRedrawAngle(int16_t& start, int16_t& end) const;
250     void DrawCommonCircle(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea);
251 
252     Point center_;
253     Point backgroundImagePos_;
254     Point progressImagePos_;
255     uint16_t radius_;
256     int16_t startAngle_;
257     int16_t endAngle_;
258 };
259 } // namespace OHOS
260 #endif // GRAPHIC_LITE_UI_CIRCLE_PROGRESS_H
261