• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "progress_bar.h"
16 #include <cstdio>
17 #include <cstdlib>
18 #include <cstring>
19 #include "log/log.h"
20 #include "securec.h"
21 
22 namespace updater {
23 constexpr int DEFAULT_NORMAL_COLOR = 0xFF;
24 constexpr int MAX_PROGRESS_VALUE = 100;
25 constexpr uint32_t DEFAULT_PROGRESS_COLOR = 0x00;
26 
ProgressBar(const int startX,const int startY,const int w,const int h,Frame * parent)27 ProgressBar::ProgressBar(const int startX, const int startY, const int w, const int h, Frame *parent)
28 {
29     startX_ = startX;
30     startY_ = startY;
31     this->CreateBuffer(w, h, View::PixelFormat::BGRA888);
32     parent_ = parent;
33     SetFocusAble(false);
34     parent_->ViewRegister(this);
35     progressColor_.r = DEFAULT_NORMAL_COLOR;
36     progressColor_.g = DEFAULT_NORMAL_COLOR;
37     progressColor_.b = DEFAULT_NORMAL_COLOR;
38     progressColor_.a = DEFAULT_NORMAL_COLOR;
39     normalColor_.r = DEFAULT_NORMAL_COLOR;
40     normalColor_.g = DEFAULT_PROGRESS_COLOR;
41     normalColor_.b = DEFAULT_PROGRESS_COLOR;
42     normalColor_.a = DEFAULT_NORMAL_COLOR;
43     DrawBackground();
44 }
45 
SetProgressValue(int value)46 void ProgressBar::SetProgressValue(int value)
47 {
48     if (value < 0) {
49         return;
50     }
51     if (value > MAX_PROGRESS_VALUE) {
52         value = MAX_PROGRESS_VALUE;
53     }
54     pValue_ = value;
55     OnDraw();
56     return;
57 }
58 
DrawProgress()59 void ProgressBar::DrawProgress()
60 {
61     int ret = 0;
62     int pixelLen = pValue_ * viewWidth_ / MAX_PROGRESS_VALUE;
63     if (pixelLen > viewWidth_) {
64         pixelLen = viewWidth_;
65     }
66     char *tmpBuf = static_cast<char*>(GetBuffer());
67     BRGA888Pixel pixBuf[pixelLen];
68     for (int a = 0; a < pixelLen; a++) {
69         pixBuf[a].r = progressColor_.r;
70         pixBuf[a].g = progressColor_.g;
71         pixBuf[a].b = progressColor_.b;
72         pixBuf[a].a = progressColor_.a;
73     }
74     for (int i = 0; i < viewHeight_; i++) {
75         ret = memcpy_s(tmpBuf + i * viewWidth_ * sizeof(BRGA888Pixel), pixelLen * sizeof(BRGA888Pixel) + 1,
76             reinterpret_cast<char*>(pixBuf), pixelLen * sizeof(BRGA888Pixel));
77         UPDATER_ERROR_CHECK(ret == 0, "memcpy_s error", break);
78     }
79     return;
80 }
81 
OnDraw()82 void ProgressBar::OnDraw()
83 {
84     SyncBuffer();
85     DrawBackground();
86     DrawProgress();
87     if (parent_ != nullptr) {
88         parent_->OnDraw();
89     } else {
90         LOG(INFO) << "no draw";
91     }
92     return;
93 }
94 
DrawBackground()95 void ProgressBar::DrawBackground()
96 {
97     int ret = 0;
98     char *tmpBuf = static_cast<char*>(GetBuffer());
99     BRGA888Pixel pixBuf[viewWidth_];
100     for (int a = 0; a < viewWidth_; a++) {
101         pixBuf[a].r = normalColor_.r;
102         pixBuf[a].g = normalColor_.g;
103         pixBuf[a].b = normalColor_.b;
104         pixBuf[a].a = normalColor_.a;
105     }
106     for (int i = 0; i < viewHeight_; i++) {
107         ret = memcpy_s(tmpBuf + i * viewWidth_ * sizeof(BRGA888Pixel), viewWidth_ * sizeof(BRGA888Pixel) + 1,
108             reinterpret_cast<char*>(pixBuf), viewWidth_ * sizeof(BRGA888Pixel));
109         UPDATER_ERROR_CHECK(ret == 0, "memcpy_s error", break);
110     }
111     return;
112 }
113 } // namespace updater
114