📄 calcwindow.cpp
字号:
#include "calcwindow.h"
#include "aboutdialog.h"
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
CalculatorWindow::CalculatorWindow(QWidget* parent)
: QMainWindow(parent), __rep(Function::Degrees, CalculatorRep::Normal,
CalculatorRep::Dec)
{
setupUi(this);
initialize();
setupConnections();
}
void CalculatorWindow::initialize()
{
helpWindow = 0;
staDialog = 0;
setWindowFlags(Qt::WindowMinimizeButtonHint);
createActions();
createMenus();
move(0,0);
setFixedSize(frameSize()); // prevent the window from ever growing or shrinking
setWindowIcon(QIcon(":/images/calculator.png")); // put at last of initialization
}
void CalculatorWindow::setupConnections()
{
connect(num0Button, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(num1Button, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(num2Button, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(num3Button, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(num4Button, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(num5Button, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(num6Button, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(num7Button, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(num8Button, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(num9Button, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(answerButton, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(piButton, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(decimalButton, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(expButton, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(modularButton, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(plusButton, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(subtractButton, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(multiplyButton, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(divideButton, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(negateButton, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(leftParenthesisButton, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(rightParenthesisButton, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(logButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(lnButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(exponentButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(powerButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(squareButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(sqrtButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(factorialButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(reciprocalButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(sinButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(cosButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(tanButton, SIGNAL(clicked()), this, SLOT(functionClicked()));
connect(equalButton, SIGNAL(clicked()), this, SLOT(equalClicked()));
connect(clearButton, SIGNAL(clicked()), this, SLOT(clearClicked()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked()));
connect(replayButton, SIGNAL(clicked()), this, SLOT(replayClicked()));
connect(inputLineEdit, SIGNAL(textEdited(const QString&)),
this, SLOT(inputEdited(const QString&)));
connect(staClearButton, SIGNAL(clicked()), this, SLOT(statisticClicked()));
connect(staStoreButton, SIGNAL(clicked()), this, SLOT(statisticClicked()));
connect(staNumButton, SIGNAL(clicked()), this, SLOT(statisticClicked()));
connect(staSumButton, SIGNAL(clicked()), this, SLOT(statisticClicked()));
connect(staAverageButton, SIGNAL(clicked()), this, SLOT(statisticClicked()));
connect(staStdDiffButton, SIGNAL(clicked()), this, SLOT(statisticClicked()));
connect(staMinButton, SIGNAL(clicked()), this, SLOT(statisticClicked()));
connect(staMaxButton, SIGNAL(clicked()), this, SLOT(statisticClicked()));
connect(memPlusButton, SIGNAL(clicked()), this, SLOT(memoryClicked()));
connect(memSubtractButton, SIGNAL(clicked()), this, SLOT(memoryClicked()));
connect(memRecallButton, SIGNAL(clicked()), this, SLOT(memoryClicked()));
connect(memClearButton, SIGNAL(clicked()), this, SLOT(memoryClicked()));
connect(degreeRadioButton, SIGNAL(clicked()), this, SLOT(stateClicked()));
connect(radianRadioButton, SIGNAL(clicked()), this, SLOT(stateClicked()));
connect(baseOctRadioButton, SIGNAL(clicked()), this, SLOT(baseClicked()));
connect(baseDecRadioButton, SIGNAL(clicked()), this, SLOT(baseClicked()));
connect(baseHexRadioButton, SIGNAL(clicked()), this, SLOT(baseClicked()));
connect(normRadioButton, SIGNAL(clicked()), this, SLOT(formatClicked()));
connect(sciRadioButton, SIGNAL(clicked()), this, SLOT(formatClicked()));
connect(fixRadioButton, SIGNAL(clicked()), this, SLOT(formatClicked()));
connect(arcCheckBox, SIGNAL(stateChanged(int)), this, SLOT(triangleToggled()));
connect(hyperCheckBox, SIGNAL(stateChanged(int)), this, SLOT(triangleToggled()));
connect(inputLineEdit, SIGNAL(cursorPositionChanged(int, int)),
this, SLOT(inputModify()));
connect(inputLineEdit, SIGNAL(selectionChanged()), this, SLOT(inputModify()));
connect(staButton, SIGNAL(toggled(bool)), this, SLOT(toggleStaDialog(bool)));
}
void CalculatorWindow::input(const QString& str)
{
if (inputLineEdit->text().isEmpty())
if (str == "+" || str == "-" || str == "*" || str == "/") {
inputLineEdit->setText(QString("Ans") + str);
return;
}
if (__rep.newCalculation()) {
if (str == "+" || str == "-" || str == "*" || str == "/")
inputLineEdit->setText(QString("Ans") + str);
else
inputLineEdit->setText(str);
__rep.setNewCalculation(false);
statusLabel->setText(QString("<font color=green>") +
tr("waiting for input") + QString("</font>"));
}
else
inputLineEdit->insert(str);
}
void CalculatorWindow::digitClicked()
{
QPushButton* clickedButton = qobject_cast<QPushButton*>(sender());
if (clickedButton == answerButton)
input("Ans");
else if (clickedButton == piButton)
input("PI");
else
input(clickedButton->text());
}
void CalculatorWindow::operatorClicked()
{
QPushButton* clickedButton = qobject_cast<QPushButton*>(sender());
if (clickedButton == expButton)
input("E");
else if (clickedButton == modularButton)
input("%");
else if (clickedButton == plusButton)
input("+");
else if (clickedButton == subtractButton || clickedButton == negateButton)
input("-");
else if (clickedButton == multiplyButton)
input("*");
else if (clickedButton == divideButton)
input("/");
else
input(clickedButton->text());
}
void CalculatorWindow::functionClicked()
{
QPushButton* clickedButton = qobject_cast<QPushButton*>(sender());
if (clickedButton == exponentButton)
input("exp(");
else if (clickedButton == powerButton)
input("^");
else if (clickedButton == squareButton)
input("^2");
else if (clickedButton == factorialButton)
input("fct(");
else if (clickedButton == reciprocalButton)
input("^-1");
else {
input(clickedButton->text());
input("(");
}
}
void CalculatorWindow::equalClicked()
{
if (inputLineEdit->text().isEmpty())
return;
try {
__rep.setExpression(inputLineEdit->text());
__rep.calculate();
postCalculate();
outputLineEdit->setText(__rep.resultString());
}
catch (Exception& e) {
statusLabel->setText(QString("<font color=red>") +
tr(e.debugPrint()) + QString("</font>"));
}
__rep.setNewCalculation(true);
}
void CalculatorWindow::postCalculate()
{
if (1e29 <= __rep.result()) {
if (fixRadioButton->isChecked()) {
normRadioButton->setChecked(true);
__rep.setOutputFormat(CalculatorRep::Normal);
}
fixRadioButton->setEnabled(false);
}
else
fixRadioButton->setEnabled(true);
}
void CalculatorWindow::replayClicked()
{
inputLineEdit->setText(__rep.expression());
__rep.setNewCalculation(false);
}
void CalculatorWindow::clearClicked()
{
inputLineEdit->setText("");
statusLabel->setText(QString("<font color=green>") +
tr("waiting for input") + QString("</font>"));
}
void CalculatorWindow::deleteClicked()
{
inputLineEdit->backspace();
}
void CalculatorWindow::inputEdited(const QString& text)
{
if (__rep.newCalculation() && __rep.expression().size() <= text.size()) {
clearClicked();
input(QString(text[text.size() - 1]));
__rep.setNewCalculation(false);
statusLabel->setText(QString("<font color=green>") +
tr("waiting for input") + QString("</font>"));
}
}
void CalculatorWindow::inputModify()
{
if (__rep.newCalculation()) {
__rep.setNewCalculation(false);
statusLabel->setText(QString("<font color=green>") +
tr("waiting for input") + QString("</font>"));
}
}
void CalculatorWindow::stateClicked()
{
QRadioButton* clickedButton = qobject_cast<QRadioButton*>(sender());
if (clickedButton == degreeRadioButton)
__rep.setRDState(Function::Degrees);
else if (clickedButton == radianRadioButton)
__rep.setRDState(Function::Radians);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -