Introduce old redir program
[lcapit-junk-code.git] / qt-course / exer355-base-converter / MyWidget.cpp
blob54935ef16b2bc587a3b7277db0e4885da4525a9d
1 #include <QLabel>
2 #include <QLineEdit>
3 #include <QVBoxLayout>
4 #include <QHBoxLayout>
6 #include "MyWidget.h"
8 MyWidget::MyWidget(QWidget *parent)
9 : QWidget(parent)
11 QLabel *m_label;
13 /* The labels */
15 QVBoxLayout *labels = new QVBoxLayout();
17 m_label = new QLabel("Binario", this);
18 labels->addWidget(m_label);
20 m_label = new QLabel("Octal", this);
21 labels->addWidget(m_label);
23 m_label = new QLabel("Decimal", this);
24 labels->addWidget(m_label);
26 m_label = new QLabel("Hexadecimal", this);
27 labels->addWidget(m_label);
29 /* QlineEdits */
31 QVBoxLayout *lines = new QVBoxLayout();
32 for (int i = 0; i < NR_LABELS; i++) {
33 m_qlines[i] = new QLineEdit(this);
34 lines->addWidget(m_qlines[i]);
35 connect(m_qlines[i], SIGNAL(textEdited(const QString &)),
36 this, SLOT(m_update_lines()));
39 /* Put them together */
40 QHBoxLayout *main_layout = new QHBoxLayout(this);
41 main_layout->addItem(labels);
42 main_layout->addItem(lines);
45 MyWidget::~MyWidget()
49 void MyWidget::resetLines()
51 for (int i = 0; i < NR_LABELS; i++)
52 m_qlines[i]->setText("");
55 void MyWidget::m_update_lines()
57 int base, i;
59 for (i = 0; i < NR_LABELS; i++) {
60 if (m_qlines[i]->isModified()) {
61 if (m_qlines[i]->text() == "") {
62 resetLines();
63 return;
66 switch (i) {
67 case 0:
68 base = 2;
69 break;
70 case 1:
71 base = 8;
72 break;
73 case 2:
74 base = 10;
75 break;
76 case 3:
77 base = 16;
78 break;
80 break;
84 int num = m_qlines[i]->text().toInt(0, base);
85 m_qlines[0]->setText(QString::number(num, 2));
86 m_qlines[1]->setText(QString::number(num, 8));
87 m_qlines[2]->setText(QString::number(num, 10));
88 m_qlines[3]->setText(QString::number(num, 16).toUpper());