Added portable mode
This commit is contained in:
		
							parent
							
								
									83232bfc76
								
							
						
					
					
						commit
						493262876f
					
				@ -1,10 +1,13 @@
 | 
				
			|||||||
#include "settings.h"
 | 
					#include "settings.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <QFile>
 | 
				
			||||||
#include <QSettings>
 | 
					#include <QSettings>
 | 
				
			||||||
#include <QStandardPaths>
 | 
					#include <QStandardPaths>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace
 | 
					namespace
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					const QString iniFileName = "settings.ini";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const QString qs_guiGroup = "GUI";
 | 
					const QString qs_guiGroup = "GUI";
 | 
				
			||||||
const QString qs_captureHotkey = "captureHotkey";
 | 
					const QString qs_captureHotkey = "captureHotkey";
 | 
				
			||||||
const QString qs_repeatCaptureHotkey = "repeatCaptureHotkey";
 | 
					const QString qs_repeatCaptureHotkey = "repeatCaptureHotkey";
 | 
				
			||||||
@ -95,15 +98,14 @@ void cleanupOutdated(QSettings& settings)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void Settings::save() const
 | 
					void Settings::save() const
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  const auto baseDataPath =
 | 
					  std::unique_ptr<QSettings> ptr;
 | 
				
			||||||
      QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
 | 
					  if (isPortable_) {
 | 
				
			||||||
  tessdataPath = baseDataPath + "/tessdata";
 | 
					    ptr = std::make_unique<QSettings>(iniFileName, QSettings::IniFormat);
 | 
				
			||||||
  translatorsDir = baseDataPath + "/translators";
 | 
					  } else {
 | 
				
			||||||
 | 
					    ptr = std::make_unique<QSettings>();
 | 
				
			||||||
 | 
					    QFile::remove(iniFileName);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					  auto& settings = *ptr;
 | 
				
			||||||
void Settings::save()
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
  QSettings settings;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  settings.beginGroup(qs_guiGroup);
 | 
					  settings.beginGroup(qs_guiGroup);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -156,7 +158,15 @@ void Settings::save()
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void Settings::load()
 | 
					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);
 | 
					  settings.beginGroup(qs_guiGroup);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -218,3 +228,20 @@ void Settings::load()
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  settings.endGroup();
 | 
					  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";
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -24,6 +24,9 @@ public:
 | 
				
			|||||||
  void save() const;
 | 
					  void save() const;
 | 
				
			||||||
  void load();
 | 
					  void load();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  bool isPortable() const;
 | 
				
			||||||
 | 
					  void setPortable(bool isPortable);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  QString captureHotkey{"Ctrl+Alt+Z"};
 | 
					  QString captureHotkey{"Ctrl+Alt+Z"};
 | 
				
			||||||
  QString repeatCaptureHotkey{"Ctrl+Alt+S"};
 | 
					  QString repeatCaptureHotkey{"Ctrl+Alt+S"};
 | 
				
			||||||
  QString showLastHotkey{"Ctrl+Alt+X"};
 | 
					  QString showLastHotkey{"Ctrl+Alt+X"};
 | 
				
			||||||
@ -59,4 +62,7 @@ public:
 | 
				
			|||||||
  QStringList translators{"google.js"};
 | 
					  QStringList translators{"google.js"};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ResultMode resultShowType{ResultMode::Widget};  // dialog
 | 
					  ResultMode resultShowType{ResultMode::Widget};  // dialog
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					  bool isPortable_{false};
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,4 @@
 | 
				
			|||||||
#include "settingseditor.h"
 | 
					#include "settingseditor.h"
 | 
				
			||||||
#include "debug.h"
 | 
					 | 
				
			||||||
#include "languagecodes.h"
 | 
					#include "languagecodes.h"
 | 
				
			||||||
#include "manager.h"
 | 
					#include "manager.h"
 | 
				
			||||||
#include "ui_settingseditor.h"
 | 
					#include "ui_settingseditor.h"
 | 
				
			||||||
