|
7 | 7 | #include <QPushButton>
|
8 | 8 | #include <QSignalMapper>
|
9 | 9 |
|
10 |
| -CalculatorWidget::CalculatorWidget(QWidget* parent, Qt::WindowFlags flags) |
11 |
| -: QWidget(parent, flags) |
12 |
| -{ |
13 |
| - QGridLayout* layout = new QGridLayout; |
14 |
| - layout->setSizeConstraint(QLayout::SetFixedSize); |
15 |
| - setLayout(layout); |
16 |
| - |
17 |
| - QSizePolicy policy = sizePolicy(); |
18 |
| - |
19 |
| - displayLabel = new QLabel(this); |
20 |
| - layout->addWidget(displayLabel, 0, 0, 1, 3); |
21 |
| - displayLabel->setAutoFillBackground(true); |
22 |
| - displayLabel->setBackgroundRole(QPalette::Base); |
23 |
| - displayLabel->setAlignment(Qt::AlignRight); |
24 |
| - displayLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); |
25 |
| - policy = displayLabel->sizePolicy(); |
26 |
| - policy.setVerticalPolicy(QSizePolicy::Fixed); |
27 |
| - displayLabel->setSizePolicy(policy); |
28 |
| - |
29 |
| - signalMapper = new QSignalMapper(this); |
30 |
| - QPushButton* button = new QPushButton(QString::number(0), this); |
31 |
| - QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map())); |
32 |
| - signalMapper->setMapping(button, 0); |
33 |
| - layout->addWidget(button, 4, 1); |
34 |
| - digitButtons.push_back(button); |
35 |
| - for (unsigned int i = 1; i < 10; ++i) |
36 |
| - { |
37 |
| - QPushButton* button = new QPushButton(QString::number(i), this); |
38 |
| - QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map())); |
39 |
| - signalMapper->setMapping(button, i); |
40 |
| - layout->addWidget(button, 1+(9-i)/3, (i-1)%3); |
41 |
| - digitButtons.push_back(button); |
42 |
| - } |
43 |
| - QObject::connect(signalMapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int))); |
44 |
| - |
45 |
| - clearButton = new QPushButton("C", this); |
46 |
| - layout->addWidget(clearButton, 1, 4); |
47 |
| - QObject::connect(clearButton, SIGNAL(clicked()), SLOT(clearButtonClicked())); |
48 |
| - |
49 |
| - additionButton = new QPushButton("+", this); |
50 |
| - layout->addWidget(additionButton, 2, 4); |
51 |
| - QObject::connect(additionButton, SIGNAL(clicked()), SLOT(addButtonClicked())); |
52 |
| - |
53 |
| - subtractionButton = new QPushButton("-", this); |
54 |
| - layout->addWidget(subtractionButton, 3, 4); |
55 |
| - QObject::connect(subtractionButton, SIGNAL(clicked()), SLOT(subtractButtonClicked())); |
56 |
| - |
57 |
| - calculateButton = new QPushButton("=", this); |
58 |
| - layout->addWidget(calculateButton, 4, 4); |
59 |
| - QObject::connect(calculateButton, SIGNAL(clicked()), SLOT(calculateButtonClicked())); |
| 10 | +CalculatorWidget::CalculatorWidget(QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags) { |
| 11 | + QGridLayout *layout = new QGridLayout; |
| 12 | + layout->setSizeConstraint(QLayout::SetFixedSize); |
| 13 | + setLayout(layout); |
| 14 | + |
| 15 | + QSizePolicy policy = sizePolicy(); |
| 16 | + |
| 17 | + displayLabel = new QLabel(this); |
| 18 | + layout->addWidget(displayLabel, 0, 0, 1, 3); |
| 19 | + displayLabel->setAutoFillBackground(true); |
| 20 | + displayLabel->setBackgroundRole(QPalette::Base); |
| 21 | + displayLabel->setAlignment(Qt::AlignRight); |
| 22 | + displayLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); |
| 23 | + policy = displayLabel->sizePolicy(); |
| 24 | + policy.setVerticalPolicy(QSizePolicy::Fixed); |
| 25 | + displayLabel->setSizePolicy(policy); |
| 26 | + |
| 27 | + signalMapper = new QSignalMapper(this); |
| 28 | + QPushButton *button = new QPushButton(QString::number(0), this); |
| 29 | + QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map())); |
| 30 | + signalMapper->setMapping(button, 0); |
| 31 | + layout->addWidget(button, 4, 1); |
| 32 | + digitButtons.push_back(button); |
| 33 | + for (unsigned int i = 1; i < 10; ++i) { |
| 34 | + QPushButton *button = new QPushButton(QString::number(i), this); |
| 35 | + QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map())); |
| 36 | + signalMapper->setMapping(button, i); |
| 37 | + layout->addWidget(button, 1+(9-i)/3, (i-1)%3); |
| 38 | + digitButtons.push_back(button); |
| 39 | + } |
| 40 | + QObject::connect(signalMapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int))); |
| 41 | + |
| 42 | + clearButton = new QPushButton("C", this); |
| 43 | + layout->addWidget(clearButton, 1, 4); |
| 44 | + QObject::connect(clearButton, SIGNAL(clicked()), SLOT(clearButtonClicked())); |
| 45 | + |
| 46 | + additionButton = new QPushButton("+", this); |
| 47 | + layout->addWidget(additionButton, 2, 4); |
| 48 | + QObject::connect(additionButton, SIGNAL(clicked()), SLOT(addButtonClicked())); |
| 49 | + |
| 50 | + subtractionButton = new QPushButton("-", this); |
| 51 | + layout->addWidget(subtractionButton, 3, 4); |
| 52 | + QObject::connect(subtractionButton, SIGNAL(clicked()), SLOT(subtractButtonClicked())); |
| 53 | + |
| 54 | + calculateButton = new QPushButton("=", this); |
| 55 | + layout->addWidget(calculateButton, 4, 4); |
| 56 | + QObject::connect(calculateButton, SIGNAL(clicked()), SLOT(calculateButtonClicked())); |
60 | 57 | }
|
61 | 58 |
|
62 |
| -int CalculatorWidget::calculate(const QString& expression) |
63 |
| -{ |
64 |
| - int result = 0; |
65 |
| - char operation = '+'; |
66 |
| - QRegExp regexp("(\\d+)"); |
67 |
| - int pos = 0; |
68 |
| - while ((pos = regexp.indexIn(expression, pos)) != -1) { |
69 |
| - int value = regexp.cap(1).toInt(); |
70 |
| - switch (operation) { |
71 |
| - case '+': result += value; break; |
72 |
| - case '-': result -= value; break; |
73 |
| - } |
74 |
| - pos += regexp.matchedLength(); |
75 |
| - if (pos < expression.length()) { |
76 |
| - operation = expression.at(pos).toAscii(); |
77 |
| - } |
78 |
| - } |
79 |
| - return result; |
| 59 | +int CalculatorWidget::calculate(const QString& expression) { |
| 60 | + int result = 0; |
| 61 | + char operation = '+'; |
| 62 | + QRegExp regexp("(\\d+)"); |
| 63 | + int pos = 0; |
| 64 | + while ((pos = regexp.indexIn(expression, pos)) != -1) { |
| 65 | + int value = regexp.cap(1).toInt(); |
| 66 | + switch (operation) { |
| 67 | + case '+': result += value; break; |
| 68 | + case '-': result -= value; break; |
| 69 | + } |
| 70 | + pos += regexp.matchedLength(); |
| 71 | + if (pos < expression.length()) { |
| 72 | + operation = expression.at(pos).toAscii(); |
| 73 | + } |
| 74 | + } |
| 75 | + return result; |
80 | 76 | }
|
81 | 77 |
|
82 |
| -QString CalculatorWidget::display() |
83 |
| -{ |
84 |
| - return displayLabel->text(); |
| 78 | +QString CalculatorWidget::display() { |
| 79 | + return displayLabel->text(); |
85 | 80 | }
|
86 | 81 |
|
87 |
| -void CalculatorWidget::keyPressEvent(QKeyEvent* event) |
88 |
| -{ |
89 |
| - keyclickedButton = 0; |
90 |
| - int key = event->key(); |
91 |
| - if (key >= Qt::Key_0 && key <= Qt::Key_9) |
92 |
| - { |
93 |
| - keyclickedButton = digitButtons[key - Qt::Key_0]; |
94 |
| - } |
95 |
| - else |
96 |
| - { |
97 |
| - switch(key) |
98 |
| - { |
99 |
| - case Qt::Key_Plus: keyclickedButton = additionButton; break; |
100 |
| - case Qt::Key_Minus: keyclickedButton = subtractionButton; break; |
101 |
| - case Qt::Key_Return: |
102 |
| - case Qt::Key_Enter: |
103 |
| - case Qt::Key_Equal: keyclickedButton = calculateButton; break; |
104 |
| - case Qt::Key_Escape: keyclickedButton = clearButton; break; |
105 |
| - } |
106 |
| - } |
107 |
| - if (0 != keyclickedButton) |
108 |
| - { |
109 |
| - keyclickedButton->click(); |
110 |
| - keyclickedButton->setDown(true); |
111 |
| - } |
| 82 | +void CalculatorWidget::keyPressEvent(QKeyEvent *event) { |
| 83 | + keyclickedButton = 0; |
| 84 | + int key = event->key(); |
| 85 | + if (key >= Qt::Key_0 && key <= Qt::Key_9) { |
| 86 | + keyclickedButton = digitButtons[key - Qt::Key_0]; |
| 87 | + } |
| 88 | + else { |
| 89 | + switch(key) |
| 90 | + { |
| 91 | + case Qt::Key_Plus: keyclickedButton = additionButton; break; |
| 92 | + case Qt::Key_Minus: keyclickedButton = subtractionButton; break; |
| 93 | + case Qt::Key_Return: |
| 94 | + case Qt::Key_Enter: |
| 95 | + case Qt::Key_Equal: keyclickedButton = calculateButton; break; |
| 96 | + case Qt::Key_Escape: keyclickedButton = clearButton; break; |
| 97 | + } |
| 98 | + } |
| 99 | + if (0 != keyclickedButton) { |
| 100 | + keyclickedButton->click(); |
| 101 | + keyclickedButton->setDown(true); |
| 102 | + } |
112 | 103 | }
|
113 | 104 |
|
114 |
| -void CalculatorWidget::keyReleaseEvent(QKeyEvent* event) |
115 |
| -{ |
116 |
| - if (0 != keyclickedButton) |
117 |
| - { |
118 |
| - keyclickedButton->setDown(false); |
119 |
| - keyclickedButton = 0; |
120 |
| - } |
| 105 | +void CalculatorWidget::keyReleaseEvent(QKeyEvent *event) { |
| 106 | + if (0 != keyclickedButton) { |
| 107 | + keyclickedButton->setDown(false); |
| 108 | + keyclickedButton = 0; |
| 109 | + } |
121 | 110 | }
|
122 | 111 |
|
123 |
| -void CalculatorWidget::addButtonClicked() |
124 |
| -{ |
125 |
| - displayLabel->setText(displayLabel->text()+"+"); |
| 112 | +void CalculatorWidget::addButtonClicked() { |
| 113 | + displayLabel->setText(displayLabel->text()+"+"); |
126 | 114 | }
|
127 | 115 |
|
128 |
| -void CalculatorWidget::buttonClicked(int index) |
129 |
| -{ |
130 |
| - displayLabel->setText(displayLabel->text()+QString::number(index)); |
| 116 | +void CalculatorWidget::buttonClicked(int index) { |
| 117 | + displayLabel->setText(displayLabel->text()+QString::number(index)); |
131 | 118 | }
|
132 | 119 |
|
133 |
| -void CalculatorWidget::calculateButtonClicked() |
134 |
| -{ |
135 |
| - displayLabel->setText(QString::number(calculate(displayLabel->text()))); |
| 120 | +void CalculatorWidget::calculateButtonClicked() { |
| 121 | + displayLabel->setText(QString::number(calculate(displayLabel->text()))); |
136 | 122 | }
|
137 | 123 |
|
138 |
| -void CalculatorWidget::clearButtonClicked() |
139 |
| -{ |
140 |
| - displayLabel->setText(""); |
| 124 | +void CalculatorWidget::clearButtonClicked() { |
| 125 | + displayLabel->setText(""); |
141 | 126 | }
|
142 | 127 |
|
143 |
| -void CalculatorWidget::subtractButtonClicked() |
144 |
| -{ |
145 |
| - displayLabel->setText(displayLabel->text()+"-"); |
| 128 | +void CalculatorWidget::subtractButtonClicked() { |
| 129 | + displayLabel->setText(displayLabel->text()+"-"); |
146 | 130 | }
|
0 commit comments