11#include < calculatorTab.hpp>
22
3+ #include < vector>
4+ #include < utility>
5+
6+ #include < calculator.hpp>
7+
8+ std::vector<std::pair<std::string,char >> allButtons {
9+ std::make_pair (" (Button" , ' (' ),
10+ std::make_pair (" )Button" , ' )' ),
11+ std::make_pair (" 9Button" , ' 9' ),
12+ std::make_pair (" 6Button" , ' 6' ),
13+ std::make_pair (" 3Button" , ' 3' ),
14+ std::make_pair (" 0Button" , ' 0' ),
15+ std::make_pair (" 8Button" , ' 8' ),
16+ std::make_pair (" 5Button" , ' 5' ),
17+ std::make_pair (" 2Button" , ' 2' ),
18+ std::make_pair (" DotButton" , ' .' ),
19+ std::make_pair (" 7Button" , ' 7' ),
20+ std::make_pair (" 4Button" , ' 4' ),
21+ std::make_pair (" 1Button" , ' 1' ),
22+ std::make_pair (" PlusButton" , ' +' ),
23+ std::make_pair (" MinusButton" , ' -' ),
24+ std::make_pair (" MultiplyButton" , ' *' ),
25+ std::make_pair (" DivideButton" , ' /' ),
26+ };
27+
328CalculatorTab::CalculatorTab ()
429{
530 this ->inflateFromXMLRes (" xml/tabs/calculator.xml" );
31+
32+ for (auto &e : allButtons)
33+ {
34+ char c = e.second ;
35+
36+ this ->getView (e.first )->registerAction (
37+ " " , brls::BUTTON_A,
38+ [this , c](brls::View *view) {
39+ onButtonPressed (c);
40+ return true ;
41+ },
42+ false , brls::SOUND_CLICK
43+ );
44+ }
45+
46+ BRLS_REGISTER_CLICK_BY_ID (" EqualButton" , this ->onEqualButtonPressed );
47+ BRLS_REGISTER_CLICK_BY_ID (" DelButton" , this ->onDeleteButtonPressed );
648}
749
850brls::View* CalculatorTab::create ()
951{
1052 return new CalculatorTab ();
53+ }
54+
55+ void CalculatorTab::onButtonPressed (char c)
56+ {
57+ expressionStr += c;
58+ updateScreenBufferFromExpStr ();
59+ }
60+
61+ bool CalculatorTab::onEqualButtonPressed (brls::View *view)
62+ {
63+ if (expressionStr != " " )
64+ {
65+ double result = Calculator::getInstance ().evaluateExpression (expressionStr);
66+ screenBuffer += " = " + std::to_string (result);
67+ updateScreen ();
68+
69+ expressionStr = " " ;
70+ }
71+ return true ;
72+ }
73+
74+ bool CalculatorTab::onDeleteButtonPressed (brls::View *view)
75+ {
76+ if (expressionStr != " " )
77+ {
78+ expressionStr.pop_back ();
79+ updateScreenBufferFromExpStr ();
80+ }
81+
82+ return true ;
83+ }
84+
85+ void CalculatorTab::updateScreen ()
86+ {
87+ screenLabel->setText (screenBuffer);
88+ }
89+
90+ void CalculatorTab::updateScreenBufferFromExpStr ()
91+ {
92+ screenBuffer = expressionStr;
93+ if (expressionStr.length () >= 30 )
94+ screenBuffer = expressionStr.substr (29 );
1195}
0 commit comments