fix zooming

This commit is contained in:
Martin Rotter 2022-04-12 19:37:29 +02:00
parent 994c377049
commit bc2dbfe70e
2 changed files with 48 additions and 5 deletions

View File

@ -29,6 +29,12 @@ TextBrowserViewer::TextBrowserViewer(QWidget* parent) : QTextBrowser(parent), m_
}
QVariant TextBrowserViewer::loadResource(int type, const QUrl& name) {
if (!m_reloadingWithResources) {
if (type == QTextDocument::ResourceType::ImageResource) {
m_resourcesForHtml.append(name);
}
}
return {};
}
@ -127,11 +133,19 @@ void TextBrowserViewer::bindToBrowser(WebBrowser* browser) {
void TextBrowserViewer::findText(const QString& text, bool backwards) {
if (!text.isEmpty()) {
bool found =
QTextBrowser::find(text, backwards ? QTextDocument::FindFlag::FindBackward : QTextDocument::FindFlag(0));
if (!found) {
textCursor().clearSelection();
moveCursor(QTextCursor::MoveOperation::Start);
QTextBrowser::find(text, backwards ? QTextDocument::FindFlag::FindBackward : QTextDocument::FindFlag(0));
}
}
else {
textCursor().clearSelection();
moveCursor(QTextCursor::MoveOperation::Left);
moveCursor(QTextCursor::MoveOperation::Start);
}
}
@ -204,6 +218,10 @@ void TextBrowserViewer::setUrl(const QUrl& url) {
void TextBrowserViewer::setHtml(const QString& html, const QUrl& base_url) {
m_currentUrl = base_url;
if (!m_reloadingWithResources) {
m_resourcesForHtml.clear();
}
QTextBrowser::setHtml(html);
setZoomFactor(m_zoomFactor);
@ -243,20 +261,23 @@ void TextBrowserViewer::setVerticalScrollBarPosition(double pos) {
}
void TextBrowserViewer::applyFont(const QFont& fon) {
m_baseFont = fon;
setFont(fon);
setZoomFactor(zoomFactor());
}
qreal TextBrowserViewer::zoomFactor() const {
return font().pointSizeF() / 8.0;
return m_zoomFactor;
}
void TextBrowserViewer::setZoomFactor(qreal zoom_factor) {
m_zoomFactor = zoom_factor;
auto fon = font();
fon.setPointSizeF(8.0 * zoom_factor);
applyFont(fon);
fon.setPointSizeF(m_baseFont.pointSizeF() * zoom_factor);
setFont(fon);
}
void TextBrowserViewer::contextMenuEvent(QContextMenuEvent* event) {
@ -268,6 +289,14 @@ void TextBrowserViewer::contextMenuEvent(QContextMenuEvent* event) {
return;
}
if (m_actionReloadWithImages.isNull()) {
m_actionReloadWithImages.reset(new QAction(qApp->icons()->fromTheme(QSL("viewimage"), QSL("view-refresh")),
tr("Reload with images"),
this));
connect(m_actionReloadWithImages.data(), &QAction::triggered, this, &TextBrowserViewer::reloadWithImages);
}
auto anchor = anchorAt(event->pos());
if (!anchor.isEmpty()) {
@ -314,10 +343,18 @@ void TextBrowserViewer::resizeEvent(QResizeEvent* event) {
}
void TextBrowserViewer::wheelEvent(QWheelEvent* event) {
// NOTE: Skip base class implemetation.
QAbstractScrollArea::wheelEvent(event);
updateMicroFocus();
}
void TextBrowserViewer::reloadWithImages() {
m_reloadingWithResources = true;
m_loadedResources.clear();
setHtml(html(), m_currentUrl);
}
void TextBrowserViewer::onAnchorClicked(const QUrl& url) {
if (!url.isEmpty()) {
bool open_externally_now =

View File

@ -47,6 +47,7 @@ class TextBrowserViewer : public QTextBrowser, public WebViewer {
virtual void wheelEvent(QWheelEvent* event);
private slots:
void reloadWithImages();
void onAnchorClicked(const QUrl& url);
signals:
@ -68,7 +69,12 @@ class TextBrowserViewer : public QTextBrowser, public WebViewer {
private:
QUrl m_currentUrl;
QPointer<RootItem> m_root;
QFont m_baseFont;
qreal m_zoomFactor = 1.0;
QScopedPointer<QAction> m_actionReloadWithImages;
bool m_reloadingWithResources;
QList<QUrl> m_resourcesForHtml;
QMap<QUrl, QByteArray> m_loadedResources;
};
#endif // TEXTBROWSERVIEWER_H