refine markdown editor

This commit is contained in:
Le Tan 2018-04-28 21:37:44 +08:00
parent 5dbdcb8cba
commit d683afa271

View File

@ -190,7 +190,7 @@ bool VMdEditor::scrollToBlock(int p_blockNumber)
} }
// Get the visual offset of a block. // Get the visual offset of a block.
#define GETVISUALOFFSETY (contentOffsetY() + (int)rect.y()) #define GETVISUALOFFSETY(x) (contentOffsetY() + (int)(x).y())
void VMdEditor::makeBlockVisible(const QTextBlock &p_block) void VMdEditor::makeBlockVisible(const QTextBlock &p_block)
{ {
@ -213,9 +213,9 @@ void VMdEditor::makeBlockVisible(const QTextBlock &p_block)
bool moved = false; bool moved = false;
QAbstractTextDocumentLayout *layout = document()->documentLayout(); QAbstractTextDocumentLayout *layout = document()->documentLayout();
QRectF rect = layout->blockBoundingRect(p_block); QRectF rt = layout->blockBoundingRect(p_block);
int y = GETVISUALOFFSETY; int y = GETVISUALOFFSETY(rt);
int rectHeight = (int)rect.height(); int rectHeight = (int)rt.height();
// Handle the case rectHeight >= height. // Handle the case rectHeight >= height.
if (rectHeight >= height) { if (rectHeight >= height) {
@ -224,18 +224,18 @@ void VMdEditor::makeBlockVisible(const QTextBlock &p_block)
while (y + rectHeight < height && vbar->value() > vbar->minimum()) { while (y + rectHeight < height && vbar->value() > vbar->minimum()) {
moved = true; moved = true;
vbar->setValue(vbar->value() - vbar->singleStep()); vbar->setValue(vbar->value() - vbar->singleStep());
rect = layout->blockBoundingRect(p_block); rt = layout->blockBoundingRect(p_block);
rectHeight = (int)rect.height(); rectHeight = (int)rt.height();
y = GETVISUALOFFSETY; y = GETVISUALOFFSETY(rt);
} }
} else if (y > 0) { } else if (y > 0) {
// Need to scroll down. // Need to scroll down.
while (y > 0 && vbar->value() < vbar->maximum()) { while (y > 0 && vbar->value() < vbar->maximum()) {
moved = true; moved = true;
vbar->setValue(vbar->value() + vbar->singleStep()); vbar->setValue(vbar->value() + vbar->singleStep());
rect = layout->blockBoundingRect(p_block); rt = layout->blockBoundingRect(p_block);
rectHeight = (int)rect.height(); rectHeight = (int)rt.height();
y = GETVISUALOFFSETY; y = GETVISUALOFFSETY(rt);
} }
if (y < 0) { if (y < 0) {
@ -245,36 +245,28 @@ void VMdEditor::makeBlockVisible(const QTextBlock &p_block)
} }
} }
if (moved) {
qDebug() << "scroll to make huge block visible";
}
return; return;
} }
// There is an extra line leading in the layout, so there will always be a scroll
// action to scroll the page down.
while (y < 0 && vbar->value() > vbar->minimum()) { while (y < 0 && vbar->value() > vbar->minimum()) {
moved = true; moved = true;
vbar->setValue(vbar->value() - vbar->singleStep()); vbar->setValue(vbar->value() - vbar->singleStep());
rect = layout->blockBoundingRect(p_block); rt = layout->blockBoundingRect(p_block);
rectHeight = (int)rect.height(); y = GETVISUALOFFSETY(rt);
y = GETVISUALOFFSETY;
} }
if (moved) { if (moved) {
qDebug() << "scroll page down to make block visible";
return; return;
} }
while (y + rectHeight > height && vbar->value() < vbar->maximum()) { while (y + rectHeight > height && vbar->value() < vbar->maximum()) {
moved = true; moved = true;
vbar->setValue(vbar->value() + vbar->singleStep()); vbar->setValue(vbar->value() + vbar->singleStep());
rect = layout->blockBoundingRect(p_block); rt = layout->blockBoundingRect(p_block);
rectHeight = (int)rect.height(); rectHeight = (int)rt.height();
y = GETVISUALOFFSETY; y = GETVISUALOFFSETY(rt);
}
if (moved) {
qDebug() << "scroll page up to make block visible";
} }
} }
@ -396,9 +388,9 @@ bool VMdEditor::isBlockVisible(const QTextBlock &p_block)
} }
QAbstractTextDocumentLayout *layout = document()->documentLayout(); QAbstractTextDocumentLayout *layout = document()->documentLayout();
QRectF rect = layout->blockBoundingRect(p_block); QRectF rt = layout->blockBoundingRect(p_block);
int y = GETVISUALOFFSETY; int y = GETVISUALOFFSETY(rt);
int rectHeight = (int)rect.height(); int rectHeight = (int)rt.height();
return (y >= 0 && y < height) || (y < 0 && y + rectHeight > 0); return (y >= 0 && y < height) || (y < 0 && y + rectHeight > 0);
} }