From d4401f9705cef1e04d0a165684b0906677ec498c Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Thu, 4 Apr 2019 13:38:57 +0200 Subject: [PATCH] Update qhexedit2 library to version 0.8.6 See issue #1808. --- libs/qhexedit/src/commands.h | 4 +- libs/qhexedit/src/qhexedit.cpp | 75 +++++++++++++++++++++------------- libs/qhexedit/src/qhexedit.h | 56 +++++++++++++------------ 3 files changed, 79 insertions(+), 56 deletions(-) diff --git a/libs/qhexedit/src/commands.h b/libs/qhexedit/src/commands.h index 9c34683a..40fcfa21 100644 --- a/libs/qhexedit/src/commands.h +++ b/libs/qhexedit/src/commands.h @@ -14,8 +14,8 @@ remove characters. A manipulation stores allways two actions 1. redo (or do) action 2. undo action. -CharCommand also supports command compression via mergeWidht(). This allows -the user to execute a undo command contation e.g. 3 steps in a single command. +CharCommand also supports command compression via mergeWidht(). This enables +the user to perform an undo command e.g. 3 steps in a single command. If you for example insert a new byt "34" this means for the editor doing 3 steps: insert a "00", overwrite it with "03" and the overwrite it with "34". These 3 steps are combined into a single step, insert a "34". diff --git a/libs/qhexedit/src/qhexedit.cpp b/libs/qhexedit/src/qhexedit.cpp index 417aa548..5ac480fb 100644 --- a/libs/qhexedit/src/qhexedit.cpp +++ b/libs/qhexedit/src/qhexedit.cpp @@ -114,7 +114,7 @@ int QHexEdit::addressWidth() if (size > Q_INT64_C(0x100000000)){ n += 8; size /= Q_INT64_C(0x100000000);} if (size > 0x10000){ n += 4; size /= 0x10000;} if (size > 0x100){ n += 2; size /= 0x100;} - if (size > 0x10){ n += 1; size /= 0x10;} + if (size > 0x10){ n += 1;} if (n > _addressWidth) return n; @@ -423,6 +423,12 @@ QString QHexEdit::selectionToReadableString() return toReadable(ba); } +QString QHexEdit::selectedData() +{ + QByteArray ba = _chunks->data(getSelectionBegin(), getSelectionEnd() - getSelectionBegin()).toHex(); + return ba; +} + void QHexEdit::setFont(const QFont &font) { QWidget::setFont(font); @@ -807,16 +813,14 @@ void QHexEdit::mouseMoveEvent(QMouseEvent * event) void QHexEdit::mousePressEvent(QMouseEvent * event) { - if (event->button() != Qt::RightButton) + _blink = false; + viewport()->update(); + qint64 cPos = cursorPosition(event->pos()); + if (cPos >= 0) { - _blink = false; - viewport()->update(); - qint64 cPos = cursorPosition(event->pos()); - if (cPos >= 0) - { + if (event->button() != Qt::RightButton) resetSelection(cPos); - setCursorPosition(cPos); - } + setCursorPosition(cPos); } } @@ -900,7 +904,7 @@ void QHexEdit::paintEvent(QPaintEvent *event) if (_asciiArea) { int ch = (uchar)_dataShown.at(bPosLine + colIdx); - if ( ch < 0x20 ) + if ( ch < ' ' || ch > '~' ) ch = '.'; r.setRect(pxPosAsciiX2, pxPosY - _pxCharHeight + _pxSelectionSub, _pxCharWidth, _pxCharHeight); painter.fillRect(r, c); @@ -913,23 +917,38 @@ void QHexEdit::paintEvent(QPaintEvent *event) painter.setPen(viewport()->palette().color(QPalette::WindowText)); } - // paint cursor - if (_blink && !_readOnly && hasFocus()) - painter.fillRect(_cursorRect, this->palette().color(QPalette::WindowText)); - else + // _cursorPosition counts in 2, _bPosFirst counts in 1 + int hexPositionInShowData = _cursorPosition - 2 * _bPosFirst; + + // due to scrolling the cursor can go out of the currently displayed data + if ((hexPositionInShowData >= 0) && (hexPositionInShowData < _hexDataShown.size())) { - painter.fillRect(QRect(_pxCursorX - pxOfsX, _pxCursorY - _pxCharHeight, _pxCharWidth, _pxCharHeight), viewport()->palette().color(QPalette::Base)); - if (_editAreaIsAscii) { - QByteArray ba = _dataShown.mid((_cursorPosition - _bPosFirst) / 2, 1); - if (ba != "") + // paint cursor + if (_readOnly) { - if (ba.at(0) <= ' ') - ba[0] = '.'; - painter.drawText(_pxCursorX - pxOfsX, _pxCursorY, ba); + // make the background stick out + QColor color = viewport()->palette().dark().color(); + painter.fillRect(QRect(_pxCursorX - pxOfsX, _pxCursorY - _pxCharHeight + _pxSelectionSub, _pxCharWidth, _pxCharHeight), color); + } + else + { + if (_blink && hasFocus()) + painter.fillRect(_cursorRect, this->palette().color(QPalette::WindowText)); + } + + if (_editAreaIsAscii) + { + // every 2 hex there is 1 ascii + int asciiPositionInShowData = hexPositionInShowData / 2; + int ch = (uchar)_dataShown.at(asciiPositionInShowData); + if (ch < ' ' || ch > '~') + ch = '.'; + painter.drawText(_pxCursorX - pxOfsX, _pxCursorY, QChar(ch)); + } + else + { + painter.drawText(_pxCursorX - pxOfsX, _pxCursorY, _hexDataShown.mid(hexPositionInShowData, 1)); } - } else { - painter.drawText(_pxCursorX - pxOfsX, _pxCursorY, _hexDataShown.mid(_cursorPosition - _bPosFirst, 1)); - } } // emit event, if size has changed @@ -952,11 +971,11 @@ void QHexEdit::resizeEvent(QResizeEvent *) pxFixGaps += _pxGapHexAscii; // +1 because the last hex value do not have space. so it is effective one char more - int charWidth = (viewport()->width() - pxFixGaps ) / _pxCharWidth + 1; + int charWidth = (viewport()->width() - pxFixGaps ) / _pxCharWidth + 1; // 2 hex alfa-digits 1 space 1 ascii per byte = 4; if ascii is disabled then 3 // to prevent devision by zero use the min value 1 - setBytesPerLine(std::max(charWidth / (_asciiArea ? 4 : 3),1)); + setBytesPerLine(std::max(charWidth / (_asciiArea ? 4 : 3),1)); } adjust(); } @@ -1016,12 +1035,12 @@ void QHexEdit::setSelection(qint64 pos) } } -int QHexEdit::getSelectionBegin() +qint64 QHexEdit::getSelectionBegin() { return _bSelectionBegin; } -int QHexEdit::getSelectionEnd() +qint64 QHexEdit::getSelectionEnd() { return _bSelectionEnd; } diff --git a/libs/qhexedit/src/qhexedit.h b/libs/qhexedit/src/qhexedit.h index 0bac26fb..f2fb0e68 100644 --- a/libs/qhexedit/src/qhexedit.h +++ b/libs/qhexedit/src/qhexedit.h @@ -19,7 +19,7 @@ /** \mainpage QHexEdit is a binary editor widget for Qt. -\version Version 0.8.3 +\version Version 0.8.6 \image html qhexedit.png */ @@ -34,12 +34,12 @@ the mouse or the keyboard to navigate inside the widget. If you hit the keys (0..9, a..f) you will change the data. Changed data is highlighted and can be accessed via data(). -Normaly QHexEdit works in the overwrite Mode. You can set overwriteMode(false) +Normally QHexEdit works in the overwrite mode. You can set overwrite mode(false) and insert data. In this case the size of data() increases. It is also possible to delete bytes (del or backspace), here the size of data decreases. You can select data with keyboard hits or mouse movements. The copy-key will -copy the selected data into the clipboard. The cut-key copies also but delets +copy the selected data into the clipboard. The cut-key copies also but deletes it afterwards. In overwrite mode, the paste function overwrites the content of the (does not change the length) data. In insert mode, clipboard data will be inserted. The clipboard content is expected in ASCII Hex notation. Unknown @@ -65,13 +65,13 @@ class QHEXEDIT_API QHexEdit : public QAbstractScrollArea */ Q_PROPERTY(bool addressArea READ addressArea WRITE setAddressArea) - /*! Property address area color sets (setAddressAreaColor()) the backgorund - color of address areas. You can also read the color (addressaAreaColor()). + /*! Property address area color sets (setAddressAreaColor()) the background + color of address areas. You can also read the color (addressAreaColor()). */ Q_PROPERTY(QColor addressAreaColor READ addressAreaColor WRITE setAddressAreaColor) /*! Property addressOffset is added to the Numbers of the Address Area. - A offset in the address area (left side) is sometimes usefull, whe you show + A offset in the address area (left side) is sometimes useful, whe you show only a segment of a complete memory picture. With setAddressOffset() you set this property - with addressOffset() you get the current value. */ @@ -88,8 +88,8 @@ class QHEXEDIT_API QHexEdit : public QAbstractScrollArea /*! Set and get bytes number per line.*/ Q_PROPERTY(int bytesPerLine READ bytesPerLine WRITE setBytesPerLine) - /*! Porperty cursorPosition sets or gets the position of the editor cursor - in QHexEdit. Every byte in data has to cursor positions: the lower and upper + /*! Property cursorPosition sets or gets the position of the editor cursor + in QHexEdit. Every byte in data has two cursor positions: the lower and upper Nibble. Maximum cursor position is factor two of data.size(). */ Q_PROPERTY(qint64 cursorPosition READ cursorPosition WRITE setCursorPosition) @@ -106,7 +106,7 @@ class QHEXEDIT_API QHexEdit : public QAbstractScrollArea */ Q_PROPERTY(bool hexCaps READ hexCaps WRITE setHexCaps) - /*! Property defines the dynamic calculation of bytesPerLine parameter depends of width of widget. + /*! Property defines the dynamic calculation of bytesPerLine parameter depends of width of widget. set this property true to avoid horizontal scrollbars and show the maximal possible data. defalut value is false*/ Q_PROPERTY(bool dynamicBytesPerLine READ dynamicBytesPerLine WRITE setDynamicBytesPerLine) @@ -114,26 +114,26 @@ class QHEXEDIT_API QHexEdit : public QAbstractScrollArea */ Q_PROPERTY(bool highlighting READ highlighting WRITE setHighlighting) - /*! Property highlighting color sets (setHighlightingColor()) the backgorund + /*! Property highlighting color sets (setHighlightingColor()) the background color of highlighted text areas. You can also read the color (highlightingColor()). */ Q_PROPERTY(QColor highlightingColor READ highlightingColor WRITE setHighlightingColor) - /*! Porperty overwrite mode sets (setOverwriteMode()) or gets (overwriteMode()) the mode + /*! Property overwrite mode sets (setOverwriteMode()) or gets (overwriteMode()) the mode in which the editor works. In overwrite mode the user will overwrite existing data. The size of data will be constant. In insert mode the size will grow, when inserting new data. */ Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode) - /*! Property selection color sets (setSelectionColor()) the backgorund + /*! Property selection color sets (setSelectionColor()) the background color of selected text areas. You can also read the color (selectionColor()). */ Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor) - /*! Porperty readOnly sets (setReadOnly()) or gets (isReadOnly) the mode + /*! Property readOnly sets (setReadOnly()) or gets (isReadOnly) the mode in which the editor works. In readonly mode the the user can only navigate through the data and select data; modifying is not possible. This property's default is false. @@ -151,18 +151,18 @@ public: // Access to data of qhexedit - /*! Sets the data of QHexEdit. The QIODevice will be opend just before reading + /*! Sets the data of QHexEdit. The QIODevice will be opened just before reading and closed immediately afterwards. This is to allow other programs to rewrite the file while editing it. */ bool setData(QIODevice &iODevice); - /*! Givs back the data as a QByteArray starting at position \param pos and + /*! Gives back the data as a QByteArray starting at position \param pos and delivering \param count bytes. */ QByteArray dataAt(qint64 pos, qint64 count=-1); - /*! Givs back the data into a \param iODevice starting at position \param pos + /*! Gives back the data into a \param iODevice starting at position \param pos and delivering \param count bytes. */ bool write(QIODevice &iODevice, qint64 pos=0, qint64 count=-1); @@ -209,18 +209,18 @@ public: void replace(qint64 pos, qint64 len, const QByteArray &ba); - // Utility functioins + // Utility functions /*! Calc cursor position from graphics position * \param point from where the cursor position should be calculated - * \return Cursor postioin + * \return Cursor position */ qint64 cursorPosition(QPoint point); - /*! Ensure the cursor to be visble + /*! Ensure the cursor to be visbile */ void ensureVisible(); - /*! Find first occurence of ba in QHexEdit data + /*! Find first occurrence of ba in QHexEdit data * \param ba Data to find * \param from Point where the search starts * \return pos if fond, else -1 @@ -232,7 +232,7 @@ public: */ bool isModified(); - /*! Find last occurence of ba in QHexEdit data + /*! Find last occurrence of ba in QHexEdit data * \param ba Data to find * \param from Point where the search starts * \return pos if fond, else -1 @@ -243,6 +243,10 @@ public: */ QString selectionToReadableString(); + /*! Return the selected content of QHexEdit as QByteArray + */ + QString selectedData(); + /*! Set Font of QHexEdit * \param font */ @@ -342,8 +346,8 @@ private: void resetSelection(qint64 pos); // set selectionStart and selectionEnd to pos void resetSelection(); // set selectionEnd to selectionStart void setSelection(qint64 pos); // set min (if below init) or max (if greater init) - int getSelectionBegin(); - int getSelectionEnd(); + qint64 getSelectionBegin(); + qint64 getSelectionEnd(); // Private utility functions void init(); @@ -352,13 +356,13 @@ private: private slots: void adjust(); // recalc pixel positions - void dataChangedPrivate(int idx=0); // emit dataChanged() signal + void dataChangedPrivate(int idx=0); // emit dataChanged() signal void refresh(); // ensureVisible() and readBuffers() void updateCursor(); // update blinking cursor private: // Name convention: pixel positions start with _px - int _pxCharWidth, _pxCharHeight; // char dimensions (dpendend on font) + int _pxCharWidth, _pxCharHeight; // char dimensions (dependend on font) int _pxPosHexX; // X-Pos of HeaxArea int _pxPosAdrX; // X-Pos of Address Area int _pxPosAsciiX; // X-Pos of Ascii Area @@ -403,7 +407,7 @@ private: QBuffer _bData; // buffer, when setup with QByteArray Chunks *_chunks; // IODevice based access to data QTimer _cursorTimer; // for blinking cursor - qint64 _cursorPosition; // absolute positioin of cursor, 1 Byte == 2 tics + qint64 _cursorPosition; // absolute position of cursor, 1 Byte == 2 tics QRect _cursorRect; // physical dimensions of cursor QByteArray _data; // QHexEdit's data, when setup with QByteArray QByteArray _dataShown; // data in the current View