-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexedit.cpp
216 lines (193 loc) · 7.38 KB
/
hexedit.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/******************************************************************************
**
** bits - an application to arithmeticall and logically combine and
** manipulate bit patterns and their corresponding hex numbers.
**
** Tony Camuso
** Created December, 2011
**
** bits (bitview) is free software. You can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** GNU General Public License http://www.gnu.org/licenses/gpl.html
**
** Copyright (c) 2011-2014 by Tony Camuso.
**
******************************************************************************/
#include "hexedit.h"
/*/////////////////////////////////////////////////////////////////////////////
//
// HexEdit::HexEdit - constructor
//
*/
HexEdit::HexEdit(int hexenum, QWidget *parent, bitfield_t bf) :
QComboBox(parent)
{
// All the objects owned by this object must be instantiated here.
// To expect them to be correctly instantiated outside of this is
// dangerous. Ask me how I found that out.
//
hexeditId = hexenum;
hexBitField = new bitfield(bf);
hexeditLabel = new QLabel(parent);
hexbits = new QRadioButton(parent);
hexshift = new QRadioButton(parent);
this->setInsertPolicy(QComboBox::InsertAtTop);
this->setEditable(true);
this->setMaxCount(32);
this->hexBitField->setBitField(bf);
this->lineEdit()->setInputMask(hexBitField->getCurrentBitMask());
hexbits->setFocusPolicy(Qt::ClickFocus);
hexshift->setFocusPolicy(Qt::ClickFocus);
quint64 initVal = 0;
prevHexVal = ~initVal;
prevHexStr = "";
this->updateHexEdit(initVal);
QFont font;
#ifdef Q_WS_WIN
font.setPointSize(font.pointSize() + 3);
font.setFamily("Lucida Console");
#endif
#ifdef Q_WS_X11
font.setFamily("Monospace");
font.setPointSize(12);
#endif
this->setFont(font);
this->setStyleSheet("color: aqua; background-color: black");
}
HexEdit::~HexEdit(){}
/*/////////////////////////////////////////////////////////////////////////////
//
// int2hexstr (QString& hexStr, quint64 hexVal)
//
// Converts an unsigned 64-bit integer to a string expressed as a
// hexadecimal number using the enigmatic QString::arg() member
// function. It's not too easy to understand the syntax of this
// thing, but once you get it right, it's rock solid. No more
// cryptic than printf, really.
//
// One thing that will bite you is that the "width" parameter is
// a minimum width, not a maximum, so unless you mask off the
// upper bits of the number, you won't get what you expected if
// you're looking for bitfields less than 64-bit.
*/
QString& HexEdit::int2hexstr(QString &hexStr, quint64 hexVal)
{
int width = this->hexBitField->getCurrentHexDigits();
int binDigits = this->hexBitField->getCurrentBinDigits();
// Mask off any unused upper bits
//
hexVal &= 0xFFFFFFFFFFFFFFFF >> (64 - binDigits);
// See if the hexVal parameter is negative. If so, we will
// color the string red.
//
int bin = this->hexBitField->getCurrentBinDigits();
quint64 bitMask = (quint64)1 << (bin - 1);
bool negFlag = hexVal & bitMask ? true : false;
QString redOpen = negFlag ? "<font color=\"Magenta\">" : "";
QString redClose = negFlag ? "</font>" : "";
hexStr = QString("%1").arg((quint64)hexVal, width, 16, QChar('0'));
hexStr = hexStr.toUpper();
//hexStr = redOpen + hexStr + redClose;
return hexStr;
}
/*/////////////////////////////////////////////////////////////////////////////
//
// hexstr2int (QString& hexStr)
//
// Convert a string representing a hexidecimal number into an unsigned
// 64-bit integer using the somewhat cryptic QString member function,
// QString::toUlongLong(). Still beats doing it yourself, and once you
// get it right, you don't have to fool with it again.
//
*/
quint64 HexEdit::hexstr2int(QString& hexStr)
{
bool ok;
hexStr = hexStr.remove('.');
hexStr = hexStr.trimmed();
// Convert the string to unsigned 64 bit integer, interpreting the
// string as base 16 (hexadecimal).
//
quint64 hexVal = hexStr.toULongLong(&ok, 16);
hexVal = maskHexVal(hexVal);
return hexVal;
}
/*/////////////////////////////////////////////////////////////////////////////
//
// updateHexEdit (quint64 hexVal)
//
// Updates the string in the ComboBox window with a string that represents
// the unsigned 64-bit number passed as "quint64 hexVal". The string will
// be formatted to the correct bitfield width by the call to int2hexstr().
//
// Additionally, if the value being passed as an argument is different
// than the previous value being displayed, we will save the item in
// the ComboBox drop list and save the new string and number values to
// be compared with the value passed to this routine the next time it's
// called.
//
// The reason we update the contents of the ComboBox display (currentText)
// on every call, is that we might be getting called because of a change
// in bitfield width, where there is no change in the value of the data,
// but there is a change in the way it must be displayed.
//
*/
void HexEdit::updateHexEdit(quint64 hexVal)
{
QString hexStr;
hexVal = maskHexVal(hexVal);
hexStr = int2hexstr(hexStr, hexVal);
this->setEditText(hexStr);
if(hexVal != prevHexVal)
{
this->insertItem(1, hexStr);
prevHexVal = hexVal;
prevHexStr = hexStr;
}
}
/*/////////////////////////////////////////////////////////////////////////////
//
// updateHexEditBitField (bitfiedl_t bf)
//
// This function is called whenever the user has changed the size of the
// bitfield. In order to make the transition without having junk appear
// in the combo box window.
// . Read the current contents of the ComboBox window
// . Update the bitfield width parameters, including the input mask.
// . Convert to a hex value the contents we read from the ComboBox
// at the beginning of the procedure.
// . Update the contents of the ComboBox with the new string, which was
// created from the previous number, but is now in the context of the
// new bitfield width.
*/
void HexEdit::updateHexEditBitField(bitfield_t bf)
{
QString hexStr = this->currentText();
this->hexBitField->setBitField(bf);
this->lineEdit()->setInputMask(hexBitField->getCurrentBitMask());
quint64 hexVal = this->hexstr2int(hexStr);
this->updateHexEdit(hexVal);
}
quint64 HexEdit::getHexVal()
{
QString hexStr = this->currentText();
quint64 hexVal = this->hexstr2int(hexStr);
return hexVal;
}
quint64 HexEdit::maskHexVal(quint64 hexVal)
{
quint64 bitMask = 0xFFFFFFFFFFFFFFFF;
bitMask >>= (64 - this->hexBitField->getCurrentBinDigits());
quint64 newVal = hexVal & bitMask;
return newVal;
}
bool HexEdit::isNewData( quint64 numData) {return( numData != prevHexVal );}
bool HexEdit::isNewData( QString &strData) {return( strData != prevHexStr );}