diff --git a/deepin-calculator.pro b/deepin-calculator.pro index b354f7b6..4adf2f8f 100644 --- a/deepin-calculator.pro +++ b/deepin-calculator.pro @@ -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 \ diff --git a/mainwindow.cpp b/mainwindow.cpp index f5292cbf..d0978e80 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -28,6 +28,8 @@ #include #include +#define ENABLE_SCIENTIFIC; //open scientific mode + MainWindow::MainWindow(QWidget *parent) : DMainWindow(parent), m_settings(DSettings::instance()), diff --git a/modules/basicmodule.cpp b/modules/basicmodule.cpp index 33887669..2bcac84d 100644 --- a/modules/basicmodule.cpp +++ b/modules/basicmodule.cpp @@ -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); @@ -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() @@ -58,10 +61,12 @@ BasicModule::~BasicModule() void BasicModule::switchToSimpleKeypad() { + m_keypadLayout->setCurrentIndex(0); } void BasicModule::switchToScientificKeypad() { + m_keypadLayout->setCurrentIndex(1); } void BasicModule::initTheme() @@ -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(m_basicKeypad->button(BasicKeypad::Key_Equals)))->animateClick(); + (static_cast(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(m_basicKeypad->button(BasicKeypad::Key_Point)))->animateClick(); + (static_cast(m_basicKeypad->button(Buttons::Key_Point)))->animateClick(); break; case Qt::Key_Escape: - (static_cast(m_basicKeypad->button(BasicKeypad::Key_Clear)))->animateClick(); + (static_cast(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) { @@ -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(m_basicKeypad->button(BasicKeypad::Key_Clear)); + TextButton *btn = static_cast(m_basicKeypad->button(Buttons::Key_Clear)); if (isAllClear) { btn->setText("AC"); diff --git a/modules/basicmodule.h b/modules/basicmodule.h index 5b36e38f..326bc3c2 100644 --- a/modules/basicmodule.h +++ b/modules/basicmodule.h @@ -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 @@ -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; diff --git a/modules/expressionbar.cpp b/modules/expressionbar.cpp index 1a3d9493..6b935f32 100644 --- a/modules/expressionbar.cpp +++ b/modules/expressionbar.cpp @@ -81,6 +81,7 @@ void ExpressionBar::enterNumberEvent(const QString &text) void ExpressionBar::enterSymbolEvent(const QString &text) { m_inputEdit->insert(text); + m_isContinue = true; } @@ -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)"); } diff --git a/widgets/basickeypad.cpp b/widgets/basickeypad.cpp index aba84af2..af9812fa 100644 --- a/widgets/basickeypad.cpp +++ b/widgets/basickeypad.cpp @@ -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())); @@ -93,6 +66,7 @@ QPushButton* BasicKeypad::button(Buttons key) return m_keys.value(key).first; } +//键盘按键时显示按下效果 void BasicKeypad::animate(Buttons key) { TextButton *btn = static_cast(button(key)); diff --git a/widgets/basickeypad.h b/widgets/basickeypad.h index 495ff78e..ee728cb5 100644 --- a/widgets/basickeypad.h +++ b/widgets/basickeypad.h @@ -25,19 +25,13 @@ #include #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(); @@ -45,25 +39,46 @@ class BasicKeypad : public QWidget 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> 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 diff --git a/widgets/buttons.h b/widgets/buttons.h new file mode 100644 index 00000000..af8312a1 --- /dev/null +++ b/widgets/buttons.h @@ -0,0 +1,23 @@ +#ifndef BUTTONS_H +#define BUTTONS_H + +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, + Key_LBracket, Key_RBracket, Key_sin, Key_cos, + Key_tan, Key_arcsin, Key_arccos, Key_arctan, + Key_lg, Key_ln, Key_pi, Key_e, Key_Sqrt, + Key_Factorial, Key_Power, Key_Square +}; + +struct KeyDescription { + QString text; + Buttons button; + int row; + int column; +}; + +#endif // BUTTONS_H diff --git a/widgets/inputedit.cpp b/widgets/inputedit.cpp index d205ea64..5d562f16 100644 --- a/widgets/inputedit.cpp +++ b/widgets/inputedit.cpp @@ -41,11 +41,13 @@ InputEdit::InputEdit(QWidget *parent) connect(this, &QLineEdit::textChanged, this, &InputEdit::handleTextChanged); connect(this, &QLineEdit::cursorPositionChanged, this, &InputEdit::handleCursorPositionChanged); + /* connect(this, &QLineEdit::selectionChanged, [=] { int pos = this->cursorPosition(); this->cursorPositionChanged(pos, pos); }); + */ } InputEdit::~InputEdit() diff --git a/widgets/scientifickeypad.cpp b/widgets/scientifickeypad.cpp index 3955e7d8..28444272 100644 --- a/widgets/scientifickeypad.cpp +++ b/widgets/scientifickeypad.cpp @@ -22,62 +22,50 @@ DWIDGET_USE_NAMESPACE -const ScientificKeypad::KeyDescription ScientificKeypad::keyDescriptions[] = { - { "C", Key_Clear, 1, 0 }, - { "%", Key_Percent, 1, 1 }, - { "", Key_Backspace, 1, 2 }, - { "÷", Key_Div, 1, 3 }, - { "sin", Key_1, 1, 4}, - { "cos", Key_1, 1, 5}, - { "tan", Key_1, 1, 6}, - - { "7", Key_7, 2, 0 }, - { "8", Key_8, 2, 1 }, - { "9", Key_9, 2, 2 }, - { "×", Key_Mult, 2, 3 }, - { "arcsin", Key_Mult, 2, 4 }, - { "arccos", Key_Mult, 2, 5 }, - { "arctan", Key_Mult, 2, 6 }, - - { "4", Key_4, 3, 0 }, - { "5", Key_5, 3, 1 }, - { "6", Key_6, 3, 2 }, - { "-", Key_Min, 3, 3 }, - { "√", Key_Mult, 3, 4 }, - { "lg", Key_Mult, 3, 5 }, - { "ln", Key_Mult, 3, 6 }, - - { "1", Key_1, 4, 0 }, - { "2", Key_2, 4, 1 }, - { "3", Key_3, 4, 2 }, - { "+", Key_Plus, 4, 3 }, - { "π", Key_Min, 4, 4 }, - { "e", Key_Min, 4, 5 }, - { "!", Key_Min, 4, 6 }, - - { "0", Key_0, 5, 0 }, - { ".", Key_Point, 5, 1 }, - { "√", Key_Brackets, 5, 2 }, - { "=", Key_Equals, 5, 3 }, - { "(", Key_Equals, 5, 4 }, - { ")", Key_Equals, 5, 5 }, - { "^", Key_Equals, 5, 6 } -}; +static QPushButton* createSpecialKeyButton(Buttons key) { + IconButton *button = new IconButton(10, 13); + + if (key == Buttons::Key_Div) { + button->setIcon(":/images/div_normal.svg"); + } else if (key == Buttons::Key_Mult) { + button->setIcon(":/images/mult_normal.svg"); + } else if (key == Buttons::Key_Min) { + button->setIcon(":/images/min_normal.svg"); + } else if (key == Buttons::Key_Plus) { + button->setIcon(":/images/plus_normal.svg"); + } else if (key == Buttons::Key_Backspace) { + button->setIconStateSizes(23, 26); + button->setIcon(QString(":/images/delete_%1_normal.svg") + .arg(DThemeManager::instance()->theme())); + } + + return button; +} ScientificKeypad::ScientificKeypad(QWidget *parent) : QWidget(parent), - m_layout(new QGridLayout(this)) + m_layout(new QGridLayout(this)), + m_mapper(new QSignalMapper(this)) { m_layout->setMargin(0); m_layout->setSpacing(1); initButtons(); + initUI(); + + connect(m_mapper, SIGNAL(mapped(int)), SIGNAL(buttonPressed(int))); + connect(DThemeManager::instance(), &DThemeManager::themeChanged, this, &ScientificKeypad::handleThemeChanged); } ScientificKeypad::~ScientificKeypad() { } +QPushButton* ScientificKeypad::button(Buttons key) +{ + return m_keys.value(key).first; +} + void ScientificKeypad::initButtons() { const int count = sizeof(keyDescriptions) / sizeof(keyDescriptions[0]); @@ -85,10 +73,8 @@ void ScientificKeypad::initButtons() const KeyDescription *desc = keyDescriptions + i; QPushButton *button; - if (desc->button == Key_Backspace) { - button = new IconButton; - (static_cast(button))->setIcon(QString(":/images/delete_%1_normal.svg") - .arg(DThemeManager::instance()->theme())); + if (desc->text.isEmpty()) { + button = createSpecialKeyButton(desc->button); } else { button = new TextButton(desc->text); } @@ -96,5 +82,46 @@ void ScientificKeypad::initButtons() m_layout->addWidget(button, desc->row, desc->column); const QPair hashValue(button, desc); m_keys.insert(desc->button, hashValue); + + connect(button, &QPushButton::clicked, m_mapper, static_cast(&QSignalMapper::map)); + m_mapper->setMapping(button, desc->button); } } + +void ScientificKeypad::initUI() +{ + QHashIterator> i(m_keys); + + while (i.hasNext()) { + i.next(); + i.value().first->setFocusPolicy(Qt::NoFocus); + } + + button(Key_Div)->setObjectName("SymbolButton"); + button(Key_Mult)->setObjectName("SymbolButton"); + button(Key_Min)->setObjectName("SymbolButton"); + button(Key_Plus)->setObjectName("SymbolButton"); + + button(Key_arccos)->setObjectName("SymbolButton"); + button(Key_arcsin)->setObjectName("SymbolButton"); + button(Key_arctan)->setObjectName("SymbolButton"); + button(Key_cos)->setObjectName("SymbolButton"); + button(Key_sin)->setObjectName("SymbolButton"); + button(Key_tan)->setObjectName("SymbolButton"); + button(Key_lg)->setObjectName("SymbolButton"); + button(Key_ln)->setObjectName("SymbolButton"); + button(Key_Sqrt)->setObjectName("SymbolButton"); + button(Key_Factorial)->setObjectName("SymbolButton"); + button(Key_Power)->setObjectName("SymbolButton"); + button(Key_LBracket)->setObjectName("SymbolButton"); + button(Key_RBracket)->setObjectName("SymbolButton"); + button(Key_Square)->setObjectName("SymbolButton"); + button(Key_Percent)->setObjectName("SymbolButton"); + button(Key_Equals)->setObjectName("EqualButton"); +} + +void ScientificKeypad::handleThemeChanged() +{ + IconButton *btn = static_cast(button(Key_Backspace)); + btn->setIcon(QString(":/images/delete_%1_normal.svg").arg(DThemeManager::instance()->theme())); +} diff --git a/widgets/scientifickeypad.h b/widgets/scientifickeypad.h index e160fc68..ee57382b 100644 --- a/widgets/scientifickeypad.h +++ b/widgets/scientifickeypad.h @@ -25,36 +25,74 @@ #include #include "textbutton.h" #include "iconbutton.h" +#include "buttons.h" class ScientificKeypad : 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 - }; ScientificKeypad(QWidget *parent = nullptr); ~ScientificKeypad(); - static const struct KeyDescription { - QString text; - Buttons button; - int row; - int column; - } keyDescriptions[]; + QPushButton *button(Buttons key); + +signals: + void buttonPressed(int); + +protected: + void initUI(); private: void initButtons(); + void handleThemeChanged(); private: QGridLayout *m_layout; + QSignalMapper *m_mapper; QHash> m_keys; + const KeyDescription keyDescriptions[35] = { + { "C", Key_Clear, 1, 0 }, + { "e", Key_e, 1, 1 }, + { "", Key_Backspace, 1, 2 }, + { "", Key_Div, 1, 3 }, + { "sin", Key_sin, 1, 4}, + { "cos", Key_cos, 1, 5}, + { "tan", Key_tan, 1, 6}, + + { "7", Key_7, 2, 0 }, + { "8", Key_8, 2, 1 }, + { "9", Key_9, 2, 2 }, + { "", Key_Mult, 2, 3 }, + { "arcsin", Key_arcsin, 2, 4 }, + { "arccos", Key_arccos, 2, 5 }, + { "arctan", Key_arctan, 2, 6 }, + + { "4", Key_4, 3, 0 }, + { "5", Key_5, 3, 1 }, + { "6", Key_6, 3, 2 }, + { "", Key_Min, 3, 3 }, + { "√", Key_Sqrt, 3, 4 }, + { "lg", Key_lg, 3, 5 }, + { "ln", Key_ln, 3, 6 }, + + { "1", Key_1, 4, 0 }, + { "2", Key_2, 4, 1 }, + { "3", Key_3, 4, 2 }, + { "", Key_Plus, 4, 3 }, + { "X²", Key_Square, 4, 4 }, + { "%", Key_Percent, 4, 5 }, + { "!", Key_Factorial, 4, 6 }, + + { "0", Key_0, 5, 0 }, + { ".", Key_Point, 5, 1 }, + { "π", Key_pi, 5, 2 }, + { "=", Key_Equals, 5, 3 }, + { "(", Key_LBracket, 5, 4 }, + { ")", Key_RBracket, 5, 5 }, + { "^", Key_Power, 5, 6 } + }; }; #endif diff --git a/widgets/textbutton.cpp b/widgets/textbutton.cpp index d8142b44..9d4a5eaa 100644 --- a/widgets/textbutton.cpp +++ b/widgets/textbutton.cpp @@ -67,7 +67,7 @@ void TextButton::init() m_effect->setColor(QColor(12, 155, 246, 255 * 0.6)); m_effect->setBlurRadius(30); m_effect->setXOffset(-2); - m_effect->setYOffset(-4); + m_effect->setYOffset(-4); } } }