mcu_hi3321_watch/tjd/ui/app/setting/TjdUiAppSettingPage.cpp
2025-05-26 20:15:20 +08:00

820 lines
26 KiB
C++

#include "TjdUiAppSettingPage.h"
#include "TjdUiAppListModel.h"
#include "TjdUiAppSettingPresenter.h"
#include "TjdUiAppSettingView.h"
#include "TjdUiAppStartUpModel.h"
#include "TjdUiImageIds.h"
#include "TjdUiMultiLanguageExt.h"
#include "TjdUiSettingCenter.h"
#include "TjdUiUtils.h"
#include "common/image_cache_manager.h"
#include "graphic_service.h"
#include "sql_setting.h"
#include "sys_config.h"
#include "sys_typedef.h"
#include <TjdUiAppSettingModel.h>
#include <iomanip>
#include <sstream>
#define SETTING_LOADING_IMAGE_BIN_PATH TJD_IMAGE_PATH "img_loading.bin"
#define MENU_LIST_BIN_PATH TJD_IMAGE_PATH "img_menu_list.bin"
namespace TJD {
constexpr uint8_t REBOUND_SIZE = 200;
#ifndef WAIT_ANIMATOR_COUNT
#define WAIT_ANIMATOR_COUNT 11
#endif
static int16_t VerticalCenter(int16_t height, int16_t target) { return (target - height) / 2; }
static int16_t HorizontalCenter(int16_t width, int16_t target) { return (target - width) / 2; }
static void InitLabel(OHOS::UILabel &label, uint8_t size, int16_t y, const char *text)
{
label.SetFont(TJD_VECTOR_FONT_FILENAME, size);
label.SetText(text);
label.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT);
label.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
label.SetPosition(HorizontalCenter(label.GetWidth(), OHOS::HORIZONTAL_RESOLUTION), y);
}
static void InitLabel(OHOS::UILabel &label, uint8_t size, int16_t x, int16_t y, const char *text)
{
label.SetFont(TJD_VECTOR_FONT_FILENAME, size);
label.SetText(text);
label.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT);
label.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
label.SetPosition(x, y);
}
static void InitLabelHorCenter(OHOS::UILabel &label, uint8_t size, int16_t y, int16_t target, const char *text)
{
label.SetFont(TJD_VECTOR_FONT_FILENAME, size);
label.SetText(text);
label.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT);
label.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
label.SetPosition(HorizontalCenter(label.GetWidth(), target), y);
}
static void InitLabelVerCenter(OHOS::UILabel &label, uint8_t size, int16_t x, int16_t target, const char *text)
{
label.SetFont(TJD_VECTOR_FONT_FILENAME, size);
label.SetText(text);
label.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT);
label.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
label.SetPosition(x, VerticalCenter(label.GetHeight(), target));
}
static void InitLabelCenter(OHOS::UILabel &label, uint8_t size, int16_t pWidth, int16_t pHeight, const char *text)
{
label.SetFont(TJD_VECTOR_FONT_FILENAME, size);
label.SetText(text);
label.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT);
label.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
label.SetPosition(HorizontalCenter(label.GetWidth(), pWidth), VerticalCenter(label.GetHeight(), pHeight));
}
enum InputInfoIndex
{
INPUT_KEY_NORMAL = 0,
INPUT_KEY_PRESS,
INPUT_ENTER_NORMAL,
INPUT_ENTER_PRESS,
INPUT_CANCEL_NORMAL,
INPUT_CANCEL_PRESS,
INPUT_MAX
};
static OHOS::ImageInfo *imgInputInfo[INPUT_MAX];
static void InputImageInfo()
{
int resId[INPUT_MAX] = {IMG_SETTING_KEY_BJ, IMG_SETTING_KEY_PRESS_BJ, IMG_SETTING_OK,
IMG_SETTING_OK_PRESS, IMG_SETTING_CANCEL, IMG_SETTING_CANCEL_PRESS};
auto &imgManager = OHOS::ImageCacheManager::GetInstance();
for (int i = 0; i < INPUT_MAX; i++) {
if (imgInputInfo[i] == nullptr) {
imgInputInfo[i] = imgManager.LoadOneInMultiRes(resId[i], IMAGE_BIN_PATH);
}
}
}
static OHOS::ImageAnimatorInfo g_waitInfo_[WAIT_ANIMATOR_COUNT];
static void WaitAnimatorImageInfo()
{
const int waitAnimatorId[WAIT_ANIMATOR_COUNT] = {
IMG_LOADING_LOADING_01_06, IMG_LOADING_LOADING_02_06, IMG_LOADING_LOADING_03_06, IMG_LOADING_LOADING_04_06,
IMG_LOADING_LOADING_05_06, IMG_LOADING_LOADING_06_06, IMG_LOADING_LOADING_07_06, IMG_LOADING_LOADING_08_06,
IMG_LOADING_LOADING_09_06, IMG_LOADING_LOADING_10_06, IMG_LOADING_LOADING_11_06,
};
auto &imgManager = OHOS::ImageCacheManager::GetInstance();
for (int i = 0; i < WAIT_ANIMATOR_COUNT; i++) {
g_waitInfo_[i].imageInfo = imgManager.LoadOneInMultiRes(waitAnimatorId[i], SETTING_LOADING_IMAGE_BIN_PATH);
g_waitInfo_[i].pos = {178, 154};
g_waitInfo_[i].width = 121;
g_waitInfo_[i].height = 121;
g_waitInfo_[i].imageType = OHOS::IMG_SRC_IMAGE_INFO;
}
}
static void WaitAnimatorImageInfoUnload()
{
OHOS::ImageCacheManager::GetInstance().UnloadAllInMultiRes(SETTING_LOADING_IMAGE_BIN_PATH);
}
#pragma region 公共控件
#pragma region 确认
CommonEnterCancelViewBase::CommonEnterCancelViewBase(const char *title, const char *desc) { InitView(title, desc); }
void CommonEnterCancelViewBase::InitView(const char *title, const char *desc)
{
Resize(OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
InitLabelHorCenter(title_, 32, 17, 466, title);
InitLabel(desc_, 34, 151, desc);
btnCancel_.SetPosition(102, 326, 92, 92);
btnCancel_.SetTouchable(true);
btnCancel_.SetViewId("BtnCancel");
btnCancel_.SetOnClickListener(&enterCallback_);
btnOK_.SetPosition(270, 326, 92, 92);
btnOK_.SetTouchable(true);
btnOK_.SetViewId("BtnOK");
btnOK_.SetOnClickListener(&enterCallback_);
if (title != nullptr) {
Add(&title_);
}
if (desc != nullptr) {
Add(&desc_);
}
Add(&btnCancel_);
Add(&btnOK_);
}
void CommonEnterCancelViewBase::SetSrc(OHOS::ImageInfo *icon)
{
icon_.SetPosition(183, 64, 100, 100);
if (icon != nullptr) {
icon_.SetSrc(icon);
}
title_.SetFont(TJD_VECTOR_FONT_FILENAME, 34);
title_.SetPosition(HorizontalCenter(title_.GetWidth(), OHOS::HORIZONTAL_RESOLUTION), 182);
desc_.SetFont(TJD_VECTOR_FONT_FILENAME, 34);
desc_.SetPosition(HorizontalCenter(desc_.GetWidth(), OHOS::HORIZONTAL_RESOLUTION), 256);
}
void CommonEnterCancelViewBase::SetControlSrc(OHOS::ImageInfo *enter, OHOS::ImageInfo *cancel)
{
if (enter != nullptr) {
btnOK_.SetSrc(enter);
}
if (cancel != nullptr) {
btnCancel_.SetSrc(cancel);
}
}
void CommonEnterCancelViewBase::SetText(const char *title, const char *desc)
{
title_.SetText(title);
desc_.SetText(desc);
}
bool CommonEnterCancelViewBase::SettingItemEnterOnClick::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
{
std::string viewId = view.GetViewId();
if (viewId == "BtnOK") {
dynamic_cast<CommonEnterCancelViewBase *>(view.GetParent())->EnterCallback();
} else if (viewId == "BtnCancel") {
dynamic_cast<CommonEnterCancelViewBase *>(view.GetParent())->CancelCallback();
}
return false;
}
#pragma endregion
#pragma region 等待动画
CommonWaitAnimatorViewBase::CommonWaitAnimatorViewBase(const char *desc) { InitView(desc); }
CommonWaitAnimatorViewBase::~CommonWaitAnimatorViewBase()
{
RemoveAll();
if (waitTimer_ != nullptr) {
delete waitTimer_;
waitTimer_ = nullptr;
}
}
void CommonWaitAnimatorViewBase::InitView(const char *desc, bool isLoop)
{
SetPosition(0, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
/* 动画页面不允许滑动 */
SetIntercept(true);
animator_.SetPosition(150, 113, 166, 164);
InitLabel(desc_, 34, 287, desc);
waitTimer_ = new OHOS::GraphicTimer(3000, TimerCallback, this, isLoop);
Add(&animator_);
Add(&desc_);
}
void CommonWaitAnimatorViewBase::SetText(const char *desc) { desc_.SetText(desc); }
void CommonWaitAnimatorViewBase::Start()
{
WaitAnimatorImageInfo();
animator_.SetImageAnimatorSrc(g_waitInfo_, 11, 100);
animator_.Start();
waitTimer_->Start();
}
void CommonWaitAnimatorViewBase::Stop()
{
animator_.Stop();
waitTimer_->Stop();
WaitAnimatorImageInfoUnload();
}
void CommonWaitAnimatorViewBase::TimerCallback(void *arg)
{
GraphicService::GetInstance()->PostGraphicEvent(std::bind(&CompleteEventProc, arg));
}
void CommonWaitAnimatorViewBase::CompleteEventProc(void *arg)
{
CommonWaitAnimatorViewBase *self = static_cast<CommonWaitAnimatorViewBase *>(arg);
self->Stop();
self->CompleteCallback();
}
#pragma endregion
#pragma region 选择
CommonSwitchViewBase::CommonSwitchViewBase(const char *desc, int16_t width, int16_t height)
{
InitView(desc, width, height);
}
void CommonSwitchViewBase::InitView(const char *desc, int16_t width, int16_t height)
{
SetStyle(OHOS::STYLE_BACKGROUND_OPA, 0xff);
SetStyle(OHOS::STYLE_BORDER_RADIUS, height / 2);
SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff262626);
SetTouchable(true);
Resize(width, height);
InitLabelVerCenter(desc_, 34, 42, GetHeight(), desc);
switch_.SetPosition(width - 104, VerticalCenter(38, height), 72, 38);
switch_.SetTouchable(false);
Add(&desc_);
Add(&switch_);
}
void CommonSwitchViewBase::SetSrc(OHOS::ImageInfo *open, OHOS::ImageInfo *close)
{
if (open != nullptr && close != nullptr) {
switch_.SetImages(open, close);
}
}
bool CommonSwitchViewBase::OnClickEvent(const OHOS::ClickEvent &event)
{
auto state = GetSwitchState();
if (state == OHOS::UICheckBox::UICheckBoxState::SELECTED) {
state = OHOS::UICheckBox::UICheckBoxState::UNSELECTED;
} else if (state == OHOS::UICheckBox::UICheckBoxState::UNSELECTED) {
state = OHOS::UICheckBox::UICheckBoxState::SELECTED;
}
switch_.OnClickEvent(event);
SetSwitchState(state);
SwitchCallback(*this, state);
return UIView::OnClickEvent(event);
}
#pragma endregion
#pragma region 错误弹窗
CommonErrorWindowsViewBase::CommonErrorWindowsViewBase()
{
SetPosition(0, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
InitLabel(desc_, 34, 217, "错误弹窗");
errorTimer_ = new OHOS::GraphicTimer(2000, CompleteEvent, this);
SetVisible(false);
Add(&desc_);
}
CommonErrorWindowsViewBase::~CommonErrorWindowsViewBase()
{
if (errorTimer_ != nullptr) {
delete errorTimer_;
errorTimer_ = nullptr;
}
}
void CommonErrorWindowsViewBase::SetPeriod(int32_t periodMs)
{
if (errorTimer_ != nullptr)
errorTimer_->SetPeriod(periodMs);
}
void CommonErrorWindowsViewBase::ShowMessage(OHOS::UIView *view, const char *desc)
{
retView_ = view;
view->SetVisible(false);
desc_.SetText(desc);
desc_.SetPosition(HorizontalCenter(desc_.GetWidth(), OHOS::HORIZONTAL_RESOLUTION),
VerticalCenter(desc_.GetHeight(), OHOS::VERTICAL_RESOLUTION));
SetVisible(true);
if (errorTimer_ != nullptr) {
errorTimer_->Start();
}
}
void CommonErrorWindowsViewBase::HideErrorView()
{
SetVisible(false);
if (retView_ != nullptr) {
if (!retView_->IsVisible()) {
retView_->SetVisible(true);
}
}
}
void CommonErrorWindowsViewBase::CompleteEvent(void *arg)
{
GraphicService::GetInstance()->PostGraphicEvent(std::bind([arg]() {
CommonErrorWindowsViewBase *self = static_cast<CommonErrorWindowsViewBase *>(arg);
self->HideErrorView();
}));
}
#pragma endregion
#pragma region 输入
CommonInputViewBase::CommonInputViewBase(InputViewType type) { InitView(type); }
CommonInputViewBase::~CommonInputViewBase()
{
RemoveAll();
editView_.RemoveAll();
}
void CommonInputViewBase::InitView(InputViewType type)
{
InputImageInfo();
Resize(OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
type_ = type;
switch (type) {
case InputViewType::INPUT_TYPE_PASSWORD:
InitPasswordView();
break;
default:
break;
}
}
void CommonInputViewBase::ResetPassword()
{
password_.clear();
for (int i = 0; i < 4; i++) {
passwrodEdit_[i].SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff575757);
passwrodEdit_[i].Invalidate();
}
currentPasswordIndex_ = 0;
hideText_.SetVisible(true);
editView_.SetVisible(false);
}
void CommonInputViewBase::SetHideText(const char *text)
{
hideText_.SetText(text);
hideText_.SetX(HorizontalCenter(hideText_.GetWidth(), OHOS::HORIZONTAL_RESOLUTION));
hideText_.Invalidate();
}
void CommonInputViewBase::Enter() { InputOkCallBack(password_.c_str()); }
void CommonInputViewBase::Push(char key)
{
if (type_ == InputViewType::INPUT_TYPE_PASSWORD) {
hideText_.SetVisible(false);
editView_.SetVisible(true);
if (currentPasswordIndex_ < 4) {
passwrodEdit_[currentPasswordIndex_].SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xffffffff);
passwrodEdit_[currentPasswordIndex_].Invalidate();
++currentPasswordIndex_;
password_.push_back(key);
}
if (password_.size() == 4) {
InputOkCallBack(password_.c_str());
}
}
}
void CommonInputViewBase::Delete()
{
if (type_ == InputViewType::INPUT_TYPE_PASSWORD) {
if (currentPasswordIndex_ > 0) {
--currentPasswordIndex_;
passwrodEdit_[currentPasswordIndex_].SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff575757);
passwrodEdit_[currentPasswordIndex_].Invalidate();
password_.pop_back();
}
if (password_.size() == 0) {
hideText_.SetVisible(true);
editView_.SetVisible(false);
CancelCallBack();
}
}
}
void CommonInputViewBase::InitPasswordView()
{
password_.clear();
password_.resize(4);
InitLabel(hideText_, 38, 61, "请在次输入密码");
hideText_.SetStyle(OHOS::STYLE_TEXT_OPA, 0xff * 0.6);
hideText_.SetText("请输入密码");
hideText_.SetX(HorizontalCenter(hideText_.GetWidth(), OHOS::HORIZONTAL_RESOLUTION));
int16_t editCircleWidth = 32;
editView_.SetPosition(110, 70, 248, 32);
editView_.SetVisible(false);
int16_t editStartXInterval = 40;
for (int i = 0; i < 4; i++) {
passwrodEdit_[i].SetPosition((editCircleWidth + editStartXInterval) * i, 0, editCircleWidth, editCircleWidth);
passwrodEdit_[i].SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff575757);
passwrodEdit_[i].SetStyle(OHOS::STYLE_BACKGROUND_OPA, 0xff);
passwrodEdit_[i].SetStyle(OHOS::STYLE_BORDER_RADIUS, editCircleWidth / 2);
editView_.Add(&passwrodEdit_[i]);
}
currentPasswordIndex_ = 0;
Add(&hideText_);
Add(&editView_);
int16_t startX = 35;
int16_t startY = 129;
int16_t xInterval = 6;
int16_t yInterval = 9;
int16_t width = 128;
int16_t height = 66;
for (int i = 0; i < 12; i++) {
key_[i].SetViewId(keyId_[i].c_str());
key_[i].SetOnClickListener(&listener_);
key_[i].SetStyle(OHOS::STYLE_BACKGROUND_OPA, 0);
key_[i].SetStyleForState(OHOS::STYLE_BACKGROUND_OPA, 0, OHOS::UIButton::ButtonState::PRESSED);
if (i == 9 || i == 11) {
continue;
}
key_[i].SetText(keyId_[i].c_str());
key_[i].SetFont(TJD_VECTOR_FONT_FILENAME, 50);
key_[i].SetPosition(startX + (width + xInterval) * (i % 3), startY + (height + yInterval) * (i / 3), width,
height);
if (imgInputInfo[InputInfoIndex::INPUT_KEY_NORMAL] != nullptr &&
imgInputInfo[InputInfoIndex::INPUT_KEY_PRESS] != nullptr) {
key_[i].SetImageSrc(imgInputInfo[InputInfoIndex::INPUT_KEY_NORMAL],
imgInputInfo[InputInfoIndex::INPUT_KEY_PRESS]);
}
Add(&key_[i]);
}
key_[9].SetPosition(82, 370, 80, 66);
key_[11].SetPosition(317, 370, 128, 66);
if (imgInputInfo[InputInfoIndex::INPUT_ENTER_NORMAL] != nullptr &&
imgInputInfo[InputInfoIndex::INPUT_ENTER_PRESS] != nullptr) {
key_[9].SetImageSrc(imgInputInfo[InputInfoIndex::INPUT_ENTER_NORMAL],
imgInputInfo[InputInfoIndex::INPUT_ENTER_PRESS]);
}
if (imgInputInfo[InputInfoIndex::INPUT_CANCEL_NORMAL] != nullptr &&
imgInputInfo[InputInfoIndex::INPUT_CANCEL_PRESS] != nullptr) {
key_[11].SetImageSrc(imgInputInfo[InputInfoIndex::INPUT_CANCEL_NORMAL],
imgInputInfo[InputInfoIndex::INPUT_CANCEL_PRESS]);
}
Add(&key_[9]);
Add(&key_[11]);
}
bool CommonInputViewBase::KeyBoardListener::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
{
std::string viewId = view.GetViewId();
if (viewId.empty()) {
return false;
}
if (viewId == "10") {
dynamic_cast<CommonInputViewBase *>(view.GetParent())->Enter();
} else if (viewId == "12") {
dynamic_cast<CommonInputViewBase *>(view.GetParent())->Delete();
} else {
dynamic_cast<CommonInputViewBase *>(view.GetParent())->Push(viewId[0]);
}
return false;
}
#pragma endregion
#pragma region 大型按钮
CommonMultiButtonViewBase::CommonMultiButtonViewBase(const char *title, const char *value, bool isIcon, int16_t width,
int16_t height)
{
InitView(title, value, isIcon, width, height);
}
CommonMultiButtonViewBase::~CommonMultiButtonViewBase() { RemoveAll(); }
void CommonMultiButtonViewBase::InitView(const char *title, const char *value, bool isIcon, int16_t width,
int16_t height)
{
Resize(width, height);
SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff262626);
SetStyle(OHOS::STYLE_BORDER_RADIUS, 60);
SetTouchable(true);
InitLabel(title_, 34, 42, 28, title);
InitLabel(value_, 28, 42, 71, value);
value_.SetStyle(OHOS::STYLE_TEXT_OPA, 0xff * 0.4);
hasValue_ = value != nullptr;
if (value == nullptr) {
title_.SetPosition(42, VerticalCenter(title_.GetHeight(), GetHeight()));
}
if (isIcon) {
icon_.SetPosition(400, 37, 15, 26);
Add(&icon_);
}
Add(&title_);
Add(&value_);
// Add(&icon_);
}
void CommonMultiButtonViewBase::SetSrc(OHOS::ImageInfo *more)
{
if (more != nullptr) {
icon_.SetSrc(more);
}
}
void CommonMultiButtonViewBase::Resize(int16_t width, int16_t height)
{
SetHeight(height);
SetWidth(width);
if (hasValue_ == false) {
title_.SetPosition(42, VerticalCenter(title_.GetHeight(), height));
}
}
void CommonMultiButtonViewBase::SetPosition(int16_t x, int16_t y, int16_t width, int16_t height)
{
SetX(x);
SetY(y);
Resize(width, height);
}
bool CommonMultiButtonViewBase::OnClickEvent(const OHOS::ClickEvent &event)
{
EnterCallback(*this, event);
return UIView::OnClickEvent(event);
}
#pragma endregion
#pragma region 单确认按钮框架
void CommonEnterViewBase::InitView(const char *title)
{
Resize(OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
InitLabelHorCenter(title_, 32, 17, 466, title);
enterView_.SetPosition(187, 352, 92, 92);
enterView_.SetStyle(OHOS::STYLE_BACKGROUND_OPA, 0x0);
enterView_.SetTouchable(true);
enterView_.SetOnClickListener(&listener_);
auto &imgManager = OHOS::ImageCacheManager::GetInstance();
auto enterRes = imgManager.LoadOneInMultiRes(IMG_SETTING_CONFIRM_BAT, IMAGE_BIN_PATH);
enterIcon_.SetPosition(0, 0, 92, 92);
enterIcon_.SetSrc(enterRes);
enterView_.Add(&enterIcon_);
Add(&title_);
Add(&enterView_);
}
void CommonEnterViewBase::SetSrc(OHOS::ImageInfo *enter)
{
if (enter != nullptr) {
enterIcon_.SetSrc(enter);
}
}
void CommonEnterViewBase::SetFullSrc(OHOS::ImageInfo *enter)
{
if (enter != nullptr) {
enterIcon_.SetPosition(0, 0, 362, 86);
enterIcon_.SetSrc(enter);
}
}
void CommonEnterViewBase::SetEnterColor(uint32_t enterColor)
{
enterView_.SetStyle(OHOS::STYLE_BACKGROUND_COLOR, enterColor);
}
bool CommonEnterViewBase::EnterOnClickListener::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
{
dynamic_cast<CommonEnterViewBase *>(view.GetParent())->EnterCallback();
return false;
}
#pragma endregion
#pragma region 梯度条
CommonGradingBarViewBase::~CommonGradingBarViewBase() { RemoveAll(); }
void CommonGradingBarViewBase::InitView(OHOS::ImageInfo *leftInfo, OHOS::ImageInfo *rightInfo)
{
Resize(420, 62);
SetStyle(OHOS::STYLE_BACKGROUND_OPA, 0xff);
SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff262626);
SetStyle(OHOS::STYLE_BORDER_RADIUS, 31);
SetTouchable(true);
leftIcon_.SetPosition(21, 11, 40, 40);
leftIcon_.SetViewId("LeftIcon");
if (leftInfo != nullptr) {
leftIcon_.SetSrc(leftInfo);
}
rightIcon_.SetPosition(359, 11, 40, 40);
rightIcon_.SetViewId("RightIcon");
if (rightInfo != nullptr) {
rightIcon_.SetSrc(rightInfo);
}
const int startX = 64;
const int startY = 25;
const int width = 50;
const int height = 12;
const int interval = 8;
for (int i = 0; i < MAX_LEVEL; i++) {
level_[i].SetPosition(startX + (width + interval) * i, startY, width, height);
level_[i].SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff575757);
level_[i].SetStyle(OHOS::STYLE_BORDER_RADIUS, 6);
Add(&level_[i]);
}
Add(&leftIcon_);
Add(&rightIcon_);
}
bool CommonGradingBarViewBase::OnClickEvent(const OHOS::ClickEvent &event)
{
auto touchX = event.GetCurrentPos().x; // 获取点击的x坐标
auto x = GetX(); // 获取当前控件的x坐标
auto width = GetWidth(); // 获取当前控件的宽度
// 计算左边1/3和右边1/3的边界
auto leftThird = x + width / 3;
auto rightThird = x + 2 * width / 3;
if (touchX < leftThird) {
LeftOnClickEvent();
} else if (touchX > rightThird) {
RightOnClickEvent();
}
return UIView::OnClickEvent(event);
}
void CommonGradingBarViewBase::ChangeValue(uint8_t value)
{
for (int i = 0; i < MAX_LEVEL; i++) {
if (i < value) {
level_[i].SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff00DEA2);
} else {
level_[i].SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff575757);
}
level_[i].Invalidate();
}
value_ = value;
}
void CommonGradingBarViewBase::LeftOnClickEvent()
{
if (value_ > 0) {
ChangeValue(--value_);
}
OnChangeCallback(*this, value_);
}
void CommonGradingBarViewBase::RightOnClickEvent()
{
if (value_ < MAX_LEVEL) {
ChangeValue(++value_);
}
OnChangeCallback(*this, value_);
}
#pragma endregion
#pragma region 单选按钮
CommonRadioButtonViewBase::~CommonRadioButtonViewBase() { RemoveAll(); }
void CommonRadioButtonViewBase::InitView(const char *name, const char *desc, int16_t width, int16_t height)
{
Resize(width, height);
SetStyle(OHOS::STYLE_BACKGROUND_OPA, 0xff);
SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff262626);
SetStyle(OHOS::STYLE_BORDER_RADIUS, 55);
SetTouchable(true);
radio_.SetPosition(width - 27 - 40, VerticalCenter(40, height), 40, 40);
radio_.SetTouchable(false);
radio_.SetName(name);
InitLabelVerCenter(desc_, 34, 42, height, desc);
SetTouchable(true);
Add(&radio_);
Add(&desc_);
}
void CommonRadioButtonViewBase::SetSrc(OHOS::ImageInfo *icon, int16_t x, int16_t y, int16_t width, int16_t height)
{
icon_.SetPosition(x, y, width, height);
if (icon != nullptr) {
InitLabelCenter(desc_, 34, GetWidth(), GetHeight(), nullptr);
icon_.SetSrc(icon);
}
Add(&icon_);
}
void CommonRadioButtonViewBase::SetIconScale(const OHOS::Vector2<float> scale, const OHOS::Vector2<float> pivot)
{
scale_.x_ = scale.x_;
scale_.y_ = scale.y_;
pivot_.x_ = pivot.x_;
pivot_.y_ = pivot.y_;
icon_.Scale(scale_, pivot_);
}
void CommonRadioButtonViewBase::SetControlSrc(OHOS::ImageInfo *select, OHOS::ImageInfo *unselect)
{
if (select != nullptr && unselect != nullptr) {
// radio_.SetPosition(382, 30, 40, 40);
radio_.SetImages(select, unselect);
}
}
void CommonRadioButtonViewBase::FindRadioButtonAndChangeState(OHOS::UIView *view)
{
if (view == nullptr) {
return;
}
int count = 0;
UIView *childView = static_cast<UIViewGroup *>(view)->GetChildrenHead();
while (childView != nullptr) {
if (childView->GetViewType() == (OHOS::UIViewType)COMMON_RADIO_BUTTON) {
auto &radioButton = static_cast<CommonRadioButtonViewBase *>(childView)->GetRadioButton();
// 如果是单选按钮,且不是当前按钮
if ((radioButton.GetName() != nullptr) && (strcmp(radioButton.GetName(), radio_.GetName()) != 0)) {
radioButton.SetState(OHOS::UICheckBox::UICheckBoxState::UNSELECTED, true);
}
}
childView = childView->GetNextSibling();
}
}
bool CommonRadioButtonViewBase::OnClickEvent(const OHOS::ClickEvent &event)
{
if (isFindRadioButton_ && GetParent() != nullptr) {
FindRadioButtonAndChangeState(GetParent());
}
if (isFindRadioButton_) {
if (radio_.GetState() != OHOS::UICheckBox::UICheckBoxState::SELECTED) {
radio_.SetState(OHOS::UICheckBox::UICheckBoxState::SELECTED, true);
OnChangeCallback(*this, radio_.GetState());
}
} else {
ToggleRadioButton();
OnChangeCallback(*this, radio_.GetState());
}
return UIView::OnClickEvent(event);
}
#pragma endregion
#pragma endregion 公共控件
} // namespace TJD