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 #include "components/ui_abstract_progress.h"
17 #include "common/image.h"
18 #include "draw/draw_utils.h"
19 #include "gfx_utils/graphic_log.h"
20 #include "imgdecode/cache_manager.h"
21 #include "themes/theme_manager.h"
22 
23 namespace OHOS {
UIAbstractProgress()24 UIAbstractProgress::UIAbstractProgress()
25     : enableBackground_(true),
26       backgroundStyleAllocFlag_(false),
27       foregroundStyleAllocFlag_(false),
28       backgroundImage_(nullptr),
29       foregroundImage_(nullptr),
30       rangeMax_(MAX_PERCENT_VALUE),
31       rangeMin_(MIN_PERCENT_VALUE),
32       curValue_(0),
33       step_(1),
34       lastValue_(0)
35 {
36     style_ = &(StyleDefault::GetBackgroundTransparentStyle());
37     Theme* theme = ThemeManager::GetInstance().GetCurrent();
38     if (theme != nullptr) {
39         backgroundStyle_ = &(theme->GetProgressBackgroundStyle());
40         foregroundStyle_ = &(theme->GetProgressForegroundStyle());
41     } else {
42         backgroundStyle_ = &(StyleDefault::GetProgressBackgroundStyle());
43         foregroundStyle_ = &(StyleDefault::GetProgressForegroundStyle());
44     }
45 }
46 
~UIAbstractProgress()47 UIAbstractProgress::~UIAbstractProgress()
48 {
49     if (backgroundImage_ != nullptr) {
50         delete backgroundImage_;
51         backgroundImage_ = nullptr;
52     }
53 
54     if (foregroundImage_ != nullptr) {
55         delete foregroundImage_;
56         foregroundImage_ = nullptr;
57     }
58 
59     if (backgroundStyleAllocFlag_) {
60         delete backgroundStyle_;
61         backgroundStyle_ = nullptr;
62         backgroundStyleAllocFlag_ = false;
63     }
64 
65     if (foregroundStyleAllocFlag_) {
66         delete foregroundStyle_;
67         foregroundStyle_ = nullptr;
68         foregroundStyleAllocFlag_ = false;
69     }
70 }
71 
SetRange(int32_t rangeMax,int32_t rangeMin)72 void UIAbstractProgress::SetRange(int32_t rangeMax, int32_t rangeMin)
73 {
74     if ((rangeMax_ == rangeMax) && (rangeMin_ == rangeMin)) {
75         return;
76     }
77 
78     if (rangeMax >= rangeMin) {
79         rangeMax_ = rangeMax;
80         rangeMin_ = rangeMin;
81         lastValue_ = rangeMin;
82         SetValue(curValue_);
83     } else {
84         GRAPHIC_LOGW("UIAbstractProgress::SetRange rangeMax less than rangeMin !\n");
85     }
86 };
87 
SetValue(int32_t value)88 void UIAbstractProgress::SetValue(int32_t value)
89 {
90     if (value < rangeMin_) {
91         curValue_ = rangeMin_;
92     } else if (value > rangeMax_) {
93         curValue_ = rangeMax_;
94     } else {
95         curValue_ = value;
96     }
97 
98     if ((curValue_ != lastValue_) &&
99         ((curValue_ == rangeMin_) || (curValue_ == rangeMax_) ||
100         (MATH_ABS(curValue_ - lastValue_) >= static_cast<int32_t>(step_)))) {
101         Invalidate();
102         lastValue_ = curValue_;
103     }
104 }
105 
GetCurrentPos(int16_t distance) const106 int16_t UIAbstractProgress::GetCurrentPos(int16_t distance) const
107 {
108     uint32_t delta = lastValue_ - rangeMin_;
109     uint32_t rangeSize = GetRangeSize();
110     if (rangeSize == 0) {
111         return distance;
112     }
113     int16_t result = static_cast<int64_t>(distance) * delta / rangeSize;
114     return result;
115 }
116 
GetRangeSize() const117 uint32_t UIAbstractProgress::GetRangeSize() const
118 {
119     return (rangeMax_ < rangeMin_) ? 0 : (rangeMax_ - rangeMin_);
120 }
121 
SetImage(const char * foregroundImage,const char * backgroundImage)122 void UIAbstractProgress::SetImage(const char* foregroundImage, const char* backgroundImage)
123 {
124     if (!InitImage()) {
125         return;
126     }
127     backgroundImage_->SetSrc(backgroundImage);
128     foregroundImage_->SetSrc(foregroundImage);
129 }
130 
SetImage(const ImageInfo * foregroundImage,const ImageInfo * backgroundImage)131 void UIAbstractProgress::SetImage(const ImageInfo* foregroundImage, const ImageInfo* backgroundImage)
132 {
133     if (!InitImage()) {
134         return;
135     }
136     backgroundImage_->SetSrc(backgroundImage);
137     foregroundImage_->SetSrc(foregroundImage);
138 }
139 
SetBackgroundStyle(const Style & style)140 void UIAbstractProgress::SetBackgroundStyle(const Style& style)
141 {
142     if (!backgroundStyleAllocFlag_) {
143         backgroundStyle_ = new Style();
144         if (backgroundStyle_ == nullptr) {
145             GRAPHIC_LOGE("new Style1 fail");
146             return;
147         }
148         backgroundStyleAllocFlag_ = true;
149     }
150     *backgroundStyle_ = style;
151 }
152 
SetBackgroundStyle(uint8_t key,int64_t value)153 void UIAbstractProgress::SetBackgroundStyle(uint8_t key, int64_t value)
154 {
155     if (!backgroundStyleAllocFlag_) {
156         backgroundStyle_ = new Style(*backgroundStyle_);
157         if (backgroundStyle_ == nullptr) {
158             GRAPHIC_LOGE("new Style1 fail");
159             return;
160         }
161         backgroundStyleAllocFlag_ = true;
162     }
163     backgroundStyle_->SetStyle(key, value);
164 }
165 
GetBackgroundStyle() const166 const Style& UIAbstractProgress::GetBackgroundStyle() const
167 {
168     return *backgroundStyle_;
169 }
170 
GetBackgroundStyle(uint8_t key) const171 int64_t UIAbstractProgress::GetBackgroundStyle(uint8_t key) const
172 {
173     return backgroundStyle_->GetStyle(key);
174 }
175 
SetForegroundStyle(const Style & style)176 void UIAbstractProgress::SetForegroundStyle(const Style& style)
177 {
178     if (!foregroundStyleAllocFlag_) {
179         foregroundStyle_ = new Style();
180         if (foregroundStyle_ == nullptr) {
181             GRAPHIC_LOGE("new Style1 fail");
182             return;
183         }
184         foregroundStyleAllocFlag_ = true;
185     }
186     *foregroundStyle_ = style;
187 }
188 
SetForegroundStyle(uint8_t key,int64_t value)189 void UIAbstractProgress::SetForegroundStyle(uint8_t key, int64_t value)
190 {
191     if (!foregroundStyleAllocFlag_) {
192         foregroundStyle_ = new Style(*foregroundStyle_);
193         if (foregroundStyle_ == nullptr) {
194             GRAPHIC_LOGE("new Style1 fail");
195             return;
196         }
197         foregroundStyleAllocFlag_ = true;
198     }
199     foregroundStyle_->SetStyle(key, value);
200 }
201 
GetForegroundStyle() const202 const Style& UIAbstractProgress::GetForegroundStyle() const
203 {
204     return *foregroundStyle_;
205 }
206 
GetForegroundStyle(uint8_t key) const207 int64_t UIAbstractProgress::GetForegroundStyle(uint8_t key) const
208 {
209     return foregroundStyle_->GetStyle(key);
210 }
211 
InitImage()212 bool UIAbstractProgress::InitImage()
213 {
214     if (backgroundImage_ == nullptr) {
215         backgroundImage_ = new Image();
216         if (backgroundImage_ == nullptr) {
217             GRAPHIC_LOGE("new Image fail");
218             return false;
219         }
220     }
221     if (foregroundImage_ == nullptr) {
222         foregroundImage_ = new Image();
223         if (foregroundImage_ == nullptr) {
224             GRAPHIC_LOGE("new Image fail");
225             return false;
226         }
227     }
228     return true;
229 }
230 } // namespace OHOS
231