|
| 1 | +#include "CalculatorWidget.h" |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <QGridLayout> |
| 5 | +#include <QKeyEvent> |
| 6 | +#include <QLabel> |
| 7 | +#include <QPushButton> |
| 8 | +#include <QSignalMapper> |
| 9 | + |
| 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())); |
| 57 | +} |
| 58 | + |
| 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; |
| 76 | +} |
| 77 | + |
| 78 | +QString CalculatorWidget::display() { |
| 79 | + return displayLabel->text(); |
| 80 | +} |
| 81 | + |
| 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 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void CalculatorWidget::keyReleaseEvent(QKeyEvent *event) { |
| 106 | + if (0 != keyclickedButton) { |
| 107 | + keyclickedButton->setDown(false); |
| 108 | + keyclickedButton = 0; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void CalculatorWidget::addButtonClicked() { |
| 113 | + displayLabel->setText(displayLabel->text()+"+"); |
| 114 | +} |
| 115 | + |
| 116 | +void CalculatorWidget::buttonClicked(int index) { |
| 117 | + displayLabel->setText(displayLabel->text()+QString::number(index)); |
| 118 | +} |
| 119 | + |
| 120 | +void CalculatorWidget::calculateButtonClicked() { |
| 121 | + displayLabel->setText(QString::number(calculate(displayLabel->text()))); |
| 122 | +} |
| 123 | + |
| 124 | +void CalculatorWidget::clearButtonClicked() { |
| 125 | + displayLabel->setText(""); |
| 126 | +} |
| 127 | + |
| 128 | +void CalculatorWidget::subtractButtonClicked() { |
| 129 | + displayLabel->setText(displayLabel->text()+"-"); |
| 130 | +} |
0 commit comments