From 8def37b5d1361432e262201784e66e7115dea65b Mon Sep 17 00:00:00 2001 From: maomao <2144439972@qq.com> Date: Mon, 21 Apr 2025 17:13:47 +0800 Subject: [PATCH] =?utf8?q?=E3=80=90=E8=AE=A1=E7=AE=97=E5=99=A8=E3=80=91UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../calculator/include/CalculatorModel.h | 44 ++ .../calculator/include/CalculatorPresenter.h | 42 ++ .../calculator/include/CalculatorView.h | 79 +++ .../calculator/src/CalculatorModel.cpp | 152 ++++ .../calculator/src/CalculatorPresenter.cpp | 177 +++++ .../calculator/src/CalculatorView.cpp | 669 ++++++++++++++++++ 6 files changed, 1163 insertions(+) create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorModel.h create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorPresenter.h create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorView.h create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorModel.cpp create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorPresenter.cpp create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorView.cpp diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorModel.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorModel.h new file mode 100644 index 000000000..9412c3319 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorModel.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) CompanyNameMagicTag 2021-2021. All rights reserved. + * Description: CalculatorModel.h + * Author: + * Create: 2021-10-12 + */ +#ifndef CALCULATOR_MODEL_H +#define CALCULATOR_MODEL_H + +#include "los_mux.h" +#include "string" +#include "PhoneService.h" +#include "AppViewIDs.h" +#include "components/ui_label_ext.h" +#include"ui_resource_string.h" +#include "message.h" +#include "msg_center.h" +#include "stack" + +namespace OHOS { + + +class CalculatorModel { +public: + static CalculatorModel *GetInstance(void); + + + void number_push_stack(double ); + void symbol_push_stack(char symbol); + double calculate_stack(); + uint8_t get_stcak_size(); + void symbol_stack_top_uddata(char data); + void clean_stack(); + void symbol_stack_pop(void); +private: + CalculatorModel(); + CalculatorModel(const CalculatorModel &); + virtual ~CalculatorModel(); + std::stack numberStack; + std::stack symbolStack; + +}; +} +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorPresenter.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorPresenter.h new file mode 100644 index 000000000..8550f31dc --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorPresenter.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) CompanyNameMagicTag 2021-2021. All rights reserved. + * Description: CalculatorPresenter.h + * Author: + * Create: 2021-10-12 + */ + +#ifndef CALCULATOR_PRESENTER_H +#define CALCULATOR_PRESENTER_H + +#include "Presenter.h" +#include "ohos_types.h" +#include "CallerLogModel.h" +//#include "BaseView.h" +#include "cmsis_os.h" + +namespace OHOS { +class CalculatorView; +class CalculatorPresenter : public Presenter, + public UIView::OnClickListener, + public UIView::OnDragListener, + public RootView::OnKeyActListener +{ +public: + CalculatorPresenter(); + ~CalculatorPresenter() override; + + void OnStart() override; + + + static CalculatorPresenter *GetInstance(void); + bool OnClick(UIView &view, const ClickEvent &event) override; + bool OnKeyAct(UIView &view, const KeyEvent &event) override; + bool OnDragEnd(UIView &view, const DragEvent &event) override; + + +private: + +}; +} + +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorView.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorView.h new file mode 100644 index 000000000..f892a1001 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/include/CalculatorView.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) CompanyNameMagicTag 2021-2021. All rights reserved. + * Description: CalculatorView.h + * Author: + * Create: 2021-10-12 + */ + +#ifndef CALCULATOR_VIEW_H +#define CALCULATOR_VIEW_H + +#include "View.h" +#include "CalculatorPresenter.h" +#include "CalculatorView.h" +//#include "BaseView.h" +#include "components/ui_view_group.h" +#include "components/ui_image_animator.h" +#include "components/ui_scroll_view.h" +#include "components/ui_label.h" +#include "components/ui_label_ext.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/abstract_adapter.h" +#include "components/ui_image_view.h" + +#include "PhoneService.h" +#include "vector.h" +#include "ChangeSliceListener.h" +#include "cmsis_os2.h" + +namespace OHOS { + +class CalculatorView : + public UIViewGroup, +public View { +public: + CalculatorView(); + ~CalculatorView(); + + + void OnStart() override; + static CalculatorView *GetInstance(); + void number_text_updata_buf(const char *buf); + void symbol_text_updata_buf(const char *buf); + void symbol_text_updata_single_buf(const char *buf); + void symbol_text_updata_calculate_buf(const char *buf); + + void start_call_roll_timer(); + void start_call_roll_timer_move(); + + void show_hint_label(bool data); + + void show_long_label(); + bool string_have_dot(std::string buf); + + osTimerId_t call_roll_timer=NULL; + osTimerId_t call_roll_timer_move=NULL; + +private: + uint8_t number_falg=0; + bool button_init(); + bool Label_init(); + UILabelButton *Button[20]={nullptr}; + + UIImageView *image1_{nullptr}; + + UIViewGroup *group{nullptr}; + UILabel *number{nullptr}; + UILabelExt *hint{nullptr}; + + std::string number_string=" "; + + UIView::OnClickListener* clickLeftListener{nullptr}; + UIView:: OnDragListener* dragListener{nullptr}; + + +}; +} + +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorModel.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorModel.cpp new file mode 100644 index 000000000..c2a67bd91 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorModel.cpp @@ -0,0 +1,152 @@ +/* + * Copyright (c) CompanyNameMagicTag 2021-2021. All rights reserved. + * Description: CalculatorModel.cpp + * Author: + * Create: 2021-10-12 + */ +#include "DialingScreenView.h" + +#include "service_id_define.h" +#include "cmsis_os.h" +#include "samgr_lite.h" +#include "calculator/include/CalculatorModel.h" + +#ifdef __cplusplus +extern "C" { +#endif + +using namespace OHOS; + + +static bool g_isWatchCallView = false; + +CalculatorModel::CalculatorModel() +{ + +} + +CalculatorModel::~CalculatorModel() +{ + +} + + +void CalculatorModel:: number_push_stack(double data) +{ + // printf("%lf\r\n",data); + if(!symbolStack.empty()) + { + if(symbolStack.top()=='-') + { + char symbol_= symbolStack.top(); + symbolStack.pop(); + data=-data; + symbolStack.push('+'); + + } + } + numberStack.push(data); +} + + +void CalculatorModel:: symbol_stack_top_uddata(char data) +{ + // printf("%c\r\n",data); + if(!symbolStack.empty()) + { + symbolStack.pop(); + symbolStack.push(data); + } +} + + +void CalculatorModel:: symbol_stack_pop(void) +{ + if(!symbolStack.empty()) + { + symbolStack.pop(); + } +} + +uint8_t CalculatorModel::get_stcak_size() +{ + return numberStack.size(); +} + +void CalculatorModel:: symbol_push_stack(char symbol) +{ + // printf("%c\r\n",symbol); + + if(numberStack.empty()) + { + return ; + } + symbolStack.push(symbol); + return ; + +} + +void CalculatorModel:: clean_stack() +{ + while (!numberStack.empty()) + { + numberStack.pop(); + } + + while (!symbolStack.empty()) + { + symbolStack.pop(); + } +} + +double CalculatorModel:: calculate_stack() +{ + double sum=0; + if(numberStack.empty()) + return sum; + double number_1= numberStack.top(); + numberStack.pop(); + sum=number_1; + while (!numberStack.empty()) + { + double number_= numberStack.top(); + numberStack.pop(); + + if(symbolStack.empty()) + break; + char symbol= symbolStack.top(); + if(symbol=='+') + { + sum+=number_; + } + if(symbol=='x') + { + sum*=number_; + } + if(symbol=='-') + { + sum=-number_; + } + if(symbol=='/') + { + if(sum==0) + { + return sum; + } + sum=number_/sum; + } + // printf("sum:%lf\r\n",sum); + } + return sum; +} + + +CalculatorModel *CalculatorModel::GetInstance(void) +{ + static CalculatorModel callLogModel; + return &callLogModel; +} + +#ifdef __cplusplus +} +#endif diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorPresenter.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorPresenter.cpp new file mode 100644 index 000000000..e2162996b --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorPresenter.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) CompanyNameMagicTag 2021-2021. All rights reserved. + * Description: CalculatorPresenter.cpp + * Author: + * Create: 2021-10-12 + */ + +#include "calculator/include/CalculatorPresenter.h" +#include "calculator/include/CalculatorView.h" +#include "gfx_utils/mem_check.h" +#include "common/key_code.h" +#include "custom_utils.h" +#include "NativeRegisterManager.h" +#include "uicomponents/UIComponents.h" +namespace OHOS { + + REGIST_MENU_PRO( VIEW_CALCULATOR, CalculatorView, CalculatorPresenter, + PNG_APPLIST_INFORMATION_IMAGE_BIG, PNG_APPLIST_INFORMATION_IMAGE, PNG_APPLIST_INFORMATION_IMAGE, "计算器", STR_CALCULATOR); + +static CalculatorPresenter *g_pMainView = nullptr; +CalculatorPresenter::CalculatorPresenter() +{ + g_pMainView = this; +#ifdef MEM_CHECK + MemCheck::GetInstance()->EnableLeakCheck(true); +#endif +} + +CalculatorPresenter::~CalculatorPresenter() +{ + g_pMainView = nullptr; +#ifdef MEM_CHECK + MemCheck::GetInstance()->EnableLeakCheck(false); +#endif +} + +CalculatorPresenter *CalculatorPresenter::GetInstance(void) +{ + return g_pMainView; +} + +void CalculatorPresenter::OnStart() +{ + Presenter::OnStart(); +} + + + + +bool CalculatorPresenter::OnClick(UIView &view, const ClickEvent &event) +{ + UNUSED(event); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OnClick %s",view.GetViewId()); + if (strcmp(view.GetViewId(), "0") == 0) { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "1") == 0) + { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "2") == 0) + { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "3") == 0) + { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "4") == 0) + { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "5") == 0) + { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + } + + else if (strcmp(view.GetViewId(), "6") == 0) + { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "7") == 0) + { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + }else if (strcmp(view.GetViewId(), "8") == 0) + { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "9") == 0) + { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), ".") == 0) + { + CalculatorView::GetInstance()->number_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "÷") == 0) + { + CalculatorView::GetInstance()->symbol_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "x") == 0) + { + CalculatorView::GetInstance()->symbol_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "+") == 0) + { + CalculatorView::GetInstance()->symbol_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "-") == 0) + { + CalculatorView::GetInstance()->symbol_text_updata_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "C") == 0) + { + CalculatorView::GetInstance()->symbol_text_updata_single_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "%") == 0) + { + CalculatorView::GetInstance()->symbol_text_updata_single_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(), "=") == 0) + { + CalculatorView::GetInstance()->symbol_text_updata_calculate_buf(view.GetViewId()); + return true; + } + else if (strcmp(view.GetViewId(),"D") == 0) + { + CalculatorView::GetInstance()->symbol_text_updata_single_buf(view.GetViewId()); + return true; + } + + return false; +} + +bool CalculatorPresenter::OnDragEnd(OHOS::UIView& view, const OHOS::DragEvent& event) +{ + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainCallingPresenter OnDragEnd"); + if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT) + { + if(event.GetPreLastPoint().x-event.GetStartPoint().x>150) + NativeAbility::GetInstance().Back(TransitionType::TRANSITION_BACK_TO_HEXAGONS); + } + return true; +} + +bool CalculatorPresenter::OnKeyAct(UIView &view, const KeyEvent &event) +{ + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AlipayPresenter::OnKeyAct"); + uint16 keyID = event.GetKeyId(); + uint16_t keyState = event.GetState(); + // 如果是power键就返回上一级 + if (keyID == static_cast(ZliteKeyCode::ZLITE_KEY_POWER) && keyState == 1 && uapi_get_screen_state() == 0) + { + uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId(); + NativeAbility::GetInstance().Back(TransitionType::TRANSITION_BACK_TO_HEXAGONS); + } + return true; + +} +} \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorView.cpp new file mode 100644 index 000000000..d032f7bb2 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/calculator/src/CalculatorView.cpp @@ -0,0 +1,669 @@ +/* + * Copyright (c) CompanyNameMagicTag 2021-2021. All rights reserved. + * Description: CalculatorView.cpp + * Author: + * Create: 2021-10-12 + */ + +#include "calculator/include/CalculatorView.h" +#include "calculator/include/CalculatorPresenter.h" +#include "calculator/include/CalculatorModel.h" +#include "wearable_log.h" +#include +#include +#include + +namespace OHOS { + +static CalculatorView *g_pMainView = nullptr; +CalculatorView::CalculatorView() +{ + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "::CalculatorView::CalculatorView "); + g_pMainView = this; +} + +CalculatorView::~CalculatorView() +{ + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "::CalculatorView::~CalculatorView "); + RootView::GetInstance()->ClearOnKeyActListener(); + g_pMainView = nullptr; + group->RemoveAll(); + if(number) + { + delete number; + number =nullptr; + } + for(int i=0;i<20;i++) + { + if(Button[i]) + { + delete Button[i]; + Button[i] =nullptr; + } + } + if(image1_) + { + delete image1_; + image1_=nullptr; + } + if(hint) + { + delete hint; + hint=nullptr; + } + if(group) + { + delete group; + group =nullptr; + } + + if(call_roll_timer) + { + osTimerStop(call_roll_timer); + osTimerDelete(call_roll_timer); + call_roll_timer = NULL; + } + + if(call_roll_timer_move) + { + osTimerStop(call_roll_timer_move); + osTimerDelete(call_roll_timer_move); + call_roll_timer_move = NULL; + } + +} +static int8_t roll_falg=0;//0.1s一次变换 + +static int8_t roll_falg_2=0;//0.1s一次变换 + + static void label_Timer_CallBack(int data) +{ + roll_falg--; + if(roll_falg<=0) + { + if(CalculatorView::GetInstance()) + { + GraphicService::GetInstance()->PostGraphicEvent(std::bind(&CalculatorView::show_hint_label, CalculatorView::GetInstance(),false)); + } + + + if(g_pMainView->call_roll_timer) + { + osTimerStop(g_pMainView->call_roll_timer); + osTimerDelete(g_pMainView->call_roll_timer); + g_pMainView->call_roll_timer = NULL; + } + } +} + +static void label_Timer_CallBack_move(int data) +{ + roll_falg_2--; + if(roll_falg_2<=0) + { + if(CalculatorView::GetInstance()) + { + GraphicService::GetInstance()->PostGraphicEvent(std::bind(&CalculatorView::show_long_label, CalculatorView::GetInstance())); + } + + if(g_pMainView->call_roll_timer_move) + { + osTimerStop(g_pMainView->call_roll_timer_move); + osTimerDelete(g_pMainView->call_roll_timer_move); + g_pMainView->call_roll_timer_move = NULL; + } + } +} + + + +CalculatorView *CalculatorView::GetInstance() +{ + return g_pMainView; +} + + +void CalculatorView::show_hint_label(bool data) +{ + hint->SetVisible(data); + Invalidate(); +} + +void CalculatorView::show_long_label(void) +{ + number->SetText(number_string.c_str()); + number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER); + + Invalidate(); +} + +void CalculatorView::start_call_roll_timer() +{ + uint32_t tick; + osStatus_t retTimer; + if(call_roll_timer) + { + roll_falg=10; + } + else + { + roll_falg=10; + + call_roll_timer = osTimerNew((osTimerFunc_t)label_Timer_CallBack, osTimerPeriodic, NULL, NULL); + tick =100;// osMs2Tick(1000); + retTimer = osTimerStart(call_roll_timer, tick); + if (retTimer != osOK && call_roll_timer != NULL) { + osTimerDelete(call_roll_timer); + call_roll_timer = NULL; + return ; + } + } +} + + +void CalculatorView::start_call_roll_timer_move() +{ + uint32_t tick; + osStatus_t retTimer; + if(call_roll_timer_move) + { + roll_falg_2=15; + } + else + { + roll_falg_2=15; + + call_roll_timer_move = osTimerNew((osTimerFunc_t)label_Timer_CallBack_move, osTimerPeriodic, NULL, NULL); + tick =100;// osMs2Tick(1000); + retTimer = osTimerStart(call_roll_timer_move, tick); + if (retTimer != osOK && call_roll_timer_move != NULL) { + osTimerDelete(call_roll_timer_move); + call_roll_timer_move = NULL; + return ; + } + } +} + +bool CalculatorView::string_have_dot(std::string buf) +{ + for(int i=0;i15) + { + //超出长度限制 + start_call_roll_timer(); + show_hint_label(true); + return; + } + + number_string=number_string+buf; + + // if(number_string.length()>12) + // { + // number->SetText(number_string.c_str()+number_string.length()-12); + // number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER); + // number_falg=1; + // start_call_roll_timer_move(); + // Invalidate(); + // return; + // } + + number->SetText(number_string.c_str()); + number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER); + + number_falg=1; + + Invalidate(); + return; +} + +void CalculatorView::symbol_text_updata_buf(const char *buf) +{ + + if(g_pMainView->call_roll_timer_move) + { + osTimerStop(g_pMainView->call_roll_timer_move); + osTimerDelete(g_pMainView->call_roll_timer_move); + g_pMainView->call_roll_timer_move = NULL; + } + + char new_buf[64]={0}; + + std::string text_buf=number_string; + if(number_falg==0) + { + if(strcmp(buf,"÷")==0) + CalculatorModel::GetInstance()->symbol_stack_top_uddata('/'); + else + CalculatorModel::GetInstance()->symbol_stack_top_uddata(buf[0]); + + number_string=new_buf; + number->SetText(buf); + number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER); + + Invalidate(); + return; + } + double number_float=strtod(text_buf.c_str(),NULL); + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%lf %s",number_float,text_buf.c_str()); + + char new_buf_[64]={0}; + sprintf_s(new_buf_,64,"%.010lf",number_float); + number_float=strtod(new_buf_,NULL); + + CalculatorModel::GetInstance()->number_push_stack(number_float); + + if( CalculatorModel::GetInstance()->get_stcak_size()>=2) + { + number_float =CalculatorModel::GetInstance()->calculate_stack(); + CalculatorModel::GetInstance()->number_push_stack(number_float); + Invalidate(); + + } + + sprintf_s(new_buf,64,"%s",buf); + + number_string=new_buf; + number->SetText(new_buf); + number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER); + + Invalidate(); + + if(strcmp(buf,"÷")==0) + CalculatorModel::GetInstance()->symbol_push_stack('/'); + else + CalculatorModel::GetInstance()->symbol_push_stack(buf[0]); + + number_falg=0; + + return; +} + +void CalculatorView::symbol_text_updata_single_buf(const char *buf) +{ + if(g_pMainView->call_roll_timer_move) + { + osTimerStop(g_pMainView->call_roll_timer_move); + osTimerDelete(g_pMainView->call_roll_timer_move); + g_pMainView->call_roll_timer_move = NULL; + } + + char new_buf[64]={0}; + + std::string text_buf=number_string; + + double number_float=strtod(text_buf.c_str(),NULL); + if(buf[0]=='C') + { + CalculatorModel::GetInstance()->clean_stack(); + number->SetText(""); + number_string=""; + Invalidate(); + } + else if (buf[0]=='%') + { + number_float=0.01*number_float; + + double number_float_=number_float; + int decimal_places = 0; + double frac; + + while (modf(number_float_ * pow(10, decimal_places), &frac) != 0.0f) + { + decimal_places++; + } + + // printf("该浮点数的小数部分有 %d %lf 位。\n", decimal_places,number_float); + switch (decimal_places) + { + case 0: + sprintf_s(new_buf,64,"%.00lf",number_float); + break; + case 1: + sprintf_s(new_buf,64,"%.01lf",number_float); + break; + case 2: + sprintf_s(new_buf,64,"%.02lf",number_float); + break; + case 3: + sprintf_s(new_buf,64,"%.03lf",number_float); + break; + case 4: + sprintf_s(new_buf,64,"%.04lf",number_float); + break; + case 6: + sprintf_s(new_buf,64,"%.06lf",number_float); + break; + case 7: + sprintf_s(new_buf,64,"%.07lf",number_float); + break; + case 8: + sprintf_s(new_buf,64,"%.08lf",number_float); + break; + default: + sprintf_s(new_buf,64,"%.09lf",number_float); + break; + } + number_string=new_buf; + number->SetText(new_buf); + number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER); + Invalidate(); + } + else if (buf[0]=='D') + { + number_string.pop_back(); + number->SetText(number_string.c_str()); + number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER); + if(number_falg==0) + { + CalculatorModel::GetInstance()->symbol_stack_pop(); + number_falg=1; + } + else + { + number_falg=1; + } + + + Invalidate(); + } + + return; +} + +void CalculatorView::symbol_text_updata_calculate_buf(const char *buf) +{ + if(g_pMainView->call_roll_timer_move) + { + osTimerStop(g_pMainView->call_roll_timer_move); + osTimerDelete(g_pMainView->call_roll_timer_move); + g_pMainView->call_roll_timer_move = NULL; + } + + char new_buf[64]={0}; + std::string text_buf=number_string; + double number_float=0; + + if(text_buf[0]>='0' && text_buf[0]<='9') + { + number_float=strtod(text_buf.c_str(),NULL); + char new_buf_[64]={0}; + sprintf_s(new_buf_,64,"%.010lf",number_float); + number_float=strtod(new_buf_,NULL); + + CalculatorModel::GetInstance()->number_push_stack(number_float); + } + + number_float =CalculatorModel::GetInstance()->calculate_stack(); + + char new_buf_[64]={0}; + sprintf_s(new_buf_,64,"%.010lf",number_float); + number_float=strtod(new_buf_,NULL); + + double number_float_=number_float; + int decimal_places = 0; + double frac; + + while (modf(number_float_ * pow(10, decimal_places), &frac) != 0.0f) + { + decimal_places++; + } + switch (decimal_places) + { + case 0: + sprintf_s(new_buf,64,"%.00lf",number_float); + break; + case 1: + sprintf_s(new_buf,64,"%.01lf",number_float); + break; + case 2: + sprintf_s(new_buf,64,"%.02lf",number_float); + break; + case 3: + sprintf_s(new_buf,64,"%.03lf",number_float); + break; + case 4: + sprintf_s(new_buf,64,"%.04lf",number_float); + break; + case 6: + sprintf_s(new_buf,64,"%.06lf",number_float); + break; + case 7: + sprintf_s(new_buf,64,"%.07lf",number_float); + break; + case 8: + sprintf_s(new_buf,64,"%.08lf",number_float); + break; + default: + sprintf_s(new_buf,64,"%.09lf",number_float); + break; + } + + // CalculatorModel::GetInstance()->number_push_stack(number_float); + number_falg=2; + + number_string=new_buf; + + number->SetText(new_buf); + number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER); + Invalidate(); + + + // number->SetText(""); + return; +} + + + + +static char *dial_id_str[]={"7","8","9","C","%","4","5","6","÷","x","1","2","3","-","+","","0",".","=",""}; + +bool CalculatorView::button_init() +{ + uint8_t showdial_i=0; + for(int i=0;i<4;i++) + { + for(int j=0;j<5;j++) + { + if(showdial_i==15 || showdial_i==19) + { + showdial_i++; + continue; + } + if (Button[showdial_i] == nullptr) { + Button[showdial_i] = new UILabelButton(); + } + if(Button[showdial_i]==nullptr) + { + return false; + } + Button[showdial_i]->SetPosition(17+(j*88), 140+(i*68)); // widget's left position and top position + + Button[showdial_i]->Resize(80, 60); // widget's width and height + Button[showdial_i]->SetViewId(dial_id_str[showdial_i]);// Set style + + Button[showdial_i]->SetStyle(STYLE_BACKGROUND_COLOR, 0xff191919); + Button[showdial_i]->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xff191919, UIButton::PRESSED); + if(j>2) + { + Button[showdial_i]->SetStyle(STYLE_BACKGROUND_COLOR, 0xFFFF8A36); + Button[showdial_i]->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xFFFF8A36, UIButton::PRESSED); + if(i==0) + { + Button[showdial_i]->SetStyle(STYLE_BACKGROUND_COLOR, 0xFFAAAAAA); + Button[showdial_i]->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xFFAAAAAA, UIButton::PRESSED); + } + } + + Button[showdial_i]->SetText(dial_id_str[showdial_i]); + Button[showdial_i]->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 40); + + + group->Add(Button[showdial_i]); + Button[showdial_i]->SetVisible(true); + + clickLeftListener = (UIView::OnClickListener*)OHOS::CalculatorPresenter::GetInstance(); + if (clickLeftListener == nullptr) { + return false; + } + + Button[showdial_i]->SetOnClickListener(clickLeftListener); + + showdial_i++; + } + } + + return true; +} + +bool CalculatorView::Label_init() +{ + if (number == nullptr) { + number = new UILabel(); + } + if(number==nullptr) + { + return false; + } + number->SetPosition(65, 73); // widget's left position and top position + number->Resize(310, 60); // widget's width and height + number->SetViewId("label5");// Set style + number->SetStyle(STYLE_TEXT_FONT, 50); + + // Set additional property + number->SetText(""); + number->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE); + number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER); + number->SetRollSpeed(0); + number->SetFont(DEFAULT_VECTOR_FONT_FILENAME_EN, 50); + + // Add to rootView + group->Add(number); + + number->SetVisible(true); + + if (image1_ == nullptr) { + image1_ = new UIImageView(); + } + image1_->SetPosition(378, 85); // widget's left position and top position + image1_->Resize(60, 60); // widget's width and height + image1_->SetViewId("D");// Set style + image1_->SetAutoEnable(false); + image1_->SetResizeMode(UIImageView::CENTER); + + + char* DimagesPath = "/user/res/Vector.bin"; + + image1_->SetSrc(DimagesPath); + clickLeftListener = (UIView::OnClickListener*)OHOS::CalculatorPresenter::GetInstance(); + if (clickLeftListener == nullptr) { + return false; + } + + image1_->SetOnClickListener(clickLeftListener); + image1_->SetTouchable(true); + // Add to rootView + group->Add(image1_); + + image1_->SetVisible(true); + + + if (hint == nullptr) { + hint = new UILabelExt(); + } + hint->SetPosition(109, 169); // widget's left position and top position + hint->Resize(247, 60); // widget's width and height + hint->SetViewId("label2");// Set style + hint->SetStyle(STYLE_BACKGROUND_COLOR, 0xff191919); + hint->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + hint->SetStyle(STYLE_BORDER_RADIUS, 25); + hint->SetStyle(STYLE_PADDING_BOTTOM, 8); + hint->SetStyle(STYLE_TEXT_FONT, 30); + hint->SetStyle(STYLE_LINE_HEIGHT, 20); + + // Set additional property + hint->SetTextId(STR_NUM_LENGTH); + hint->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE); + hint->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + hint->SetRollSpeed(0); + hint->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); + group->Add(hint); + hint->SetVisible(false); + + + return true; +} + +void CalculatorView::OnStart() +{ + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CalculatorView::OnStart"); + View::OnStart(); + + AddViewToRootContainer(new UIViewGroup()); + GetRootContainer()->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + GetRootContainer()->SetTouchable(true); + GetRootContainer()->SetDraggable(true); + + RootView::OnKeyActListener *keyactListener = (RootView::OnKeyActListener *)OHOS::CalculatorPresenter::GetInstance(); + RootView::GetInstance()->SetOnKeyActListener(keyactListener); + + group = new UIViewGroup(); + if (group == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CallingView::UIViewGroup::new group error"); + return ; + } + group->SetPosition(0, 0); + group->Resize(VERTICAL_RESOLUTION, HORIZONTAL_RESOLUTION); // 454 : value + group->SetTouchable(true); + group->SetDraggable(true); + + dragListener = (UIView::OnDragListener*)OHOS::CalculatorPresenter::GetInstance(); + if (dragListener == nullptr) { + + return; + } + group->SetOnDragListener(dragListener); + AddViewToRootContainer(group); + + if(!button_init()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CallingView::UIViewGroup::new group error"); + return; + } + if(!Label_init()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CallingView::UIViewGroup::new group error"); + return; + } + + +} + + + + +} \ No newline at end of file -- 2.17.1