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