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