Skip to content

Commit

Permalink
Fix GitHub Issue #99: Program doesn't remember grid size
Browse files Browse the repository at this point in the history
  • Loading branch information
juzzlin committed Aug 7, 2020
1 parent 8f5078a commit 340530b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Bug fixes:

Other:

* Fix GitHub Issue #99: Program doesn't remember grid size

1.18.0
======

Expand Down
6 changes: 5 additions & 1 deletion src/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ QWidgetAction * MainWindow::createGridSizeAction()

// The ugly cast is needed because there are QSpinBox::valueChanged(int) and QSpinBox::valueChanged(QString)
// In Qt > 5.10 one can use QOverload<double>::of(...)
connect(m_gridSizeSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MainWindow::gridSizeChanged);
const auto signal = static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged);
connect(m_gridSizeSpinBox, signal, this, &MainWindow::gridSizeChanged);
connect(m_gridSizeSpinBox, signal, Settings::saveGridSize);

m_gridSizeSpinBox->setValue(Settings::loadGridSize());

return action;
}
Expand Down
35 changes: 35 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,50 @@

#include <QSettings>
#include <QStandardPaths>
#include <QTimer>

#include <memory>

namespace {
const auto settingsGroupApplication = "Application";
const auto settingsGroupMainWindow = "MainWindow";
const auto recentImagePathKey = "recentImagePath";
const auto gridSizeKey = "gridSize";
const auto recentPathKey = "recentPath";
const auto windowSizeKey = "size";
} // namespace

int Settings::loadGridSize()
{
QSettings settings;
settings.beginGroup(settingsGroupMainWindow);
const auto gridSize = settings.value(gridSizeKey, 0).toInt();
settings.endGroup();
return gridSize;
}

void Settings::saveGridSize(int value)
{
static std::unique_ptr<QTimer> gridSizeTimer;
static int sGridSizeValue;
sGridSizeValue = value;
if (!gridSizeTimer) {
gridSizeTimer = std::make_unique<QTimer>();
gridSizeTimer->setSingleShot(true);
gridSizeTimer->setInterval(500);
gridSizeTimer->connect(gridSizeTimer.get(), &QTimer::timeout, [&]() {
QSettings settings;
settings.beginGroup(settingsGroupMainWindow);
settings.setValue(gridSizeKey, sGridSizeValue);
settings.endGroup();
});
}
if (gridSizeTimer->isActive()) {
gridSizeTimer->stop();
}
gridSizeTimer->start();
}

QString Settings::loadRecentPath()
{
QSettings settings;
Expand Down
4 changes: 4 additions & 0 deletions src/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

namespace Settings {

int loadGridSize();

void saveGridSize(int value);

QString loadRecentPath();

void saveRecentPath(QString path);
Expand Down

0 comments on commit 340530b

Please sign in to comment.