[go: up one dir, main page]

Menu

[d56ae5]: / unarybitpanel.cpp  Maximize  Restore  History

Download this file

189 lines (159 with data), 5.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "unarybitpanel.h"
#include "settings.h"
#include <QLabel>
UnaryBitPanel::UnaryBitPanel(QWidget *parent)
: QFrame(parent)
{
setFrameStyle(QFrame::Panel | QFrame::Raised);
setLineWidth(0);
QVBoxLayout *layout = new QVBoxLayout;
layout->setSpacing(2);
layout->setContentsMargins(10, 10, 10, 10);
operatorCombo = new QComboBox(this);
QStringList ops;
ops << tr("! Boolean not") << tr("- Negative") << tr("~ Bitwise not");
operatorCombo->addItems(ops);
QHBoxLayout *opLayout = new QHBoxLayout;
opLayout->addWidget(new QLabel(tr("Operator ")));
opLayout->addWidget(operatorCombo);
opLayout->addStretch();
layout->addLayout(opLayout);
table = new QTableWidget;
// input do-it format result comment
table->setColumnCount(4);
table->setRowCount(1);
QStringList headers;
headers << tr("Input") << tr("Operator") << tr("Result") << tr("Comment");
table->setHorizontalHeaderLabels(headers);
//number = new BinaryNumber;
//number->setBits(32354,16);
//layout->addWidget(number);
input = new BinaryNumber;
inputEdit = new IntegerEdit;
fontSize = ebe["font_size"].toInt();
table->setCellWidget(0, 0, inputEdit);
table->setCellWidget(0, 2, input);
table->setColumnWidth(0, 8 * fontSize);
table->setColumnWidth(1, 8 * fontSize);
table->setColumnWidth(2, 16 * fontSize + 5);
table->setColumnWidth(3, 16 * fontSize);
layout->addWidget(table);
setLayout(layout);
connect(operatorCombo, SIGNAL(activated(QString)), this,
SLOT(selectOperator(QString)));
selectOperator(tr("! Boolean not"));
}
void UnaryBitPanel::setFontHeightAndWidth(int height, int width)
{
fontHeight = height;
fontWidth = width;
}
void UnaryBitPanel::selectOperator(QString o)
{
input->clear();
if (o[0] == QChar('!')) {
op = o;
doit = new QPushButton("!");
table->setRowCount(1);
table->setCellWidget(0, 1, doit);
table->setCellWidget(0, 3, new QLabel(""));
connect(doit, SIGNAL(clicked()), this, SLOT(notStep1()));
} else if (o[0] == QChar('-')) {
op = o;
doit = new QPushButton("-");
table->setRowCount(1);
table->setCellWidget(0, 1, doit);
table->setCellWidget(0, 3, new QLabel(""));
connect(doit, SIGNAL(clicked()), this, SLOT(negateStep1()));
} else if (o[0] == QChar('~')) {
op = o;
doit = new QPushButton("~");
table->setRowCount(1);
table->setCellWidget(0, 1, doit);
table->setCellWidget(0, 3, new QLabel(""));
connect(doit, SIGNAL(clicked()), this, SLOT(bitwiseNotStep1()));
} else {
qDebug() << tr("Unknown operator:") << o;
}
}
void UnaryBitPanel::notStep1()
{
value = inputEdit->value();
input->setBits(value, 16);
table->setCellWidget(0, 3, new QLabel(tr("Value converted to binary")));
table->setRowCount(2);
table->setCellWidget(0, 1, new QLabel(""));
doit = new QPushButton("!");
table->setCellWidget(1, 1, doit);
connect(doit, SIGNAL(clicked()), this, SLOT(notStep2()));
}
void UnaryBitPanel::notStep2()
{
output1 = new BinaryNumber;
output1->setBits(!value, 8);
table->setCellWidget(1, 2, output1);
table->setCellWidget(1, 3, new QLabel(tr("Result as a binary bool")));
table->setCellWidget(1, 1, new QLabel(""));
}
void UnaryBitPanel::bitwiseNotStep1()
{
value = inputEdit->value();
input->setBits(value, 16);
table->setCellWidget(0, 3, new QLabel(tr("Value converted to binary")));
table->setRowCount(2);
table->setCellWidget(0, 1, new QLabel(""));
doit = new QPushButton("~");
table->setCellWidget(1, 1, doit);
connect(doit, SIGNAL(clicked()), this, SLOT(bitwiseNotStep2()));
}
void UnaryBitPanel::bitwiseNotStep2()
{
output1 = new BinaryNumber;
output1->setBits(~value, 16);
table->setCellWidget(1, 2, output1);
table->setCellWidget(1, 3, new QLabel(tr("All bits flipped")));
table->setCellWidget(1, 1, new QLabel(""));
}
void UnaryBitPanel::negateStep1()
{
value = inputEdit->value();
input->setBits(value, 16);
table->setCellWidget(0, 3, new QLabel(tr("Value converted to binary")));
table->setRowCount(2);
table->setCellWidget(0, 1, new QLabel(""));
doit = new QPushButton("-");
table->setCellWidget(1, 1, doit);
connect(doit, SIGNAL(clicked()), this, SLOT(negateStep2()));
}
void UnaryBitPanel::negateStep2()
{
output1 = new BinaryNumber;
output1->setBits(~value, 16);
table->setRowCount(3);
table->setCellWidget(1, 2, output1);
table->setCellWidget(1, 3, new QLabel(tr("All bits flipped")));
table->setCellWidget(1, 1, new QLabel(""));
doit = new QPushButton("-");
table->setCellWidget(2, 1, doit);
connect(doit, SIGNAL(clicked()), this, SLOT(negateStep3()));
}
void UnaryBitPanel::negateStep3()
{
add1 = new BinaryNumber;
add1->setText("+ 1", 16);
table->setCellWidget(2, 2, add1);
table->setCellWidget(2, 3, new QLabel(tr("Adding 1")));
table->setRowCount(4);
table->setCellWidget(2, 1, new QLabel(""));
doit = new QPushButton("-");
table->setCellWidget(3, 1, doit);
connect(doit, SIGNAL(clicked()), this, SLOT(negateStep4()));
}
void UnaryBitPanel::negateStep4()
{
output2 = new BinaryNumber;
output2->setBits(-value, 16);
table->setCellWidget(3, 2, output2);
table->setCellWidget(3, 3, new QLabel(tr("Twos complement")));
table->setCellWidget(3, 1, new QLabel(""));
}