add config enable_auto_save which is false by default

This commit is contained in:
Le Tan 2018-02-05 22:26:38 +08:00
parent bf70c23110
commit 4bcb60e903
9 changed files with 137 additions and 18 deletions

View File

@ -1,6 +1,8 @@
#include "vsettingsdialog.h"
#include <QtWidgets>
#include <QRegExp>
#include <QToolTip>
#include "vconfigmanager.h"
#include "utils/vutils.h"
#include "vconstants.h"
@ -419,29 +421,66 @@ VReadEditTab::VReadEditTab(QWidget *p_parent)
m_webZoomFactorSpin->setMinimum(c_webZoomFactorMin);
m_webZoomFactorSpin->setSingleStep(0.25);
connect(m_customWebZoom, &QCheckBox::stateChanged,
this, [this](int p_state){
this, [this](int p_state) {
this->m_webZoomFactorSpin->setEnabled(p_state == Qt::Checked);
});
QHBoxLayout *zoomFactorLayout = new QHBoxLayout();
zoomFactorLayout->addWidget(m_customWebZoom);
zoomFactorLayout->addWidget(m_webZoomFactorSpin);
// Swap file.
m_swapFile = new QCheckBox(tr("Swap file"));
m_swapFile->setToolTip(tr("Automatically save changes to a swap file for backup"));
connect(m_swapFile, &QCheckBox::stateChanged,
this, &VReadEditTab::showTipsAboutAutoSave);
// Auto save.
m_autoSave = new QCheckBox(tr("Auto save"));
m_autoSave->setToolTip(tr("Automatically save the note when editing"));
connect(m_autoSave, &QCheckBox::stateChanged,
this, &VReadEditTab::showTipsAboutAutoSave);
QVBoxLayout *readLayout = new QVBoxLayout();
readLayout->addLayout(zoomFactorLayout);
m_readBox->setLayout(readLayout);
QFormLayout *editLayout = new QFormLayout();
editLayout->addRow(m_swapFile);
editLayout->addRow(m_autoSave);
m_editBox->setLayout(editLayout);
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addWidget(m_readBox);
mainLayout->addWidget(m_editBox);
setLayout(mainLayout);
}
void VReadEditTab::showTipsAboutAutoSave()
{
if (m_autoSave->isChecked() && m_swapFile->isChecked()) {
// Show a tooltip.
QPoint pos = m_editBox->mapToGlobal(QPoint(0, 0));
QToolTip::showText(pos,
tr("It's recommended to enable \"Swap file\" "
"or \"Auto save\", not both"),
m_editBox);
}
}
bool VReadEditTab::loadConfiguration()
{
if (!loadWebZoomFactor()) {
return false;
}
if (!loadSwapFile()) {
return false;
}
if (!loadAutoSave()) {
return false;
}
return true;
}
@ -451,6 +490,14 @@ bool VReadEditTab::saveConfiguration()
return false;
}
if (!saveSwapFile()) {
return false;
}
if (!saveAutoSave()) {
return false;
}
return true;
}
@ -484,6 +531,30 @@ bool VReadEditTab::saveWebZoomFactor()
return true;
}
bool VReadEditTab::loadSwapFile()
{
m_swapFile->setChecked(g_config->getEnableBackupFile());
return true;
}
bool VReadEditTab::saveSwapFile()
{
g_config->setEnableBackupFile(m_swapFile->isChecked());
return true;
}
bool VReadEditTab::loadAutoSave()
{
m_autoSave->setChecked(g_config->getEnableAutoSave());
return true;
}
bool VReadEditTab::saveAutoSave()
{
g_config->setEnableAutoSave(m_autoSave->isChecked());
return true;
}
VNoteManagementTab::VNoteManagementTab(QWidget *p_parent)
: QWidget(p_parent)
{

View File

@ -66,10 +66,24 @@ private:
bool loadWebZoomFactor();
bool saveWebZoomFactor();
bool loadSwapFile();
bool saveSwapFile();
bool loadAutoSave();
bool saveAutoSave();
void showTipsAboutAutoSave();
// Web zoom factor.
QCheckBox *m_customWebZoom;
QDoubleSpinBox *m_webZoomFactorSpin;
// Swap file.
QCheckBox *m_swapFile;
// Auto save.
QCheckBox *m_autoSave;
QGroupBox *m_readBox;
QGroupBox *m_editBox;
};

View File

@ -165,6 +165,9 @@ startup_pages=
; Timer interval to check file modification or save file to tmp file in milliseconds
file_timer_interval=2000
; Whether enable auto save file
enable_auto_save=false
; Directory for the backup file
; A directory "." means to put the backup file in the same directory as the edited file
backup_directory=.

