add config setting for Web zoom factor

This commit is contained in:
Le Tan 2017-03-29 22:47:29 +08:00
parent 6949de8723
commit 1f845d5ba8
6 changed files with 177 additions and 34 deletions

View File

@ -4,12 +4,15 @@
#include "utils/vutils.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)
{
m_tabs = new QTabWidget;
m_tabs->addTab(new VGeneralTab(), tr("General"));
m_tabs->addTab(new VReadEditTab(), tr("Read/Edit"));
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &VSettingsDialog::saveConfiguration);
@ -28,12 +31,22 @@ VSettingsDialog::VSettingsDialog(QWidget *p_parent)
void VSettingsDialog::loadConfiguration()
{
// General Tab.
{
VGeneralTab *generalTab = dynamic_cast<VGeneralTab *>(m_tabs->widget(0));
Q_ASSERT(generalTab);
bool ret = generalTab->loadConfiguration();
if (!ret) {
if (!generalTab->loadConfiguration()) {
goto err;
}
}
// Read/Edit Tab.
{
VReadEditTab *readEditTab = dynamic_cast<VReadEditTab *>(m_tabs->widget(1));
Q_ASSERT(readEditTab);
if (!readEditTab->loadConfiguration()) {
goto err;
}
}
return;
err:
@ -46,12 +59,22 @@ err:
void VSettingsDialog::saveConfiguration()
{
// General Tab.
{
VGeneralTab *generalTab = dynamic_cast<VGeneralTab *>(m_tabs->widget(0));
Q_ASSERT(generalTab);
bool ret = generalTab->saveConfiguration();
if (!ret) {
if (!generalTab->saveConfiguration()) {
goto err;
}
}
// Read/Edit Tab.
{
VReadEditTab *readEditTab = dynamic_cast<VReadEditTab *>(m_tabs->widget(1));
Q_ASSERT(readEditTab);
if (!readEditTab->saveConfiguration()) {
goto err;
}
}
accept();
return;
@ -64,7 +87,7 @@ err:
const QVector<QString> VGeneralTab::c_availableLangs = { "System", "English", "Chinese" };
VGeneralTab::VGeneralTab(QWidget *p_parent)
: QWidget(p_parent), m_langChanged(false)
: QWidget(p_parent)
{
QLabel *langLabel = new QLabel(tr("&Language:"));
m_langCombo = new QComboBox(this);
@ -73,8 +96,6 @@ VGeneralTab::VGeneralTab(QWidget *p_parent)
for (auto lang : langs) {
m_langCombo->addItem(lang.second, lang.first);
}
connect(m_langCombo, SIGNAL(currentIndexChanged(int)),
this, SLOT(handleIndexChange(int)));
langLabel->setBuddy(m_langCombo);
QFormLayout *optionLayout = new QFormLayout();
@ -112,7 +133,7 @@ bool VGeneralTab::loadLanguage()
return true;
}
bool found = false;
// Lang is the value, not name.
// lang is the value, not name.
for (int i = 0; i < m_langCombo->count(); ++i) {
if (m_langCombo->itemData(i).toString() == lang) {
found = true;
@ -129,16 +150,91 @@ bool VGeneralTab::loadLanguage()
bool VGeneralTab::saveLanguage()
{
if (m_langChanged) {
vconfig.setLanguage(m_langCombo->currentData().toString());
QString curLang = m_langCombo->currentData().toString();
vconfig.setLanguage(curLang);
return true;
}
VReadEditTab::VReadEditTab(QWidget *p_parent)
: QWidget(p_parent)
{
m_readBox = new QGroupBox(tr("Read Mode (For Markdown Only)"));
m_editBox = new QGroupBox(tr("Edit Mode"));
// Web Zoom Factor.
m_customWebZoom = new QCheckBox(tr("Custom Web zoom factor"), this);
m_customWebZoom->setToolTip(tr("Set the zoom factor of the Web page when reading"));
connect(m_customWebZoom, &QCheckBox::stateChanged,
this, &VReadEditTab::customWebZoomChanged);
m_webZoomFactorSpin = new QDoubleSpinBox(this);
m_webZoomFactorSpin->setMaximum(c_webZoomFactorMax);
m_webZoomFactorSpin->setMinimum(c_webZoomFactorMin);
m_webZoomFactorSpin->setSingleStep(0.25);
QHBoxLayout *zoomFactorLayout = new QHBoxLayout();
zoomFactorLayout->addWidget(m_customWebZoom);
zoomFactorLayout->addWidget(m_webZoomFactorSpin);
QFormLayout *readLayout = new QFormLayout();
readLayout->addRow(zoomFactorLayout);
m_readBox->setLayout(readLayout);
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addWidget(m_readBox);
mainLayout->addWidget(m_editBox);
m_editBox->hide();
setLayout(mainLayout);
}
bool VReadEditTab::loadConfiguration()
{
if (!loadWebZoomFactor()) {
return false;
}
return true;
}
void VGeneralTab::handleIndexChange(int p_index)
bool VReadEditTab::saveConfiguration()
{
if (p_index == -1) {
return;
if (!saveWebZoomFactor()) {
return false;
}
return true;
}
bool VReadEditTab::loadWebZoomFactor()
{
qreal factor = vconfig.getWebZoomFactor();
bool customFactor = vconfig.isCustomWebZoomFactor();
if (customFactor) {
if (factor < c_webZoomFactorMin || factor > c_webZoomFactorMax) {
factor = 1;
}
m_customWebZoom->setChecked(true);
m_webZoomFactorSpin->setValue(factor);
} else {
m_customWebZoom->setChecked(false);
m_webZoomFactorSpin->setValue(factor);
m_webZoomFactorSpin->setEnabled(false);
}
return true;
}
bool VReadEditTab::saveWebZoomFactor()
{
if (m_customWebZoom->isChecked()) {
vconfig.setWebZoomFactor(m_webZoomFactorSpin->value());
} else {
vconfig.setWebZoomFactor(-1);
}
return true;
}
void VReadEditTab::customWebZoomChanged(int p_state)
{
if (p_state == Qt::Unchecked) {
m_webZoomFactorSpin->setEnabled(false);
} else {
m_webZoomFactorSpin->setEnabled(true);
}
m_langChanged = true;
}

View File

@ -8,6 +8,9 @@
class QDialogButtonBox;
class QTabWidget;
class QComboBox;
class QGroupBox;
class QDoubleSpinBox;
class QCheckBox;
class VGeneralTab : public QWidget
{
@ -17,21 +20,39 @@ public:
bool loadConfiguration();
bool saveConfiguration();
private slots:
void handleIndexChange(int p_index);
private:
bool loadLanguage();
bool saveLanguage();
// Language
QComboBox *m_langCombo;
// Whether language changes.
bool m_langChanged;
static const QVector<QString> c_availableLangs;
};
class VReadEditTab : public QWidget
{
Q_OBJECT
public:
explicit VReadEditTab(QWidget *p_parent = 0);
bool loadConfiguration();
bool saveConfiguration();
QGroupBox *m_readBox;
QGroupBox *m_editBox;
// Web zoom factor.
QCheckBox *m_customWebZoom;
QDoubleSpinBox *m_webZoomFactorSpin;
private slots:
void customWebZoomChanged(int p_state);
private:
bool loadWebZoomFactor();
bool saveWebZoomFactor();
};
class VSettingsDialog : public QDialog
{
Q_OBJECT

View File

@ -353,3 +353,9 @@ qreal VUtils::calculateScaleFactor()
qreal factor = dpi / refDpi;
return factor < 1 ? 1 : factor;
}
bool VUtils::realEqual(qreal p_a, qreal p_b)
{
return abs(p_a - p_b) < 1e-8;
}

View File

@ -43,6 +43,7 @@ public:
static bool isImageURL(const QUrl &p_url);
static bool isImageURLText(const QString &p_url);
static qreal calculateScaleFactor();
static bool realEqual(qreal p_a, qreal p_b);
private:
// <value, name>

View File

@ -89,7 +89,7 @@ void VConfigManager::initialize()
m_enableMathjax = getConfigFromSettings("global", "enable_mathjax").toBool();
m_webZoomFactor = getConfigFromSettings("global", "web_zoom_factor").toReal();
if (m_webZoomFactor < 0) {
if (!isCustomWebZoomFactor()) {
// Calculate the zoom factor based on DPI.
m_webZoomFactor = VUtils::calculateScaleFactor();
qDebug() << "set WebZoomFactor to" << m_webZoomFactor;
@ -264,3 +264,23 @@ void VConfigManager::updatePaletteColor()
// Update markdown editor palette
updateMarkdownEditStyle();
}
void VConfigManager::setWebZoomFactor(qreal p_factor)
{
if (isCustomWebZoomFactor()) {
if (VUtils::realEqual(m_webZoomFactor, p_factor)) {
return;
} else if (VUtils::realEqual(p_factor, -1)) {
m_webZoomFactor = VUtils::calculateScaleFactor();
setConfigToSettings("global", "web_zoom_factor", -1);
return;
}
} else {
if (VUtils::realEqual(p_factor, -1)) {
return;
}
}
m_webZoomFactor = p_factor;
setConfigToSettings("global", "web_zoom_factor", m_webZoomFactor);
}

View File

@ -6,7 +6,6 @@
#include <QVector>
#include <QSettings>
#include "vnotebook.h"
#include "hgmarkdownhighlighter.h"
#include "vmarkdownconverter.h"
@ -124,7 +123,8 @@ public:
inline void setEnableMathjax(bool p_enabled);
inline qreal getWebZoomFactor() const;
inline void setWebZoomFactor(qreal p_factor);
void setWebZoomFactor(qreal p_factor);
inline bool isCustomWebZoomFactor();
private:
void updateMarkdownEditStyle();
@ -543,12 +543,11 @@ inline qreal VConfigManager::getWebZoomFactor() const
return m_webZoomFactor;
}
inline void VConfigManager::setWebZoomFactor(qreal p_factor)
inline bool VConfigManager::isCustomWebZoomFactor()
{
if (m_webZoomFactor == p_factor) {
return;
}
m_webZoomFactor = p_factor;
setConfigToSettings("global", "web_zoom_factor", m_webZoomFactor);
qreal factorFromIni = getConfigFromSettings("global", "web_zoom_factor").toReal();
// -1 indicates let system automatically calculate the factor.
return factorFromIni > 0;
}
#endif // VCONFIGMANAGER_H