Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加了scientific mode #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion deepin-calculator.pro
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ HEADERS += mainwindow.h \
widgets/iconbutton.h \
widgets/inputedit.h \
widgets/scientifickeypad.h \
widgets/textbutton.h
widgets/textbutton.h \
widgets/buttons.h
SOURCES += main.cpp \
mainwindow.cpp \
dsettings.cpp \
Expand Down
2 changes: 2 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <QLabel>
#include <QDebug>

#define ENABLE_SCIENTIFIC; //open scientific mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 define 看上去并没有用到?


MainWindow::MainWindow(QWidget *parent)
: DMainWindow(parent),
m_settings(DSettings::instance()),
Expand Down
106 changes: 64 additions & 42 deletions modules/basicmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ BasicModule::BasicModule(QWidget *parent)
: QWidget(parent),
m_expressionBar(new ExpressionBar),
m_basicKeypad(new BasicKeypad),
m_scientificKyepad(new ScientificKeypad),
m_keypadLayout(new QStackedLayout)
{
m_keypadLayout->addWidget(m_basicKeypad);
m_keypadLayout->addWidget(m_scientificKyepad);

QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(m_expressionBar);
Expand All @@ -50,6 +52,7 @@ BasicModule::BasicModule(QWidget *parent)
connect(m_expressionBar, &ExpressionBar::keyPress, this, &BasicModule::handleEditKeyPress);
connect(m_expressionBar, &ExpressionBar::clearStateChanged, this, &BasicModule::handleClearStateChanged);
connect(m_basicKeypad, &BasicKeypad::buttonPressed, this, &BasicModule::handleKeypadButtonPress);
connect(m_scientificKyepad, &ScientificKeypad::buttonPressed, this, &BasicModule::handleKeypadButtonPress);
}

BasicModule::~BasicModule()
Expand All @@ -58,10 +61,12 @@ BasicModule::~BasicModule()

void BasicModule::switchToSimpleKeypad()
{
m_keypadLayout->setCurrentIndex(0);
}

void BasicModule::switchToScientificKeypad()
{
m_keypadLayout->setCurrentIndex(1);
}