View File

@ -276,9 +276,6 @@ void VConfigManager::initialize()
m_backupExtension = ".";
}
m_enableBackupFile = getConfigFromSettings("global",
"enable_backup_file").toBool();
m_vimExemptionKeys = getConfigFromSettings("global",
"vim_exemption_keys").toString();

View File

@ -406,6 +406,7 @@ public:
// Whether backup file is enabled.
bool getEnableBackupFile() const;
void setEnableBackupFile(bool p_enabled);
// Get defined external editors.
QVector<VExternalEditor> getExternalEditors() const;
@ -442,6 +443,9 @@ public:
bool getEnableWildCardInSimpleSearch() const;
bool getEnableAutoSave() const;
void setEnableAutoSave(bool p_enabled);
private:
// Look up a config from user and default settings.
QVariant getConfigFromSettings(const QString &section, const QString &key) const;
@ -791,9 +795,6 @@ private:
// Extension of the backup file.
QString m_backupExtension;
// Whether enable backup file.
bool m_enableBackupFile;
// Skipped keys in Vim mode.
// c: Ctrl+C
// v: Ctrl+V
@ -1912,7 +1913,13 @@ inline const QString &VConfigManager::getBackupExtension() const
inline bool VConfigManager::getEnableBackupFile() const
{
return m_enableBackupFile;
return getConfigFromSettings("global",
"enable_backup_file").toBool();
}
inline void VConfigManager::setEnableBackupFile(bool p_enabled)
{
setConfigToSettings("global", "enable_backup_file", p_enabled);
}
inline const QString &VConfigManager::getVimExemptionKeys() const
@ -2054,4 +2061,15 @@ inline bool VConfigManager::getEnableWildCardInSimpleSearch() const
return getConfigFromSettings("global",
"enable_wildcard_in_simple_search").toBool();
}
inline bool VConfigManager::getEnableAutoSave() const
{
return getConfigFromSettings("global",
"enable_auto_save").toBool();
}
inline void VConfigManager::setEnableAutoSave(bool p_enabled)
{
setConfigToSettings("global", "enable_auto_save", p_enabled);
}
#endif // VCONFIGMANAGER_H

View File

@ -62,6 +62,8 @@ VEditArea::VEditArea(QWidget *parent)
this, &VEditArea::handleFileTimerTimeout);
timer->start();
m_autoSave = g_config->getEnableAutoSave();
}
void VEditArea::setupUI()
@ -184,6 +186,9 @@ VEditTab *VEditArea::openFile(VFile *p_file, OpenFileMode p_mode, bool p_forceMo
return NULL;
}
// Update auto save settings.
m_autoSave = g_config->getEnableAutoSave();
// If it is DocType::Unknown, open it using system default method.
if (p_file->getDocType() == DocType::Unknown) {
QUrl url = QUrl::fromLocalFile(p_file->fetchPath());
@ -1074,14 +1079,15 @@ void VEditArea::recordClosedFile(const VFileSessionInfo &p_file)
}
void VEditArea::handleFileTimerTimeout()
{
checkFileChangeOutside();
}
void VEditArea::checkFileChangeOutside()
{
int nrWin = splitter->count();
for (int i = 0; i < nrWin; ++i) {
getWindow(i)->checkFileChangeOutside();
// Check whether opened files have been changed outside.
VEditWindow *win = getWindow(i);
win->checkFileChangeOutside();
if (m_autoSave) {
win->saveAll();
}
}
}

View File

@ -172,11 +172,7 @@ private:
// Init targets for Captain mode.
void registerCaptainTargets();
// Check whether opened files have been changed outside.
void checkFileChangeOutside();
// Captain mode functions.
// Activate tab @p_idx.
static bool activateTabByCaptain(void *p_target, void *p_data, int p_idx);
@ -216,6 +212,9 @@ private:
// Last closed files stack.
QStack<VFileSessionInfo> m_lastClosedFiles;
// Whether auto save files.
bool m_autoSave;
};
inline VEditWindow* VEditArea::getWindow(int windowIndex) const

View File

@ -1157,6 +1157,14 @@ void VEditWindow::checkFileChangeOutside()
}
}
void VEditWindow::saveAll()
{
int nrTab = count();
for (int i = 0; i < nrTab; ++i) {
getTab(i)->saveFile();
}
}
void VEditWindow::tabRequestToClose(VEditTab *p_tab)
{
bool ok = p_tab->closeFile(false);

View File

@ -80,6 +80,9 @@ public:
// Check whether opened files have been changed outside.
void checkFileChangeOutside();
// Auto save file.
void saveAll();
protected:
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;