mirror of
https://github.com/zealdocs/zeal.git
synced 2024-09-11 14:25:52 +03:00
ui: Use predefined zooming levels
This commit is contained in:
parent
50be385ec7
commit
96bfa2d3cd
@ -29,6 +29,7 @@
|
||||
#include "searchitemdelegate.h"
|
||||
#include "settingsdialog.h"
|
||||
#include "qxtglobalshortcut/qxtglobalshortcut.h"
|
||||
#include "widgets/webview.h"
|
||||
|
||||
#include <core/application.h>
|
||||
#include <core/settings.h>
|
||||
@ -84,7 +85,7 @@ struct TabState
|
||||
, expansions(other.expansions)
|
||||
, searchScrollPosition(other.searchScrollPosition)
|
||||
, tocScrollPosition(other.tocScrollPosition)
|
||||
, webViewZoomFactor(other.webViewZoomFactor)
|
||||
, zoomLevel(other.zoomLevel)
|
||||
{
|
||||
searchModel = new Registry::SearchModel(*other.searchModel);
|
||||
tocModel = new Registry::SearchModel(*other.tocModel);
|
||||
@ -145,7 +146,7 @@ struct TabState
|
||||
int tocScrollPosition = 0;
|
||||
|
||||
QWebPage *webPage = nullptr;
|
||||
int webViewZoomFactor = 0;
|
||||
int zoomLevel = WebView::defaultZoomLevel();
|
||||
};
|
||||
|
||||
} // namespace WidgetUi
|
||||
@ -651,7 +652,7 @@ void MainWindow::setupTabBar()
|
||||
previousTabState->selections = ui->treeView->selectionModel()->selectedIndexes();
|
||||
previousTabState->searchScrollPosition = ui->treeView->verticalScrollBar()->value();
|
||||
previousTabState->tocScrollPosition = ui->tocListView->verticalScrollBar()->value();
|
||||
previousTabState->webViewZoomFactor = ui->webView->zoomFactor();
|
||||
previousTabState->zoomLevel = ui->webView->zoomLevel();
|
||||
}
|
||||
|
||||
// Load current tab state
|
||||
@ -673,7 +674,7 @@ void MainWindow::setupTabBar()
|
||||
ui->treeView->blockSignals(false);
|
||||
|
||||
ui->webView->setPage(tabState->webPage);
|
||||
ui->webView->setZoomFactor(tabState->webViewZoomFactor);
|
||||
ui->webView->setZoomLevel(tabState->zoomLevel);
|
||||
|
||||
ui->actionBack->setEnabled(ui->webView->canGoBack());
|
||||
ui->actionForward->setEnabled(ui->webView->canGoForward());
|
||||
|
@ -68,14 +68,14 @@ void SearchableWebView::setPage(QWebPage *page)
|
||||
});
|
||||
}
|
||||
|
||||
int SearchableWebView::zoomFactor() const
|
||||
int SearchableWebView::zoomLevel() const
|
||||
{
|
||||
return m_webView->zealZoomFactor();
|
||||
return m_webView->zoomLevel();
|
||||
}
|
||||
|
||||
void SearchableWebView::setZoomFactor(int value)
|
||||
void SearchableWebView::setZoomLevel(int level)
|
||||
{
|
||||
m_webView->setZealZoomFactor(value);
|
||||
m_webView->setZoomLevel(level);
|
||||
}
|
||||
|
||||
bool SearchableWebView::eventFilter(QObject *object, QEvent *event)
|
||||
|
@ -48,8 +48,8 @@ public:
|
||||
bool canGoForward() const;
|
||||
void setPage(QWebPage *page);
|
||||
|
||||
int zoomFactor() const;
|
||||
void setZoomFactor(int value);
|
||||
int zoomLevel() const;
|
||||
void setZoomLevel(int level);
|
||||
|
||||
bool eventFilter(QObject *object, QEvent *event) override;
|
||||
|
||||
|
@ -36,15 +36,53 @@ WebView::WebView(QWidget *parent) :
|
||||
{
|
||||
}
|
||||
|
||||
int WebView::zealZoomFactor() const
|
||||
int WebView::zoomLevel() const
|
||||
{
|
||||
return m_zoomFactor;
|
||||
return m_zoomLevel;
|
||||
}
|
||||
|
||||
void WebView::setZealZoomFactor(int zf)
|
||||
void WebView::setZoomLevel(int level)
|
||||
{
|
||||
m_zoomFactor = zf;
|
||||
updateZoomFactor();
|
||||
if (level == m_zoomLevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
level = qMax(0, level);
|
||||
level = qMin(level, availableZoomLevels().size() - 1);
|
||||
|
||||
m_zoomLevel = level;
|
||||
|
||||
setZoomFactor(availableZoomLevels().at(level) / 100.0);
|
||||
emit zoomLevelChanged();
|
||||
}
|
||||
|
||||
const QVector<int> &WebView::availableZoomLevels()
|
||||
{
|
||||
static const QVector<int> zoomLevels = {30, 40, 50, 67, 80, 90, 100,
|
||||
110, 120, 133, 150, 170, 200,
|
||||
220, 233, 250, 270, 285, 300};
|
||||
return zoomLevels;
|
||||
}
|
||||
|
||||
const int WebView::defaultZoomLevel()
|
||||
{
|
||||
static const int level = availableZoomLevels().indexOf(100);
|
||||
return level;
|
||||
}
|
||||
|
||||
void WebView::zoomIn()
|
||||
{
|
||||
setZoomLevel(m_zoomLevel + 1);
|
||||
}
|
||||
|
||||
void WebView::zoomOut()
|
||||
{
|
||||
setZoomLevel(m_zoomLevel - 1);
|
||||
}
|
||||
|
||||
void WebView::resetZoom()
|
||||
{
|
||||
setZoomLevel(defaultZoomLevel());
|
||||
}
|
||||
|
||||
QWebView *WebView::createWindow(QWebPage::WebWindowType type)
|
||||
@ -104,11 +142,22 @@ void WebView::mouseReleaseEvent(QMouseEvent *event)
|
||||
void WebView::wheelEvent(QWheelEvent *event)
|
||||
{
|
||||
if (event->modifiers() & Qt::ControlModifier) {
|
||||
m_zoomFactor += event->delta() / 120;
|
||||
updateZoomFactor();
|
||||
} else {
|
||||
QWebView::wheelEvent(event);
|
||||
const QPoint angleDelta = event->angleDelta();
|
||||
int delta = qAbs(angleDelta.x()) > qAbs(angleDelta.y()) ? angleDelta.x() : angleDelta.y();
|
||||
const int direction = delta > 0 ? 1 : -1;
|
||||
|
||||
int levelDelta = 0;
|
||||
while (delta * direction >= 120) {
|
||||
levelDelta += direction;
|
||||
delta -= 120 * direction;
|
||||
}
|
||||
|
||||
setZoomLevel(m_zoomLevel + levelDelta);
|
||||
event->accept();
|
||||
return;
|
||||
}
|
||||
|
||||
QWebView::wheelEvent(event);
|
||||
}
|
||||
|
||||
QUrl WebView::clickedLink(const QPoint &pos) const
|
||||
@ -119,8 +168,3 @@ QUrl WebView::clickedLink(const QPoint &pos) const
|
||||
|
||||
return frame->hitTestContent(pos).linkUrl();
|
||||
}
|
||||
|
||||
void WebView::updateZoomFactor()
|
||||
{
|
||||
setZoomFactor(1 + m_zoomFactor / 10.);
|
||||
}
|
||||
|
@ -24,6 +24,7 @@
|
||||
#ifndef ZEAL_WIDGETUI_WEBVIEW_H
|
||||
#define ZEAL_WIDGETUI_WEBVIEW_H
|
||||
|
||||
#include <QVector>
|
||||
#include <QWebView>
|
||||
|
||||
namespace Zeal {
|
||||
@ -35,8 +36,19 @@ class WebView : public QWebView
|
||||
public:
|
||||
explicit WebView(QWidget *parent = nullptr);
|
||||
|
||||
int zealZoomFactor() const;
|
||||
void setZealZoomFactor(int zf);
|
||||
int zoomLevel() const;
|
||||
void setZoomLevel(int level);
|
||||
|
||||
static const QVector<int> &availableZoomLevels();
|
||||
static const int defaultZoomLevel();
|
||||
|
||||
public slots:
|
||||
void zoomIn();
|
||||
void zoomOut();
|
||||
void resetZoom();
|
||||
|
||||
signals:
|
||||
void zoomLevelChanged();
|
||||
|
||||
protected:
|
||||
QWebView *createWindow(QWebPage::WebWindowType type) override;
|
||||
@ -46,10 +58,9 @@ protected:
|
||||
|
||||
private:
|
||||
QUrl clickedLink(const QPoint &pos) const;
|
||||
void updateZoomFactor();
|
||||
|
||||
QUrl m_clickedLink;
|
||||
int m_zoomFactor = 0;
|
||||
int m_zoomLevel = defaultZoomLevel();
|
||||
};
|
||||
|
||||
} // namespace WidgetUi
|
||||
|
Loading…
Reference in New Issue
Block a user