@ -21,6 +20,8 @@ SettingsEditor::SettingsEditor(Manager &manager, update::Loader &updater)
 | 
				
			|||||||
  connect(ui->buttonBox, &QDialogButtonBox::clicked,  //
 | 
					  connect(ui->buttonBox, &QDialogButtonBox::clicked,  //
 | 
				
			||||||
          this, &SettingsEditor::handleButtonBoxClicked);
 | 
					          this, &SettingsEditor::handleButtonBoxClicked);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  connect(ui->portable, &QCheckBox::toggled,  //
 | 
				
			||||||
 | 
					          this, &SettingsEditor::handlePortableChanged);
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    auto model = new QStringListModel(this);
 | 
					    auto model = new QStringListModel(this);
 | 
				
			||||||
    model->setStringList({tr("General"), tr("Recognition"), tr("Correction"),
 | 
					    model->setStringList({tr("General"), tr("Recognition"), tr("Correction"),
 | 
				
			||||||
@ -94,6 +95,8 @@ SettingsEditor::~SettingsEditor()
 | 
				
			|||||||
Settings SettingsEditor::settings() const
 | 
					Settings SettingsEditor::settings() const
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  Settings settings;
 | 
					  Settings settings;
 | 
				
			||||||
 | 
					  settings.setPortable(ui->portable->isChecked());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  settings.captureHotkey = ui->captureEdit->keySequence().toString();
 | 
					  settings.captureHotkey = ui->captureEdit->keySequence().toString();
 | 
				
			||||||
  settings.repeatCaptureHotkey =
 | 
					  settings.repeatCaptureHotkey =
 | 
				
			||||||
      ui->repeatCaptureEdit->keySequence().toString();
 | 
					      ui->repeatCaptureEdit->keySequence().toString();
 | 
				
			||||||
@ -138,6 +141,9 @@ Settings SettingsEditor::settings() const
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void SettingsEditor::setSettings(const Settings &settings)
 | 
					void SettingsEditor::setSettings(const Settings &settings)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					  wasPortable_ = settings.isPortable();
 | 
				
			||||||
 | 
					  ui->portable->setChecked(settings.isPortable());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ui->captureEdit->setKeySequence(settings.captureHotkey);
 | 
					  ui->captureEdit->setKeySequence(settings.captureHotkey);
 | 
				
			||||||
  ui->repeatCaptureEdit->setKeySequence(settings.repeatCaptureHotkey);
 | 
					  ui->repeatCaptureEdit->setKeySequence(settings.repeatCaptureHotkey);
 | 
				
			||||||
  ui->repeatEdit->setKeySequence(settings.showLastHotkey);
 | 
					  ui->repeatEdit->setKeySequence(settings.showLastHotkey);
 | 
				
			||||||
@ -269,7 +275,28 @@ void SettingsEditor::handleButtonBoxClicked(QAbstractButton *button)
 | 
				
			|||||||
    return;
 | 
					    return;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  if (button == ui->buttonBox->button(QDialogButtonBox::Apply)) {
 | 
					  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;
 | 
					    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());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -28,9 +28,11 @@ private:
 | 
				
			|||||||
  void updateTranslationLanguages();
 | 
					  void updateTranslationLanguages();
 | 
				
			||||||
  void adjustUpdatesView();
 | 
					  void adjustUpdatesView();
 | 
				
			||||||
  void handleButtonBoxClicked(QAbstractButton *button);
 | 
					  void handleButtonBoxClicked(QAbstractButton *button);
 | 
				
			||||||
 | 
					  void handlePortableChanged();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Ui::SettingsEditor *ui;
 | 
					  Ui::SettingsEditor *ui;
 | 
				
			||||||
  Manager &manager_;
 | 
					  Manager &manager_;
 | 
				
			||||||
  update::Loader &updater_;
 | 
					  update::Loader &updater_;
 | 
				
			||||||
  QStringList enabledTranslators_;
 | 
					  QStringList enabledTranslators_;
 | 
				
			||||||
 | 
					  bool wasPortable_{false};
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -179,6 +179,13 @@
 | 
				
			|||||||
         </property>
 | 
					         </property>
 | 
				
			||||||
        </widget>
 | 
					        </widget>
 | 
				
			||||||
       </item>
 | 
					       </item>
 | 
				
			||||||
 | 
					       <item>
 | 
				
			||||||
 | 
					        <widget class="QCheckBox" name="portable">
 | 
				
			||||||
 | 
					         <property name="text">
 | 
				
			||||||
 | 
					          <string>Portable (store data in same folder)</string>
 | 
				
			||||||
 | 
					         </property>
 | 
				
			||||||
 | 
					        </widget>
 | 
				
			||||||
 | 
					       </item>
 | 
				
			||||||
       <item>
 | 
					       <item>
 | 
				
			||||||
        <spacer name="verticalSpacer_4">
 | 
					        <spacer name="verticalSpacer_4">
 | 
				
			||||||
         <property name="orientation">
 | 
					         <property name="orientation">
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user