Debian Codegraph @

phoenix/qt/widget/hex-edit.cpp

raw
1 namespace phoenix {
2
3 void pHexEdit::setColumns(unsigned columns) {
4 update();
5 }
6
7 void pHexEdit::setLength(unsigned length) {
8 //add one if last row is not equal to column length (eg only part of the row is present)
9 bool indivisible = hexEdit.state.columns == 0 || (hexEdit.state.length % hexEdit.state.columns) != 0;
10 qtScroll->setRange(0, hexEdit.state.length / hexEdit.state.columns + indivisible - hexEdit.state.rows);
11 update();
12 }
13
14 void pHexEdit::setOffset(unsigned offset) {
15 locked = true;
16 qtScroll->setSliderPosition(hexEdit.state.offset / hexEdit.state.columns);
17 locked = false;
18 update();
19 }
20
21 void pHexEdit::setRows(unsigned rows) {
22 qtScroll->setPageStep(hexEdit.state.rows);
23 update();
24 }
25
26 void pHexEdit::update() {
27 if(!hexEdit.onRead) {
28 qtHexEdit->setPlainText("");
29 return;
30 }
31
32 unsigned cursorPosition = qtHexEdit->textCursor().position();
33
34 string output;
35 unsigned offset = hexEdit.state.offset;
36 for(unsigned row = 0; row < hexEdit.state.rows; row++) {
37 output.append(hex<8>(offset));
38 output.append(" ");
39
40 string hexdata;
41 string ansidata = " ";
42
43 for(unsigned column = 0; column < hexEdit.state.columns; column++) {
44 if(offset < hexEdit.state.length) {
45 uint8_t data = hexEdit.onRead(offset++);
46 hexdata.append(hex<2>(data));
47 hexdata.append(" ");
48 ansidata.append(data >= 0x20 && data <= 0x7e ? (char)data : '.');
49 } else {
50 hexdata.append(" ");
51 ansidata.append(" ");
52 }
53 }
54
55 output.append(hexdata);
56 output.append(ansidata);
57 if(offset >= hexEdit.state.length) break;
58 if(row != hexEdit.state.rows - 1) output.append("\n");
59 }
60
61 qtHexEdit->setPlainText(QString::fromUtf8(output));
62 QTextCursor cursor = qtHexEdit->textCursor();
63 cursor.setPosition(cursorPosition);
64 qtHexEdit->setTextCursor(cursor);
65 }
66
67 void pHexEdit::constructor() {
68 qtWidget = qtHexEdit = new QtHexEdit(*this);
69
70 qtHexEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
71 qtHexEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
72 qtHexEdit->setTextInteractionFlags(Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse);
73
74 qtLayout = new QHBoxLayout;
75 qtLayout->setAlignment(Qt::AlignRight);
76 qtLayout->setMargin(0);
77 qtLayout->setSpacing(0);
78 qtHexEdit->setLayout(qtLayout);
79
80 qtScroll = new QtHexEditScrollBar(*this);
81 qtScroll->setSingleStep(1);
82 qtLayout->addWidget(qtScroll);
83
84 connect(qtScroll, SIGNAL(actionTriggered(int)), SLOT(onScroll()));
85
86 pWidget::synchronizeState();
87 setColumns(hexEdit.state.columns);
88 setRows(hexEdit.state.rows);
89 setLength(hexEdit.state.length);
90 setOffset(hexEdit.state.offset);
91 update();
92 }
93
94 void pHexEdit::destructor() {
95 delete qtScroll;
96 delete qtLayout;
97 delete qtHexEdit;
98 qtWidget = qtHexEdit = nullptr;
99 qtLayout = nullptr;
100 qtScroll = nullptr;
101 }
102
103 void pHexEdit::orphan() {
104 destructor();
105 constructor();
106 }
107
108 void pHexEdit::keyPressEvent(QKeyEvent* event) {
109 if(!hexEdit.onRead) return;
110
111 //allow Ctrl+C (copy)
112 if(event->key() == Qt::Key_C && event->modifiers() == Qt::ControlModifier) {
113 qtHexEdit->keyPressEventAcknowledge(event);
114 return;
115 }
116
117 //disallow other text operations (cut, paste, etc)
118 if(event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)) return;
119
120 QTextCursor cursor = qtHexEdit->textCursor();
121 signed lineWidth = 10 + (hexEdit.state.columns * 3) + 1 + hexEdit.state.columns + 1;
122 signed cursorY = cursor.position() / lineWidth;
123 signed cursorX = cursor.position() % lineWidth;
124
125 unsigned nibble = 0;
126 switch(event->key()) {
127 default: return;
128
129 case Qt::Key_Left:
130 if(cursorX > 0) {
131 cursor.setPosition(cursor.position() - 1);
132 qtHexEdit->setTextCursor(cursor);
133 }
134 return;
135
136 case Qt::Key_Right:
137 if(cursorX < lineWidth - 1) {
138 cursor.setPosition(cursor.position() + 1);
139 qtHexEdit->setTextCursor(cursor);
140 }
141 return;
142
143 case Qt::Key_Home:
144 cursor.setPosition(cursorY * lineWidth + 10);
145 qtHexEdit->setTextCursor(cursor);
146 return;
147
148 case Qt::Key_End:
149 cursor.setPosition(cursorY * lineWidth + 57);
150 qtHexEdit->setTextCursor(cursor);
151 return;
152
153 case Qt::Key_Up:
154 if(cursorY > 0) {
155 cursor.setPosition(cursor.position() - lineWidth);
156 qtHexEdit->setTextCursor(cursor);
157 } else {
158 scrollTo(qtScroll->sliderPosition() - 1);
159 }
160 return;
161
162 case Qt::Key_Down:
163 if(cursorY >= rows() - 1) {
164 //cannot scroll down further
165 } else if(cursorY < hexEdit.state.rows - 1) {
166 cursor.setPosition(cursor.position() + lineWidth);
167 qtHexEdit->setTextCursor(cursor);
168 } else {
169 scrollTo(qtScroll->sliderPosition() + 1);
170 }
171 return;
172
173 case Qt::Key_PageUp:
174 scrollTo(qtScroll->sliderPosition() - hexEdit.state.rows);
175 return;
176
177 case Qt::Key_PageDown:
178 scrollTo(qtScroll->sliderPosition() + hexEdit.state.rows);
179 return;
180
181 case Qt::Key_0: nibble = 0; break;
182 case Qt::Key_1: nibble = 1; break;
183 case Qt::Key_2: nibble = 2; break;
184 case Qt::Key_3: nibble = 3; break;
185 case Qt::Key_4: nibble = 4; break;
186 case Qt::Key_5: nibble = 5; break;
187 case Qt::Key_6: nibble = 6; break;
188 case Qt::Key_7: nibble = 7; break;
189 case Qt::Key_8: nibble = 8; break;
190 case Qt::Key_9: nibble = 9; break;
191 case Qt::Key_A: nibble = 10; break;
192 case Qt::Key_B: nibble = 11; break;
193 case Qt::Key_C: nibble = 12; break;
194 case Qt::Key_D: nibble = 13; break;
195 case Qt::Key_E: nibble = 14; break;
196 case Qt::Key_F: nibble = 15; break;
197 }
198
199 if(cursorX >= 10) {
200 //not on an offset
201 cursorX -= 10;
202 if((cursorX % 3) != 2) {
203 //not on a space
204 bool cursorNibble = (cursorX % 3) == 1; //0 = high, 1 = low
205 cursorX /= 3;
206 if(cursorX < hexEdit.state.columns) {
207 //not in ANSI region
208 unsigned offset = hexEdit.state.offset + (cursorY * hexEdit.state.columns + cursorX);
209
210 if(offset >= hexEdit.state.length) return; //do not edit past end of file
211 uint8_t data = hexEdit.onRead(offset);
212
213 //write modified value
214 if(cursorNibble == 1) {
215 data = (data & 0xf0) | (nibble << 0);
216 } else {
217 data = (data & 0x0f) | (nibble << 4);
218 }
219 if(hexEdit.onWrite) hexEdit.onWrite(offset, data);
220
221 //auto-advance cursor to next nibble/byte
222 unsigned step = 1;
223 if(cursorNibble && cursorX != hexEdit.state.columns - 1) step = 2;
224 cursor.setPosition(cursor.position() + step);
225 qtHexEdit->setTextCursor(cursor);
226
227 //refresh output to reflect modified data
228 update();
229 }
230 }
231 }
232 }
233
234 //number of actual rows
235 signed pHexEdit::rows() {
236 return (max(1u, hexEdit.state.length) + hexEdit.state.columns - 1) / hexEdit.state.columns;
237 }
238
239 //number of scrollable row positions
240 signed pHexEdit::rowsScrollable() {
241 return max(0u, rows() - hexEdit.state.rows);
242 }
243
244 void pHexEdit::scrollTo(signed position) {
245 if(position > rowsScrollable()) position = rowsScrollable();
246 if(position < 0) position = 0;
247 qtScroll->setSliderPosition(position);
248 }
249
250 void pHexEdit::onScroll() {
251 if(locked) return;
252 unsigned offset = qtScroll->sliderPosition();
253 hexEdit.state.offset = offset * hexEdit.state.columns;
254 update();
255 }
256
257 void pHexEdit::QtHexEdit::keyPressEvent(QKeyEvent* event) {
258 self.keyPressEvent(event);
259 }
260
261 void pHexEdit::QtHexEdit::keyPressEventAcknowledge(QKeyEvent* event) {
262 QTextEdit::keyPressEvent(event);
263 }
264
265 void pHexEdit::QtHexEdit::wheelEvent(QWheelEvent* event) {
266 if(event->orientation() == Qt::Vertical) {
267 signed offset = event->delta() < 0 ? +1 : -1;
268 self.scrollTo(self.qtScroll->sliderPosition() + offset);
269 event->accept();
270 }
271 }
272
273 bool pHexEdit::QtHexEditScrollBar::event(QEvent* event) {
274 if(event->type() == QEvent::Wheel) {
275 QWheelEvent* wheelEvent = (QWheelEvent*)event;
276 if(wheelEvent->orientation() == Qt::Vertical) {
277 signed offset = wheelEvent->delta() < 0 ? +1 : -1;
278 self.scrollTo(sliderPosition() + offset);
279 return true;
280 }
281 }
282 return QScrollBar::event(event);
283 }
284
285 }