1 /*
2  * Copyright (c) 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DATA_PANEL_DATA_PANEL_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DATA_PANEL_DATA_PANEL_COMPONENT_H
18 
19 #include <vector>
20 
21 #include "core/components/chart/chart_component.h"
22 #include "core/components/common/properties/color.h"
23 #include "core/components/theme/theme_manager_impl.h"
24 #include "core/pipeline/base/render_component.h"
25 
26 namespace OHOS::Ace {
27 
28 enum class SegmentStyleType {
29     USE_GRADIENT,
30     USE_COLOR,
31     NONE, // user hasn't set color in css.
32 };
33 
34 class ACE_EXPORT Segment final {
35 public:
36     Segment() = default;
37     ~Segment() = default;
38 
SetSegmentName(const std::string & name)39     void SetSegmentName(const std::string& name)
40     {
41         name_ = name;
42     }
43 
GetSegmentName()44     const std::string& GetSegmentName() const
45     {
46         return name_;
47     }
48 
SetStartColor(const Color & color)49     void SetStartColor(const Color& color)
50     {
51         startColor_ = color;
52     }
53 
GetStartColor()54     const Color& GetStartColor() const
55     {
56         return startColor_;
57     }
58 
SetEndColor(const Color & color)59     void SetEndColor(const Color& color)
60     {
61         endColor_ = color;
62     }
63 
GetEndColor()64     const Color& GetEndColor() const
65     {
66         return endColor_;
67     }
68 
SetColorDescriptor(const std::string & colorDescriptor)69     void SetColorDescriptor(const std::string& colorDescriptor)
70     {
71         colorDescriptor_ = colorDescriptor;
72     }
73 
GetColorDescriptor()74     const std::string& GetColorDescriptor() const
75     {
76         return colorDescriptor_;
77     }
78 
GetColorType()79     SegmentStyleType GetColorType() const
80     {
81         return style_;
82     }
83 
SetColorType(SegmentStyleType type)84     void SetColorType(SegmentStyleType type)
85     {
86         style_ = type;
87     }
88 
GetValue()89     double GetValue() const
90     {
91         return value_;
92     }
93 
SetValue(double value)94     void SetValue(double value)
95     {
96         value_ = value;
97     }
98 
99 private:
100     std::string name_;
101     std::string colorDescriptor_;
102     Color startColor_;
103     Color endColor_;
104     double value_ = 0.0;
105     SegmentStyleType style_ = SegmentStyleType::NONE;
106 };
107 
108 class DataPanelComponent : public RenderComponent {
109     DECLARE_ACE_TYPE(DataPanelComponent, RenderComponent);
110 
111 public:
112     RefPtr<Element> CreateElement() override;
113 
114     // determine the data panel type before call InitalStyle
115     virtual void InitalStyle(const RefPtr<ThemeManager>& themeManager);
116 
SetMeasureType(MeasureType type)117     void SetMeasureType(MeasureType type)
118     {
119         measureType_ = type;
120     }
121 
GetMeasureType()122     MeasureType GetMeasureType() const
123     {
124         return measureType_;
125     }
126 
SetAutoScale(bool autoScale)127     void SetAutoScale(bool autoScale)
128     {
129         autoScale_ = autoScale;
130     }
131 
GetAutoScale()132     bool GetAutoScale() const
133     {
134         return autoScale_;
135     }
136 
137     virtual ChartType GetPanelType() const = 0;
138 
SetPanelType(ChartType type)139     void SetPanelType(ChartType type)
140     {
141         type_ = type;
142     }
143 
GetDefaultHeight()144     const Dimension& GetDefaultHeight() const
145     {
146         return height_;
147     }
148 
GetDefaultWidth()149     const Dimension& GetDefaultWidth() const
150     {
151         return width_;
152     }
153 
GetThickness()154     const Dimension& GetThickness() const
155     {
156         return thickness_;
157     }
158 
SetThickness(const Dimension & thickness)159     void SetThickness(const Dimension& thickness)
160     {
161         thickness_ = thickness;
162     }
163 
SetEffects(bool effects)164     void SetEffects(bool effects)
165     {
166         showEffects_ = effects;
167     }
168 
GetEffects()169     bool GetEffects() const
170     {
171         return showEffects_;
172     }
173 
SetTrackColor(const Color & backgroundTrackColor)174     void SetTrackColor(const Color& backgroundTrackColor)
175     {
176         backgroundTrackColor_ = backgroundTrackColor;
177     }
178 
GetTrackColor()179     Color GetTrackColor() const
180     {
181         return backgroundTrackColor_;
182     }
183 
GetAnimationDuration()184     double GetAnimationDuration() const
185     {
186         return animationDuration_;
187     }
188 
SetAnimationDuration(double animationDuration)189     void SetAnimationDuration(double animationDuration)
190     {
191         animationDuration_ = animationDuration;
192     }
193 
194 protected:
SetDefaultHeight(const Dimension & height)195     void SetDefaultHeight(const Dimension& height)
196     {
197         height_ = height;
198     }
199 
SetDefaultWidth(const Dimension & width)200     void SetDefaultWidth(const Dimension& width)
201     {
202         width_ = width;
203     }
204 
205     ChartType type_ = ChartType::PROGRESS;
206     double animationDuration_ = -1.0;
207 
208 private:
209     MeasureType measureType_ = MeasureType::DEFAULT;
210     Dimension thickness_;
211     // data panel default height and width
212     Dimension height_;
213     Dimension width_;
214     bool showEffects_ = false;
215     Color backgroundTrackColor_;
216     // autoScale is used to resize the thickness based on radius.
217     bool autoScale_ = false;
218 };
219 
220 class ProgressDataPanelComponent : public DataPanelComponent {
221     DECLARE_ACE_TYPE(ProgressDataPanelComponent, DataPanelComponent);
222 
223 public:
224     ProgressDataPanelComponent() = default;
225     ~ProgressDataPanelComponent() = default;
226 
ProgressDataPanelComponent(ChartType type)227     explicit ProgressDataPanelComponent(ChartType type)
228     {
229         type_ = type;
230     }
231 
232     RefPtr<RenderNode> CreateRenderNode() override;
GetPanelType()233     ChartType GetPanelType() const override
234     {
235         return type_;
236     }
237 
238     // InitalStyle will assign UX design color.
239     // call this method after type determined.
240     // this method will override color.
241     void InitalStyle(const RefPtr<ThemeManager>& themeManager) override;
242 
GetStartColor()243     const Color& GetStartColor() const
244     {
245         return startColor_;
246     }
247 
SetStartColor(const Color & color)248     void SetStartColor(const Color& color)
249     {
250         startColor_ = color;
251     }
252 
GetEndColor()253     const Color& GetEndColor() const
254     {
255         return endColor_;
256     }
257 
SetEndColor(const Color & color)258     void SetEndColor(const Color& color)
259     {
260         endColor_ = color;
261     }
262 
GetProgressValue()263     double GetProgressValue() const
264     {
265         return progress_;
266     }
267 
SetProgressValue(double progress)268     void SetProgressValue(double progress)
269     {
270         progress_ = progress;
271     }
272 
IsLoading()273     bool IsLoading() const
274     {
275         return isLoading_;
276     }
277 
SetIsLoadingType(bool isLoading)278     void SetIsLoadingType(bool isLoading)
279     {
280         isLoading_ = isLoading;
281     }
282 
283 private:
284     Color startColor_;
285     Color endColor_;
286     double progress_ = 0.0;
287     bool isLoading_ = false;
288 };
289 
290 class ACE_EXPORT PercentageDataPanelComponent : public DataPanelComponent {
291     DECLARE_ACE_TYPE(PercentageDataPanelComponent, DataPanelComponent);
292 
293 public:
294     PercentageDataPanelComponent() = default;
295     ~PercentageDataPanelComponent() = default;
296 
PercentageDataPanelComponent(ChartType type)297     explicit PercentageDataPanelComponent(ChartType type)
298     {
299         type_ = type;
300     }
301 
302     RefPtr<RenderNode> CreateRenderNode() override;
303 
304     // InitalStyle will assign UX design color
305     void InitalStyle(const RefPtr<ThemeManager>& themeManager) override;
306 
GetPanelType()307     ChartType GetPanelType() const override
308     {
309         return type_;
310     }
311 
AppendSegment(const Segment & segment)312     void AppendSegment(const Segment& segment)
313     {
314         segments_.push_back(segment);
315         totalValue_ += segment.GetValue();
316     }
317 
ClearSegment()318     void ClearSegment()
319     {
320         segments_.clear();
321         totalValue_ = 0.0;
322     }
323 
GetSegments()324     const std::vector<Segment>& GetSegments() const
325     {
326         return segments_;
327     }
328 
GetStartDegree()329     double GetStartDegree() const
330     {
331         return startDegree_;
332     }
333 
SetStartDegree(double startDegree)334     void SetStartDegree(double startDegree)
335     {
336         startDegree_ = startDegree;
337     }
338 
GetSweepDegree()339     double GetSweepDegree() const
340     {
341         return sweepDegree_;
342     }
343 
SetSweepDegree(double sweepDegree)344     void SetSweepDegree(double sweepDegree)
345     {
346         sweepDegree_ = sweepDegree;
347     }
348 
GetTotalValue()349     double GetTotalValue() const
350     {
351         return totalValue_;
352     }
353 
SetMaxValue(double maxValue)354     void SetMaxValue(double maxValue)
355     {
356         maxValue_ = maxValue;
357     }
358 
GetMaxValue()359     double GetMaxValue() const
360     {
361         return maxValue_;
362     }
363 
364 private:
365     double startDegree_ = 0.0;
366     double sweepDegree_ = 360.0;
367     std::vector<Segment> segments_;
368     double totalValue_ = 0.0;
369     double maxValue_ = 100.0;
370 };
371 
372 } // namespace OHOS::Ace
373 
374 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DATA_PANEL_DATA_PANEL_COMPONENT_H
375