Skip to content

Commit 0ba470c

Browse files
committed
feat: 引入编辑器渲染接口;
1 parent f40d952 commit 0ba470c

38 files changed

Lines changed: 2146 additions & 1072 deletions

3rdparty/QHexView/document/qhexdocument.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ void QHexDocument::setMetaCommentVisible(bool b) {
8686
Q_EMIT metaCommentVisibleChanged(b);
8787
}
8888

89-
bool QHexDocument::metabgVisible() { return m_metabg; }
89+
bool QHexDocument::metabgVisible() const { return m_metabg; }
9090

91-
bool QHexDocument::metafgVisible() { return m_metafg; }
91+
bool QHexDocument::metafgVisible() const { return m_metafg; }
9292

93-
bool QHexDocument::metaCommentVisible() { return m_metacomment; }
93+
bool QHexDocument::metaCommentVisible() const { return m_metacomment; }
9494

9595
void QHexDocument::insertBookMarkAdjust(qsizetype offset, qsizetype length) {
9696
QMap<qsizetype, QString> bms;
@@ -218,7 +218,7 @@ void QHexDocument::removeBookMarkAdjustRevert(
218218
_bookmarks.insert(rmbms);
219219
}
220220

221-
bool QHexDocument::isDocSaved() { return m_undostack->isClean(); }
221+
bool QHexDocument::isDocSaved() const { return m_undostack->isClean(); }
222222

223223
void QHexDocument::setDocSaved(bool b) {
224224
if (b) {
@@ -230,9 +230,9 @@ void QHexDocument::setDocSaved(bool b) {
230230
Q_EMIT documentSaved(b);
231231
}
232232

233-
bool QHexDocument::isReadOnly() { return m_readonly; }
234-
bool QHexDocument::isKeepSize() { return m_keepsize; }
235-
bool QHexDocument::isLocked() { return m_islocked; }
233+
bool QHexDocument::isReadOnly() const { return m_readonly; }
234+
bool QHexDocument::isKeepSize() const { return m_keepsize; }
235+
bool QHexDocument::isLocked() const { return m_islocked; }
236236

237237
void QHexDocument::setLockKeepSize(bool b) { m_lockKeepSize = b; }
238238

3rdparty/QHexView/document/qhexdocument.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ class QHexDocument : public QObject {
5858

5959
bool setLockedFile(bool b);
6060
bool setKeepSize(bool b);
61-
bool isReadOnly();
62-
bool isKeepSize();
63-
bool isLocked();
61+
bool isReadOnly() const;
62+
bool isKeepSize() const;
63+
bool isLocked() const;
6464

6565
void setLockKeepSize(bool b);
6666
bool lockKeepSize() const;
@@ -110,16 +110,16 @@ class QHexDocument : public QObject {
110110
QList<qsizetype> &results,
111111
const std::function<bool()> &pred = [] { return true; });
112112

113-
bool isDocSaved();
113+
bool isDocSaved() const;
114114
void setDocSaved(bool b = true);
115115

116116
void setMetafgVisible(bool b);
117117
void setMetabgVisible(bool b);
118118
void setMetaCommentVisible(bool b);
119119

120-
bool metafgVisible();
121-
bool metabgVisible();
122-
bool metaCommentVisible();
120+
bool metafgVisible() const;
121+
bool metabgVisible() const;
122+
bool metaCommentVisible() const;
123123

124124
void insertBookMarkAdjust(qsizetype offset, qsizetype length);
125125
QMap<qsizetype, QString> removeBookMarkAdjust(qsizetype offset,

3rdparty/QHexView/document/qhexrenderer.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,16 @@ void QHexRenderer::renderFrame(QPainter *painter) {
9292
int asciix = this->getAsciiColumnX();
9393
int endx = this->getEndColumnX();
9494

95+
painter->setPen(m_borderColor);
96+
9597
// x coordinates are in absolute space
9698
// y coordinates are in viewport space
9799
// see QHexView::paintEvent where the painter has been shifted horizontally
98100

101+
auto h = this->headerLineCount() * this->lineHeight();
102+
99103
if (m_headerVisible)
100-
painter->drawLine(0, this->headerLineCount() * this->lineHeight() - 1,
101-
endx,
102-
this->headerLineCount() * this->lineHeight() - 1);
104+
painter->drawLine(0, h, endx, h);
103105
if (m_addressVisible)
104106
painter->drawLine(hexx, rect.top(), hexx, rect.bottom());
105107

@@ -872,6 +874,12 @@ void QHexRenderer::drawHeader(QPainter *painter) {
872874
painter->restore();
873875
}
874876

877+
void QHexRenderer::setBorderColor(const QColor &newBorderColor) {
878+
m_borderColor = newBorderColor;
879+
}
880+
881+
QColor QHexRenderer::borderColor() const { return m_borderColor; }
882+
875883
QHexCursor *QHexRenderer::cursor() const { return m_cursor; }
876884

877885
void QHexRenderer::setCursor(QHexCursor *newCursor) { m_cursor = newCursor; }

3rdparty/QHexView/document/qhexrenderer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ class QHexRenderer : public QObject {
101101
QColor selBackgroundColor() const;
102102
void setSelBackgroundColor(const QColor &newSelBackgroundColor);
103103

104+
QColor borderColor() const;
105+
void setBorderColor(const QColor &newBorderColor);
106+
104107
QHexCursor *cursor() const;
105108
void setCursor(QHexCursor *newCursor);
106109

@@ -111,6 +114,8 @@ class QHexRenderer : public QObject {
111114

112115
QByteArray getLine(qsizetype line) const;
113116
qsizetype rendererLength() const;
117+
118+
public:
114119
int getAddressWidth() const;
115120
int getHexColumnX() const;
116121
int getAsciiColumnX() const;
@@ -178,6 +183,7 @@ class QHexRenderer : public QObject {
178183
QColor m_bytesColor = Qt::black;
179184
QColor m_selectionColor = Qt::white;
180185
QColor m_selBackgroundColor = Qt::blue;
186+
QColor m_borderColor = Qt::white;
181187
/*==============================*/
182188
};
183189

3rdparty/QHexView/qhexview.cpp

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ void QHexView::keyPressEvent(QKeyEvent *e) {
323323
}
324324

325325
QPoint QHexView::absolutePosition(const QPoint &pos) const {
326-
QPoint shift(horizontalScrollBar()->value(), 0);
326+
auto margins = viewport()->contentsMargins();
327+
QPoint shift(horizontalScrollBar()->value() - margins.left(),
328+
-margins.top() * m_renderer->lineHeight());
327329
return pos + shift;
328330
}
329331

@@ -567,23 +569,34 @@ QColor QHexView::selBackgroundColor() const {
567569
return m_renderer->selBackgroundColor();
568570
}
569571

572+
QColor QHexView::borderColor() const { return m_renderer->borderColor(); }
573+
570574
void QHexView::setSelBackgroundColor(const QColor &newSelBackgroundColor) {
571575
m_renderer->setSelBackgroundColor(newSelBackgroundColor);
572576
Q_EMIT selBackgroundColorChanged();
577+
this->viewport()->update();
578+
}
579+
580+
void QHexView::setBorderColor(const QColor &newBorderColor) {
581+
m_renderer->setBorderColor(newBorderColor);
582+
Q_EMIT borderColorChanged();
583+
this->viewport()->update();
573584
}
574585

575586
void QHexView::setFontSize(qreal size) {
576587
Q_ASSERT(size > 0);
577588
auto font = this->font();
578589
font.setPointSizeF(size * m_scaleRate);
579590
this->setFont(font);
591+
this->viewport()->update();
580592
}
581593

582594
QColor QHexView::selectionColor() const { return m_renderer->selectionColor(); }
583595

584596
void QHexView::setSelectionColor(const QColor &newSelectionColor) {
585597
m_renderer->setSelectionColor(newSelectionColor);
586598
Q_EMIT selectionColorChanged();
599+
this->viewport()->update();
587600
}
588601

589602
QColor QHexView::bytesAlterBackground() const {
@@ -593,13 +606,15 @@ QColor QHexView::bytesAlterBackground() const {
593606
void QHexView::setBytesAlterBackground(const QColor &newBytesAlterBackground) {
594607
m_renderer->setBytesAlterBackground(newBytesAlterBackground);
595608
Q_EMIT bytesAlterBackgroundChanged();
609+
this->viewport()->update();
596610
}
597611

598612
QColor QHexView::bytesColor() const { return m_renderer->bytesColor(); }
599613

600614
void QHexView::setBytesColor(const QColor &newBytesColor) {
601615
m_renderer->setBytesColor(newBytesColor);
602616
Q_EMIT bytesColorChanged();
617+
this->viewport()->update();
603618
}
604619

605620
QColor QHexView::bytesBackground() const {
@@ -609,20 +624,23 @@ QColor QHexView::bytesBackground() const {
609624
void QHexView::setBytesBackground(const QColor &newBytesBackground) {
610625
m_renderer->setBytesBackground(newBytesBackground);
611626
Q_EMIT bytesBackgroundChanged();
627+
this->viewport()->update();
612628
}
613629

614630
QColor QHexView::addressColor() const { return m_renderer->addressColor(); }
615631

616632
void QHexView::setAddressColor(const QColor &newAddressColor) {
617633
m_renderer->setAddressColor(newAddressColor);
618634
Q_EMIT addressColorChanged();
635+
this->viewport()->update();
619636
}
620637

621638
QColor QHexView::headerColor() const { return m_renderer->headerColor(); }
622639

623640
void QHexView::setHeaderColor(const QColor &newHeaderColor) {
624641
m_renderer->setHeaderColor(newHeaderColor);
625642
Q_EMIT headerColorChanged();
643+
this->viewport()->update();
626644
}
627645

628646
void QHexView::mousePressEvent(QMouseEvent *e) {
@@ -764,21 +782,27 @@ void QHexView::paintEvent(QPaintEvent *e) {
764782
const int lineHeight = m_renderer->lineHeight();
765783
const int headerCount = m_renderer->headerLineCount();
766784

785+
auto m = viewport()->contentsMargins();
786+
767787
// these are lines from the point of view of the visible rect
768788
// where the first "headerCount" are taken by the header
769-
const int first = (r.top() / lineHeight); // included
770-
const int lastPlusOne = (r.bottom() / lineHeight) + 1; // excluded
789+
const int first = (r.top() / lineHeight); // included
790+
const int lastPlusOne =
791+
(r.bottom() / lineHeight) + 1 - m.top() - m.bottom(); // excluded
771792

772793
// compute document lines, adding firstVisible and removing the header
773794
// the max is necessary if the rect covers the header
774795
const qsizetype begin = firstVisible + std::max(first - headerCount, 0);
775796
const qsizetype end = firstVisible + std::max(lastPlusOne - headerCount, 0);
776797

798+
Q_EMIT onPaintCustomEventBegin();
777799
painter.save();
778-
painter.translate(-this->horizontalScrollBar()->value(), 0);
800+
auto xOff = this->horizontalScrollBar()->value();
801+
painter.translate(-xOff + m.left(), m.top() * m_renderer->lineHeight());
779802
m_renderer->render(&painter, begin, end, firstVisible);
780803
m_renderer->renderFrame(&painter);
781804
painter.restore();
805+
Q_EMIT onPaintCustomEvent(xOff, firstVisible, begin, end);
782806
}
783807

784808
void QHexView::moveToSelection() {
@@ -1160,6 +1184,7 @@ void QHexView::adjustScrollBars() {
11601184

11611185
auto docLines = m_renderer->documentLines();
11621186
auto visLines = this->visibleLines();
1187+
auto margins = viewport()->contentsMargins();
11631188

11641189
// modified by wingsummer,fix the scrollbar bug
11651190
if (docLines > visLines && !m_document->isEmpty()) {
@@ -1180,11 +1205,13 @@ void QHexView::adjustScrollBars() {
11801205
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
11811206
// +1 to see the rightmost vertical line, +2 seems more pleasant to the
11821207
// eyes
1183-
hscrollbar->setMaximum(documentWidth - viewportWidth + 2);
1208+
hscrollbar->setMaximum(documentWidth - viewportWidth + 2 +
1209+
margins.left() + margins.right());
11841210
} else {
11851211
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
11861212
hscrollbar->setValue(0);
1187-
hscrollbar->setMaximum(documentWidth);
1213+
hscrollbar->setMaximum(documentWidth + margins.left() +
1214+
margins.right());
11881215
}
11891216
}
11901217

@@ -1203,9 +1230,10 @@ qsizetype QHexView::lastVisibleLine() const {
12031230
}
12041231

12051232
qsizetype QHexView::visibleLines() const {
1206-
auto visLines =
1207-
qsizetype(std::ceil(this->height() / m_renderer->lineHeight() -
1208-
m_renderer->headerLineCount()));
1233+
auto margins = viewport()->contentsMargins();
1234+
auto visLines = qsizetype(std::ceil(
1235+
this->height() / m_renderer->lineHeight() -
1236+
m_renderer->headerLineCount() - margins.top() - margins.bottom()));
12091237
return std::min(visLines, m_renderer->documentLines());
12101238
}
12111239

0 commit comments

Comments
 (0)