1 /*
2 * Copyright (c) 2022 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 "frameworks/bridge/declarative_frontend/jsview/models/gauge_model_impl.h"
17
18 #include "base/log/log_wrapper.h"
19 #include "bridge/declarative_frontend/jsview/js_gauge.h"
20 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
21 #include "bridge/declarative_frontend/view_stack_processor.h"
22 #include "core/components/chart/chart_component.h"
23
24 namespace OHOS::Ace::Framework {
25
Create(float values,float min,float max)26 void GaugeModelImpl::Create(float values, float min, float max)
27 {
28 auto progressChild = AceType::MakeRefPtr<ProgressComponent>(min, values, min, max, ProgressType::GAUGE);
29 progressChild->SetIndicatorFlag(true);
30 progressChild->SetInspectorTag("Gauge");
31 ViewStackProcessor::GetInstance()->ClaimElementId(progressChild);
32 ViewStackProcessor::GetInstance()->Push(progressChild);
33 RefPtr<ProgressTheme> progressTheme = JSGauge::GetTheme<ProgressTheme>();
34 progressChild->InitStyle(progressTheme);
35 }
36
SetValue(float value)37 void GaugeModelImpl::SetValue(float value)
38 {
39 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
40 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
41 if (!gaugeComponent) {
42 return;
43 }
44 gaugeComponent->SetValue(value);
45 }
46
SetStartAngle(float startAngle)47 void GaugeModelImpl::SetStartAngle(float startAngle)
48 {
49 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
50 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
51 if (!gaugeComponent) {
52 return;
53 }
54 gaugeComponent->GetTrack()->GetTrackInfo()->SetStartDegree(startAngle);
55 }
56
SetEndAngle(float endAngle)57 void GaugeModelImpl::SetEndAngle(float endAngle)
58 {
59 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
60 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
61 if (!gaugeComponent) {
62 return;
63 }
64 gaugeComponent->GetTrack()->GetTrackInfo()->SetSweepDegree(endAngle);
65 }
66
SetColors(const std::vector<Color> & colors,const std::vector<float> & values)67 void GaugeModelImpl::SetColors(const std::vector<Color>& colors, const std::vector<float>& values)
68 {
69 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
70 std::vector<double> values_old;
71 for (size_t i = 0; i < values.size(); ++i) {
72 values_old.push_back(values[i]);
73 }
74 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
75 if (!gaugeComponent) {
76 return;
77 }
78 gaugeComponent->SetSectionsStyle(colors, values_old);
79 }
80
SetStrokeWidth(const Dimension & strokeWidth)81 void GaugeModelImpl::SetStrokeWidth(const Dimension& strokeWidth)
82 {
83 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
84 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
85 if (!gaugeComponent) {
86 return;
87 }
88 gaugeComponent->SetTrackThickness(strokeWidth);
89 }
90
SetLabelMarkedText(const std::string labelTextString)91 void GaugeModelImpl::SetLabelMarkedText(const std::string labelTextString)
92 {
93 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
94 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
95 gaugeComponent->SetLabelMarkedText(labelTextString);
96 }
97
SetMarkedTextColor(const Color & color)98 void GaugeModelImpl::SetMarkedTextColor(const Color& color)
99 {
100 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
101 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
102 gaugeComponent->SetMarkedTextColor(color);
103 }
104
105 } // namespace OHOS::Ace::Framework