341 lines
9.4 KiB
C++
341 lines
9.4 KiB
C++
#include "CalculatorPlugin.h"
|
|
#include "button.h"
|
|
#include <QVBoxLayout>
|
|
#include <QWidget>
|
|
|
|
|
|
QString CalculatorPlugin::pname()
|
|
{
|
|
return "Calculator Plugin";
|
|
}
|
|
|
|
QString CalculatorPlugin::pdesc()
|
|
{
|
|
return "Simple Calculator Plugin.";
|
|
}
|
|
|
|
QWidget* CalculatorPlugin::pcontent()
|
|
{
|
|
QWidget *test = new QWidget();
|
|
display = new QLineEdit("0");
|
|
//! [1] //! [2]
|
|
display->setReadOnly(true);
|
|
display->setAlignment(Qt::AlignRight);
|
|
display->setMaxLength(15);
|
|
|
|
QFont font = display->font();
|
|
font.setPointSize(font.pointSize() + 8);
|
|
display->setFont(font);
|
|
//! [2]
|
|
|
|
//! [4]
|
|
for (int i = 0; i < NumDigitButtons; ++i)
|
|
digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));
|
|
|
|
Button *pointButton = createButton(tr("."), SLOT(pointClicked()));
|
|
Button *changeSignButton = createButton(tr("\302\261"), SLOT(changeSignClicked()));
|
|
|
|
Button *backspaceButton = createButton(tr("Backspace"), SLOT(backspaceClicked()));
|
|
Button *clearButton = createButton(tr("Clear"), SLOT(clear()));
|
|
Button *clearAllButton = createButton(tr("Clear All"), SLOT(clearAll()));
|
|
|
|
Button *clearMemoryButton = createButton(tr("MC"), SLOT(clearMemory()));
|
|
Button *readMemoryButton = createButton(tr("MR"), SLOT(readMemory()));
|
|
Button *setMemoryButton = createButton(tr("MS"), SLOT(setMemory()));
|
|
Button *addToMemoryButton = createButton(tr("M+"), SLOT(addToMemory()));
|
|
|
|
Button *divisionButton = createButton(tr("\303\267"), SLOT(multiplicativeOperatorClicked()));
|
|
Button *timesButton = createButton(tr("\303\227"), SLOT(multiplicativeOperatorClicked()));
|
|
Button *minusButton = createButton(tr("-"), SLOT(additiveOperatorClicked()));
|
|
Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
|
|
|
|
Button *squareRootButton = createButton(tr("Sqrt"), SLOT(unaryOperatorClicked()));
|
|
Button *powerButton = createButton(tr("x\302\262"), SLOT(unaryOperatorClicked()));
|
|
Button *reciprocalButton = createButton(tr("1/x"), SLOT(unaryOperatorClicked()));
|
|
Button *equalButton = createButton(tr("="), SLOT(equalClicked()));
|
|
//! [4]
|
|
|
|
//! [5]
|
|
QGridLayout *mainLayout = new QGridLayout;
|
|
//! [5] //! [6]
|
|
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
|
|
mainLayout->addWidget(display, 0, 0, 1, 6);
|
|
mainLayout->addWidget(backspaceButton, 1, 0, 1, 2);
|
|
mainLayout->addWidget(clearButton, 1, 2, 1, 2);
|
|
mainLayout->addWidget(clearAllButton, 1, 4, 1, 2);
|
|
|
|
mainLayout->addWidget(clearMemoryButton, 2, 0);
|
|
mainLayout->addWidget(readMemoryButton, 3, 0);
|
|
mainLayout->addWidget(setMemoryButton, 4, 0);
|
|
mainLayout->addWidget(addToMemoryButton, 5, 0);
|
|
|
|
for (int i = 1; i < NumDigitButtons; ++i) {
|
|
int row = ((9 - i) / 3) + 2;
|
|
int column = ((i - 1) % 3) + 1;
|
|
mainLayout->addWidget(digitButtons[i], row, column);
|
|
}
|
|
|
|
mainLayout->addWidget(digitButtons[0], 5, 1);
|
|
mainLayout->addWidget(pointButton, 5, 2);
|
|
mainLayout->addWidget(changeSignButton, 5, 3);
|
|
|
|
mainLayout->addWidget(divisionButton, 2, 4);
|
|
mainLayout->addWidget(timesButton, 3, 4);
|
|
mainLayout->addWidget(minusButton, 4, 4);
|
|
mainLayout->addWidget(plusButton, 5, 4);
|
|
|
|
mainLayout->addWidget(squareRootButton, 2, 5);
|
|
mainLayout->addWidget(powerButton, 3, 5);
|
|
mainLayout->addWidget(reciprocalButton, 4, 5);
|
|
mainLayout->addWidget(equalButton, 5, 5);
|
|
test->setLayout(mainLayout);
|
|
|
|
test->setWindowTitle(tr("Calculator Plugin"));
|
|
QIcon icon = QIcon(":/images/monkey.png");
|
|
test->setWindowIcon(icon);
|
|
return test;
|
|
}
|
|
|
|
Button *CalculatorPlugin::createButton(const QString &text, const char *member)
|
|
{
|
|
Button *button = new Button(text);
|
|
connect(button, SIGNAL(clicked()), this, member);
|
|
return button;
|
|
}
|
|
|
|
void CalculatorPlugin::digitClicked()
|
|
{
|
|
Button *clickedButton = qobject_cast<Button *>(sender());
|
|
int digitValue = clickedButton->text().toInt();
|
|
if (display->text() == "0" && digitValue == 0.0)
|
|
return;
|
|
|
|
if (waitingForOperand) {
|
|
display->clear();
|
|
waitingForOperand = false;
|
|
}
|
|
display->setText(display->text() + QString::number(digitValue));
|
|
}
|
|
void CalculatorPlugin::clear()
|
|
{
|
|
if (waitingForOperand)
|
|
return;
|
|
|
|
display->setText("0");
|
|
waitingForOperand = true;
|
|
}
|
|
|
|
void CalculatorPlugin::clearAll()
|
|
{
|
|
sumSoFar = 0.0;
|
|
factorSoFar = 0.0;
|
|
pendingAdditiveOperator.clear();
|
|
pendingMultiplicativeOperator.clear();
|
|
display->setText("0");
|
|
waitingForOperand = true;
|
|
}
|
|
|
|
void CalculatorPlugin::backspaceClicked()
|
|
{
|
|
if (waitingForOperand)
|
|
return;
|
|
|
|
QString text = display->text();
|
|
text.chop(1);
|
|
if (text.isEmpty()) {
|
|
text = "0";
|
|
waitingForOperand = true;
|
|
}
|
|
display->setText(text);
|
|
}
|
|
|
|
void CalculatorPlugin::pointClicked()
|
|
{
|
|
if (waitingForOperand)
|
|
display->setText("0");
|
|
if (!display->text().contains('.'))
|
|
display->setText(display->text() + tr("."));
|
|
waitingForOperand = false;
|
|
}
|
|
|
|
void CalculatorPlugin::changeSignClicked()
|
|
{
|
|
QString text = display->text();
|
|
double value = text.toDouble();
|
|
|
|
if (value > 0.0) {
|
|
text.prepend(tr("-"));
|
|
} else if (value < 0.0) {
|
|
text.remove(0, 1);
|
|
}
|
|
display->setText(text);
|
|
}
|
|
|
|
void CalculatorPlugin::equalClicked()
|
|
{
|
|
double operand = display->text().toDouble();
|
|
|
|
if (!pendingMultiplicativeOperator.isEmpty()) {
|
|
if (!calculate(operand, pendingMultiplicativeOperator)) {
|
|
abortOperation();
|
|
return;
|
|
}
|
|
operand = factorSoFar;
|
|
factorSoFar = 0.0;
|
|
pendingMultiplicativeOperator.clear();
|
|
}
|
|
if (!pendingAdditiveOperator.isEmpty()) {
|
|
if (!calculate(operand, pendingAdditiveOperator)) {
|
|
abortOperation();
|
|
return;
|
|
}
|
|
pendingAdditiveOperator.clear();
|
|
} else {
|
|
sumSoFar = operand;
|
|
}
|
|
|
|
display->setText(QString::number(sumSoFar));
|
|
sumSoFar = 0.0;
|
|
waitingForOperand = true;
|
|
}
|
|
|
|
bool CalculatorPlugin::calculate(double rightOperand, const QString &pendingOperator)
|
|
{
|
|
if (pendingOperator == tr("+")) {
|
|
sumSoFar += rightOperand;
|
|
} else if (pendingOperator == tr("-")) {
|
|
sumSoFar -= rightOperand;
|
|
} else if (pendingOperator == tr("\303\227")) {
|
|
factorSoFar *= rightOperand;
|
|
} else if (pendingOperator == tr("\303\267")) {
|
|
if (rightOperand == 0.0)
|
|
return false;
|
|
factorSoFar /= rightOperand;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void CalculatorPlugin::additiveOperatorClicked()
|
|
//! [10] //! [11]
|
|
{
|
|
Button *clickedButton = qobject_cast<Button *>(sender());
|
|
if (!clickedButton)
|
|
return;
|
|
QString clickedOperator = clickedButton->text();
|
|
double operand = display->text().toDouble();
|
|
|
|
//! [11] //! [12]
|
|
if (!pendingMultiplicativeOperator.isEmpty()) {
|
|
//! [12] //! [13]
|
|
if (!calculate(operand, pendingMultiplicativeOperator)) {
|
|
abortOperation();
|
|
return;
|
|
}
|
|
display->setText(QString::number(factorSoFar));
|
|
operand = factorSoFar;
|
|
factorSoFar = 0.0;
|
|
pendingMultiplicativeOperator.clear();
|
|
}
|
|
|
|
//! [13] //! [14]
|
|
if (!pendingAdditiveOperator.isEmpty()) {
|
|
//! [14] //! [15]
|
|
if (!calculate(operand, pendingAdditiveOperator)) {
|
|
abortOperation();
|
|
return;
|
|
}
|
|
display->setText(QString::number(sumSoFar));
|
|
} else {
|
|
sumSoFar = operand;
|
|
}
|
|
|
|
//! [15] //! [16]
|
|
pendingAdditiveOperator = clickedOperator;
|
|
//! [16] //! [17]
|
|
waitingForOperand = true;
|
|
}
|
|
|
|
void CalculatorPlugin::unaryOperatorClicked()
|
|
//! [8] //! [9]
|
|
{
|
|
Button *clickedButton = qobject_cast<Button *>(sender());
|
|
QString clickedOperator = clickedButton->text();
|
|
double operand = display->text().toDouble();
|
|
double result = 0.0;
|
|
|
|
if (clickedOperator == tr("Sqrt")) {
|
|
if (operand < 0.0) {
|
|
abortOperation();
|
|
return;
|
|
}
|
|
result = std::sqrt(operand);
|
|
} else if (clickedOperator == tr("x\302\262")) {
|
|
result = std::pow(operand, 2.0);
|
|
} else if (clickedOperator == tr("1/x")) {
|
|
if (operand == 0.0) {
|
|
abortOperation();
|
|
return;
|
|
}
|
|
result = 1.0 / operand;
|
|
}
|
|
display->setText(QString::number(result));
|
|
waitingForOperand = true;
|
|
}
|
|
|
|
void CalculatorPlugin::abortOperation()
|
|
{
|
|
clearAll();
|
|
display->setText(tr("####"));
|
|
}
|
|
|
|
void CalculatorPlugin::clearMemory()
|
|
{
|
|
sumInMemory = 0.0;
|
|
}
|
|
|
|
void CalculatorPlugin::readMemory()
|
|
{
|
|
display->setText(QString::number(sumInMemory));
|
|
waitingForOperand = true;
|
|
}
|
|
|
|
void CalculatorPlugin::setMemory()
|
|
{
|
|
equalClicked();
|
|
sumInMemory = display->text().toDouble();
|
|
}
|
|
|
|
void CalculatorPlugin::addToMemory()
|
|
{
|
|
equalClicked();
|
|
sumInMemory += display->text().toDouble();
|
|
}
|
|
|
|
void CalculatorPlugin::multiplicativeOperatorClicked()
|
|
{
|
|
Button *clickedButton = qobject_cast<Button *>(sender());
|
|
if (!clickedButton)
|
|
return;
|
|
QString clickedOperator = clickedButton->text();
|
|
double operand = display->text().toDouble();
|
|
|
|
if (!pendingMultiplicativeOperator.isEmpty()) {
|
|
if (!calculate(operand, pendingMultiplicativeOperator)) {
|
|
abortOperation();
|
|
return;
|
|
}
|
|
display->setText(QString::number(factorSoFar));
|
|
} else {
|
|
factorSoFar = operand;
|
|
}
|
|
|
|
pendingMultiplicativeOperator = clickedOperator;
|
|
waitingForOperand = true;
|
|
}
|
|
|
|
void CalculatorPlugin::connectToHost(QObject* host)
|
|
{
|
|
Q_UNUSED(host);
|
|
// Calculator plugin doesn't need host signals
|
|
}
|