diff options
Diffstat (limited to '')
| -rw-r--r-- | externals/qhexedit/qhexedit_p.cpp | 857 |
1 files changed, 0 insertions, 857 deletions
diff --git a/externals/qhexedit/qhexedit_p.cpp b/externals/qhexedit/qhexedit_p.cpp deleted file mode 100644 index 2a6885de8..000000000 --- a/externals/qhexedit/qhexedit_p.cpp +++ /dev/null | |||
| @@ -1,857 +0,0 @@ | |||
| 1 | #include "qhexedit_p.h" | ||
| 2 | #include "commands.h" | ||
| 3 | |||
| 4 | const int HEXCHARS_IN_LINE = 47; | ||
| 5 | const int GAP_ADR_HEX = 10; | ||
| 6 | const int GAP_HEX_ASCII = 16; | ||
| 7 | const int BYTES_PER_LINE = 16; | ||
| 8 | |||
| 9 | QHexEditPrivate::QHexEditPrivate(QScrollArea *parent) : QWidget(parent) | ||
| 10 | { | ||
| 11 | _undoStack = new QUndoStack(this); | ||
| 12 | |||
| 13 | _scrollArea = parent; | ||
| 14 | setAddressWidth(4); | ||
| 15 | setAddressOffset(0); | ||
| 16 | setAddressArea(true); | ||
| 17 | setAsciiArea(true); | ||
| 18 | setHighlighting(true); | ||
| 19 | setOverwriteMode(true); | ||
| 20 | setReadOnly(false); | ||
| 21 | setAddressAreaColor(QColor(0xd4, 0xd4, 0xd4, 0xff)); | ||
| 22 | setHighlightingColor(QColor(0xff, 0xff, 0x99, 0xff)); | ||
| 23 | setSelectionColor(QColor(0x6d, 0x9e, 0xff, 0xff)); | ||
| 24 | setFont(QFont("Courier", 10)); | ||
| 25 | |||
| 26 | _size = 0; | ||
| 27 | resetSelection(0); | ||
| 28 | |||
| 29 | setFocusPolicy(Qt::StrongFocus); | ||
| 30 | |||
| 31 | connect(&_cursorTimer, SIGNAL(timeout()), this, SLOT(updateCursor())); | ||
| 32 | _cursorTimer.setInterval(500); | ||
| 33 | _cursorTimer.start(); | ||
| 34 | } | ||
| 35 | |||
| 36 | void QHexEditPrivate::setAddressOffset(int offset) | ||
| 37 | { | ||
| 38 | _xData.setAddressOffset(offset); | ||
| 39 | adjust(); | ||
| 40 | } | ||
| 41 | |||
| 42 | int QHexEditPrivate::addressOffset() | ||
| 43 | { | ||
| 44 | return _xData.addressOffset(); | ||
| 45 | } | ||
| 46 | |||
| 47 | void QHexEditPrivate::setData(const QByteArray &data) | ||
| 48 | { | ||
| 49 | _xData.setData(data); | ||
| 50 | _undoStack->clear(); | ||
| 51 | adjust(); | ||
| 52 | setCursorPos(0); | ||
| 53 | } | ||
| 54 | |||
| 55 | QByteArray QHexEditPrivate::data() | ||
| 56 | { | ||
| 57 | return _xData.data(); | ||
| 58 | } | ||
| 59 | |||
| 60 | void QHexEditPrivate::setAddressAreaColor(const QColor &color) | ||
| 61 | { | ||
| 62 | _addressAreaColor = color; | ||
| 63 | update(); | ||
| 64 | } | ||
| 65 | |||
| 66 | QColor QHexEditPrivate::addressAreaColor() | ||
| 67 | { | ||
| 68 | return _addressAreaColor; | ||
| 69 | } | ||
| 70 | |||
| 71 | void QHexEditPrivate::setHighlightingColor(const QColor &color) | ||
| 72 | { | ||
| 73 | _highlightingColor = color; | ||
| 74 | update(); | ||
| 75 | } | ||
| 76 | |||
| 77 | QColor QHexEditPrivate::highlightingColor() | ||
| 78 | { | ||
| 79 | return _highlightingColor; | ||
| 80 | } | ||
| 81 | |||
| 82 | void QHexEditPrivate::setSelectionColor(const QColor &color) | ||
| 83 | { | ||
| 84 | _selectionColor = color; | ||
| 85 | update(); | ||
| 86 | } | ||
| 87 | |||
| 88 | QColor QHexEditPrivate::selectionColor() | ||
| 89 | { | ||
| 90 | return _selectionColor; | ||
| 91 | } | ||
| 92 | |||
| 93 | void QHexEditPrivate::setReadOnly(bool readOnly) | ||
| 94 | { | ||
| 95 | _readOnly = readOnly; | ||
| 96 | } | ||
| 97 | |||
| 98 | bool QHexEditPrivate::isReadOnly() | ||
| 99 | { | ||
| 100 | return _readOnly; | ||
| 101 | } | ||
| 102 | |||
| 103 | XByteArray & QHexEditPrivate::xData() | ||
| 104 | { | ||
| 105 | return _xData; | ||
| 106 | } | ||
| 107 | |||
| 108 | int QHexEditPrivate::indexOf(const QByteArray & ba, int from) | ||
| 109 | { | ||
| 110 | if (from > (_xData.data().length() - 1)) | ||
| 111 | from = _xData.data().length() - 1; | ||
| 112 | int idx = _xData.data().indexOf(ba, from); | ||
| 113 | if (idx > -1) | ||
| 114 | { | ||
| 115 | int curPos = idx*2; | ||
| 116 | setCursorPos(curPos + ba.length()*2); | ||
| 117 | resetSelection(curPos); | ||
| 118 | setSelection(curPos + ba.length()*2); | ||
| 119 | ensureVisible(); | ||
| 120 | } | ||
| 121 | return idx; | ||
| 122 | } | ||
| 123 | |||
| 124 | void QHexEditPrivate::insert(int index, const QByteArray & ba) | ||
| 125 | { | ||
| 126 | if (ba.length() > 0) | ||
| 127 | { | ||
| 128 | if (_overwriteMode) | ||
| 129 | { | ||
| 130 | QUndoCommand *arrayCommand= new ArrayCommand(&_xData, ArrayCommand::replace, index, ba, ba.length()); | ||
| 131 | _undoStack->push(arrayCommand); | ||
| 132 | emit dataChanged(); | ||
| 133 | } | ||
| 134 | else | ||
| 135 | { | ||
| 136 | QUndoCommand *arrayCommand= new ArrayCommand(&_xData, ArrayCommand::insert, index, ba, ba.length()); | ||
| 137 | _undoStack->push(arrayCommand); | ||
| 138 | emit dataChanged(); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | void QHexEditPrivate::insert(int index, char ch) | ||
| 144 | { | ||
| 145 | QUndoCommand *charCommand = new CharCommand(&_xData, CharCommand::insert, index, ch); | ||
| 146 | _undoStack->push(charCommand); | ||
| 147 | emit dataChanged(); | ||
| 148 | } | ||
| 149 | |||
| 150 | int QHexEditPrivate::lastIndexOf(const QByteArray & ba, int from) | ||
| 151 | { | ||
| 152 | from -= ba.length(); | ||
| 153 | if (from < 0) | ||
| 154 | from = 0; | ||
| 155 | int idx = _xData.data().lastIndexOf(ba, from); | ||
| 156 | if (idx > -1) | ||
| 157 | { | ||
| 158 | int curPos = idx*2; | ||
| 159 | setCursorPos(curPos); | ||
| 160 | resetSelection(curPos); | ||
| 161 | setSelection(curPos + ba.length()*2); | ||
| 162 | ensureVisible(); | ||
| 163 | } | ||
| 164 | return idx; | ||
| 165 | } | ||
| 166 | |||
| 167 | void QHexEditPrivate::remove(int index, int len) | ||
| 168 | { | ||
| 169 | if (len > 0) | ||
| 170 | { | ||
| 171 | if (len == 1) | ||
| 172 | { | ||
| 173 | if (_overwriteMode) | ||
| 174 | { | ||
| 175 | QUndoCommand *charCommand = new CharCommand(&_xData, CharCommand::replace, index, char(0)); | ||
| 176 | _undoStack->push(charCommand); | ||
| 177 | emit dataChanged(); | ||
| 178 | } | ||
| 179 | else | ||
| 180 | { | ||
| 181 | QUndoCommand *charCommand = new CharCommand(&_xData, CharCommand::remove, index, char(0)); | ||
| 182 | _undoStack->push(charCommand); | ||
| 183 | emit dataChanged(); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | else | ||
| 187 | { | ||
| 188 | QByteArray ba = QByteArray(len, char(0)); | ||
| 189 | if (_overwriteMode) | ||
| 190 | { | ||
| 191 | QUndoCommand *arrayCommand = new ArrayCommand(&_xData, ArrayCommand::replace, index, ba, ba.length()); | ||
| 192 | _undoStack->push(arrayCommand); | ||
| 193 | emit dataChanged(); | ||
| 194 | } | ||
| 195 | else | ||
| 196 | { | ||
| 197 | QUndoCommand *arrayCommand= new ArrayCommand(&_xData, ArrayCommand::remove, index, ba, len); | ||
| 198 | _undoStack->push(arrayCommand); | ||
| 199 | emit dataChanged(); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | void QHexEditPrivate::replace(int index, char ch) | ||
| 206 | { | ||
| 207 | QUndoCommand *charCommand = new CharCommand(&_xData, CharCommand::replace, index, ch); | ||
| 208 | _undoStack->push(charCommand); | ||
| 209 | resetSelection(); | ||
| 210 | emit dataChanged(); | ||
| 211 | } | ||
| 212 | |||
| 213 | void QHexEditPrivate::replace(int index, const QByteArray & ba) | ||
| 214 | { | ||
| 215 | QUndoCommand *arrayCommand= new ArrayCommand(&_xData, ArrayCommand::replace, index, ba, ba.length()); | ||
| 216 | _undoStack->push(arrayCommand); | ||
| 217 | resetSelection(); | ||
| 218 | emit dataChanged(); | ||
| 219 | } | ||
| 220 | |||
| 221 | void QHexEditPrivate::replace(int pos, int len, const QByteArray &after) | ||
| 222 | { | ||
| 223 | QUndoCommand *arrayCommand= new ArrayCommand(&_xData, ArrayCommand::replace, pos, after, len); | ||
| 224 | _undoStack->push(arrayCommand); | ||
| 225 | resetSelection(); | ||
| 226 | emit dataChanged(); | ||
| 227 | } | ||
| 228 | |||
| 229 | void QHexEditPrivate::setAddressArea(bool addressArea) | ||
| 230 | { | ||
| 231 | _addressArea = addressArea; | ||
| 232 | adjust(); | ||
| 233 | |||
| 234 | setCursorPos(_cursorPosition); | ||
| 235 | } | ||
| 236 | |||
| 237 | void QHexEditPrivate::setAddressWidth(int addressWidth) | ||
| 238 | { | ||
| 239 | _xData.setAddressWidth(addressWidth); | ||
| 240 | |||
| 241 | setCursorPos(_cursorPosition); | ||
| 242 | } | ||
| 243 | |||
| 244 | void QHexEditPrivate::setAsciiArea(bool asciiArea) | ||
| 245 | { | ||
| 246 | _asciiArea = asciiArea; | ||
| 247 | adjust(); | ||
| 248 | } | ||
| 249 | |||
| 250 | void QHexEditPrivate::setFont(const QFont &font) | ||
| 251 | { | ||
| 252 | QWidget::setFont(font); | ||
| 253 | adjust(); | ||
| 254 | } | ||
| 255 | |||
| 256 | void QHexEditPrivate::setHighlighting(bool mode) | ||
| 257 | { | ||
| 258 | _highlighting = mode; | ||
| 259 | update(); | ||
| 260 | } | ||
| 261 | |||
| 262 | void QHexEditPrivate::setOverwriteMode(bool overwriteMode) | ||
| 263 | { | ||
| 264 | _overwriteMode = overwriteMode; | ||
| 265 | } | ||
| 266 | |||
| 267 | bool QHexEditPrivate::overwriteMode() | ||
| 268 | { | ||
| 269 | return _overwriteMode; | ||
| 270 | } | ||
| 271 | |||
| 272 | void QHexEditPrivate::redo() | ||
| 273 | { | ||
| 274 | _undoStack->redo(); | ||
| 275 | emit dataChanged(); | ||
| 276 | setCursorPos(_cursorPosition); | ||
| 277 | update(); | ||
| 278 | } | ||
| 279 | |||
| 280 | void QHexEditPrivate::undo() | ||
| 281 | { | ||
| 282 | _undoStack->undo(); | ||
| 283 | emit dataChanged(); | ||
| 284 | setCursorPos(_cursorPosition); | ||
| 285 | update(); | ||
| 286 | } | ||
| 287 | |||
| 288 | QString QHexEditPrivate::toRedableString() | ||
| 289 | { | ||
| 290 | return _xData.toRedableString(); | ||
| 291 | } | ||
| 292 | |||
| 293 | |||
| 294 | QString QHexEditPrivate::selectionToReadableString() | ||
| 295 | { | ||
| 296 | return _xData.toRedableString(getSelectionBegin(), getSelectionEnd()); | ||
| 297 | } | ||
| 298 | |||
| 299 | void QHexEditPrivate::keyPressEvent(QKeyEvent *event) | ||
| 300 | { | ||
| 301 | int charX = (_cursorX - _xPosHex) / _charWidth; | ||
| 302 | int posX = (charX / 3) * 2 + (charX % 3); | ||
| 303 | int posBa = (_cursorY / _charHeight) * BYTES_PER_LINE + posX / 2; | ||
| 304 | |||
| 305 | |||
| 306 | /*****************************************************************************/ | ||
| 307 | /* Cursor movements */ | ||
| 308 | /*****************************************************************************/ | ||
| 309 | |||
| 310 | if (event->matches(QKeySequence::MoveToNextChar)) | ||
| 311 | { | ||
| 312 | setCursorPos(_cursorPosition + 1); | ||
| 313 | resetSelection(_cursorPosition); | ||
| 314 | } | ||
| 315 | if (event->matches(QKeySequence::MoveToPreviousChar)) | ||
| 316 | { | ||
| 317 | setCursorPos(_cursorPosition - 1); | ||
| 318 | resetSelection(_cursorPosition); | ||
| 319 | } | ||
| 320 | if (event->matches(QKeySequence::MoveToEndOfLine)) | ||
| 321 | { | ||
| 322 | setCursorPos(_cursorPosition | (2 * BYTES_PER_LINE -1)); | ||
| 323 | resetSelection(_cursorPosition); | ||
| 324 | } | ||
| 325 | if (event->matches(QKeySequence::MoveToStartOfLine)) | ||
| 326 | { | ||
| 327 | setCursorPos(_cursorPosition - (_cursorPosition % (2 * BYTES_PER_LINE))); | ||
| 328 | resetSelection(_cursorPosition); | ||
| 329 | } | ||
| 330 | if (event->matches(QKeySequence::MoveToPreviousLine)) | ||
| 331 | { | ||
| 332 | setCursorPos(_cursorPosition - (2 * BYTES_PER_LINE)); | ||
| 333 | resetSelection(_cursorPosition); | ||
| 334 | } | ||
| 335 | if (event->matches(QKeySequence::MoveToNextLine)) | ||
| 336 | { | ||
| 337 | setCursorPos(_cursorPosition + (2 * BYTES_PER_LINE)); | ||
| 338 | resetSelection(_cursorPosition); | ||
| 339 | } | ||
| 340 | |||
| 341 | if (event->matches(QKeySequence::MoveToNextPage)) | ||
| 342 | { | ||
| 343 | setCursorPos(_cursorPosition + (((_scrollArea->viewport()->height() / _charHeight) - 1) * 2 * BYTES_PER_LINE)); | ||
| 344 | resetSelection(_cursorPosition); | ||
| 345 | } | ||
| 346 | if (event->matches(QKeySequence::MoveToPreviousPage)) | ||
| 347 | { | ||
| 348 | setCursorPos(_cursorPosition - (((_scrollArea->viewport()->height() / _charHeight) - 1) * 2 * BYTES_PER_LINE)); | ||
| 349 | resetSelection(_cursorPosition); | ||
| 350 | } | ||
| 351 | if (event->matches(QKeySequence::MoveToEndOfDocument)) | ||
| 352 | { | ||
| 353 | setCursorPos(_xData.size() * 2); | ||
| 354 | resetSelection(_cursorPosition); | ||
| 355 | } | ||
| 356 | if (event->matches(QKeySequence::MoveToStartOfDocument)) | ||
| 357 | { | ||
| 358 | setCursorPos(0); | ||
| 359 | resetSelection(_cursorPosition); | ||
| 360 | } | ||
| 361 | |||
| 362 | /*****************************************************************************/ | ||
| 363 | /* Select commands */ | ||
| 364 | /*****************************************************************************/ | ||
| 365 | if (event->matches(QKeySequence::SelectAll)) | ||
| 366 | { | ||
| 367 | resetSelection(0); | ||
| 368 | setSelection(2*_xData.size() + 1); | ||
| 369 | } | ||
| 370 | if (event->matches(QKeySequence::SelectNextChar)) | ||
| 371 | { | ||
| 372 | int pos = _cursorPosition + 1; | ||
| 373 | setCursorPos(pos); | ||
| 374 | setSelection(pos); | ||
| 375 | } | ||
| 376 | if (event->matches(QKeySequence::SelectPreviousChar)) | ||
| 377 | { | ||
| 378 | int pos = _cursorPosition - 1; | ||
| 379 | setSelection(pos); | ||
| 380 | setCursorPos(pos); | ||
| 381 | } | ||
| 382 | if (event->matches(QKeySequence::SelectEndOfLine)) | ||
| 383 | { | ||
| 384 | int pos = _cursorPosition - (_cursorPosition % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE); | ||
| 385 | setCursorPos(pos); | ||
| 386 | setSelection(pos); | ||
| 387 | } | ||
| 388 | if (event->matches(QKeySequence::SelectStartOfLine)) | ||
| 389 | { | ||
| 390 | int pos = _cursorPosition - (_cursorPosition % (2 * BYTES_PER_LINE)); | ||
| 391 | setCursorPos(pos); | ||
| 392 | setSelection(pos); | ||
| 393 | } | ||
| 394 | if (event->matches(QKeySequence::SelectPreviousLine)) | ||
| 395 | { | ||
| 396 | int pos = _cursorPosition - (2 * BYTES_PER_LINE); | ||
| 397 | setCursorPos(pos); | ||
| 398 | setSelection(pos); | ||
| 399 | } | ||
| 400 | if (event->matches(QKeySequence::SelectNextLine)) | ||
| 401 | { | ||
| 402 | int pos = _cursorPosition + (2 * BYTES_PER_LINE); | ||
| 403 | setCursorPos(pos); | ||
| 404 | setSelection(pos); | ||
| 405 | } | ||
| 406 | |||
| 407 | if (event->matches(QKeySequence::SelectNextPage)) | ||
| 408 | { | ||
| 409 | int pos = _cursorPosition + (((_scrollArea->viewport()->height() / _charHeight) - 1) * 2 * BYTES_PER_LINE); | ||
| 410 | setCursorPos(pos); | ||
| 411 | setSelection(pos); | ||
| 412 | } | ||
| 413 | if (event->matches(QKeySequence::SelectPreviousPage)) | ||
| 414 | { | ||
| 415 | int pos = _cursorPosition - (((_scrollArea->viewport()->height() / _charHeight) - 1) * 2 * BYTES_PER_LINE); | ||
| 416 | setCursorPos(pos); | ||
| 417 | setSelection(pos); | ||
| 418 | } | ||
| 419 | if (event->matches(QKeySequence::SelectEndOfDocument)) | ||
| 420 | { | ||
| 421 | int pos = _xData.size() * 2; | ||
| 422 | setCursorPos(pos); | ||
| 423 | setSelection(pos); | ||
| 424 | } | ||
| 425 | if (event->matches(QKeySequence::SelectStartOfDocument)) | ||
| 426 | { | ||
| 427 | int pos = 0; | ||
| 428 | setCursorPos(pos); | ||
| 429 | setSelection(pos); | ||
| 430 | } | ||
| 431 | |||
| 432 | /*****************************************************************************/ | ||
| 433 | /* Edit Commands */ | ||
| 434 | /*****************************************************************************/ | ||
| 435 | if (!_readOnly) | ||
| 436 | { | ||
| 437 | /* Hex input */ | ||
| 438 | int key = int(event->text()[0].toLatin1()); | ||
| 439 | if ((key>='0' && key<='9') || (key>='a' && key <= 'f')) | ||
| 440 | { | ||
| 441 | if (getSelectionBegin() != getSelectionEnd()) | ||
| 442 | { | ||
| 443 | posBa = getSelectionBegin(); | ||
| 444 | remove(posBa, getSelectionEnd() - posBa); | ||
| 445 | setCursorPos(2*posBa); | ||
| 446 | resetSelection(2*posBa); | ||
| 447 | } | ||
| 448 | |||
| 449 | // If insert mode, then insert a byte | ||
| 450 | if (_overwriteMode == false) | ||
| 451 | if ((charX % 3) == 0) | ||
| 452 | { | ||
| 453 | insert(posBa, char(0)); | ||
| 454 | } | ||
| 455 | |||
| 456 | // Change content | ||
| 457 | if (_xData.size() > 0) | ||
| 458 | { | ||
| 459 | QByteArray hexValue = _xData.data().mid(posBa, 1).toHex(); | ||
| 460 | if ((charX % 3) == 0) | ||
| 461 | hexValue[0] = key; | ||
| 462 | else | ||
| 463 | hexValue[1] = key; | ||
| 464 | |||
| 465 | replace(posBa, QByteArray().fromHex(hexValue)[0]); | ||
| 466 | |||
| 467 | setCursorPos(_cursorPosition + 1); | ||
| 468 | resetSelection(_cursorPosition); | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | /* Cut & Paste */ | ||
| 473 | if (event->matches(QKeySequence::Cut)) | ||
| 474 | { | ||
| 475 | QString result = QString(); | ||
| 476 | for (int idx = getSelectionBegin(); idx < getSelectionEnd(); idx++) | ||
| 477 | { | ||
| 478 | result += _xData.data().mid(idx, 1).toHex() + " "; | ||
| 479 | if ((idx % 16) == 15) | ||
| 480 | result.append("\n"); | ||
| 481 | } | ||
| 482 | remove(getSelectionBegin(), getSelectionEnd() - getSelectionBegin()); | ||
| 483 | QClipboard *clipboard = QApplication::clipboard(); | ||
| 484 | clipboard->setText(result); | ||
| 485 | setCursorPos(getSelectionBegin()); | ||
| 486 | resetSelection(getSelectionBegin()); | ||
| 487 | } | ||
| 488 | |||
| 489 | if (event->matches(QKeySequence::Paste)) | ||
| 490 | { | ||
| 491 | QClipboard *clipboard = QApplication::clipboard(); | ||
| 492 | QByteArray ba = QByteArray().fromHex(clipboard->text().toLatin1()); | ||
| 493 | insert(_cursorPosition / 2, ba); | ||
| 494 | setCursorPos(_cursorPosition + 2 * ba.length()); | ||
| 495 | resetSelection(getSelectionBegin()); | ||
| 496 | } | ||
| 497 | |||
| 498 | |||
| 499 | /* Delete char */ | ||
| 500 | if (event->matches(QKeySequence::Delete)) | ||
| 501 | { | ||
| 502 | if (getSelectionBegin() != getSelectionEnd()) | ||
| 503 | { | ||
| 504 | posBa = getSelectionBegin(); | ||
| 505 | remove(posBa, getSelectionEnd() - posBa); | ||
| 506 | setCursorPos(2*posBa); | ||
| 507 | resetSelection(2*posBa); | ||
| 508 | } | ||
| 509 | else | ||
| 510 | { | ||
| 511 | if (_overwriteMode) | ||
| 512 | replace(posBa, char(0)); | ||
| 513 | else | ||
| 514 | remove(posBa, 1); | ||
| 515 | } | ||
| 516 | } | ||
| 517 | |||
| 518 | /* Backspace */ | ||
| 519 | if ((event->key() == Qt::Key_Backspace) && (event->modifiers() == Qt::NoModifier)) | ||
| 520 | { | ||
| 521 | if (getSelectionBegin() != getSelectionEnd()) | ||
| 522 | { | ||
| 523 | posBa = getSelectionBegin(); | ||
| 524 | remove(posBa, getSelectionEnd() - posBa); | ||
| 525 | setCursorPos(2*posBa); | ||
| 526 | resetSelection(2*posBa); | ||
| 527 | } | ||
| 528 | else | ||
| 529 | { | ||
| 530 | if (posBa > 0) | ||
| 531 | { | ||
| 532 | if (_overwriteMode) | ||
| 533 | replace(posBa - 1, char(0)); | ||
| 534 | else | ||
| 535 | remove(posBa - 1, 1); | ||
| 536 | setCursorPos(_cursorPosition - 2); | ||
| 537 | } | ||
| 538 | } | ||
| 539 | } | ||
| 540 | |||
| 541 | /* undo */ | ||
| 542 | if (event->matches(QKeySequence::Undo)) | ||
| 543 | { | ||
| 544 | undo(); | ||
| 545 | } | ||
| 546 | |||
| 547 | /* redo */ | ||
| 548 | if (event->matches(QKeySequence::Redo)) | ||
| 549 | { | ||
| 550 | redo(); | ||
| 551 | } | ||
| 552 | |||
| 553 | } | ||
| 554 | |||
| 555 | if (event->matches(QKeySequence::Copy)) | ||
| 556 | { | ||
| 557 | QString result = QString(); | ||
| 558 | for (int idx = getSelectionBegin(); idx < getSelectionEnd(); idx++) | ||
| 559 | { | ||
| 560 | result += _xData.data().mid(idx, 1).toHex() + " "; | ||
| 561 | if ((idx % 16) == 15) | ||
| 562 | result.append('\n'); | ||
| 563 | } | ||
| 564 | QClipboard *clipboard = QApplication::clipboard(); | ||
| 565 | clipboard->setText(result); | ||
| 566 | } | ||
| 567 | |||
| 568 | // Switch between insert/overwrite mode | ||
| 569 | if ((event->key() == Qt::Key_Insert) && (event->modifiers() == Qt::NoModifier)) | ||
| 570 | { | ||
| 571 | _overwriteMode = !_overwriteMode; | ||
| 572 | setCursorPos(_cursorPosition); | ||
| 573 | overwriteModeChanged(_overwriteMode); | ||
| 574 | } | ||
| 575 | |||
| 576 | ensureVisible(); | ||
| 577 | update(); | ||
| 578 | } | ||
| 579 | |||
| 580 | void QHexEditPrivate::mouseMoveEvent(QMouseEvent * event) | ||
| 581 | { | ||
| 582 | _blink = false; | ||
| 583 | update(); | ||
| 584 | int actPos = cursorPos(event->pos()); | ||
| 585 | setCursorPos(actPos); | ||
| 586 | setSelection(actPos); | ||
| 587 | } | ||
| 588 | |||
| 589 | void QHexEditPrivate::mousePressEvent(QMouseEvent * event) | ||
| 590 | { | ||
| 591 | _blink = false; | ||
| 592 | update(); | ||
| 593 | int cPos = cursorPos(event->pos()); | ||
| 594 | resetSelection(cPos); | ||
| 595 | setCursorPos(cPos); | ||
| 596 | } | ||
| 597 | |||
| 598 | void QHexEditPrivate::paintEvent(QPaintEvent *event) | ||
| 599 | { | ||
| 600 | QPainter painter(this); | ||
| 601 | |||
| 602 | // draw some patterns if needed | ||
| 603 | painter.fillRect(event->rect(), this->palette().color(QPalette::Base)); | ||
| 604 | if (_addressArea) | ||
| 605 | painter.fillRect(QRect(_xPosAdr, event->rect().top(), _xPosHex - GAP_ADR_HEX + 2, height()), _addressAreaColor); | ||
| 606 | if (_asciiArea) | ||
| 607 | { | ||
| 608 | int linePos = _xPosAscii - (GAP_HEX_ASCII / 2); | ||
| 609 | painter.setPen(Qt::gray); | ||
| 610 | painter.drawLine(linePos, event->rect().top(), linePos, height()); | ||
| 611 | } | ||
| 612 | |||
| 613 | painter.setPen(this->palette().color(QPalette::WindowText)); | ||
| 614 | |||
| 615 | // calc position | ||
| 616 | int firstLineIdx = ((event->rect().top()/ _charHeight) - _charHeight) * BYTES_PER_LINE; | ||
| 617 | if (firstLineIdx < 0) | ||
| 618 | firstLineIdx = 0; | ||
| 619 | int lastLineIdx = ((event->rect().bottom() / _charHeight) + _charHeight) * BYTES_PER_LINE; | ||
| 620 | if (lastLineIdx > _xData.size()) | ||
| 621 | lastLineIdx = _xData.size(); | ||
| 622 | int yPosStart = ((firstLineIdx) / BYTES_PER_LINE) * _charHeight + _charHeight; | ||
| 623 | |||
| 624 | // paint address area | ||
| 625 | if (_addressArea) | ||
| 626 | { | ||
| 627 | for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight) | ||
| 628 | { | ||
| 629 | QString address = QString("%1") | ||
| 630 | .arg(lineIdx + _xData.addressOffset(), _xData.realAddressNumbers(), 16, QChar('0')); | ||
| 631 | painter.drawText(_xPosAdr, yPos, address); | ||
| 632 | } | ||
| 633 | } | ||
| 634 | |||
| 635 | // paint hex area | ||
| 636 | QByteArray hexBa(_xData.data().mid(firstLineIdx, lastLineIdx - firstLineIdx + 1).toHex()); | ||
| 637 | QBrush highLighted = QBrush(_highlightingColor); | ||
| 638 | QPen colHighlighted = QPen(this->palette().color(QPalette::WindowText)); | ||
| 639 | QBrush selected = QBrush(_selectionColor); | ||
| 640 | QPen colSelected = QPen(Qt::white); | ||
| 641 | QPen colStandard = QPen(this->palette().color(QPalette::WindowText)); | ||
| 642 | |||
| 643 | painter.setBackgroundMode(Qt::TransparentMode); | ||
| 644 | |||
| 645 | for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight) | ||
| 646 | { | ||
| 647 | QByteArray hex; | ||
| 648 | int xPos = _xPosHex; | ||
| 649 | for (int colIdx = 0; ((lineIdx + colIdx) < _xData.size() && (colIdx < BYTES_PER_LINE)); colIdx++) | ||
| 650 | { | ||
| 651 | int posBa = lineIdx + colIdx; | ||
| 652 | if ((getSelectionBegin() <= posBa) && (getSelectionEnd() > posBa)) | ||
| 653 | { | ||
| 654 | painter.setBackground(selected); | ||
| 655 | painter.setBackgroundMode(Qt::OpaqueMode); | ||
| 656 | painter.setPen(colSelected); | ||
| 657 | } | ||
| 658 | else | ||
| 659 | { | ||
| 660 | if (_highlighting) | ||
| 661 | { | ||
| 662 | // hilight diff bytes | ||
| 663 | painter.setBackground(highLighted); | ||
| 664 | if (_xData.dataChanged(posBa)) | ||
| 665 | { | ||
| 666 | painter.setPen(colHighlighted); | ||
| 667 | painter.setBackgroundMode(Qt::OpaqueMode); | ||
| 668 | } | ||
| 669 | else | ||
| 670 | { | ||
| 671 | painter.setPen(colStandard); | ||
| 672 | painter.setBackgroundMode(Qt::TransparentMode); | ||
| 673 | } | ||
| 674 | } | ||
| 675 | } | ||
| 676 | |||
| 677 | // render hex value | ||
| 678 | if (colIdx == 0) | ||
| 679 | { | ||
| 680 | hex = hexBa.mid((lineIdx - firstLineIdx) * 2, 2); | ||
| 681 | painter.drawText(xPos, yPos, hex); | ||
| 682 | xPos += 2 * _charWidth; | ||
| 683 | } else { | ||
| 684 | hex = hexBa.mid((lineIdx + colIdx - firstLineIdx) * 2, 2).prepend(" "); | ||
| 685 | painter.drawText(xPos, yPos, hex); | ||
| 686 | xPos += 3 * _charWidth; | ||
| 687 | } | ||
| 688 | |||
| 689 | } | ||
| 690 | } | ||
| 691 | painter.setBackgroundMode(Qt::TransparentMode); | ||
| 692 | painter.setPen(this->palette().color(QPalette::WindowText)); | ||
| 693 | |||
| 694 | // paint ascii area | ||
| 695 | if (_asciiArea) | ||
| 696 | { | ||
| 697 | for (int lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx += BYTES_PER_LINE, yPos +=_charHeight) | ||
| 698 | { | ||
| 699 | int xPosAscii = _xPosAscii; | ||
| 700 | for (int colIdx = 0; ((lineIdx + colIdx) < _xData.size() && (colIdx < BYTES_PER_LINE)); colIdx++) | ||
| 701 | { | ||
| 702 | painter.drawText(xPosAscii, yPos, _xData.asciiChar(lineIdx + colIdx)); | ||
| 703 | xPosAscii += _charWidth; | ||
| 704 | } | ||
| 705 | } | ||
| 706 | } | ||
| 707 | |||
| 708 | // paint cursor | ||
| 709 | if (_blink && !_readOnly && hasFocus()) | ||
| 710 | { | ||
| 711 | if (_overwriteMode) | ||
| 712 | painter.fillRect(_cursorX, _cursorY + _charHeight - 2, _charWidth, 2, this->palette().color(QPalette::WindowText)); | ||
| 713 | else | ||
| 714 | painter.fillRect(_cursorX, _cursorY, 2, _charHeight, this->palette().color(QPalette::WindowText)); | ||
| 715 | } | ||
| 716 | |||
| 717 | if (_size != _xData.size()) | ||
| 718 | { | ||
| 719 | _size = _xData.size(); | ||
| 720 | emit currentSizeChanged(_size); | ||
| 721 | } | ||
| 722 | } | ||
| 723 | |||
| 724 | void QHexEditPrivate::setCursorPos(int position) | ||
| 725 | { | ||
| 726 | // delete cursor | ||
| 727 | _blink = false; | ||
| 728 | update(); | ||
| 729 | |||
| 730 | // cursor in range? | ||
| 731 | if (_overwriteMode) | ||
| 732 | { | ||
| 733 | if (position > (_xData.size() * 2 - 1)) | ||
| 734 | position = _xData.size() * 2 - 1; | ||
| 735 | } else { | ||
| 736 | if (position > (_xData.size() * 2)) | ||
| 737 | position = _xData.size() * 2; | ||
| 738 | } | ||
| 739 | |||
| 740 | if (position < 0) | ||
| 741 | position = 0; | ||
| 742 | |||
| 743 | // calc position | ||
| 744 | _cursorPosition = position; | ||
| 745 | _cursorY = (position / (2 * BYTES_PER_LINE)) * _charHeight + 4; | ||
| 746 | int x = (position % (2 * BYTES_PER_LINE)); | ||
| 747 | _cursorX = (((x / 2) * 3) + (x % 2)) * _charWidth + _xPosHex; | ||
| 748 | |||
| 749 | // immiadately draw cursor | ||
| 750 | _blink = true; | ||
| 751 | update(); | ||
| 752 | emit currentAddressChanged(_cursorPosition/2); | ||
| 753 | } | ||
| 754 | |||
| 755 | int QHexEditPrivate::cursorPos(QPoint pos) | ||
| 756 | { | ||
| 757 | int result = -1; | ||
| 758 | // find char under cursor | ||
| 759 | if ((pos.x() >= _xPosHex) && (pos.x() < (_xPosHex + HEXCHARS_IN_LINE * _charWidth))) | ||
| 760 | { | ||
| 761 | int x = (pos.x() - _xPosHex) / _charWidth; | ||
| 762 | if ((x % 3) == 0) | ||
| 763 | x = (x / 3) * 2; | ||
| 764 | else | ||
| 765 | x = ((x / 3) * 2) + 1; | ||
| 766 | int y = ((pos.y() - 3) / _charHeight) * 2 * BYTES_PER_LINE; | ||
| 767 | result = x + y; | ||
| 768 | } | ||
| 769 | return result; | ||
| 770 | } | ||
| 771 | |||
| 772 | int QHexEditPrivate::cursorPos() | ||
| 773 | { | ||
| 774 | return _cursorPosition; | ||
| 775 | } | ||
| 776 | |||
| 777 | void QHexEditPrivate::resetSelection() | ||
| 778 | { | ||
| 779 | _selectionBegin = _selectionInit; | ||
| 780 | _selectionEnd = _selectionInit; | ||
| 781 | } | ||
| 782 | |||
| 783 | void QHexEditPrivate::resetSelection(int pos) | ||
| 784 | { | ||
| 785 | if (pos < 0) | ||
| 786 | pos = 0; | ||
| 787 | pos = pos / 2; | ||
| 788 | _selectionInit = pos; | ||
| 789 | _selectionBegin = pos; | ||
| 790 | _selectionEnd = pos; | ||
| 791 | } | ||
| 792 | |||
| 793 | void QHexEditPrivate::setSelection(int pos) | ||
| 794 | { | ||
| 795 | if (pos < 0) | ||
| 796 | pos = 0; | ||
| 797 | pos = pos / 2; | ||
| 798 | if (pos >= _selectionInit) | ||
| 799 | { | ||
| 800 | _selectionEnd = pos; | ||
| 801 | _selectionBegin = _selectionInit; | ||
| 802 | } | ||
| 803 | else | ||
| 804 | { | ||
| 805 | _selectionBegin = pos; | ||
| 806 | _selectionEnd = _selectionInit; | ||
| 807 | } | ||
| 808 | } | ||
| 809 | |||
| 810 | int QHexEditPrivate::getSelectionBegin() | ||
| 811 | { | ||
| 812 | return _selectionBegin; | ||
| 813 | } | ||
| 814 | |||
| 815 | int QHexEditPrivate::getSelectionEnd() | ||
| 816 | { | ||
| 817 | return _selectionEnd; | ||
| 818 | } | ||
| 819 | |||
| 820 | |||
| 821 | void QHexEditPrivate::updateCursor() | ||
| 822 | { | ||
| 823 | if (_blink) | ||
| 824 | _blink = false; | ||
| 825 | else | ||
| 826 | _blink = true; | ||
| 827 | update(_cursorX, _cursorY, _charWidth, _charHeight); | ||
| 828 | } | ||
| 829 | |||
| 830 | void QHexEditPrivate::adjust() | ||
| 831 | { | ||
| 832 | _charWidth = fontMetrics().width(QLatin1Char('9')); | ||
| 833 | _charHeight = fontMetrics().height(); | ||
| 834 | |||
| 835 | _xPosAdr = 0; | ||
| 836 | if (_addressArea) | ||
| 837 | _xPosHex = _xData.realAddressNumbers()*_charWidth + GAP_ADR_HEX; | ||
| 838 | else | ||
| 839 | _xPosHex = 0; | ||
| 840 | _xPosAscii = _xPosHex + HEXCHARS_IN_LINE * _charWidth + GAP_HEX_ASCII; | ||
| 841 | |||
| 842 | // tell QAbstractScollbar, how big we are | ||
| 843 | setMinimumHeight(((_xData.size()/16 + 1) * _charHeight) + 5); | ||
| 844 | if(_asciiArea) | ||
| 845 | setMinimumWidth(_xPosAscii + (BYTES_PER_LINE * _charWidth)); | ||
| 846 | else | ||
| 847 | setMinimumWidth(_xPosHex + HEXCHARS_IN_LINE * _charWidth); | ||
| 848 | |||
| 849 | update(); | ||
| 850 | } | ||
| 851 | |||
| 852 | void QHexEditPrivate::ensureVisible() | ||
| 853 | { | ||
| 854 | // scrolls to cursorx, cusory (which are set by setCursorPos) | ||
| 855 | // x-margin is 3 pixels, y-margin is half of charHeight | ||
| 856 | _scrollArea->ensureVisible(_cursorX, _cursorY + _charHeight/2, 3, _charHeight/2 + 2); | ||
| 857 | } | ||