Introduce old redir program
[lcapit-junk-code.git] / qt-course / simple-calculator / MyWidget.cpp
blobb3c1522959dcd08157c02558099cbe7c921318ee
1 #include <QString>
2 #include "MyWidget.h"
4 #define CONNECT_NUMBUTTON(button) \
5 connect(button, SIGNAL(clicked(bool)), this, \
6 SLOT(slot_numButtonClicked()))
8 #define CONNECT_OPERBUTTON(button) \
9 connect(button, SIGNAL(clicked(bool)), this, \
10 SLOT(slot_operButtonClicked()))
12 #define DISABLE_BUTTON(button) button->setEnable(false)
13 #define ENABLE_BUTTON(button) button->setEnable(true)
15 MyWidget::MyWidget(QWidget *parent)
16 : QWidget(parent)
18 m_ui = new Ui::MyWidgetBase();
19 m_ui->setupUi(this);
21 CONNECT_NUMBUTTON(m_ui->m_button0);
22 CONNECT_NUMBUTTON(m_ui->m_button1);
23 CONNECT_NUMBUTTON(m_ui->m_button2);
24 CONNECT_NUMBUTTON(m_ui->m_button3);
25 CONNECT_NUMBUTTON(m_ui->m_button4);
26 CONNECT_NUMBUTTON(m_ui->m_button5);
27 CONNECT_NUMBUTTON(m_ui->m_button6);
28 CONNECT_NUMBUTTON(m_ui->m_button7);
29 CONNECT_NUMBUTTON(m_ui->m_button8);
30 CONNECT_NUMBUTTON(m_ui->m_button9);
32 CONNECT_OPERBUTTON(m_ui->m_buttonSum);
33 CONNECT_OPERBUTTON(m_ui->m_buttonMult);
34 CONNECT_OPERBUTTON(m_ui->m_buttonSub);
35 CONNECT_OPERBUTTON(m_ui->m_buttonDiv);
36 CONNECT_OPERBUTTON(m_ui->m_buttonRes);
38 resetDisplay = true;
39 buttonsEnabled(true);
42 MyWidget::~MyWidget()
46 void MyWidget::slot_numButtonClicked()
48 QPushButton *btn = dynamic_cast<QPushButton *>(sender());
49 if (!btn)
50 return;
52 QString text = m_ui->m_display->text();
53 if (resetDisplay) {
54 text = "";
55 resetDisplay = false;
58 text += btn->text();
59 m_ui->m_display->setText(text);
62 void MyWidget::buttonsEnabled(bool value)
64 m_ui->m_buttonSum->setEnabled(value);
65 m_ui->m_buttonMult->setEnabled(value);
66 m_ui->m_buttonSub->setEnabled(value);
67 m_ui->m_buttonDiv->setEnabled(value);
68 m_ui->m_buttonRes->setEnabled(!value);
71 void MyWidget::slot_operButtonClicked()
73 int result;
74 QPushButton *btn = dynamic_cast<QPushButton *>(sender());
75 if (!btn)
76 return;
78 resetDisplay = true;
80 if (btn->text() == "=") {
81 int lvalue = m_ui->m_display->text().toInt();
82 if (m_oper == "+")
83 result = m_number + lvalue;
84 else if (m_oper == "-")
85 result = m_number - lvalue;
86 else if (m_oper == "*")
87 result = m_number * lvalue;
88 else // m_oper == "/"
89 result = m_number / lvalue;
90 m_ui->m_display->setText(QString::number(result));
91 buttonsEnabled(true);
92 } else {
93 m_oper = btn->text();
94 m_number = m_ui->m_display->text().toInt();
95 buttonsEnabled(false);