-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.h
532 lines (427 loc) · 18.2 KB
/
mainwindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCoreApplication>
#include <QObject>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QVector>
#include <QString>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow()
{
// Instantiate and Initialize Vertical Layout
QVBoxLayout *vBoxLayout = new QVBoxLayout;
// Set spacing and margins to 0
vBoxLayout->setSpacing(0);
vBoxLayout->setMargin(0);
{
// Initialize Display Row
labelDisp = new QLabel("");
// Set Aligment to be Right Justified
labelDisp->setAlignment(Qt::AlignRight);
// Style Display by using CSS Style Sheet as String
labelDisp->setStyleSheet("QLabel { "
"font-size: 18px;"
"background-color : #D9F3F3; "
"border: 1px solid black;"
" }");
// Set maximum witdt to prevent overflow
labelDisp->setMaximumWidth(300);
// Add display to the vertical layout
vBoxLayout->addWidget(labelDisp);
// Initialize First Row Elements
buttonMult = new QPushButton("*");
buttonDiv = new QPushButton("/");
buttonHex = new QPushButton("Hex");
buttonDec = new QPushButton("Dec");
// Connect Slots (Click Handler) to the initialized first frow elements
QObject::connect(buttonMult, SIGNAL (clicked()), this, SLOT (buttonMultAction()));
QObject::connect(buttonDiv, SIGNAL (clicked()), this, SLOT (buttonDivAction()));
QObject::connect(buttonHex, SIGNAL (clicked()), this, SLOT (buttonHexAction()));
QObject::connect(buttonDec, SIGNAL (clicked()), this, SLOT (buttonDecAction()));
// Create a horizontal layout to display 4 buttons next to each other
QHBoxLayout *hBoxLayout1 = new QHBoxLayout;
// Again set horizontal layout spacing to 0
hBoxLayout1->setSpacing(0);
// Add first row buttons to the horizontal layout
hBoxLayout1->addWidget(buttonMult);
hBoxLayout1->addWidget(buttonDiv);
hBoxLayout1->addWidget(buttonHex);
hBoxLayout1->addWidget(buttonDec);
// Add horizontal layout to the vertical layout
vBoxLayout -> addLayout(hBoxLayout1);
// Initialize Second Row Elements - For Comments Look At First Row - They are Same
buttonAdd = new QPushButton("+");
buttonSub = new QPushButton("-");
buttonEq = new QPushButton("=");
buttonClr = new QPushButton("Clr");
QObject::connect(buttonAdd, SIGNAL (clicked()), this, SLOT (buttonAddAction()));
QObject::connect(buttonSub, SIGNAL (clicked()), this, SLOT (buttonSubAction()));
QObject::connect(buttonEq, SIGNAL (clicked()), this, SLOT (buttonEqAction()));
QObject::connect(buttonClr, SIGNAL (clicked()), this, SLOT (buttonClrAction()));
QHBoxLayout *hBoxLayout2 = new QHBoxLayout;
hBoxLayout2->setSpacing(0);
hBoxLayout2->addWidget(buttonAdd);
hBoxLayout2->addWidget(buttonSub);
hBoxLayout2->addWidget(buttonEq);
hBoxLayout2->addWidget(buttonClr);
vBoxLayout->addLayout(hBoxLayout2);
// Initialize Third Row Elements
button0 = new QPushButton("0");
button1 = new QPushButton("1");
button2 = new QPushButton("2");
button3 = new QPushButton("3");
QObject::connect(button0, SIGNAL (clicked()), this, SLOT (button0Action()));
QObject::connect(button1, SIGNAL (clicked()), this, SLOT (button1Action()));
QObject::connect(button2, SIGNAL (clicked()), this, SLOT (button2Action()));
QObject::connect(button3, SIGNAL (clicked()), this, SLOT (button3Action()));
QHBoxLayout *hBoxLayout3 = new QHBoxLayout;
hBoxLayout3->setSpacing(0);
hBoxLayout3->addWidget(button0);
hBoxLayout3->addWidget(button1);
hBoxLayout3->addWidget(button2);
hBoxLayout3->addWidget(button3);
vBoxLayout -> addLayout(hBoxLayout3);
// Initialize Fourth Row Elements - For Comments Look At First Row - They are Same
button4 = new QPushButton("4");
button5 = new QPushButton("5");
button6 = new QPushButton("6");
button7 = new QPushButton("7");
QObject::connect(button4, SIGNAL (clicked()), this, SLOT (button4Action()));
QObject::connect(button5, SIGNAL (clicked()), this, SLOT (button5Action()));
QObject::connect(button6, SIGNAL (clicked()), this, SLOT (button6Action()));
QObject::connect(button7, SIGNAL (clicked()), this, SLOT (button7Action()));
QHBoxLayout *hBoxLayout4 = new QHBoxLayout;
hBoxLayout4->setSpacing(0);
hBoxLayout4->addWidget(button4);
hBoxLayout4->addWidget(button5);
hBoxLayout4->addWidget(button6);
hBoxLayout4->addWidget(button7);
vBoxLayout -> addLayout(hBoxLayout4);
// Initialize Fifth Row Elements - For Comments Look At First Row - They are Same
button8 = new QPushButton("8");
button9 = new QPushButton("9");
buttonA = new QPushButton("A");
buttonB = new QPushButton("B");
QObject::connect(button8, SIGNAL (clicked()), this, SLOT (button8Action()));
QObject::connect(button9, SIGNAL (clicked()), this, SLOT (button9Action()));
QObject::connect(buttonA, SIGNAL (clicked()), this, SLOT (buttonAAction()));
QObject::connect(buttonB, SIGNAL (clicked()), this, SLOT (buttonBAction()));
QHBoxLayout *hBoxLayout5 = new QHBoxLayout;
hBoxLayout5->setSpacing(0);
hBoxLayout5->addWidget(button8);
hBoxLayout5->addWidget(button9);
hBoxLayout5->addWidget(buttonA);
hBoxLayout5->addWidget(buttonB);
vBoxLayout -> addLayout(hBoxLayout5);
// Initialize Sixth Row Elements - For Comments Look At First Row - They are Same
buttonC = new QPushButton("C");
buttonD = new QPushButton("D");
buttonE = new QPushButton("E");
buttonF = new QPushButton("F");
QObject::connect(buttonC, SIGNAL (clicked()), this, SLOT (buttonCAction()));
QObject::connect(buttonD, SIGNAL (clicked()), this, SLOT (buttonDAction()));
QObject::connect(buttonE, SIGNAL (clicked()), this, SLOT (buttonEAction()));
QObject::connect(buttonF, SIGNAL (clicked()), this, SLOT (buttonFAction()));
QHBoxLayout *hBoxLayout6 = new QHBoxLayout;
hBoxLayout6->setSpacing(0);
hBoxLayout6->addWidget(buttonC);
hBoxLayout6->addWidget(buttonD);
hBoxLayout6->addWidget(buttonE);
hBoxLayout6->addWidget(buttonF);
vBoxLayout -> addLayout(hBoxLayout6);
// Disable Hex Buttons
updateHexButtons();
// Disable Buttons
updateOperatorButtons(false);
}
// Create widget to display previosly created item
QWidget *widget = new QWidget();
// Set Layout of the widget as the previously created vertical layout
widget->setLayout(vBoxLayout);
// Set is as central widget so that user is able to see it
setCentralWidget(widget);
}
QString calculateResult(QString inputString){
int indexCounter = 0;
QVector<QString> inputs(inputString.size(), "");
for(QChar &inputChar: inputString){
if (inputChar == '*' || inputChar == '/' || inputChar == '+' || inputChar == '-'){
indexCounter += 1;
inputs[indexCounter] += inputChar;
indexCounter += 1;
} else{
inputs[indexCounter] += inputChar;
}
}
inputs.erase(inputs.begin()+indexCounter+1, inputs.end());
if(isHex){
for(QString &elem: inputs){
if (elem != '*' && elem != '/' && elem != '+' && elem != '-'){
bool bStatus = false;
int nHex = elem.toInt(&bStatus,16);
if(bStatus)
elem = QString::number(nHex);
else
return "Error: HEX Conversion!";
}
}
}
QVector<QString> afterMult;
while (inputs.size() != 0){
QString input = inputs.front();
inputs.erase(inputs.begin());
if(input == "*" || input == "/"){
int secondOperand = inputs.front().toInt();
inputs.pop_front();
int firstOperand = afterMult.back().toInt();
afterMult.pop_back();
if(input == "*")
afterMult.push_back(QString::number(firstOperand * secondOperand));
else{
if(secondOperand == 0)
return "Error: Division by Zero";
afterMult.push_back(QString::number(firstOperand / secondOperand));
}
}else{
afterMult.push_back(input);
}
}
QVector<QString> afterSum;
while (afterMult.size() != 0){
QString input = afterMult.front();
afterMult.erase(afterMult.begin());
if(input == "+" || input == "-"){
int secondOperand = afterMult.front().toInt();
afterMult.pop_front();
int firstOperand = afterSum.back().toInt();
afterSum.pop_back();
if(input == "+")
afterSum.push_back(QString::number(firstOperand+secondOperand));
else
afterSum.push_back(QString::number(firstOperand-secondOperand));
}else{
afterSum.push_back(input);
}
}
// Return first element which is result of calculation
// if Hex is pressed, convert result to hex - base 16, use toUpper to have it with uppercase letters
if(isHex){
return QString::number( afterSum.front().toInt(), 16).toUpper();
}
return afterSum.front();
}
// Reset Display - Set it to Empty String
void resetDisplay(){
labelDisp->setText("");
reset = false;
}
// Lock Hex Buttons when Mode is DEC
void updateHexButtons(){
buttonA->setEnabled(isHex);
buttonB->setEnabled(isHex);
buttonC->setEnabled(isHex);
buttonD->setEnabled(isHex);
buttonE->setEnabled(isHex);
buttonF->setEnabled(isHex);
}
// Lock Operator Buttons after an Operator is Present
void updateOperatorButtons(bool value){
buttonMult->setEnabled(value);
buttonDiv->setEnabled(value);
buttonAdd->setEnabled(value);
buttonSub->setEnabled(value);
buttonEq->setEnabled(value);
}
private slots:
void buttonMultAction(){
// Add * operator to the screen
labelDisp->setText(labelDisp->text() + "*");
updateOperatorButtons(false);
}
void buttonDivAction(){
// Add / operator to the screen
labelDisp->setText(labelDisp->text() + "/");
updateOperatorButtons(false);
}
// Enable Hex Buttons
void buttonHexAction(){
// Let other components to know Hex is enabled
isHex = true;
// Enable Hex Buttons
updateHexButtons();
// Tell user hex is enabled
labelDisp->setText("Number System: HEX");
// After clicking a hex digit, notification will be lost and entered digit will be seen
reset = true;
// A digit is expecting from now on, disable operator buttons
updateOperatorButtons(false);
}
// Disable Hex Buttons - Look for buttonHexAction() for comments - Functionality Same
void buttonDecAction(){
isHex = false;
updateHexButtons();
labelDisp->setText("Number System: DEC");
reset = true;
updateOperatorButtons(false);
}
// Add + operator to the screen and disable operators
void buttonAddAction(){
labelDisp->setText(labelDisp->text() + "+");
updateOperatorButtons(false);
}
// Add - operator to the screen and disable operators
void buttonSubAction(){
labelDisp->setText(labelDisp->text() + "-");
updateOperatorButtons(false);
}
// Read Screen then call calculateResult, set reset to be true and disable operator buttons
void buttonEqAction(){
labelDisp->setText(calculateResult(labelDisp->text()));
reset = true;
updateOperatorButtons(false);
}
// Set empty string to the screen, disable operator buttons, set reset to false
void buttonClrAction(){
labelDisp->setText("");
reset = false;
updateOperatorButtons(false);
}
// This comment are valid for all these 0-F actions buttons
// If reset is true, which means screen needs to be reseted, then reset it by calling resetDisplay()
// After cleaning screen, add pressed digit to the screen
// Then enable operator buttons, it is enough to have one digit to accept next operator
void button0Action(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "0");
updateOperatorButtons(true);
}
void button1Action(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "1");
updateOperatorButtons(true);
}
void button2Action(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "2");
updateOperatorButtons(true);
}
void button3Action(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "3");
updateOperatorButtons(true);
}
// If reset is true, which means screen needs to be reseted, then reset it by calling resetDisplay()
// After cleaning screen, add pressed digit to the screen
// Then enable operator buttons, it is enough to have one digit to accept next operator
void button4Action(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "4");
updateOperatorButtons(true);
}
void button5Action(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "5");
updateOperatorButtons(true);
}
void button6Action(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "6");
updateOperatorButtons(true);
}
void button7Action(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "7");
updateOperatorButtons(true);
}
// If reset is true, which means screen needs to be reseted, then reset it by calling resetDisplay()
// After cleaning screen, add pressed digit to the screen
// Then enable operator buttons, it is enough to have one digit to accept next operator
void button8Action(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "8");
updateOperatorButtons(true);
}
void button9Action(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "9");
updateOperatorButtons(true);
}
void buttonAAction(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "A");
updateOperatorButtons(true);
}
void buttonBAction(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "B");
updateOperatorButtons(true);
}
// If reset is true, which means screen needs to be reseted, then reset it by calling resetDisplay()
// After cleaning screen, add pressed digit to the screen
// Then enable operator buttons, it is enough to have one digit to accept next operator
void buttonCAction(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "C");
updateOperatorButtons(true);
}
void buttonDAction(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "D");
updateOperatorButtons(true);
}
void buttonEAction(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "E");
updateOperatorButtons(true);
}
void buttonFAction(){
if(reset) resetDisplay();
labelDisp->setText(labelDisp->text() + "F");
updateOperatorButtons(true);
}
private:
// Instantiate Display - QLabel
QLabel *labelDisp;
// Instantiate *, /, Hex, Dec - Buttons
QPushButton *buttonMult;
QPushButton *buttonDiv;
QPushButton *buttonHex;
QPushButton *buttonDec;
// Instantiate +, -, Eq, Clr - Buttons
QPushButton *buttonAdd;
QPushButton *buttonSub;
QPushButton *buttonEq;
QPushButton *buttonClr;
// Instantiate 0, 1, 2, 3 - Buttons
QPushButton *button0;
QPushButton *button1;
QPushButton *button2;
QPushButton *button3;
// Instantiate 4, 5, 6, 7 - Buttons
QPushButton *button4;
QPushButton *button5;
QPushButton *button6;
QPushButton *button7;
// Instantiate 8, 9, A, B - Buttons
QPushButton *button8;
QPushButton *button9;
QPushButton *buttonA;
QPushButton *buttonB;
// Instantiate C, D, E, F - Buttons
QPushButton *buttonC;
QPushButton *buttonD;
QPushButton *buttonE;
QPushButton *buttonF;
// is Hex Button Pressed
bool isHex = false;
// is Reset Required
bool reset = false;
};
#endif // MAINWINDOW_H