-rw-r--r-- | noncore/tools/euroconv/calckeypad.cpp | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/noncore/tools/euroconv/calckeypad.cpp b/noncore/tools/euroconv/calckeypad.cpp index f1d61c0..4d0c37f 100644 --- a/noncore/tools/euroconv/calckeypad.cpp +++ b/noncore/tools/euroconv/calckeypad.cpp | |||
@@ -21,187 +21,194 @@ | |||
21 | KeyPad::KeyPad(LCDDisplay *lcd, QWidget *parent, const char *name ) | 21 | KeyPad::KeyPad(LCDDisplay *lcd, QWidget *parent, const char *name ) |
22 | : QGrid(5, parent, name){ | 22 | : QGrid(5, parent, name){ |
23 | 23 | ||
24 | int i; | 24 | int i; |
25 | QButton *btnButton; | 25 | QButton *btnButton; |
26 | 26 | ||
27 | // save lcd at init | 27 | // save lcd at init |
28 | display = lcd; | 28 | display = lcd; |
29 | // Init variable | 29 | // Init variable |
30 | dCurrent = 0; | 30 | dCurrent = 0; |
31 | iLenCurrent = 1; | 31 | iLenCurrent = 1; |
32 | bIsDec = false; | 32 | bIsDec = false; |
33 | dDecCurrent = 0; | 33 | dDecCurrent = 0; |
34 | iPreviousOperator = 0; | 34 | iPreviousOperator = 0; |
35 | dPreviousValue = 0; | 35 | dPreviousValue = 0; |
36 | 36 | ||
37 | // First line | 37 | // First line |
38 | btnClear = new QPushButton("CE/C", this, "Clear"); | 38 | btnClear = new QPushButton("CE/C", this, "Clear"); |
39 | btn7 = new QPushButton("7", this, "7"); | 39 | btn7 = new QPushButton("7", this, "7"); |
40 | btn8 = new QPushButton("8", this, "8"); | 40 | btn8 = new QPushButton("8", this, "8"); |
41 | btn9 = new QPushButton("9", this, "9"); | 41 | btn9 = new QPushButton("9", this, "9"); |
42 | btnPlus = new QPushButton("+", this, "+"); | 42 | btnPlus = new QPushButton("+", this, "+"); |
43 | // 2nd line | 43 | // 2nd line |
44 | btnPercent = new QPushButton("%", this, "percent"); | 44 | btnPercent = new QPushButton("%", this, "percent"); |
45 | btn4 = new QPushButton("4", this, "4"); | 45 | btn4 = new QPushButton("4", this, "4"); |
46 | btn5 = new QPushButton("5", this, "5"); | 46 | btn5 = new QPushButton("5", this, "5"); |
47 | btn6 = new QPushButton("6", this, "6"); | 47 | btn6 = new QPushButton("6", this, "6"); |
48 | btnMinus = new QPushButton("-", this, "-"); | 48 | btnMinus = new QPushButton("-", this, "-"); |
49 | // 3rd line | 49 | // 3rd line |
50 | btnF1 = new QPushButton("",this, "F1"); | 50 | btnF1 = new QPushButton("",this, "F1"); |
51 | btn1 = new QPushButton("1", this, "1"); | 51 | btn1 = new QPushButton("1", this, "1"); |
52 | btn2 = new QPushButton("2", this, "2"); | 52 | btn2 = new QPushButton("2", this, "2"); |
53 | btn3 = new QPushButton("3", this, "3"); | 53 | btn3 = new QPushButton("3", this, "3"); |
54 | btnMultiply = new QPushButton("X", this, "X"); | 54 | btnMultiply = new QPushButton("X", this, "X"); |
55 | // 4th line | 55 | // 4th line |
56 | btnAbout = new QPushButton("?", this, "About"); | 56 | btnAbout = new QPushButton("?", this, "About"); |
57 | btn0 = new QPushButton("0", this, "0"); | 57 | btn0 = new QPushButton("0", this, "0"); |
58 | btnDot = new QPushButton(".", this, "dot"); | 58 | btnDot = new QPushButton(".", this, "dot"); |
59 | btnEqual = new QPushButton("=", this, "equal"); | 59 | btnEqual = new QPushButton("=", this, "equal"); |
60 | btnDivide = new QPushButton("/", this, "divide"); | 60 | btnDivide = new QPushButton("/", this, "divide"); |
61 | 61 | ||
62 | // Digit key | 62 | // Digit key |
63 | grpbtnDigits = new QButtonGroup(0, "digits"); | 63 | grpbtnDigits = new QButtonGroup(0, "digits"); |
64 | grpbtnDigits->insert(btn0, 0); | 64 | grpbtnDigits->insert(btn0, 0); |
65 | grpbtnDigits->insert(btn1, 1); | 65 | grpbtnDigits->insert(btn1, 1); |
66 | grpbtnDigits->insert(btn2, 2); | 66 | grpbtnDigits->insert(btn2, 2); |
67 | grpbtnDigits->insert(btn3, 3); | 67 | grpbtnDigits->insert(btn3, 3); |
68 | grpbtnDigits->insert(btn4, 4); | 68 | grpbtnDigits->insert(btn4, 4); |
69 | grpbtnDigits->insert(btn5, 5); | 69 | grpbtnDigits->insert(btn5, 5); |
70 | grpbtnDigits->insert(btn6, 6); | 70 | grpbtnDigits->insert(btn6, 6); |
71 | grpbtnDigits->insert(btn7, 7); | 71 | grpbtnDigits->insert(btn7, 7); |
72 | grpbtnDigits->insert(btn8, 8); | 72 | grpbtnDigits->insert(btn8, 8); |
73 | grpbtnDigits->insert(btn9, 9); | 73 | grpbtnDigits->insert(btn9, 9); |
74 | 74 | ||
75 | // set appearance of buttons | 75 | // set appearance of buttons |
76 | for(i=0; i<10; i++){ | 76 | for(i=0; i<10; i++){ |
77 | btnButton = grpbtnDigits->find(i); | 77 | btnButton = grpbtnDigits->find(i); |
78 | btnButton->setFixedSize(30,30); | 78 | btnButton->setFixedSize(30,30); |
79 | } | 79 | } |
80 | 80 | ||
81 | // Operator key | 81 | // Operator key |
82 | grpbtnOperators = new QButtonGroup(0, "operator"); | 82 | grpbtnOperators = new QButtonGroup(0, "operator"); |
83 | grpbtnOperators->insert(btnPlus, 1); | 83 | grpbtnOperators->insert(btnPlus, 1); |
84 | grpbtnOperators->insert(btnMinus,2); | 84 | grpbtnOperators->insert(btnMinus,2); |
85 | grpbtnOperators->insert(btnMultiply,3); | 85 | grpbtnOperators->insert(btnMultiply,3); |
86 | grpbtnOperators->insert(btnDivide,4); | 86 | grpbtnOperators->insert(btnDivide,4); |
87 | grpbtnOperators->insert(btnEqual,5); | 87 | grpbtnOperators->insert(btnEqual,5); |
88 | // set appearance of buttons | 88 | // set appearance of buttons |
89 | for(i=1; i<6; i++){ | 89 | for(i=1; i<6; i++){ |
90 | btnButton = grpbtnOperators->find(i); | 90 | btnButton = grpbtnOperators->find(i); |
91 | btnButton->setFixedSize(30,30); | 91 | btnButton->setFixedSize(30,30); |
92 | } | 92 | } |
93 | btnClear->setFixedSize(30,30); | 93 | btnClear->setFixedSize(30,30); |
94 | btnClear->setPalette(QPalette( QColor(255, 99, 71) ) ); | 94 | btnClear->setPalette(QPalette( QColor(255, 99, 71) ) ); |
95 | btnDot->setFixedSize(30,30); | 95 | btnDot->setFixedSize(30,30); |
96 | btnPercent->setFixedSize(30,30); | 96 | btnPercent->setFixedSize(30,30); |
97 | btnF1->setFixedSize(30,30); | 97 | btnF1->setFixedSize(30,30); |
98 | btnAbout->setFixedSize(30,30); | 98 | btnAbout->setFixedSize(30,30); |
99 | 99 | ||
100 | 100 | ||
101 | // SIGNALS AND SLOTS | 101 | // SIGNALS AND SLOTS |
102 | connect(grpbtnDigits, SIGNAL(clicked(int) ), this, SLOT(enterDigits(int))); | 102 | connect(grpbtnDigits, SIGNAL(clicked(int) ), this, SLOT(enterDigits(int))); |
103 | connect(grpbtnOperators, SIGNAL(clicked(int)), this, SLOT(operatorPressed(int))); | 103 | connect(grpbtnOperators, SIGNAL(clicked(int)), this, SLOT(operatorPressed(int))); |
104 | connect(btnClear, SIGNAL(clicked()), this, SLOT(clearLCD())); | 104 | connect(btnClear, SIGNAL(clicked()), this, SLOT(clearLCD())); |
105 | connect(btnAbout, SIGNAL(clicked()), this, SLOT(aboutDialog())); | 105 | connect(btnAbout, SIGNAL(clicked()), this, SLOT(aboutDialog())); |
106 | connect(btnDot, SIGNAL(clicked()), this, SLOT(gotoDec())); | 106 | connect(btnDot, SIGNAL(clicked()), this, SLOT(gotoDec())); |
107 | } | 107 | } |
108 | 108 | ||
109 | 109 | ||
110 | /*********************************************************************** | 110 | /*********************************************************************** |
111 | * SLOT: enterDigits calculates the value to display and display it. | 111 | * SLOT: enterDigits calculates the value to display and display it. |
112 | **********************************************************************/ | 112 | **********************************************************************/ |
113 | void KeyPad::enterDigits(int i){ | 113 | void KeyPad::enterDigits(int i){ |
114 | 114 | ||
115 | if(!dCurrent) | 115 | if(!dCurrent) |
116 | dCurrent = i; | 116 | dCurrent = i; |
117 | else if(!bIsDec){ | 117 | else if(iLenCurrent > 9) |
118 | if(iLenCurrent > 9) | ||
119 | return; | 118 | return; |
119 | else if(!bIsDec){ | ||
120 | dCurrent *= 10; | 120 | dCurrent *= 10; |
121 | dCurrent += i; | 121 | dCurrent += i; |
122 | iLenCurrent++; | 122 | iLenCurrent++; |
123 | }else{ | 123 | }else{ |
124 | dCurrent += i*dDecCurrent; | 124 | dCurrent += i*dDecCurrent; |
125 | dDecCurrent /= 10; | 125 | dDecCurrent /= 10; |
126 | } | 126 | } |
127 | 127 | ||
128 | display->setValue(dCurrent); | 128 | display->setValue(dCurrent); |
129 | } | 129 | } |
130 | 130 | ||
131 | /*********************************************************************** | 131 | /*********************************************************************** |
132 | * SLOT: Operator has been pressed | 132 | * SLOT: Operator has been pressed |
133 | **********************************************************************/ | 133 | **********************************************************************/ |
134 | void KeyPad::operatorPressed(int i){ | 134 | void KeyPad::operatorPressed(int i){ |
135 | 135 | ||
136 | if(iPreviousOperator){ | 136 | if(iPreviousOperator){ |
137 | switch(iPreviousOperator){ | 137 | switch(iPreviousOperator){ |
138 | case 1: // Plus + | 138 | case 1: // Plus + |
139 | dCurrent += dPreviousValue; | 139 | dCurrent += dPreviousValue; |
140 | break; | 140 | break; |
141 | 141 | ||
142 | case 2: // Minus - | 142 | case 2: // Minus - |
143 | dCurrent = dPreviousValue - dCurrent; | 143 | dCurrent = dPreviousValue - dCurrent; |
144 | break; | 144 | break; |
145 | 145 | ||
146 | case 3: // Multiply * | 146 | case 3: // Multiply * |
147 | dCurrent *= dPreviousValue; | 147 | dCurrent *= dPreviousValue; |
148 | break; | 148 | break; |
149 | 149 | ||
150 | case 4: // Divide / | 150 | case 4: // Divide / |
151 | dCurrent = dPreviousValue / dCurrent; | 151 | dCurrent = dPreviousValue / dCurrent; |
152 | break; | 152 | break; |
153 | 153 | ||
154 | } | 154 | } |
155 | display->setValue(dCurrent); | 155 | display->setValue(dCurrent); |
156 | } | 156 | } |
157 | 157 | ||
158 | dPreviousValue = dCurrent; | 158 | if(i==5){ |
159 | iPreviousOperator = i; | 159 | // ensure we won't be able to enter more digits |
160 | 160 | iLenCurrent = 100; | |
161 | // reset LCD for next digit | 161 | iPreviousOperator = 0; |
162 | dCurrent = 0; | 162 | dPreviousValue = 0; |
163 | iLenCurrent = 1; | 163 | }else{ |
164 | bIsDec = false; | 164 | dPreviousValue = dCurrent; |
165 | dDecCurrent = 0; | 165 | iPreviousOperator = i; |
166 | |||
167 | // reset LCD for next digit | ||
168 | dCurrent = 0; | ||
169 | iLenCurrent = 1; | ||
170 | bIsDec = false; | ||
171 | dDecCurrent = 0; | ||
172 | } | ||
166 | } | 173 | } |
167 | /*********************************************************************** | 174 | /*********************************************************************** |
168 | * SLOT: clearLCD CE/C has been pressed | 175 | * SLOT: clearLCD CE/C has been pressed |
169 | **********************************************************************/ | 176 | **********************************************************************/ |
170 | void KeyPad::clearLCD(void){ | 177 | void KeyPad::clearLCD(void){ |
171 | 178 | ||
172 | dCurrent = 0; | 179 | dCurrent = 0; |
173 | iLenCurrent = 1; | 180 | iLenCurrent = 1; |
174 | bIsDec = false; | 181 | bIsDec = false; |
175 | dDecCurrent = 0; | 182 | dDecCurrent = 0; |
176 | iPreviousOperator = 0; | 183 | iPreviousOperator = 0; |
177 | dPreviousValue = 0; | 184 | dPreviousValue = 0; |
178 | display->setValue(0); | 185 | display->setValue(0); |
179 | } | 186 | } |
180 | 187 | ||
181 | /*********************************************************************** | 188 | /*********************************************************************** |
182 | * SLOT: gotoDec Dot has been pressed | 189 | * SLOT: gotoDec Dot has been pressed |
183 | **********************************************************************/ | 190 | **********************************************************************/ |
184 | void KeyPad::gotoDec(void){ | 191 | void KeyPad::gotoDec(void){ |
185 | 192 | ||
186 | bIsDec = true; | 193 | bIsDec = true; |
187 | dDecCurrent = 0.1; | 194 | dDecCurrent = 0.1; |
188 | } | 195 | } |
189 | 196 | ||
190 | 197 | ||
191 | 198 | ||
192 | /*********************************************************************** | 199 | /*********************************************************************** |
193 | * SLOT: Display About Dialog... | 200 | * SLOT: Display About Dialog... |
194 | **********************************************************************/ | 201 | **********************************************************************/ |
195 | void KeyPad::aboutDialog(void){ | 202 | void KeyPad::aboutDialog(void){ |
196 | 203 | ||
197 | QMessageBox::about( this, "About Euroconv", | 204 | QMessageBox::about( this, "About Euroconv", |
198 | "Euroconv is an Euro converter\n\n" | 205 | "Euroconv is an Euro converter\n\n" |
199 | "Copyright 2003 TALC Informatique.\n" | 206 | "Copyright 2003 TALC Informatique.\n" |
200 | "Under GPL license\n\n" | 207 | "Under GPL license\n\n" |
201 | "Written by Eric Santonacci for Opie\n" | 208 | "Written by Eric Santonacci for Opie\n" |
202 | "http://opie.handhelds.org\n\n" | 209 | "http://opie.handhelds.org\n\n" |
203 | "Version 0.2\n\n" | 210 | "Version 0.3\n\n" |
204 | "Any comment or feedback to:\n" | 211 | "Any comment or feedback to:\n" |
205 | "Eric.Santonacci@talc.fr\n"); | 212 | "Eric.Santonacci@talc.fr\n"); |
206 | 213 | ||
207 | } | 214 | } |