constrain the width of images in read mode

This commit is contained in:
Le Tan 2017-05-10 19:36:07 +08:00
parent e5021f4501
commit a154e568de
6 changed files with 52 additions and 2 deletions

View File

@ -28,7 +28,10 @@ enable_code_block_highlight=true
enable_preview_images=true enable_preview_images=true
; Enable image preview constraint in edit mode to constrain the widht of the preview ; Enable image preview constraint in edit mode to constrain the widht of the preview
enable_preview_image_constraint=false enable_preview_image_constraint=true
; Enable image constraint in read mode to constrain the width of the image
enable_image_constraint=true
[session] [session]
tools_dock_checked=true tools_dock_checked=true

View File

@ -130,6 +130,9 @@ void VConfigManager::initialize()
m_enablePreviewImageConstraint = getConfigFromSettings("global", m_enablePreviewImageConstraint = getConfigFromSettings("global",
"enable_preview_image_constraint").toBool(); "enable_preview_image_constraint").toBool();
m_enableImageConstraint = getConfigFromSettings("global",
"enable_image_constraint").toBool();
} }
void VConfigManager::readPredefinedColorsFromSettings() void VConfigManager::readPredefinedColorsFromSettings()

View File

@ -165,6 +165,9 @@ public:
inline bool getEnablePreviewImageConstraint() const; inline bool getEnablePreviewImageConstraint() const;
inline void setEnablePreviewImageConstraint(bool p_enabled); inline void setEnablePreviewImageConstraint(bool p_enabled);
inline bool getEnableImageConstraint() const;
inline void setEnableImageConstraint(bool p_enabled);
// Get the folder the ini file exists. // Get the folder the ini file exists.
QString getConfigFolder() const; QString getConfigFolder() const;
@ -278,6 +281,9 @@ private:
// Constrain the width of image preview in edit mode. // Constrain the width of image preview in edit mode.
bool m_enablePreviewImageConstraint; bool m_enablePreviewImageConstraint;
// Constrain the width of image in read mode.
bool m_enableImageConstraint;
// The name of the config file in each directory, obsolete. // The name of the config file in each directory, obsolete.
// Use c_dirConfigFile instead. // Use c_dirConfigFile instead.
static const QString c_obsoleteDirConfigFile; static const QString c_obsoleteDirConfigFile;
@ -735,4 +741,20 @@ inline void VConfigManager::setEnablePreviewImageConstraint(bool p_enabled)
m_enablePreviewImageConstraint); m_enablePreviewImageConstraint);
} }
inline bool VConfigManager::getEnableImageConstraint() const
{
return m_enableImageConstraint;
}
inline void VConfigManager::setEnableImageConstraint(bool p_enabled)
{
if (m_enableImageConstraint == p_enabled) {
return;
}
m_enableImageConstraint = p_enabled;
setConfigToSettings("global", "enable_image_constraint",
m_enableImageConstraint);
}
#endif // VCONFIGMANAGER_H #endif // VCONFIGMANAGER_H

View File

@ -352,6 +352,14 @@ void VMainWindow::initMarkdownMenu()
initRenderBackgroundMenu(markdownMenu); initRenderBackgroundMenu(markdownMenu);
QAction *constrainImageAct = new QAction(tr("Constrain The Width of Images in Read Mode"), this);
constrainImageAct->setToolTip(tr("Constrain the width of images to the window in read mode (re-open current tabs to make it work)"));
constrainImageAct->setCheckable(true);
connect(constrainImageAct, &QAction::triggered,
this, &VMainWindow::enableImageConstraint);
markdownMenu->addAction(constrainImageAct);
constrainImageAct->setChecked(vconfig.getEnableImageConstraint());
markdownMenu->addSeparator(); markdownMenu->addSeparator();
QAction *mermaidAct = new QAction(tr("&Mermaid Diagram"), this); QAction *mermaidAct = new QAction(tr("&Mermaid Diagram"), this);
@ -1301,6 +1309,13 @@ void VMainWindow::enableImagePreviewConstraint(bool p_checked)
vconfig.setEnablePreviewImageConstraint(p_checked); vconfig.setEnablePreviewImageConstraint(p_checked);
} }
void VMainWindow::enableImageConstraint(bool p_checked)
{
vconfig.setEnableImageConstraint(p_checked);
vnote->updateTemplate();
}
void VMainWindow::shortcutHelp() void VMainWindow::shortcutHelp()
{ {
QString locale = VUtils::getLocale(); QString locale = VUtils::getLocale();

View File

@ -78,6 +78,7 @@ private slots:
void enableCodeBlockHighlight(bool p_checked); void enableCodeBlockHighlight(bool p_checked);
void enableImagePreview(bool p_checked); void enableImagePreview(bool p_checked);
void enableImagePreviewConstraint(bool p_checked); void enableImagePreviewConstraint(bool p_checked);
void enableImageConstraint(bool p_checked);
protected: protected:
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;

View File

@ -157,8 +157,14 @@ void VNote::updateTemplate()
} }
QString cssStyle; QString cssStyle;
if (!rgb.isEmpty()) { if (!rgb.isEmpty()) {
cssStyle = "body { background-color: #" + rgb + " !important; }"; cssStyle += "body { background-color: #" + rgb + " !important; }\n";
} }
if (vconfig.getEnableImageConstraint()) {
// Constain the image width.
cssStyle += "img { max-width: 100% !important; height: auto !important; }\n";
}
QString styleHolder("<!-- BACKGROUND_PLACE_HOLDER -->"); QString styleHolder("<!-- BACKGROUND_PLACE_HOLDER -->");
QString cssHolder("CSS_PLACE_HOLDER"); QString cssHolder("CSS_PLACE_HOLDER");