From: hcj <1224667075@qq.com> Date: Fri, 13 Sep 2024 04:07:14 +0000 (+0800) Subject: 计算器综合修改 #186 X-Git-Tag: release_V1.2.70_20241020_135402~492 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=99e30d3590de090b9367b3e69cda7655029f5b9c;p=mirrors%2FFD230.git 计算器综合修改 #186 --- diff --git a/code/sdk/application/wearable/fd230app/nativeui/calculator/include/CalculatorModel.h b/code/sdk/application/wearable/fd230app/nativeui/calculator/include/CalculatorModel.h index f3b884dc4..cbb79ae07 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/calculator/include/CalculatorModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/calculator/include/CalculatorModel.h @@ -25,17 +25,17 @@ public: static CalculatorModel *GetInstance(void); - void number_push_stack(float); - float symbol_push_stack(char symbol); - float calculate_stack(); + void number_push_stack(double ); + void symbol_push_stack(char symbol); + double calculate_stack(); uint8_t get_stcak_size(); - void symbol_stack_top_uddata(float data); + void symbol_stack_top_uddata(char data); void clean_stack(); private: CalculatorModel(); CalculatorModel(const CalculatorModel &); virtual ~CalculatorModel(); - std::stack numberStack; + std::stack numberStack; std::stack symbolStack; }; diff --git a/code/sdk/application/wearable/fd230app/nativeui/calculator/include/CalculatorView.h b/code/sdk/application/wearable/fd230app/nativeui/calculator/include/CalculatorView.h index 2b58c9afb..99e983ff0 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/calculator/include/CalculatorView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/calculator/include/CalculatorView.h @@ -25,6 +25,7 @@ #include "PhoneService.h" #include "vector.h" #include "ChangeSliceListener.h" +#include "cmsis_os2.h" namespace OHOS { @@ -43,6 +44,11 @@ public: void symbol_text_updata_single_buf(const char *buf); void symbol_text_updata_calculate_buf(const char *buf); + void start_call_roll_timer(); + void show_hint_label(bool data); + + osTimerId_t call_roll_timer=NULL; + private: uint8_t number_falg=0; bool button_init(); @@ -53,7 +59,7 @@ private: UIViewGroup *group{nullptr}; UILabel *number{nullptr}; - + UILabelExt *hint{nullptr}; UIView::OnClickListener* clickLeftListener{nullptr}; UIView:: OnDragListener* dragListener{nullptr}; diff --git a/code/sdk/application/wearable/fd230app/nativeui/calculator/src/CalculatorModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/calculator/src/CalculatorModel.cpp index 799bf7fdc..a161d49dd 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/calculator/src/CalculatorModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/calculator/src/CalculatorModel.cpp @@ -31,9 +31,9 @@ CalculatorModel::~CalculatorModel() } -void CalculatorModel:: number_push_stack(float data) +void CalculatorModel:: number_push_stack(double data) { - printf("%f\r\n",data); + printf("%lf\r\n",data); if(!symbolStack.empty()) { if(symbolStack.top()=='-') @@ -49,7 +49,7 @@ void CalculatorModel:: number_push_stack(float data) } -void CalculatorModel:: symbol_stack_top_uddata(float data) +void CalculatorModel:: symbol_stack_top_uddata(char data) { printf("%c\r\n",data); if(!symbolStack.empty()) @@ -64,16 +64,16 @@ uint8_t CalculatorModel::get_stcak_size() return numberStack.size(); } -float CalculatorModel:: symbol_push_stack(char symbol) +void CalculatorModel:: symbol_push_stack(char symbol) { printf("%c\r\n",symbol); if(numberStack.empty()) { - return 0; + return ; } symbolStack.push(symbol); - return 0; + return ; } @@ -90,17 +90,17 @@ void CalculatorModel:: clean_stack() } } -float CalculatorModel:: calculate_stack() +double CalculatorModel:: calculate_stack() { - float sum=0; + double sum=0; if(numberStack.empty()) return sum; - float number_1= numberStack.top(); + double number_1= numberStack.top(); numberStack.pop(); sum=number_1; while (!numberStack.empty()) { - float number_= numberStack.top(); + double number_= numberStack.top(); numberStack.pop(); if(symbolStack.empty()) @@ -120,9 +120,13 @@ float CalculatorModel:: calculate_stack() } if(symbol=='/') { + if(sum==0) + { + return sum; + } sum=number_/sum; } - printf("sum:%f\r\n",sum); + printf("sum:%lf\r\n",sum); } return sum; } diff --git a/code/sdk/application/wearable/fd230app/nativeui/calculator/src/CalculatorView.cpp b/code/sdk/application/wearable/fd230app/nativeui/calculator/src/CalculatorView.cpp index ee0164100..65f46bacd 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/calculator/src/CalculatorView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/calculator/src/CalculatorView.cpp @@ -9,6 +9,7 @@ #include "calculator/include/CalculatorPresenter.h" #include "calculator/include/CalculatorModel.h" #include "wearable_log.h" +#include namespace OHOS { @@ -43,11 +44,42 @@ CalculatorView::~CalculatorView() 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; + + } +} +static int8_t roll_falg=0;//0.1s一次变换 + + static void label_Timer_CallBack(int data) +{ + roll_falg--; + if(roll_falg<=0) + { + if(CalculatorView::GetInstance()) + CalculatorView::GetInstance()->show_hint_label(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; + } + } } CalculatorView *CalculatorView::GetInstance() @@ -55,15 +87,53 @@ CalculatorView *CalculatorView::GetInstance() return g_pMainView; } + +void CalculatorView::show_hint_label(bool data) +{ + hint->SetVisible(data); + RootView::GetInstance()->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::number_text_updata_buf(const char *buf) { - char new_buf[16]={0}; + char new_buf[64]={0}; const char*text_buf=NULL; if(number_falg==1) text_buf=number->GetText(); else text_buf=""; - sprintf_s(new_buf,16,"%s%s",text_buf,buf); + if(strlen(text_buf)>20) + { + //超出长度限制 + start_call_roll_timer(); + show_hint_label(true); + return; + } + sprintf_s(new_buf,64,"%s%s",text_buf,buf); number->SetText(new_buf); number_falg=1; RootView::GetInstance()->Invalidate(); @@ -72,7 +142,7 @@ void CalculatorView::number_text_updata_buf(const char *buf) void CalculatorView::symbol_text_updata_buf(const char *buf) { - char new_buf[16]={0}; + char new_buf[64]={0}; const char*text_buf=number->GetText(); if(number_falg==0) { @@ -85,7 +155,8 @@ void CalculatorView::symbol_text_updata_buf(const char *buf) RootView::GetInstance()->Invalidate(); return; } - float number_float=strtof(text_buf,NULL); + double number_float=strtod(text_buf,NULL); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%lf %s",number_float,text_buf); CalculatorModel::GetInstance()->number_push_stack(number_float); @@ -98,7 +169,7 @@ void CalculatorView::symbol_text_updata_buf(const char *buf) } - sprintf_s(new_buf,16,"%s",buf); + sprintf_s(new_buf,64,"%s",buf); number->SetText(new_buf); RootView::GetInstance()->Invalidate(); @@ -117,7 +188,7 @@ void CalculatorView::symbol_text_updata_single_buf(const char *buf) char new_buf[16]={0}; const char*text_buf=number->GetText(); - float number_float=strtof(text_buf,NULL); + double number_float=strtod(text_buf,NULL); if(buf[0]=='C') { CalculatorModel::GetInstance()->clean_stack(); @@ -126,15 +197,55 @@ void CalculatorView::symbol_text_updata_single_buf(const char *buf) } else if (buf[0]=='%') { - number_float=0.01*number_float; - sprintf_s(new_buf,16,"%.02f",number_float); + 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->SetText(new_buf); RootView::GetInstance()->Invalidate(); } else if (buf[0]=='D') { number_float=0; - sprintf_s(new_buf,16,"%.02f",number_float); + sprintf_s(new_buf,16,"%.00lf",number_float); number->SetText(new_buf); number_falg=2; RootView::GetInstance()->Invalidate(); @@ -145,17 +256,60 @@ void CalculatorView::symbol_text_updata_single_buf(const char *buf) void CalculatorView::symbol_text_updata_calculate_buf(const char *buf) { - char new_buf[32]={0}; + char new_buf[64]={0}; const char*text_buf=number->GetText(); - float number_float=strtof(text_buf,NULL); + + double number_float=strtod(text_buf,NULL); + CalculatorModel::GetInstance()->number_push_stack(number_float); number_float =CalculatorModel::GetInstance()->calculate_stack(); + 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; + } + // CalculatorModel::GetInstance()->number_push_stack(number_float); number_falg=2; - sprintf_s(new_buf,32,"%.02f",number_float); + number->SetText(new_buf); RootView::GetInstance()->Invalidate(); @@ -194,18 +348,21 @@ bool CalculatorView::button_init() 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); - Button[showdial_i]->SetStyleForState(STYLE_BORDER_COLOR, 0xff0b1928, UIButton::PRESSED); + group->Add(Button[showdial_i]); Button[showdial_i]->SetVisible(true); @@ -240,7 +397,7 @@ bool CalculatorView::Label_init() // Set additional property number->SetText(""); - number->SetLineBreakMode(UILabel::LINE_BREAK_WRAP); + number->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE); number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER); number->SetRollSpeed(0); number->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 50); @@ -254,8 +411,10 @@ bool CalculatorView::Label_init() image1_ = new UIImageView(); } image1_->SetPosition(378, 85); // widget's left position and top position - image1_->Resize(100, 100); // widget's width and height + 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"; @@ -272,6 +431,31 @@ bool CalculatorView::Label_init() 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->SetText("超过数字长度范围"); + 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; } diff --git a/code/sdk/application/wearable/fd230app/nativeui/phone/src/PhoneDirView.cpp b/code/sdk/application/wearable/fd230app/nativeui/phone/src/PhoneDirView.cpp index e198a81d4..a53ca83d8 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/phone/src/PhoneDirView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/phone/src/PhoneDirView.cpp @@ -113,8 +113,8 @@ namespace OHOS if (label1_phone_name == nullptr) { label1_phone_name = new UILabelExt(); } - label1_phone_name->SetPosition(0, 52); // widget's left position and top position - label1_phone_name->Resize(466, 30); // widget's width and height + label1_phone_name->SetPosition(40+20, 52); // widget's left position and top position + label1_phone_name->Resize(466-80-40, 30); // widget's width and height label1_phone_name->SetViewId("label1");// Set style label1_phone_name->SetStyle(STYLE_TEXT_FONT, 30);