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_box_progress.h
28 *
29 * @brief Defines the attributes and common functions of a linear progress bar.
30 *
31 * @since 1.0
32 * @version 1.0
33 */
34
35#ifndef GRAPHIC_LITE_UI_BOX_PROGRESS_H
36#define GRAPHIC_LITE_UI_BOX_PROGRESS_H
37
38#include "components/ui_abstract_progress.h"
39
40namespace OHOS {
41/**
42 * @brief Represents a linear progress bar.
43 *
44 * This class is used to set the range and current value to display the linear progress bar
45 * which can be in multiple directions.
46 *
47 * @see UIAbstractProgress
48 * @since 1.0
49 * @version 1.0
50 */
51class UIBoxProgress : public UIAbstractProgress {
52public:
53    /**
54     * @brief Enumerates the directions of the progress bar.
55     */
56    enum class Direction : uint8_t {
57        /** Filling from left to right */
58        DIR_LEFT_TO_RIGHT,
59        /** Filling from right to left */
60        DIR_RIGHT_TO_LEFT,
61        /** Filling from top to bottom */
62        DIR_TOP_TO_BOTTOM,
63        /** Filling from bottom to top */
64        DIR_BOTTOM_TO_TOP,
65    };
66
67    /**
68     * @brief A constructor used to create a <b>UIBoxProgress</b> instance.
69     *
70     * @since 1.0
71     * @version 1.0
72     */
73    UIBoxProgress();
74
75    /**
76     * @brief A destructor used to delete the <b>UIBoxProgress</b> instance.
77     *
78     * @since 1.0
79     * @version 1.0
80     */
81    virtual ~UIBoxProgress() {}
82
83    /**
84     * @brief Obtains the view type.
85     *
86     * @return Returns the view type, as defined in {@link UIViewType}.
87     * @since 1.0
88     * @version 1.0
89     */
90    UIViewType GetViewType() const override
91    {
92        return UI_BOX_PROGRESS;
93    }
94
95    /**
96     * @brief Sets the width for the view holding this progress bar.
97     *
98     * The width of the view must be greater than or equal to the actual width of the progress bar
99     * to ensure a normal display. \n
100     *
101     * @param width Indicates the width of the view.
102     * @see SetHeight | SetValidWidth | SetValidHeight
103     * @since 1.0
104     * @version 1.0
105     */
106    void SetWidth(int16_t width) override
107    {
108        UIView::SetWidth(width);
109        if (!isValidWidthSet_) {
110            progressWidth_ = width;
111        }
112    }
113
114    /**
115     * @brief Sets the height for this view.
116     *
117     * The height of the view must be greater than or equal to the actual height of the progress bar
118     * to ensure a normal display. \n
119     *
120     * @param height Indicates the height to set.
121     * @see SetWidth | SetValidWidth | SetValidHeight
122     * @since 1.0
123     * @version 1.0
124     */
125    void SetHeight(int16_t height) override
126    {
127        UIView::SetHeight(height);
128        if (!isValidHeightSet_) {
129            progressHeight_ = height;
130        }
131    }
132
133    /**
134     * @brief Sets the direction for this progress bar.
135     *
136     * @param direction Indicates the direction to set. The default direction is from left to right.
137     * For details, see {@link Direction}.
138     * @see GetDirection
139     * @since 1.0
140     * @version 1.0
141     */
142    void SetDirection(const Direction& direction)
143    {
144        direction_ = direction;
145    }
146
147    /**
148     * @brief Obtains the direction of this progress bar.
149     *
150     * @return Returns the direction of this progress bar, as defined in {@link Direction}.
151     * @see SetDirection
152     * @since 1.0
153     * @version 1.0
154     */
155    Direction GetDirection() const
156    {
157        return direction_;
158    }
159
160    /**
161     * @brief Sets the actual width for this progress bar.
162     *
163     * The progress bar is centered in the view after the setting. By default, the width of the progress bar
164     * is the same as that of the view. \n
165     * If the width of the progress bar is greater than that of the view, the excess part cannot be displayed. \n
166     *
167     * @param width Indicates the actual width of this progress bar.
168     * @see GetValidWidth
169     * @since 1.0
170     * @version 1.0
171     */
172    void SetValidWidth(int16_t width)
173    {
174        progressWidth_ = width;
175        isValidWidthSet_ = true;
176    }
177
178    /**
179     * @brief Obtains the actual width of this progress bar.
180     *
181     * @return Returns the actual width of this progress bar.
182     * @see SetValidWidth
183     * @since 1.0
184     * @version 1.0
185     */
186    int16_t GetValidWidth() const
187    {
188        return progressWidth_;
189    }
190
191    /**
192     * @brief Sets the actual height for this progress bar.
193     *
194     * The progress bar is centered in the view after the setting. By default, the height of the progress bar
195     * is the same as that of the view. \n
196     * If the height of the progress bar is greater than that of the view, the excess part cannot be displayed. \n
197     *
198     * @param height Indicates the actual height to set.
199     * @see GetValidHeight
200     * @since 1.0
201     * @version 1.0
202     */
203    void SetValidHeight(int16_t height)
204    {
205        progressHeight_ = height;
206        isValidHeightSet_ = true;
207    }
208
209    /**
210     * @brief Obtains the actual height of this progress bar.
211     *
212     * @return Returns the actual height of this progress bar.
213     * @see SetValidHeight
214     * @since 1.0
215     * @version 1.0
216     */
217    int16_t GetValidHeight() const
218    {
219        return progressHeight_;
220    }
221
222    void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
223
224protected:
225    void GetBackgroundParam(Point& startPoint, int16_t& width, int16_t& height, uint16_t& radius, const Style& style);
226    void DrawBackground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea);
227    void DrawForeground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, Rect& coords);
228    void DrawRoundCap(BufferInfo& gfxDstBuffer,
229                      const Image* image,
230                      const Point& imgPos,
231                      const Rect& rect,
232                      const Rect& invalidatedArea,
233                      uint16_t radius,
234                      const Style& style);
235    void DrawValidRect(BufferInfo& gfxDstBuffer,
236                       const Image* image,
237                       const Rect& rect,
238                       const Rect& invalidatedArea,
239                       const Style& style,
240                       uint16_t radius);
241
242    uint16_t progressWidth_;
243    uint16_t progressHeight_;
244    Direction direction_;
245    bool isValidWidthSet_ : 1;
246    bool isValidHeightSet_ : 1;
247};
248} // namespace OHOS
249#endif // GRAPHIC_LITE_UI_BOX_PROGRESS_H
250