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) { QVariant TextBrowserViewer::loadResource(int type, const QUrl& name) {
if (!m_reloadingWithResources) {
if (type == QTextDocument::ResourceType::ImageResource) {
m_resourcesForHtml.append(name);
}
}
return {}; return {};
} }
@ -127,11 +133,19 @@ void TextBrowserViewer::bindToBrowser(WebBrowser* browser) {
void TextBrowserViewer::findText(const QString& text, bool backwards) { void TextBrowserViewer::findText(const QString& text, bool backwards) {
if (!text.isEmpty()) { if (!text.isEmpty()) {
QTextBrowser::find(text, backwards ? QTextDocument::FindFlag::FindBackward : QTextDocument::FindFlag(0)); 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 { else {
textCursor().clearSelection(); 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) { void TextBrowserViewer::setHtml(const QString& html, const QUrl& base_url) {
m_currentUrl = base_url; m_currentUrl = base_url;
if (!m_reloadingWithResources) {
m_resourcesForHtml.clear();
}
QTextBrowser::setHtml(html); QTextBrowser::setHtml(html);
setZoomFactor(m_zoomFactor); setZoomFactor(m_zoomFactor);
@ -243,20 +261,23 @@ void TextBrowserViewer::setVerticalScrollBarPosition(double pos) {
} }
void TextBrowserViewer::applyFont(const QFont& fon) { void TextBrowserViewer::applyFont(const QFont& fon) {
m_baseFont = fon;
setFont(fon); setFont(fon);
setZoomFactor(zoomFactor());
} }
qreal TextBrowserViewer::zoomFactor() const { qreal TextBrowserViewer::zoomFactor() const {
return font().pointSizeF() / 8.0; return m_zoomFactor;
} }
void TextBrowserViewer::setZoomFactor(qreal zoom_factor) { void TextBrowserViewer::setZoomFactor(qreal zoom_factor) {
m_zoomFactor = zoom_factor; m_zoomFactor = zoom_factor;
auto fon = font(); 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) { void TextBrowserViewer::contextMenuEvent(QContextMenuEvent* event) {
@ -268,6 +289,14 @@ void TextBrowserViewer::contextMenuEvent(QContextMenuEvent* event) {
return; 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()); auto anchor = anchorAt(event->pos());
if (!anchor.isEmpty()) { if (!anchor.isEmpty()) {
@ -314,10 +343,18 @@ void TextBrowserViewer::resizeEvent(QResizeEvent* event) {
} }
void TextBrowserViewer::wheelEvent(QWheelEvent* event) { void TextBrowserViewer::wheelEvent(QWheelEvent* event) {
// NOTE: Skip base class implemetation.
QAbstractScrollArea::wheelEvent(event); QAbstractScrollArea::wheelEvent(event);
updateMicroFocus(); updateMicroFocus();
} }
void TextBrowserViewer::reloadWithImages() {
m_reloadingWithResources = true;
m_loadedResources.clear();
setHtml(html(), m_currentUrl);
}
void TextBrowserViewer::onAnchorClicked(const QUrl& url) { void TextBrowserViewer::onAnchorClicked(const QUrl& url) {
if (!url.isEmpty()) { if (!url.isEmpty()) {
bool open_externally_now = bool open_externally_now =

View File

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