allow user to zoom in/out Web page

1. Mouse wheel with Ctrl pressing will zoom in/out the Markdown Web
page;
2. Ctrl+-, Ctrl++ will zoom in/out the Markdown Web page;
3. Ctrl+0 recover the Web page zoom factor to 1.
This commit is contained in:
Le Tan 2017-03-30 22:42:43 +08:00
parent 1f845d5ba8
commit ce1cefa793
7 changed files with 75 additions and 9 deletions

View File

@ -2,10 +2,9 @@
#include <QtWidgets>
#include "vconfigmanager.h"
#include "utils/vutils.h"
#include "vconstants.h"
extern VConfigManager vconfig;
static const qreal c_webZoomFactorMax = 5;
static const qreal c_webZoomFactorMin = 0.25;
VSettingsDialog::VSettingsDialog(QWidget *p_parent)
: QDialog(p_parent)

View File

@ -35,6 +35,14 @@ var scrollToAnchor = function(anchor) {
}
};
window.onwheel = function(e) {
e = e || window.event;
var ctrl = !!e.ctrlKey;
if (ctrl) {
e.preventDefault();
}
}
window.onscroll = function() {
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset;
var eles = document.querySelectorAll("h1, h2, h3, h4, h5, h6");
@ -129,7 +137,7 @@ document.onkeydown = function(e) {
return;
default:
content.keyPressEvent(key);
content.keyPressEvent(key, ctrl, shift);
keyState = 0;
return;
}

View File

@ -5,4 +5,7 @@ enum class DocType { Html, Markdown };
enum class ClipboardOpType { Invalid, CopyFile, CopyDir };
enum class OpenFileMode {Read = 0, Edit};
static const qreal c_webZoomFactorMax = 5;
static const qreal c_webZoomFactorMin = 0.25;
#endif

View File

@ -55,7 +55,7 @@ void VDocument::setLog(const QString &p_log)
emit logChanged(p_log);
}
void VDocument::keyPressEvent(int p_key)
void VDocument::keyPressEvent(int p_key, bool p_ctrl, bool p_shift)
{
emit keyPressed(p_key);
emit keyPressed(p_key, p_ctrl, p_shift);
}

View File

@ -24,7 +24,7 @@ public slots:
void setToc(const QString &toc);
void setHeader(const QString &anchor);
void setLog(const QString &p_log);
void keyPressEvent(int p_key);
void keyPressEvent(int p_key, bool p_ctrl, bool p_shift);
void updateText();
signals:
@ -34,7 +34,7 @@ signals:
void headerChanged(const QString &anchor);
void htmlChanged(const QString &html);
void logChanged(const QString &p_log);
void keyPressed(int p_key);
void keyPressed(int p_key, bool p_ctrl, bool p_shift);
private:
QString m_toc;

View File

@ -17,6 +17,7 @@
#include "vmdedit.h"
#include "dialog/vfindreplacedialog.h"
#include "veditarea.h"
#include "vconstants.h"
extern VConfigManager vconfig;
@ -592,16 +593,67 @@ bool VEditTab::checkToc()
return ret;
}
void VEditTab::handleWebKeyPressed(int p_key)
void VEditTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool /* p_shift */)
{
Q_ASSERT(webPreviewer);
switch (p_key) {
// Esc
case 27:
m_editArea->getFindReplaceDialog()->closeDialog();
break;
// Dash
case 189:
if (p_ctrl) {
// Zoom out.
zoomWebPage(false);
}
break;
// Equal
case 187:
if (p_ctrl) {
// Zoom in.
zoomWebPage(true);
}
break;
// 0
case 48:
if (p_ctrl) {
// Recover zoom.
webPreviewer->setZoomFactor(1);
}
break;
default:
break;
}
}
void VEditTab::wheelEvent(QWheelEvent *p_event)
{
if (!isEditMode && webPreviewer) {
QPoint angle = p_event->angleDelta();
Qt::KeyboardModifiers modifiers = p_event->modifiers();
if (!angle.isNull() && (angle.y() != 0) && (modifiers & Qt::ControlModifier)) {
zoomWebPage(angle.y() > 0);
p_event->accept();
return;
}
}
p_event->ignore();
}
void VEditTab::zoomWebPage(bool p_zoomIn, qreal p_step)
{
Q_ASSERT(webPreviewer);
qreal curFactor = webPreviewer->zoomFactor();
qreal newFactor = p_zoomIn ? curFactor + p_step : curFactor - p_step;
if (newFactor < c_webZoomFactorMin) {
newFactor = c_webZoomFactorMin;
} else if (newFactor > c_webZoomFactorMax) {
newFactor = c_webZoomFactorMax;
}
webPreviewer->setZoomFactor(newFactor);
}

View File

@ -51,6 +51,9 @@ public:
QString getSelectedText() const;
void clearSearchedWordHighlight();
protected:
void wheelEvent(QWheelEvent *p_event) Q_DECL_OVERRIDE;
signals:
void getFocused();
void outlineChanged(const VToc &toc);
@ -65,7 +68,7 @@ private slots:
void updateTocFromHeaders(const QVector<VHeader> &headers);
void handleTextChanged();
void noticeStatusChanged();
void handleWebKeyPressed(int p_key);
void handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift);
private:
void setupUI();
@ -83,6 +86,7 @@ private:
// Check if @tableOfContent is outdated (such as renaming the file).
// Return true if we need to update toc.
bool checkToc();
void zoomWebPage(bool p_zoomIn, qreal p_step = 0.25);
QPointer<VFile> m_file;
bool isEditMode;