void BasicModule::initTheme()
Expand All @@ -87,74 +92,74 @@ void BasicModule::handleEditKeyPress(QKeyEvent *e)
switch (e->key()) {
case Qt::Key_0:
m_expressionBar->enterNumberEvent("");
m_basicKeypad->animate(BasicKeypad::Key_0);
m_basicKeypad->animate(Buttons::Key_0);
break;
case Qt::Key_1:
m_expressionBar->enterNumberEvent("");
m_basicKeypad->animate(BasicKeypad::Key_1);
m_basicKeypad->animate(Buttons::Key_1);
break;
case Qt::Key_2:
case Qt::Key_2: case Qt::Key_Launch2:
m_expressionBar->enterNumberEvent("");
m_basicKeypad->animate(BasicKeypad::Key_2);
m_basicKeypad->animate(Buttons::Key_2);
break;
case Qt::Key_3:
m_expressionBar->enterNumberEvent("");
m_basicKeypad->animate(BasicKeypad::Key_3);
m_basicKeypad->animate(Buttons::Key_3);
break;
case Qt::Key_4:
m_expressionBar->enterNumberEvent("");
m_basicKeypad->animate(BasicKeypad::Key_4);
m_basicKeypad->animate(Buttons::Key_4);
break;
case Qt::Key_5:
m_expressionBar->enterNumberEvent("");
m_basicKeypad->animate(BasicKeypad::Key_5);
m_basicKeypad->animate(Buttons::Key_5);
break;
case Qt::Key_6:
m_expressionBar->enterNumberEvent("");
m_basicKeypad->animate(BasicKeypad::Key_6);
m_basicKeypad->animate(Buttons::Key_6);
break;
case Qt::Key_7:
m_expressionBar->enterNumberEvent("");
m_basicKeypad->animate(BasicKeypad::Key_7);
m_basicKeypad->animate(Buttons::Key_7);
break;
case Qt::Key_8:
m_expressionBar->enterNumberEvent("");
m_basicKeypad->animate(BasicKeypad::Key_8);
m_basicKeypad->animate(Buttons::Key_8);
break;
case Qt::Key_9:
m_expressionBar->enterNumberEvent("");
m_basicKeypad->animate(BasicKeypad::Key_9);
m_basicKeypad->animate(Buttons::Key_9);
break;
case Qt::Key_Plus:
m_basicKeypad->animate(BasicKeypad::Key_Plus);
m_basicKeypad->animate(Buttons::Key_Plus);
break;
case Qt::Key_Minus: case Qt::Key_Underscore:
m_basicKeypad->animate(BasicKeypad::Key_Min);
m_basicKeypad->animate(Buttons::Key_Min);
break;
case Qt::Key_Asterisk: case Qt::Key_X:
m_basicKeypad->animate(BasicKeypad::Key_Mult);
m_basicKeypad->animate(Buttons::Key_Mult);
break;
case Qt::Key_Slash:
m_basicKeypad->animate(BasicKeypad::Key_Div);
m_basicKeypad->animate(Buttons::Key_Div);
break;
case Qt::Key_Enter: case Qt::Key_Return: case Qt::Key_Equal:
(static_cast<QPushButton *>(m_basicKeypad->button(BasicKeypad::Key_Equals)))->animateClick();
(static_cast<QPushButton *>(m_basicKeypad->button(Buttons::Key_Equals)))->animateClick();
break;
case Qt::Key_Backspace:
m_basicKeypad->animate(BasicKeypad::Key_Backspace);
m_basicKeypad->animate(Buttons::Key_Backspace);
break;
case Qt::Key_Period:
(static_cast<QPushButton *>(m_basicKeypad->button(BasicKeypad::Key_Point)))->animateClick();
(static_cast<QPushButton *>(m_basicKeypad->button(Buttons::Key_Point)))->animateClick();
break;
case Qt::Key_Escape:
(static_cast<QPushButton *>(m_basicKeypad->button(BasicKeypad::Key_Clear)))->animateClick();
(static_cast<QPushButton *>(m_basicKeypad->button(Buttons::Key_Clear)))->animateClick();
break;
case Qt::Key_ParenLeft: case Qt::Key_ParenRight:
m_expressionBar->setContinue(true);
m_basicKeypad->animate(BasicKeypad::Key_Brackets);
m_basicKeypad->animate(Buttons::Key_Brackets);
break;
case Qt::Key_Percent:
m_basicKeypad->animate(BasicKeypad::Key_Percent);
m_basicKeypad->animate(Buttons::Key_Percent);
break;
case Qt::Key_C:
if (isPressCtrl) {
Expand All @@ -167,32 +172,49 @@ void BasicModule::handleEditKeyPress(QKeyEvent *e)
void BasicModule::handleKeypadButtonPress(int key)
{
switch (key) {
case BasicKeypad::Key_0: m_expressionBar->enterNumberEvent("0"); break;
case BasicKeypad::Key_1: m_expressionBar->enterNumberEvent("1"); break;
case BasicKeypad::Key_2: m_expressionBar->enterNumberEvent("2"); break;
case BasicKeypad::Key_3: m_expressionBar->enterNumberEvent("3"); break;
case BasicKeypad::Key_4: m_expressionBar->enterNumberEvent("4"); break;
case BasicKeypad::Key_5: m_expressionBar->enterNumberEvent("5"); break;
case BasicKeypad::Key_6: m_expressionBar->enterNumberEvent("6"); break;
case BasicKeypad::Key_7: m_expressionBar->enterNumberEvent("7"); break;
case BasicKeypad::Key_8: m_expressionBar->enterNumberEvent("8"); break;
case BasicKeypad::Key_9: m_expressionBar->enterNumberEvent("9"); break;
case BasicKeypad::Key_Plus: m_expressionBar->enterSymbolEvent("+"); break;
case BasicKeypad::Key_Min: m_expressionBar->enterSymbolEvent("-"); break;
case BasicKeypad::Key_Mult: m_expressionBar->enterSymbolEvent("*"); break;
case BasicKeypad::Key_Div: m_expressionBar->enterSymbolEvent("/"); break;
case BasicKeypad::Key_Percent: m_expressionBar->enterSymbolEvent("%"); break;
case BasicKeypad::Key_Equals: m_expressionBar->enterEqualEvent(); break;
case BasicKeypad::Key_Clear: m_expressionBar->enterClearEvent(); break;
case BasicKeypad::Key_Backspace: m_expressionBar->enterBackspaceEvent(); break;
case BasicKeypad::Key_Point: m_expressionBar->enterPointEvent(); break;
case BasicKeypad::Key_Brackets: m_expressionBar->enterBracketsEvent(); break;
case Buttons::Key_0: m_expressionBar->enterNumberEvent("0"); break;
case Buttons::Key_1: m_expressionBar->enterNumberEvent("1"); break;
case Buttons::Key_2: m_expressionBar->enterNumberEvent("2"); break;
case Buttons::Key_3: m_expressionBar->enterNumberEvent("3"); break;
case Buttons::Key_4: m_expressionBar->enterNumberEvent("4"); break;
case Buttons::Key_5: m_expressionBar->enterNumberEvent("5"); break;
case Buttons::Key_6: m_expressionBar->enterNumberEvent("6"); break;
case Buttons::Key_7: m_expressionBar->enterNumberEvent("7"); break;
case Buttons::Key_8: m_expressionBar->enterNumberEvent("8"); break;
case Buttons::Key_9: m_expressionBar->enterNumberEvent("9"); break;
case Buttons::Key_Plus: m_expressionBar->enterSymbolEvent("+"); break;
case Buttons::Key_Min: m_expressionBar->enterSymbolEvent("-"); break;
case Buttons::Key_Mult: m_expressionBar->enterSymbolEvent("*"); break;
case Buttons::Key_Div: m_expressionBar->enterSymbolEvent("/"); break;
case Buttons::Key_Percent: m_expressionBar->enterSymbolEvent("%"); break;
case Buttons::Key_Equals: m_expressionBar->enterEqualEvent(); break;
case Buttons::Key_Clear: m_expressionBar->enterClearEvent(); break;
case Buttons::Key_Backspace: m_expressionBar->enterBackspaceEvent(); break;
case Buttons::Key_Point: m_expressionBar->enterPointEvent(); break;
case Buttons::Key_Brackets: m_expressionBar->enterBracketsEvent(); break;

case Buttons::Key_arccos: m_expressionBar->enterSymbolEvent("arccos("); break;
case Buttons::Key_arcsin: m_expressionBar->enterSymbolEvent("arcsin("); break;
case Buttons::Key_arctan: m_expressionBar->enterSymbolEvent("arctan("); break;
case Buttons::Key_sin: m_expressionBar->enterSymbolEvent("sin("); break;
case Buttons::Key_cos: m_expressionBar->enterSymbolEvent("cos("); break;
case Buttons::Key_tan: m_expressionBar->enterSymbolEvent("tan("); break;
case Buttons::Key_Sqrt: m_expressionBar->enterSymbolEvent("√("); break;
case Buttons::Key_lg: m_expressionBar->enterSymbolEvent("lg("); break;
case Buttons::Key_ln: m_expressionBar->enterSymbolEvent("ln("); break;
case Buttons::Key_pi: m_expressionBar->enterNumberEvent("π"); break;
case Buttons::Key_e: m_expressionBar->enterNumberEvent("e"); break;
case Buttons::Key_Factorial: m_expressionBar->enterSymbolEvent("!"); break;
case Buttons::Key_LBracket: m_expressionBar->enterSymbolEvent("("); break;
case Buttons::Key_RBracket: m_expressionBar->enterSymbolEvent(")"); break;
case Buttons::Key_Power: m_expressionBar->enterSymbolEvent("^"); break;
case Buttons::Key_Square: m_expressionBar->enterNumberEvent("^2"); break;
}
}

void BasicModule::handleClearStateChanged(bool isAllClear)
{
TextButton *btn = static_cast<TextButton *>(m_basicKeypad->button(BasicKeypad::Key_Clear));
TextButton *btn = static_cast<TextButton *>(m_basicKeypad->button(Buttons::Key_Clear));

if (isAllClear) {
btn->setText("AC");
Expand Down
2 changes: 2 additions & 0 deletions modules/basicmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "../widgets/textbutton.h"
#include "../widgets/iconbutton.h"
#include "../widgets/basickeypad.h"
#include "../widgets/scientifickeypad.h"
#include "expressionbar.h"

class BasicModule : public QWidget
Expand All @@ -52,6 +53,7 @@ class BasicModule : public QWidget
private:
ExpressionBar *m_expressionBar;
BasicKeypad *m_basicKeypad;
ScientificKeypad *m_scientificKyepad;
QStackedLayout *m_keypadLayout;
QColor m_expBarColor;
QColor m_expBarSepColor;
Expand Down
4 changes: 3 additions & 1 deletion modules/expressionbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void ExpressionBar::enterNumberEvent(const QString &text)
void ExpressionBar::enterSymbolEvent(const QString &text)
{
m_inputEdit->insert(text);

m_isContinue = true;
}

Expand Down Expand Up @@ -208,5 +209,6 @@ QString ExpressionBar::formatExpression(const QString &text)
{
return QString(text).replace(QString::fromUtf8("+"), "+").replace(QString::fromUtf8("-"), "-")
.replace(QString::fromUtf8("×"), "*").replace(QString::fromUtf8("÷"), "/")
.replace(QString::fromUtf8(","), "");
.replace(QString::fromUtf8(","), "").replace(QString::fromUtf8("√"), "sqrt")
.replace(QString::fromUtf8("π"), "arccos(-1)");
}
40 changes: 7 additions & 33 deletions widgets/basickeypad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,18 @@

DWIDGET_USE_NAMESPACE

const BasicKeypad::KeyDescription BasicKeypad::keyDescriptions[] = {
{ "C", Key_Clear, 1, 0 },
{ "%", Key_Percent, 1, 1 },
{ "", Key_Backspace, 1, 2 },
{ "", Key_Div, 1, 3 },

{ "7", Key_7, 2, 0 },
{ "8", Key_8, 2, 1 },
{ "9", Key_9, 2, 2 },
{ "", Key_Mult, 2, 3 },

{ "4", Key_4, 3, 0 },
{ "5", Key_5, 3, 1 },
{ "6", Key_6, 3, 2 },
{ "", Key_Min, 3, 3 },

{ "1", Key_1, 4, 0 },
{ "2", Key_2, 4, 1 },
{ "3", Key_3, 4, 2 },
{ "", Key_Plus, 4, 3 },

{ "0", Key_0, 5, 0 },
{ ".", Key_Point, 5, 1 },
{ "( )", Key_Brackets, 5, 2 },
{ "=", Key_Equals, 5, 3 }
};

static QPushButton* createSpecialKeyButton(BasicKeypad::Buttons key) {
static QPushButton* createSpecialKeyButton(Buttons key) {
IconButton *button = new IconButton(10, 13);

if (key == BasicKeypad::Key_Div) {
if (key == Buttons::Key_Div) {
button->setIcon(":/images/div_normal.svg");
} else if (key == BasicKeypad::Key_Mult) {
} else if (key == Buttons::Key_Mult) {
button->setIcon(":/images/mult_normal.svg");
} else if (key == BasicKeypad::Key_Min) {
} else if (key == Buttons::Key_Min) {
button->setIcon(":/images/min_normal.svg");
} else if (key == BasicKeypad::Key_Plus) {
} else if (key == Buttons::Key_Plus) {
button->setIcon(":/images/plus_normal.svg");
} else if (key == BasicKeypad::Key_Backspace) {
} else if (key == Buttons::Key_Backspace) {
button->setIconStateSizes(23, 26);
button->setIcon(QString(":/images/delete_%1_normal.svg")
.arg(DThemeManager::instance()->theme()));
Expand Down Expand Up @@ -93,6 +66,7 @@ QPushButton* BasicKeypad::button(Buttons key)
return m_keys.value(key).first;
}

//键盘按键时显示按下效果
void BasicKeypad::animate(Buttons key)
{
TextButton *btn = static_cast<TextButton *>(button(key));
Expand Down
47 changes: 31 additions & 16 deletions widgets/basickeypad.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,60 @@
#include <QSignalMapper>
#include "textbutton.h"
#include "iconbutton.h"
#include "buttons.h"

class BasicKeypad : public QWidget
{
Q_OBJECT

public:
enum Buttons {
Key_Clear, Key_Percent, Key_Backspace, Key_Div,
Key_7, Key_8, Key_9, Key_Mult,
Key_4, Key_5, Key_6, Key_Min,
Key_1, Key_2, Key_3, Key_Plus,
Key_0, Key_Point, Key_Brackets, Key_Equals
};

BasicKeypad(QWidget *parent = nullptr);
~BasicKeypad();

QPushButton *button(Buttons key);
void animate(Buttons key);

static const struct KeyDescription {
QString text;
Buttons button;
int row;
int column;
} keyDescriptions[];

signals:
void buttonPressed(int);

private:
protected:
void initButtons();
void initUI();
void handleThemeChanged();

private:
protected:
QGridLayout *m_layout;
QSignalMapper *m_mapper;
QHash<Buttons, QPair<QPushButton *, const KeyDescription *>> m_keys;

private:
const KeyDescription keyDescriptions[20]={
{ "C", Key_Clear, 1, 0 },
{ "%", Key_Percent, 1, 1 },
{ "", Key_Backspace, 1, 2 },
{ "", Key_Div, 1, 3 },

{ "7", Key_7, 2, 0 },
{ "8", Key_8, 2, 1 },
{ "9", Key_9, 2, 2 },
{ "", Key_Mult, 2, 3 },

{ "4", Key_4, 3, 0 },
{ "5", Key_5, 3, 1 },
{ "6", Key_6, 3, 2 },
{ "", Key_Min, 3, 3 },

{ "1", Key_1, 4, 0 },
{ "2", Key_2, 4, 1 },
{ "3", Key_3, 4, 2 },
{ "", Key_Plus, 4, 3 },

{ "0", Key_0, 5, 0 },
{ ".", Key_Point, 5, 1 },
{ "( )", Key_Brackets, 5, 2 },
{ "=", Key_Equals, 5, 3 }
};
};

#endif
Loading