File: hex-edit.cpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (302 lines) | stat: -rw-r--r-- 8,489 bytes parent folder | download | duplicates (2)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#if defined(Hiro_HexEdit)

namespace hiro {

auto pHexEdit::construct() -> void {
  qtWidget = qtHexEdit = new QtHexEdit(*this);

  qtHexEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  qtHexEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  qtHexEdit->setTextInteractionFlags(Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse);

  qtLayout = new QHBoxLayout;
  qtLayout->setAlignment(Qt::AlignRight);
  qtLayout->setMargin(0);
  qtLayout->setSpacing(0);
  qtHexEdit->setLayout(qtLayout);

  qtScrollBar = new QtHexEditScrollBar(*this);
  qtScrollBar->setSingleStep(1);
  qtLayout->addWidget(qtScrollBar);

  qtScrollBar->connect(qtScrollBar, SIGNAL(actionTriggered(int)), SLOT(onScroll()));

  pWidget::construct();
  setBackgroundColor(state().backgroundColor);
  setForegroundColor(state().foregroundColor);
  _setState();
}

auto pHexEdit::destruct() -> void {
  delete qtScrollBar;
  delete qtLayout;
  delete qtHexEdit;
  qtWidget = qtHexEdit = nullptr;
  qtLayout = nullptr;
  qtScrollBar = nullptr;
}

auto pHexEdit::setAddress(u32 address) -> void {
  _setState();
}

auto pHexEdit::setBackgroundColor(Color color) -> void {
  static auto defaultColor = qtHexEdit->palette().color(QPalette::Base);

  auto palette = qtHexEdit->palette();
  palette.setColor(QPalette::Base, CreateColor(color, defaultColor));
  qtHexEdit->setPalette(palette);
  qtHexEdit->setAutoFillBackground((bool)color);
}

auto pHexEdit::setColumns(u32 columns) -> void {
  _setState();
}

auto pHexEdit::setForegroundColor(Color color) -> void {
  static auto defaultColor = qtHexEdit->palette().color(QPalette::Text);

  auto palette = qtHexEdit->palette();
  palette.setColor(QPalette::Text, color ? CreateColor(color) : defaultColor);
  qtHexEdit->setPalette(palette);
}

auto pHexEdit::setLength(u32 length) -> void {
  _setState();
}

auto pHexEdit::setRows(u32 rows) -> void {
  _setState();
}

auto pHexEdit::update() -> void {
  if(!state().onRead) {
    qtHexEdit->setPlainText("");
    return;
  }

  u32 cursorPosition = qtHexEdit->textCursor().position();

  string output;
  u32 address = state().address;
  for(u32 row = 0; row < state().rows; row++) {
    output.append(hex(address, 8L));
    output.append("  ");

    string hexdata;
    string ansidata = " ";

    for(u32 column = 0; column < state().columns; column++) {
      if(address < state().length) {
        u8 data = self().doRead(address++);
        hexdata.append(hex(data, 2L));
        hexdata.append(" ");
        ansidata.append(data >= 0x20 && data <= 0x7e ? (char)data : '.');
      } else {
        hexdata.append("   ");
        ansidata.append(" ");
      }
    }

    output.append(hexdata);
    output.append(ansidata);
    if(address >= state().length) break;
    if(row != state().rows - 1) output.append("\n");
  }

  qtHexEdit->setPlainText(QString::fromUtf8(output));
  QTextCursor cursor = qtHexEdit->textCursor();
  cursor.setPosition(cursorPosition);
  qtHexEdit->setTextCursor(cursor);
}

auto pHexEdit::_keyPressEvent(QKeyEvent* event) -> void {
  if(!state().onRead) return;

  //allow Ctrl+C (copy)
  if(event->key() == Qt::Key_C && event->modifiers() == Qt::ControlModifier) {
    qtHexEdit->keyPressEventAcknowledge(event);
    return;
  }

  //disallow other text operations (cut, paste, etc)
  if(event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)) return;

  QTextCursor cursor = qtHexEdit->textCursor();
  s32 lineWidth = 10 + (state().columns * 3) + 1 + state().columns + 1;
  s32 cursorY = cursor.position() / lineWidth;
  s32 cursorX = cursor.position() % lineWidth;

