Skip to content

Commit

Permalink
QHexRenderer: Update font metrics (#55), refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Dax89 committed May 21, 2021
1 parent f3df32f commit 4d06da8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 130 deletions.
17 changes: 4 additions & 13 deletions document/qhexcursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,10 @@ struct QHexPosition {
int nibbleindex;

QHexPosition() = default;
QHexPosition(const QHexPosition&) = default;

QHexPosition& operator=(const QHexPosition& rhs) {
line = rhs.line;
column = rhs.column;
nibbleindex = rhs.nibbleindex;
return *this;
}

qint64 offset() const { return static_cast<qint64>(line * lineWidth) + column; }
int operator-(const QHexPosition& rhs) const { return this->offset() - rhs.offset(); }
bool operator==(const QHexPosition& rhs) const { return (line == rhs.line) && (column == rhs.column) && (nibbleindex == rhs.nibbleindex); }
bool operator!=(const QHexPosition& rhs) const { return (line != rhs.line) || (column != rhs.column) || (nibbleindex != rhs.nibbleindex); }
inline qint64 offset() const { return static_cast<qint64>(line * lineWidth) + column; }
inline int operator-(const QHexPosition& rhs) const { return this->offset() - rhs.offset(); }
inline bool operator==(const QHexPosition& rhs) const { return (line == rhs.line) && (column == rhs.column) && (nibbleindex == rhs.nibbleindex); }
inline bool operator!=(const QHexPosition& rhs) const { return (line != rhs.line) || (column != rhs.column) || (nibbleindex != rhs.nibbleindex); }
};

class QHexCursor : public QObject
Expand Down
10 changes: 5 additions & 5 deletions document/qhexdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ QHexDocument::QHexDocument(QHexBuffer *buffer, QObject *parent): QObject(parent)
m_buffer = buffer;
m_buffer->setParent(this); // Take Ownership
m_areaindent = DEFAULT_AREA_IDENTATION;
m_hexLineWidth = DEFAULT_HEX_LINE_LENGTH;
m_hexlinewidth = DEFAULT_HEX_LINE_LENGTH;

m_cursor = new QHexCursor(this);
m_cursor->setLineWidth(m_hexLineWidth);
m_cursor->setLineWidth(m_hexlinewidth);
m_metadata = new QHexMetadata(this);
m_metadata->setLineWidth(m_hexLineWidth);
m_metadata->setLineWidth(m_hexlinewidth);

connect(m_metadata, &QHexMetadata::metadataChanged, this, &QHexDocument::lineChanged);
connect(m_metadata, &QHexMetadata::metadataCleared, this, &QHexDocument::documentChanged);
Expand All @@ -36,10 +36,10 @@ QHexCursor *QHexDocument::cursor() const { return m_cursor; }

int QHexDocument::areaIndent() const { return m_areaindent;}
void QHexDocument::setAreaIndent(quint8 value) { m_areaindent = value; }
int QHexDocument::hexLineWidth() const { return m_hexLineWidth; }
int QHexDocument::hexLineWidth() const { return m_hexlinewidth; }
void QHexDocument::setHexLineWidth(quint8 value)
{
m_hexLineWidth = value;
m_hexlinewidth = value;
m_cursor->setLineWidth(value);
m_metadata->setLineWidth(value);
}
Expand Down
2 changes: 1 addition & 1 deletion document/qhexdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class QHexDocument: public QObject
QHexCursor* m_cursor;
quint64 m_baseaddress;
quint8 m_areaindent;
quint8 m_hexLineWidth;
quint8 m_hexlinewidth;
};

template<typename T> QHexDocument* QHexDocument::fromDevice(QIODevice* iodevice, QObject *parent)
Expand Down
9 changes: 5 additions & 4 deletions document/qhexrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#define HEX_UNPRINTABLE_CHAR '.'

QHexRenderer::QHexRenderer(QHexDocument* document, const QFontMetrics &fontmetrics, QObject *parent) : QObject(parent), m_document(document), m_fontmetrics(fontmetrics)
QHexRenderer::QHexRenderer(QHexDocument* document, const QFontMetricsF& fontmetrics, QObject *parent) : QObject(parent), m_document(document), m_fontmetrics(fontmetrics)
{
m_selectedarea = QHexRenderer::HexArea;
m_cursorenabled = false;
Expand All @@ -25,9 +25,9 @@ void QHexRenderer::renderFrame(QPainter *painter)
// see QHexView::paintEvent where the painter has been shifted horizontally

painter->drawLine(0,
headerLineCount() * lineHeight() - 1,
this->headerLineCount() * this->lineHeight() - 1,
endx,
headerLineCount() * lineHeight() - 1);
this->headerLineCount() * this->lineHeight() - 1);

painter->drawLine(hexx,
rect.top(),
Expand Down Expand Up @@ -64,6 +64,7 @@ void QHexRenderer::render(QPainter *painter, quint64 begin, quint64 end, quint64
}
}

void QHexRenderer::updateMetrics(const QFontMetricsF& fm) { m_fontmetrics = fm; }
void QHexRenderer::enableCursor(bool b) { m_cursorenabled = b; }

void QHexRenderer::selectArea(const QPoint &pt)
Expand Down Expand Up @@ -417,7 +418,7 @@ void QHexRenderer::drawAscii(QPainter *painter, const QPalette &palette, const Q

void QHexRenderer::drawHeader(QPainter *painter, const QPalette &palette)
{
QRect rect = QRect(0, 0, this->getEndColumnX(), headerLineCount() * lineHeight());
QRect rect = QRect(0, 0, this->getEndColumnX(), this->headerLineCount() * this->lineHeight());
QString hexheader;

for(quint8 i = 0; i < this->hexLineWidth(); i++)
Expand Down
3 changes: 2 additions & 1 deletion document/qhexrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class QHexRenderer : public QObject
enum { HeaderArea, AddressArea, HexArea, AsciiArea, ExtraArea };

public:
explicit QHexRenderer(QHexDocument* document, const QFontMetrics& fontmetrics, QObject *parent = nullptr);
explicit QHexRenderer(QHexDocument* document, const QFontMetricsF& fontmetrics, QObject *parent = nullptr);
void renderFrame(QPainter* painter);
void render(QPainter* painter, quint64 start, quint64 end, quint64 firstline); // begin included, end excluded
void updateMetrics(const QFontMetricsF& fm);
void enableCursor(bool b = true);
void selectArea(const QPoint& pt);

Expand Down
Loading

0 comments on commit 4d06da8

Please sign in to comment.