-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
78 lines (69 loc) · 2.55 KB
/
window.cpp
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
#include "window.h"
#include "parser.h"
// creates layout, buttons, and places them
Window::Window(QWidget *parent) : QWidget(parent) {
QGridLayout *mainLayout = new QGridLayout;
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(mainLayout);
QSize buttonSize(50, 50);
// create buttons
display = new QLineEdit();
QPushButton *buttonDivision = createButton(QChar(0x00F7), buttonSize, SLOT(clickedButton()));
QPushButton *buttonMultiplication = createButton(QChar(0x00D7), buttonSize, SLOT(clickedButton()));
QPushButton *buttonSubtract = createButton("-", buttonSize, SLOT(clickedButton()));
QPushButton *buttonAdd = createButton("+", buttonSize, SLOT(clickedButton()));
QPushButton *buttonEnter = createButton("=", buttonSize, SLOT(clickedEnter()));
QPushButton *buttonClear = createButton("clear", buttonSize, SLOT(clickedClear()));
QPushButton *buttonDigits[10];
for (int i = 0; i < 10; i++) {
buttonDigits[i] = createButton(QString::number(i), buttonSize, SLOT(clickedButton()));
}
// add buttons to layout
mainLayout->addWidget(display, 0, 0, 1, 6);
mainLayout->addWidget(buttonDivision, 1, 3);
mainLayout->addWidget(buttonMultiplication, 2, 3);
mainLayout->addWidget(buttonSubtract, 3, 3);
mainLayout->addWidget(buttonAdd, 4, 3);
mainLayout->addWidget(buttonEnter, 5, 3);
mainLayout->addWidget(buttonClear, 5, 2);
mainLayout->addWidget(buttonDigits[0], 4, 1);
for (int i=1; i < 10; i++) {
int row = 3 - (i - 1) / 3;
int col = (i - 1) % 3;
mainLayout->addWidget(buttonDigits[i], row, col);
}
}
// concatenates button text to the display
void Window::clickedButton() {
QPushButton *obj = qobject_cast<QPushButton *> (sender());
if (obj->text() == QChar(0x00F7)) {
display->setText(display->text() + "/");
return;
}
else if (obj->text() == QChar(0x00D7)) {
display->setText(display->text() + "*");
return;
}
else {
display->setText(display->text() + obj->text());
}
}
// calls evaluation functions then displays the result
void Window::clickedEnter() {
parser expression;
QString expressionRPN = expression.convertToRPN(display->text());
QString text = expression.evaluateRPN(expressionRPN);
display->setText(text);
}
// clears display
void Window::clickedClear() {
display->clear();
}
// creates and returns button object
QPushButton *Window::createButton(QString text, QSize size, const char *slot) {
QPushButton *button = new QPushButton(text);
button->setFixedSize(size);
connect(button, SIGNAL(clicked()), this, slot);
return button;
}
Window::~Window() {}