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