| 1 | #include "HexViewer.h" |
| 2 | #include "OpenMSXConnection.h" |
| 3 | #include "CommClient.h" |
| 4 | #include "Settings.h" |
| 5 | #include <QScrollBar> |
| 6 | #include <QPaintEvent> |
| 7 | #include <QPainter> |
| 8 | #include <QToolTip> |
| 9 | #include <QAction> |
| 10 | #include <algorithm> |
| 11 | #include <cmath> |
| 12 | |
| 13 | static const int EXTRA_SPACING = 4; |
| 14 | |
| 15 | class HexRequest : public ReadDebugBlockCommand |
| 16 | { |
| 17 | public: |
| 18 | HexRequest(const QString& debuggable, unsigned offset_, unsigned size, |
| 19 | uint8_t* target, HexViewer& viewer_) |
| 20 | : ReadDebugBlockCommand(debuggable, offset_, size, target) |
| 21 | , offset(offset_) |
| 22 | , viewer(viewer_) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | void replyOk(const QString& message) override |
| 27 | { |
| 28 | copyData(message); |
| 29 | viewer.hexdataTransfered(this); |
| 30 | } |
| 31 | |
| 32 | void cancel() override |
| 33 | { |
| 34 | viewer.transferCancelled(this); |
| 35 | } |
| 36 | |
| 37 | unsigned offset; |
| 38 | |
| 39 | private: |
| 40 | HexViewer& viewer; |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | HexViewer::HexViewer(QWidget* parent) |
| 45 | : QFrame(parent) |
| 46 | { |
| 47 | setFrameStyle(WinPanel | Sunken); |
| 48 | setFocusPolicy(Qt::StrongFocus); |
| 49 | setBackgroundRole(QPalette::Base); |
| 50 | setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding)); |
| 51 | |
| 52 | vertScrollBar = new QScrollBar(Qt::Vertical, this); |
| 53 | vertScrollBar->setMinimum(0); |
| 54 | vertScrollBar->setSingleStep(1); |
| 55 | |
| 56 | frameL = frameT = frameB = frameWidth(); |
| 57 | frameR = frameL + vertScrollBar->sizeHint().width(); |
| 58 | |
| 59 | settingsChanged(); |
| 60 | |
| 61 | createActions(); |
| 62 | |
| 63 | connect(vertScrollBar, &QScrollBar::valueChanged, |
| 64 | this, &HexViewer::scrollBarChanged); |
| 65 | } |
| 66 | |
| 67 | void HexViewer::createActions() |
| 68 | { |
| 69 | fillWidthAction = new QAction(tr("&Fill with"), this); |
| 70 | fillWidthAction->setShortcut(tr("Ctrl+F")); |
| 71 | fillWidthAction->setStatusTip(tr("Fill the width with as many bytes as possible.")); |
| 72 | |
| 73 | fillWidth2Action = new QAction(tr("Power of &2"), this); |
| 74 | fillWidth2Action->setShortcut(tr("Ctrl+2")); |
| 75 | fillWidth2Action->setStatusTip(tr("Fill the with with the maximum power of 2 possible.")); |
| 76 | |
| 77 | setWith8Action = new QAction(tr("&8 bytes."), this); |
| 78 | setWith8Action->setShortcut(tr("Ctrl+8")); |
| 79 | setWith8Action->setStatusTip(tr("Set width to 8 bytes.")); |
| 80 | |
| 81 | setWith16Action = new QAction(tr("&16 bytes."), this); |
| 82 | setWith16Action->setShortcut(tr("Ctrl+6")); |
| 83 | setWith16Action->setStatusTip(tr("Set width to 16 bytes.")); |
| 84 | |
| 85 | setWith32Action = new QAction(tr("&32 bytes."), this); |
| 86 | setWith32Action->setShortcut(tr("Ctrl+3")); |
| 87 | setWith32Action->setStatusTip(tr("Set width to 32 bytes.")); |
| 88 | |
| 89 | connect(fillWidthAction, &QAction::triggered, this, &HexViewer::changeWidth); |
| 90 | connect(fillWidth2Action, &QAction::triggered, this, &HexViewer::changeWidth); |
| 91 | connect(setWith8Action, &QAction::triggered, this, &HexViewer::changeWidth); |
| 92 | connect(setWith16Action, &QAction::triggered, this, &HexViewer::changeWidth); |
| 93 | connect(setWith32Action, &QAction::triggered, this, &HexViewer::changeWidth); |
| 94 | |
| 95 | addAction(fillWidthAction); |
| 96 | addAction(fillWidth2Action); |
| 97 | addAction(setWith8Action); |
| 98 | addAction(setWith16Action); |
| 99 | addAction(setWith32Action); |
| 100 | setContextMenuPolicy(Qt::ActionsContextMenu); |
| 101 | } |
| 102 | |
| 103 | void HexViewer::changeWidth() |
| 104 | { |
| 105 | if (sender() == fillWidthAction) setDisplayMode(FILL_WIDTH); |
| 106 | else if (sender() == fillWidth2Action) setDisplayMode(FILL_WIDTH_POWEROF2); |
| 107 | else if (sender() == setWith8Action) setDisplayWidth(8); |
| 108 | else if (sender() == setWith16Action) setDisplayWidth(16); |
| 109 | else if (sender() == setWith32Action) setDisplayWidth(32); |
| 110 | } |
| 111 | |
| 112 | void HexViewer::setIsEditable(bool enabled) |
| 113 | { |
| 114 | isEditable = enabled; |
| 115 | setUseMarker(true); |
| 116 | } |
| 117 | |
| 118 | void HexViewer::setUseMarker(bool enabled) |
| 119 | { |
| 120 | useMarker = enabled; |
| 121 | // below we should check if current marker is visible etc |
| 122 | // but since in the debugger we will set this at instantiation |
| 123 | // and then never change it again this is a quicky in case we do later on |
| 124 | if (useMarker) { |
| 125 | hexMarkAddress = hexTopAddress; |
| 126 | } |
| 127 | update(); |
| 128 | } |
| 129 | |
| 130 | void HexViewer::setIsInteractive(bool enabled) |
| 131 | { |
| 132 | isInteractive = enabled; |
| 133 | vertScrollBar->setEnabled(enabled); |
| 134 | } |
| 135 | |
| 136 | void HexViewer::setDisplayMode(Mode mode) |
| 137 | { |
| 138 | displayMode = mode; |
| 139 | setSizes(); |
| 140 | } |
| 141 | |
| 142 | void HexViewer::setDisplayWidth(short width) |
| 143 | { |
| 144 | displayMode = FIXED; |
| 145 | horBytes = width; |
| 146 | setSizes(); |
| 147 | } |
| 148 | |
| 149 | void HexViewer::settingsChanged() |
| 150 | { |
| 151 | QFontMetrics fm(Settings::get().font(Settings::HEX_FONT)); |
| 152 | lineHeight = fm.height(); |
| 153 | charWidth = fm.horizontalAdvance("W"); |
| 154 | hexCharWidth = fm.horizontalAdvance("0ABCDEF") / 7; |
| 155 | xAddr = frameL + 8; |
| 156 | xData = xAddr + addressLength * hexCharWidth + charWidth; |
| 157 | dataWidth = 3 * hexCharWidth; |
| 158 | setSizes(); |
| 159 | } |
| 160 | |
| 161 | void HexViewer::setSizes() |
| 162 | { |
| 163 | visibleLines = (height() - frameT - frameB) / lineHeight; |
| 164 | partialBottomLine = (height() - frameT - frameB) != lineHeight * visibleLines; |
| 165 | |
| 166 | frameR = frameL; |
| 167 | int w; |
| 168 | |
| 169 | // fit display to width |
| 170 | if (displayMode != FIXED) { |
| 171 | // scrollbar width |
| 172 | int sbw = vertScrollBar->sizeHint().width(); |
| 173 | horBytes = 1; |
| 174 | int hb2 = 1; |
| 175 | w = width() - frameL - frameR - xData - dataWidth - 2 * charWidth - 8; |
| 176 | // calculate how many additional bytes can by displayed |
| 177 | while (w-sbw >= dataWidth + charWidth) { |
| 178 | ++horBytes; |
| 179 | if (horBytes == 2 * hb2) hb2 = horBytes; |
| 180 | w -= dataWidth + charWidth; |
| 181 | if ((horBytes & 3) == 0) w -= EXTRA_SPACING; |
| 182 | if ((horBytes & 7) == 0) w -= EXTRA_SPACING; |
| 183 | // remove scrollbar |
| 184 | if (horBytes * visibleLines >= debuggableSize) sbw = 0; |
| 185 | } |
| 186 | // limit to power of two if needed |
| 187 | if (displayMode == FILL_WIDTH_POWEROF2) |
| 188 | horBytes = hb2; |
| 189 | } |
| 190 | |
| 191 | // check if a scrollbar is needed |
| 192 | if (horBytes * visibleLines < debuggableSize) { |
| 193 | int maxLine = int(ceil(double(debuggableSize) / horBytes)) - visibleLines; |
| 194 | maxLine = std::max(maxLine, 0); |
| 195 | vertScrollBar->setMaximum(maxLine); |
| 196 | vertScrollBar->setPageStep(visibleLines); |
| 197 | frameR += vertScrollBar->sizeHint().width(); |
| 198 | vertScrollBar->setGeometry(width() - frameR, frameT, |
| 199 | vertScrollBar->sizeHint().width(), |
| 200 | height() - frameT - frameB); |
| 201 | vertScrollBar->show(); |
| 202 | } else { |
| 203 | vertScrollBar->hide(); |
| 204 | hexTopAddress = 0; |
| 205 | hexMarkAddress = 0; |
| 206 | } |
| 207 | |
| 208 | // now see were the chars are drawn |
| 209 | rightValuePos = xData + horBytes * dataWidth; |
| 210 | xChar = rightValuePos + charWidth + EXTRA_SPACING * (int(horBytes / 4) + |
| 211 | int(horBytes / 8)); |
| 212 | rightCharPos = xChar + horBytes * charWidth; |
| 213 | |
| 214 | if (isEnabled()) { |
| 215 | ///vertScrollBar->setValue(hexTopAddress / horBytes); |
| 216 | setTopLocation(horBytes * int(hexTopAddress / horBytes)); |
| 217 | } else { |
| 218 | update(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | QSize HexViewer::sizeHint() const |
| 223 | { |
| 224 | return {frameL + 16 + (6 + 3 * horBytes / 2) * fontMetrics().horizontalAdvance("A") + frameR, |
| 225 | frameT + 10 * fontMetrics().height() + frameB}; |
| 226 | } |
| 227 | |
| 228 | void HexViewer::resizeEvent(QResizeEvent* e) |
| 229 | { |
| 230 | QFrame::resizeEvent(e); |
| 231 | setSizes(); |
| 232 | } |
| 233 | |
| 234 | void HexViewer::wheelEvent(QWheelEvent* e) |
| 235 | { |
| 236 | wheelRemainder += e->angleDelta().y(); |
| 237 | const int delta = wheelRemainder / 40; |
| 238 | wheelRemainder %= 40; |
| 239 | if (delta) { |
| 240 | int v = vertScrollBar->value() - delta; |
| 241 | vertScrollBar->setValue(v); |
| 242 | } |
| 243 | e->accept(); |
| 244 | } |
| 245 | |
| 246 | void HexViewer::paintEvent(QPaintEvent* e) |
| 247 | { |
| 248 | // call parent for drawing the actual frame |
| 249 | QFrame::paintEvent(e); |
| 250 | |
| 251 | // exit if no debuggable is set |
| 252 | if (debuggableName.isEmpty()) return; |
| 253 | |
| 254 | QPainter p(this); |
| 255 | |
| 256 | // set font info |
| 257 | p.setFont(Settings::get().font(Settings::HEX_FONT)); |
| 258 | QColor fc(Settings::get().fontColor(Settings::HEX_FONT)); |
| 259 | int a = p.fontMetrics().ascent(); |
| 260 | |
| 261 | p.setPen(fc); |
| 262 | |
| 263 | // calc and set drawing bounds |
| 264 | QRect r(e->rect()); |
| 265 | if (r.left() < frameL) r.setLeft(frameL); |
| 266 | if (r.top() < frameT) r.setTop (frameT); |
| 267 | if (r.right() > width() - frameR - 1) r.setRight (width() - frameR - 1); |
| 268 | if (r.bottom() > height() - frameB - 1) r.setBottom(height() - frameB - 1); |
| 269 | p.setClipRect(r); |
| 270 | |
| 271 | // redraw background |
| 272 | p.fillRect(r, palette().color(QPalette::Base)); |
| 273 | |
| 274 | int y = frameT; |
| 275 | int address = hexTopAddress; |
| 276 | for (int i = 0; i < visibleLines+partialBottomLine; ++i) { |
| 277 | // print address |
| 278 | QString hexStr = QString("%1").arg(address, addressLength, 16, QChar('0')); |
| 279 | p.setPen(palette().color(QPalette::Text)); |
| 280 | p.drawText(xAddr, y + a, hexStr.toUpper()); |
| 281 | |
| 282 | // print bytes |
| 283 | int x = xData; |
| 284 | for (int j = 0; j < horBytes; ++j) { |
| 285 | // print data |
| 286 | if (address + j < debuggableSize) { |
| 287 | hexStr = QString("%1").arg(hexData[address + j], 2, 16, QChar('0')).toUpper(); |
| 288 | // draw marker if needed |
| 289 | if (useMarker || beingEdited) { |
| 290 | QRect b(x, y, dataWidth, lineHeight); |
| 291 | if ((address + j) == hexMarkAddress) { |
| 292 | p.fillRect(b, hasFocus ? Qt::cyan |
| 293 | : Qt::lightGray); |
| 294 | } |
| 295 | // are we being edited ?? |
| 296 | if (hasFocus && isEditable && !editedChars && |
| 297 | ((address + j) == hexMarkAddress)) { |
| 298 | if (beingEdited) { |
| 299 | p.fillRect(b, Qt::darkGreen); |
| 300 | if (cursorPosition) { |
| 301 | hexStr = QString("%1").arg(editValue, 2, 16).toUpper(); |
| 302 | } |
| 303 | } else { |
| 304 | p.drawRect(b); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // determine value colour |
| 310 | if (highlitChanges) { |
| 311 | QColor penClr = palette().color(QPalette::Text); |
| 312 | if (hexData[address + j] != previousHexData[address + j]) { |
| 313 | if ((address + j) != hexMarkAddress || !beingEdited) { |
| 314 | penClr = Qt::red; |
| 315 | } |
| 316 | } |
| 317 | if (((address + j) == hexMarkAddress) && |
| 318 | beingEdited && (cursorPosition == 0)) { |
| 319 | penClr = Qt::white; |
| 320 | } |
| 321 | p.setPen(penClr); |
| 322 | } |
| 323 | p.drawText(x, y + a, hexStr); |
| 324 | } |
| 325 | x += dataWidth; |
| 326 | |
| 327 | // at extra spacing |
| 328 | if ((j & 3) == 3) x += EXTRA_SPACING; |
| 329 | if ((j & 7) == 7) x += EXTRA_SPACING; |
| 330 | } |
| 331 | |
| 332 | // print characters |
| 333 | x += charWidth; |
| 334 | for (int j = 0; j < horBytes; ++j) { |
| 335 | if (address + j >= debuggableSize) break; |
| 336 | uint8_t chr = hexData[address + j]; |
| 337 | if (chr < 32 || chr > 127) chr = '.'; |
| 338 | // draw marker if needed |
| 339 | if (useMarker || beingEdited) { |
| 340 | QRect b(x, y, charWidth, lineHeight); |
| 341 | if ((address + j) == hexMarkAddress) { |
| 342 | p.fillRect(b, hasFocus ? Qt::cyan |
| 343 | : Qt::lightGray); |
| 344 | } |
| 345 | // are we being edited ?? |
| 346 | if (hasFocus && isEditable && editedChars && |
| 347 | ((address + j) == hexMarkAddress)) { |
| 348 | if (beingEdited) { |
| 349 | p.fillRect(b, Qt::darkGreen); |
| 350 | } else { |
| 351 | p.drawRect(b); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | // determine value colour |
| 356 | if (highlitChanges) { |
| 357 | QColor penClr = palette().color(QPalette::Text); |
| 358 | if (hexData[address + j] != previousHexData[address + j]) { |
| 359 | penClr = Qt::red; |
| 360 | } |
| 361 | if (((address + j) == hexMarkAddress) && beingEdited && |
| 362 | (cursorPosition == 0)) { |
| 363 | penClr = Qt::white; |
| 364 | } |
| 365 | p.setPen(penClr); |
| 366 | } |
| 367 | p.drawText(x, y + a, QString(chr)); |
| 368 | x += charWidth; |
| 369 | } |
| 370 | |
| 371 | y += lineHeight; |
| 372 | address += horBytes; |
| 373 | if (address >= debuggableSize) break; |
| 374 | } |
| 375 | // copy the new values to the old-values buffer |
| 376 | if (debuggableSize) { // because memcpy of nullptr is undefined, even if copying zero bytes |
| 377 | memcpy(previousHexData.data(), hexData.data(), debuggableSize); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void HexViewer::setDebuggable(const QString& name, int size) |
| 382 | { |
| 383 | debuggableSize = size; |
| 384 | hexData.assign(size, 0); |
| 385 | previousHexData.assign(size, 0); |
| 386 | if (size) { |
| 387 | debuggableName = name; |
| 388 | addressLength = 2 * int(ceil(log(double(size)) / log(2.0) / 8)); |
| 389 | hexTopAddress = 0; |
| 390 | hexMarkAddress = 0; |
| 391 | settingsChanged(); |
| 392 | } else { |
| 393 | debuggableName.clear(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void HexViewer::scrollBarChanged(int addr) |
| 398 | { |
| 399 | int start = addr * horBytes; |
| 400 | if (start == hexTopAddress) { |
| 401 | // nothing changed or a callback since we changed the value to |
| 402 | // the current hexTopAddress |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | if (!useMarker) { |
| 407 | setTopLocation(addr * horBytes); |
| 408 | emit locationChanged(addr * horBytes); |
| 409 | } else { |
| 410 | //maybe marker is still "fully" visible? |
| 411 | int size = horBytes * visibleLines; |
| 412 | hexTopAddress = start; |
| 413 | if ((start > hexMarkAddress) || ((start + size -1) < hexMarkAddress)) { |
| 414 | hexMarkAddress = (hexMarkAddress % horBytes) + |
| 415 | ((start < hexMarkAddress) ? size - horBytes : 0); |
| 416 | hexMarkAddress += start; |
| 417 | emit locationChanged(hexMarkAddress); |
| 418 | } |
| 419 | refresh(); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | void HexViewer::setLocation(int addr) |
| 424 | { |
| 425 | if (!useMarker) { |
| 426 | setTopLocation(addr); |
| 427 | } else { |
| 428 | // check if newmarker is in fully visible lines, |
| 429 | // if so we do not change hexTopAddress |
| 430 | if (addr != hexMarkAddress) { |
| 431 | emit locationChanged(addr); |
| 432 | } |
| 433 | hexMarkAddress = addr; |
| 434 | int size = horBytes * visibleLines; |
| 435 | if ((addr < hexTopAddress) || (addr >= (hexTopAddress+size))) { |
| 436 | setTopLocation(addr); |
| 437 | } |
| 438 | refresh(); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | void HexViewer::setTopLocation(int addr) |
| 443 | { |
| 444 | if (debuggableName.isEmpty()) { |
| 445 | return; |
| 446 | } |
| 447 | int start = horBytes * int(addr / horBytes); |
| 448 | if (!waitingForData || (start != hexTopAddress)) { |
| 449 | hexTopAddress = start; |
| 450 | refresh(); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | void HexViewer::hexdataTransfered(HexRequest* r) |
| 455 | { |
| 456 | transferCancelled(r); |
| 457 | update(); |
| 458 | } |
| 459 | |
| 460 | void HexViewer::transferCancelled(HexRequest* r) |
| 461 | { |
| 462 | delete r; |
| 463 | waitingForData = false; |
| 464 | // check whether a new value is available |
| 465 | if (int(hexTopAddress / horBytes) != vertScrollBar->value()) { |
| 466 | vertScrollBar->setValue(hexTopAddress / horBytes); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | void HexViewer::refresh() |
| 471 | { |
| 472 | // calculate data request |
| 473 | int size = horBytes * (visibleLines + partialBottomLine); |
| 474 | size = std::min(size, debuggableSize - hexTopAddress); |
| 475 | |
| 476 | // send data request |
| 477 | auto* req = new HexRequest( |
| 478 | debuggableName, hexTopAddress, size, &hexData[hexTopAddress], *this); |
| 479 | CommClient::instance().sendCommand(req); |
| 480 | waitingForData = true; |
| 481 | } |
| 482 | |
| 483 | void HexViewer::keyPressEvent(QKeyEvent* e) |
| 484 | { |
| 485 | // don't hanlde if not interactive |
| 486 | if ((!beingEdited && !useMarker) || !isInteractive) { |
| 487 | QFrame::keyPressEvent(e); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | bool setValue = false; |
| 492 | int newAddress = hexMarkAddress; |
| 493 | // entering a new digit ? |
| 494 | // for now hex only, do we need decimal entry also ?? |
| 495 | if (!editedChars && |
| 496 | ((e->key() >= Qt::Key_0 && e->key() <= Qt::Key_9) || |
| 497 | (e->key() >= Qt::Key_A && e->key() <= Qt::Key_F))) { |
| 498 | // calculate numercial value |
| 499 | int v = e->key() - Qt::Key_0; |
| 500 | if (v > 9) v -= Qt::Key_A - Qt::Key_0 - 10; |
| 501 | if (beingEdited) { |
| 502 | editValue = (editValue << 4) + v; |
| 503 | ++cursorPosition; |
| 504 | if (cursorPosition == 2) { |
| 505 | setValue = true; |
| 506 | ++newAddress; |
| 507 | } |
| 508 | } else { |
| 509 | editValue = v; |
| 510 | beingEdited = true; |
| 511 | cursorPosition = 1; |
| 512 | } |
| 513 | } else if (useMarker && e->key() == Qt::Key_Right) { |
| 514 | setValue = beingEdited & !editedChars; |
| 515 | ++newAddress; |
| 516 | cursorPosition = 0; |
| 517 | } else if (useMarker && e->key() == Qt::Key_Left) { |
| 518 | setValue = beingEdited & !editedChars; |
| 519 | --newAddress; |
| 520 | cursorPosition = 0; |
| 521 | } else if (useMarker && e->key() == Qt::Key_Up) { |
| 522 | setValue = beingEdited & !editedChars; |
| 523 | newAddress -= horBytes; |
| 524 | cursorPosition = 0; |
| 525 | } else if (useMarker && e->key() == Qt::Key_Down) { |
| 526 | setValue = beingEdited & !editedChars; |
| 527 | newAddress += horBytes; |
| 528 | cursorPosition = 0; |
| 529 | } else if (useMarker && e->key() == Qt::Key_Home) { |
| 530 | setValue = beingEdited & !editedChars; |
| 531 | newAddress = 0; |
| 532 | cursorPosition = 0; |
| 533 | } else if (useMarker && e->key() == Qt::Key_PageUp) { |
| 534 | setValue = beingEdited & !editedChars; |
| 535 | hexTopAddress -= horBytes * visibleLines; |
| 536 | newAddress -= horBytes * visibleLines; |
| 537 | cursorPosition = 0; |
| 538 | } else if (useMarker && e->key() == Qt::Key_PageDown) { |
| 539 | setValue = beingEdited & !editedChars; |
| 540 | hexTopAddress += horBytes * visibleLines; |
| 541 | newAddress += horBytes * visibleLines; |
| 542 | cursorPosition = 0; |
| 543 | } else if (useMarker && e->key() == Qt::Key_End) { |
| 544 | setValue = beingEdited & !editedChars; |
| 545 | newAddress = debuggableSize - 1; |
| 546 | cursorPosition = 0; |
| 547 | } else if (useMarker && e->key() == Qt::Key_Backspace) { |
| 548 | editedChars = !editedChars; |
| 549 | } else if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) { |
| 550 | if (beingEdited) |
| 551 | setValue = true; |
| 552 | else |
| 553 | cursorPosition = 0; |
| 554 | if (editedChars) |
| 555 | editValue = previousHexData[hexMarkAddress]; |
| 556 | newAddress++; |
| 557 | } else if (e->key() == Qt::Key_Shift || |
| 558 | e->key() == Qt::Key_Control || |
| 559 | e->key() == Qt::Key_Meta || |
| 560 | e->key() == Qt::Key_Alt || |
| 561 | e->key() == Qt::Key_AltGr || |
| 562 | e->key() == Qt::Key_CapsLock || |
| 563 | e->key() == Qt::Key_NumLock || |
| 564 | e->key() == Qt::Key_ScrollLock) { |
| 565 | // do nothing for these keys if editing chars |
| 566 | // if not editing chars they do nothing by deafult :-) |
| 567 | } else if (e->key() == Qt::Key_Escape) { |
| 568 | beingEdited = false; |
| 569 | e->accept(); |
| 570 | update(); |
| 571 | return; |
| 572 | } else if (editedChars) { |
| 573 | editValue = static_cast<uint8_t>((e->text().toLatin1())[0]); |
| 574 | setValue = true; |
| 575 | ++newAddress; |
| 576 | } else { |
| 577 | QFrame::keyPressEvent(e); |
| 578 | return; |
| 579 | } |
| 580 | |
| 581 | //apply changes |
| 582 | if (setValue) { |
| 583 | //TODO actually write the values to openMSX memory |
| 584 | //for now we change the value in our local buffer |
| 585 | previousHexData[hexMarkAddress] = char(editValue); |
| 586 | auto* req = new WriteDebugBlockCommand( |
| 587 | debuggableName, hexMarkAddress, 1, previousHexData.data()); |
| 588 | CommClient::instance().sendCommand(req); |
| 589 | |
| 590 | editValue = 0; |
| 591 | cursorPosition = 0; |
| 592 | beingEdited = editedChars; // keep editing if we were inputing chars |
| 593 | refresh(); |
| 594 | } |
| 595 | |
| 596 | // indicate key Event handled |
| 597 | e->accept(); |
| 598 | |
| 599 | // Move Marker if needed |
| 600 | if ((editedChars || useMarker) && (hexMarkAddress != newAddress)) { |
| 601 | if (newAddress < 0) newAddress += debuggableSize; |
| 602 | if (newAddress >= debuggableSize) newAddress -= debuggableSize; |
| 603 | // influencing hexTopAddress during Key_PageUp/Down might need following 2 lines. |
| 604 | if (hexTopAddress < 0) hexTopAddress += debuggableSize; |
| 605 | if (hexTopAddress >= debuggableSize) hexTopAddress -= debuggableSize; |
| 606 | |
| 607 | // Make scrolling downwards using cursors more "intuitive" |
| 608 | int addr = hexTopAddress + horBytes * visibleLines; |
| 609 | if ((newAddress >= addr) && (newAddress <= (addr + horBytes))) { |
| 610 | hexTopAddress += horBytes; |
| 611 | } |
| 612 | if (useMarker) { |
| 613 | setLocation(newAddress); |
| 614 | } else { |
| 615 | //we can only get here when not using Markers but if we |
| 616 | //are typing in chars in charEdit mode, so scrolling |
| 617 | //one line is covered in code above |
| 618 | hexMarkAddress = newAddress; |
| 619 | refresh(); |
| 620 | } |
| 621 | } else { |
| 622 | update(); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | int HexViewer::coorToOffset(int x, int y) const |
| 627 | { |
| 628 | int offset = -1; |
| 629 | if (x >= xData && x < rightValuePos) { |
| 630 | offset = 0; |
| 631 | x -= xData; |
| 632 | while (x > 4*dataWidth) { |
| 633 | offset += 4; |
| 634 | x -= 4*dataWidth + EXTRA_SPACING; |
| 635 | if (offset % 8 == 0) x -= EXTRA_SPACING; |
| 636 | } |
| 637 | offset += x / dataWidth; |
| 638 | } else if (x >= xChar && x < rightCharPos) { |
| 639 | offset = (x - xChar) / charWidth; |
| 640 | } |
| 641 | int yMaxOffset = frameT + (visibleLines+partialBottomLine) * lineHeight; |
| 642 | if (offset >= 0 && y < yMaxOffset) { |
| 643 | offset += horBytes * ((y - frameT) / lineHeight); |
| 644 | } |
| 645 | return offset; |
| 646 | } |
| 647 | |
| 648 | bool HexViewer::event(QEvent* e) |
| 649 | { |
| 650 | if (e->type() != QEvent::ToolTip) { |
| 651 | return QFrame::event(e); |
| 652 | } |
| 653 | |
| 654 | // calculate address for tooltip |
| 655 | auto* helpEvent = static_cast<QHelpEvent*>(e); |
| 656 | int offset = coorToOffset(helpEvent->x(), helpEvent->y()); |
| 657 | if (offset >= 0 && (hexTopAddress + offset) < debuggableSize) { |
| 658 | // create text with binary and decimal values |
| 659 | int address = hexTopAddress + offset; |
| 660 | uint8_t chr = hexData[address]; |
| 661 | QString text = QString("Address: %1").arg(QString("%1").arg(address, addressLength, 16, QChar('0')).toUpper()); |
| 662 | |
| 663 | // print 8 bit values |
| 664 | text += QString("\nBinary: %1 %2") |
| 665 | .arg(chr >> 4, 4, 2, QChar('0')) |
| 666 | .arg(chr & 0x000F, 4, 2, QChar('0')); |
| 667 | text += QString("\nDecimal: %1").arg(chr); |
| 668 | |
| 669 | // print 16 bit values if possible |
| 670 | if ((address + 1) < debuggableSize) { |
| 671 | unsigned wd = chr; |
| 672 | wd += 256 * hexData[address + 1]; |
| 673 | text += QString("\n\nWord: %1").arg(QString("%1").arg(wd, 4, 16, QChar('0')).toUpper()); |
| 674 | text += QString("\nBinary: %1 %2 %3 %4") |
| 675 | .arg((wd & 0xF000) >> 12, 4, 2, QChar('0')) |
| 676 | .arg((wd & 0x0F00) >> 8, 4, 2, QChar('0')) |
| 677 | .arg((wd & 0x00F0) >> 4, 4, 2, QChar('0')) |
| 678 | .arg((wd & 0x000F) >> 0, 4, 2, QChar('0')); |
| 679 | text += QString("\nDecimal: %1").arg(wd); |
| 680 | } |
| 681 | QToolTip::showText(helpEvent->globalPos(), text); |
| 682 | } else { |
| 683 | QToolTip::hideText(); |
| 684 | } |
| 685 | return QFrame::event(e); |
| 686 | } |
| 687 | |
| 688 | void HexViewer::mousePressEvent(QMouseEvent* e) |
| 689 | { |
| 690 | if (e->button() == Qt::LeftButton && isInteractive) { |
| 691 | int offset=coorToOffset(e->x(), e->y()); |
| 692 | if (offset >= 0) { |
| 693 | int addr = hexTopAddress + offset; |
| 694 | if (useMarker && (hexMarkAddress != addr)) { |
| 695 | setLocation(addr); |
| 696 | } else { |
| 697 | if (!useMarker) hexMarkAddress = addr; |
| 698 | editValue = 0; |
| 699 | cursorPosition = 0; |
| 700 | beingEdited = isEditable; |
| 701 | } |
| 702 | editedChars = (e->x() >= xChar); |
| 703 | } |
| 704 | update(); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | void HexViewer::focusInEvent(QFocusEvent* /*e*/) |
| 709 | { |
| 710 | hasFocus = true; |
| 711 | update(); |
| 712 | } |
| 713 | |
| 714 | void HexViewer::focusOutEvent(QFocusEvent* e) |
| 715 | { |
| 716 | if (e->lostFocus()) { |
| 717 | editValue = 0; |
| 718 | cursorPosition = 0; |
| 719 | beingEdited = false; |
| 720 | hasFocus = false; |
| 721 | } |
| 722 | update(); |
| 723 | } |