Added portable mode

This commit is contained in:
Gres 2020-03-15 15:30:24 +03:00
parent 83232bfc76
commit 493262876f
5 changed files with 81 additions and 12 deletions

View File

@ -1,10 +1,13 @@
#include "settings.h"
#include <QFile>
#include <QSettings>
#include <QStandardPaths>
namespace
{
const QString iniFileName = "settings.ini";
const QString qs_guiGroup = "GUI";
const QString qs_captureHotkey = "captureHotkey";
const QString qs_repeatCaptureHotkey = "repeatCaptureHotkey";
@ -95,15 +98,14 @@ void cleanupOutdated(QSettings& settings)
void Settings::save() const
{
const auto baseDataPath =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
tessdataPath = baseDataPath + "/tessdata";
translatorsDir = baseDataPath + "/translators";
std::unique_ptr<QSettings> ptr;
if (isPortable_) {
ptr = std::make_unique<QSettings>(iniFileName, QSettings::IniFormat);
} else {
ptr = std::make_unique<QSettings>();
QFile::remove(iniFileName);
}
void Settings::save()
{
QSettings settings;
auto& settings = *ptr;
settings.beginGroup(qs_guiGroup);
@ -156,7 +158,15 @@ void Settings::save()
void Settings::load()
{
QSettings settings;
std::unique_ptr<QSettings> ptr;
if (QFile::exists(iniFileName)) {
ptr = std::make_unique<QSettings>(iniFileName, QSettings::IniFormat);
setPortable(true);
} else {
ptr = std::make_unique<QSettings>();
setPortable(false);
}
auto& settings = *ptr;
settings.beginGroup(qs_guiGroup);
@ -218,3 +228,20 @@ void Settings::load()
settings.endGroup();
}
bool Settings::isPortable() const
{
return isPortable_;
}
void Settings::setPortable(bool isPortable)
{
isPortable_ = isPortable;
const auto baseDataPath =
isPortable
? "."
: QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
tessdataPath = baseDataPath + "/tessdata";
translatorsDir = baseDataPath + "/translators";
}

View File

@ -24,6 +24,9 @@ public:
void save() const;
void load();
bool isPortable() const;
void setPortable(bool isPortable);
QString captureHotkey{"Ctrl+Alt+Z"};
QString repeatCaptureHotkey{"Ctrl+Alt+S"};
QString showLastHotkey{"Ctrl+Alt+X"};
@ -59,4 +62,7 @@ public:
QStringList translators{"google.js"};
ResultMode resultShowType{ResultMode::Widget}; // dialog
private:
bool isPortable_{false};
};

View File

@ -1,5 +1,4 @@
#include "settingseditor.h"
#include "debug.h"
#include "languagecodes.h"
#include "manager.h"
#include "ui_settingseditor.h"
@ -21,6 +20,8 @@ SettingsEditor::SettingsEditor(Manager &manager, update::Loader &updater)
connect(ui->buttonBox, &QDialogButtonBox::clicked, //
this, &SettingsEditor::handleButtonBoxClicked);
connect(ui->portable, &QCheckBox::toggled, //
this, &SettingsEditor::handlePortableChanged);
{
auto model = new QStringListModel(this);
model->setStringList({tr("General"), tr("Recognition"), tr("Correction"),
@ -94,6 +95,8 @@ SettingsEditor::~SettingsEditor()
Settings SettingsEditor::settings() const
{
Settings settings;
settings.setPortable(ui->portable->isChecked());
settings.captureHotkey = ui->captureEdit->keySequence().toString();
settings.repeatCaptureHotkey =
ui->repeatCaptureEdit->keySequence().toString();
@ -138,6 +141,9 @@ Settings SettingsEditor::settings() const
void SettingsEditor::setSettings(const Settings &settings)
{
wasPortable_ = settings.isPortable();
ui->portable->setChecked(settings.isPortable());
ui->captureEdit->setKeySequence(settings.captureHotkey);
ui->repeatCaptureEdit->setKeySequence(settings.repeatCaptureHotkey);
ui->repeatEdit->setKeySequence(settings.showLastHotkey);
@ -269,7 +275,28 @@ void SettingsEditor::handleButtonBoxClicked(QAbstractButton *button)
return;
}
if (button == ui->buttonBox->button(QDialogButtonBox::Apply)) {
manager_.applySettings(settings());
const auto settings = this->settings();
manager_.applySettings(settings);
if (settings.isPortable() != wasPortable_) {
wasPortable_ = settings.isPortable();
handlePortableChanged();
}
return;
}
}
void SettingsEditor::handlePortableChanged()
{
Settings settings;
settings.setPortable(ui->portable->isChecked());
ui->tessdataPath->setText(settings.tessdataPath);
ui->translatorsPath->setText(settings.translatorsDir);
updateTesseractLanguages();
updateTranslators();
const auto portableChanged = wasPortable_ != settings.isPortable();
ui->pageUpdate->setEnabled(!portableChanged);
ui->pageUpdate->setToolTip(portableChanged
? tr("Portable changed. Apply settings first")
: QString());
}

View File

@ -28,9 +28,11 @@ private:
void updateTranslationLanguages();
void adjustUpdatesView();
void handleButtonBoxClicked(QAbstractButton *button);
void handlePortableChanged();
Ui::SettingsEditor *ui;
Manager &manager_;
update::Loader &updater_;
QStringList enabledTranslators_;
bool wasPortable_{false};
};

View File

@ -179,6 +179,13 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="portable">
<property name="text">
<string>Portable (store data in same folder)</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">