Skip to content

Commit 869eac8

Browse files
authored
feat: Add bottom margin with the height of a page (Megaxela#14)
* feat: Add bottom margin with the height of a page * fix: Fix crash when the document is empty
1 parent 29e30ee commit 869eac8

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

include/internal/QCodeEditor.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ class QCodeEditor : public QTextEdit
234234
*/
235235
void resizeEvent(QResizeEvent *e) override;
236236

237+
/**
238+
* @brief Method, update the bottom margin when the font changes.
239+
*/
240+
void changeEvent(QEvent *e) override;
241+
237242
/**
238243
* @brief Method, that's called on any key press, posted
239244
* into code editor widget. This method is overloaded for:
@@ -257,6 +262,12 @@ class QCodeEditor : public QTextEdit
257262
*/
258263
bool event(QEvent *e) override;
259264

265+
private Q_SLOTS:
266+
/**
267+
* @brief Slot, that updates the bottom margin.
268+
*/
269+
void updateBottomMargin();
270+
260271
private:
261272
/**
262273
* @brief Method for initializing default

src/internal/QCodeEditor.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void QCodeEditor::initFont()
5050
void QCodeEditor::performConnections()
5151
{
5252
connect(document(), &QTextDocument::blockCountChanged, this, &QCodeEditor::updateLineNumberAreaWidth);
53+
connect(document(), &QTextDocument::blockCountChanged, this, &QCodeEditor::updateBottomMargin);
5354

5455
connect(verticalScrollBar(), &QScrollBar::valueChanged, [this](int) { m_lineNumberArea->update(); });
5556

@@ -119,6 +120,14 @@ void QCodeEditor::resizeEvent(QResizeEvent *e)
119120
QTextEdit::resizeEvent(e);
120121

121122
updateLineGeometry();
123+
updateBottomMargin();
124+
}
125+
126+
void QCodeEditor::changeEvent(QEvent *e)
127+
{
128+
QTextEdit::changeEvent(e);
129+
if (e->type() == QEvent::FontChange)
130+
updateBottomMargin();
122131
}
123132

124133
void QCodeEditor::updateLineGeometry()
@@ -127,6 +136,20 @@ void QCodeEditor::updateLineGeometry()
127136
m_lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), m_lineNumberArea->sizeHint().width(), cr.height()));
128137
}
129138

139+
void QCodeEditor::updateBottomMargin()
140+
{
141+
auto doc = document();
142+
if (doc->blockCount() > 1)
143+
{
144+
// calling QTextFrame::setFrameFormat with an empty document makes the application crash
145+
auto rf = doc->rootFrame();
146+
auto format = rf->frameFormat();
147+
int margin = doc->documentMargin();
148+
format.setBottomMargin(qMax(margin, viewport()->height() - fontMetrics().height()) - margin);
149+
rf->setFrameFormat(format);
150+
}
151+
}
152+
130153
void QCodeEditor::updateLineNumberAreaWidth(int)
131154
{
132155
setViewportMargins(m_lineNumberArea->sizeHint().width(), 0, 0, 0);
@@ -818,7 +841,7 @@ bool QCodeEditor::tabReplace() const
818841
void QCodeEditor::setTabReplaceSize(int val)
819842
{
820843
m_tabReplace.fill(' ', val);
821-
setTabStopDistance(QFontMetrics(font()).horizontalAdvance(QString(val * 1000, ' ')) / 1000.0);
844+
setTabStopDistance(fontMetrics().horizontalAdvance(QString(val * 1000, ' ')) / 1000.0);
822845
}
823846

824847
int QCodeEditor::tabReplaceSize() const

0 commit comments

Comments
 (0)