  u32 nibble = 0;
  switch(event->key()) {
  default: return;

  case Qt::Key_Left:
    if(cursorX > 0) {
      cursor.setPosition(cursor.position() - 1);
      qtHexEdit->setTextCursor(cursor);
    }
    return;

  case Qt::Key_Right:
    if(cursorX < lineWidth - 1) {
      cursor.setPosition(cursor.position() + 1);
      qtHexEdit->setTextCursor(cursor);
    }
    return;

  case Qt::Key_Home:
    cursor.setPosition(cursorY * lineWidth + 10);
    qtHexEdit->setTextCursor(cursor);
    return;

  case Qt::Key_End:
    cursor.setPosition(cursorY * lineWidth + 57);
    qtHexEdit->setTextCursor(cursor);
    return;

  case Qt::Key_Up:
    if(cursorY > 0) {
      cursor.setPosition(cursor.position() - lineWidth);
      qtHexEdit->setTextCursor(cursor);
    } else {
      _scrollTo(qtScrollBar->sliderPosition() - 1);
    }
    return;

  case Qt::Key_Down:
    if(cursorY >= _rows() - 1) {
      //cannot scroll down further
    } else if(cursorY < state().rows - 1) {
      cursor.setPosition(cursor.position() + lineWidth);
      qtHexEdit->setTextCursor(cursor);
    } else {
      _scrollTo(qtScrollBar->sliderPosition() + 1);
    }
    return;

  case Qt::Key_PageUp:
    _scrollTo(qtScrollBar->sliderPosition() - state().rows);
    return;

  case Qt::Key_PageDown:
    _scrollTo(qtScrollBar->sliderPosition() + state().rows);
    return;

  case Qt::Key_0: nibble =  0; break;
  case Qt::Key_1: nibble =  1; break;
  case Qt::Key_2: nibble =  2; break;
  case Qt::Key_3: nibble =  3; break;
  case Qt::Key_4: nibble =  4; break;
  case Qt::Key_5: nibble =  5; break;
  case Qt::Key_6: nibble =  6; break;
  case Qt::Key_7: nibble =  7; break;
  case Qt::Key_8: nibble =  8; break;
  case Qt::Key_9: nibble =  9; break;
  case Qt::Key_A: nibble = 10; break;
  case Qt::Key_B: nibble = 11; break;
  case Qt::Key_C: nibble = 12; break;
  case Qt::Key_D: nibble = 13; break;
  case Qt::Key_E: nibble = 14; break;
  case Qt::Key_F: nibble = 15; break;
  }

  if(cursorX >= 10) {
    //not on an offset
    cursorX -= 10;
    if((cursorX % 3) != 2) {
      //not on a space
      bool cursorNibble = (cursorX % 3) == 1;  //0 = high, 1 = low
      cursorX /= 3;
      if(cursorX < state().columns) {
        //not in ANSI region
        u32 address = state().address + (cursorY * state().columns + cursorX);

        if(address >= state().length) return;  //do not edit past end of file
        u8 data = self().doRead(address);

        //write modified value
        if(cursorNibble == 1) {
          data = (data & 0xf0) | (nibble << 0);
        } else {
          data = (data & 0x0f) | (nibble << 4);
        }
        self().doWrite(address, data);

        //auto-advance cursor to next nibble/byte
        u32 step = 1;
        if(cursorNibble && cursorX != state().columns - 1) step = 2;
        cursor.setPosition(cursor.position() + step);
        qtHexEdit->setTextCursor(cursor);

        //refresh output to reflect modified data
        update();
      }
    }
  }
}

//number of actual rows
auto pHexEdit::_rows() -> s32 {
  return (max(1u, state().length) + state().columns - 1) / state().columns;
}

//number of scrollable row positions
auto pHexEdit::_rowsScrollable() -> s32 {
  return max(0u, _rows() - state().rows);
}

auto pHexEdit::_scrollTo(s32 position) -> void {
  if(position > _rowsScrollable()) position = _rowsScrollable();
  if(position < 0) position = 0;
  qtScrollBar->setSliderPosition(position);
}

auto pHexEdit::_setState() -> void {
  auto lock = acquire();
  //add one if last row is not equal to column length (eg only part of the row is present)
  bool indivisible = state().columns == 0 || (state().length % state().columns) != 0;
  qtScrollBar->setRange(0, state().length / state().columns + indivisible - state().rows);
  qtScrollBar->setSliderPosition(state().address / state().columns);
  qtScrollBar->setPageStep(state().rows);
  update();
}

auto QtHexEdit::keyPressEvent(QKeyEvent* event) -> void {
  p._keyPressEvent(event);
}

auto QtHexEdit::keyPressEventAcknowledge(QKeyEvent* event) -> void {
  QTextEdit::keyPressEvent(event);
}

auto QtHexEdit::wheelEvent(QWheelEvent* event) -> void {
  if(event->orientation() == Qt::Vertical) {
    s32 offset = event->delta() < 0 ? +1 : -1;
    p._scrollTo(p.qtScrollBar->sliderPosition() + offset);
    event->accept();
  }
}

auto QtHexEditScrollBar::event(QEvent* event) -> bool {
  if(event->type() == QEvent::Wheel) {
    auto wheelEvent = (QWheelEvent*)event;
    if(wheelEvent->orientation() == Qt::Vertical) {
      s32 offset = wheelEvent->delta() < 0 ? +1 : -1;
      p._scrollTo(sliderPosition() + offset);
      return true;
    }
  }
  return QScrollBar::event(event);
}

auto QtHexEditScrollBar::onScroll() -> void {
  if(p.locked()) return;
  u32 address = sliderPosition();
  p.state().address = address * p.state().columns;
  p.update();
}

}

#endif