Help/textbrowser: Keep text at the top visible when resizing and zooming

After resizing/zooming scroll to the position of the top element before
the resize. This allows us to remove the hack that delays jumping to the
anchor of an URL as well.

Fixes: QTCREATORBUG-4756
Change-Id: Ife29ba1cd0ad60448052b4d06a5dce7cc6bdf4ed
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Eike Ziller
2019-09-24 12:13:47 +02:00
parent 02fcc80f09
commit e20d90d9e3
2 changed files with 73 additions and 38 deletions

View File

@@ -99,12 +99,14 @@ void TextBrowserHelpViewer::scaleDown()
void TextBrowserHelpViewer::resetScale()
{
m_textBrowser->withFixedTopPosition([this] {
if (m_textBrowser->zoomCount != 0) {
m_textBrowser->forceFont = true;
m_textBrowser->zoomOut(m_textBrowser->zoomCount);
m_textBrowser->forceFont = false;
}
m_textBrowser->zoomCount = 0;
});
}
qreal TextBrowserHelpViewer::scale() const
@@ -114,6 +116,7 @@ qreal TextBrowserHelpViewer::scale() const
void TextBrowserHelpViewer::setScale(qreal scale)
{
m_textBrowser->withFixedTopPosition([this, &scale] {
m_textBrowser->forceFont = true;
if (scale > 10)
scale = 10;
@@ -126,6 +129,7 @@ void TextBrowserHelpViewer::setScale(qreal scale)
m_textBrowser->zoomOut(-diff);
m_textBrowser->zoomCount = int(scale);
m_textBrowser->forceFont = false;
});
}
QString TextBrowserHelpViewer::title() const
@@ -145,13 +149,11 @@ void TextBrowserHelpViewer::setSource(const QUrl &url)
slotLoadStarted();
m_textBrowser->setSource(url);
QTimer::singleShot(0, this, [this, url]() {
if (!url.fragment().isEmpty())
m_textBrowser->scrollToAnchor(url.fragment());
if (QScrollBar *hScrollBar = m_textBrowser->horizontalScrollBar())
hScrollBar->setValue(0);
slotLoadFinished();
});
}
void TextBrowserHelpViewer::setHtml(const QString &html)
@@ -330,24 +332,46 @@ QString TextBrowserHelpWidget::linkAt(const QPoint &pos)
return anchor;
}
void TextBrowserHelpWidget::withFixedTopPosition(const std::function<void()> &action)
{
const int topTextPosition = cursorForPosition({width() / 2, 0}).position();
action();
scrollToTextPosition(topTextPosition);
}
void TextBrowserHelpWidget::scrollToTextPosition(int position)
{
QTextCursor tc(document());
tc.setPosition(position);
const int dy = cursorRect(tc).top();
if (verticalScrollBar()) {
verticalScrollBar()->setValue(
std::min(verticalScrollBar()->value() + dy, verticalScrollBar()->maximum()));
}
}
void TextBrowserHelpWidget::scaleUp()
{
withFixedTopPosition([this] {
if (zoomCount < 10) {
zoomCount++;
forceFont = true;
zoomIn();
forceFont = false;
}
});
}
void TextBrowserHelpWidget::scaleDown()
{
withFixedTopPosition([this] {
if (zoomCount > -5) {
zoomCount--;
forceFont = true;
zoomOut();
forceFont = false;
}
});
}
void TextBrowserHelpWidget::contextMenuEvent(QContextMenuEvent *event)
@@ -429,6 +453,13 @@ void TextBrowserHelpWidget::mouseReleaseEvent(QMouseEvent *e)
QTextBrowser::mouseReleaseEvent(e);
}
void TextBrowserHelpWidget::resizeEvent(QResizeEvent *e)
{
const int topTextPosition = cursorForPosition({width() / 2, 0}).position();
QTextBrowser::resizeEvent(e);
scrollToTextPosition(topTextPosition);
}
void TextBrowserHelpWidget::setSource(const QUrl &name)
{
QTextBrowser::setSource(name);

View File

@@ -95,14 +95,18 @@ public:
void setSource(const QUrl &name) override;
void withFixedTopPosition(const std::function<void()> &action);
protected:
void contextMenuEvent(QContextMenuEvent *event) override;
bool eventFilter(QObject *obj, QEvent *event) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
void resizeEvent(QResizeEvent *e) override;
private:
QString linkAt(const QPoint &pos);
void scrollToTextPosition(int position);
int zoomCount;
bool forceFont;