Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5729: Move the autosave timer part to a separate class which will re…
…main in the UI module, whereas the rest of the algorithm will be moved to the core module.
- Loading branch information
1 parent
fd1f78c
commit 367e26c
Showing
9 changed files
with
163 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include "AutoSaveTimer.h" | ||
|
||
#include "iautosaver.h" | ||
#include "registry/registry.h" | ||
|
||
namespace map | ||
{ | ||
|
||
namespace | ||
{ | ||
constexpr const char* const RKEY_AUTOSAVE_INTERVAL = "user/ui/map/autoSaveInterval"; | ||
constexpr const char* const RKEY_AUTOSAVE_ENABLED = "user/ui/map/autoSaveEnabled"; | ||
} | ||
|
||
AutoSaveTimer::AutoSaveTimer() : | ||
_enabled(false), | ||
_interval(5 * 60) | ||
{} | ||
|
||
AutoSaveTimer::~AutoSaveTimer() | ||
{ | ||
_enabled = false; | ||
stopTimer(); | ||
|
||
// Destroy the timer | ||
_timer.reset(); | ||
} | ||
|
||
void AutoSaveTimer::initialise() | ||
{ | ||
// Add a page to the given group | ||
IPreferencePage& page = GlobalPreferenceSystem().getPage(_("Settings/Autosave")); | ||
|
||
// Add the checkboxes and connect them with the registry key | ||
page.appendCheckBox(_("Enable Autosave"), RKEY_AUTOSAVE_ENABLED); | ||
page.appendSlider(_("Autosave Interval (in minutes)"), RKEY_AUTOSAVE_INTERVAL, 1, 61, 1, 1); | ||
|
||
_timer.reset(new wxTimer(this)); | ||
|
||
Bind(wxEVT_TIMER, &AutoSaveTimer::onIntervalReached, this); | ||
|
||
GlobalRegistry().signalForKey(RKEY_AUTOSAVE_INTERVAL).connect( | ||
sigc::mem_fun(this, &AutoSaveTimer::registryKeyChanged) | ||
); | ||
GlobalRegistry().signalForKey(RKEY_AUTOSAVE_ENABLED).connect( | ||
sigc::mem_fun(this, &AutoSaveTimer::registryKeyChanged) | ||
); | ||
|
||
// Refresh all values from the registry right now (this might also start the timer) | ||
registryKeyChanged(); | ||
} | ||
|
||
void AutoSaveTimer::registryKeyChanged() | ||
{ | ||
// Stop the current timer | ||
stopTimer(); | ||
|
||
_enabled = registry::getValue<bool>(RKEY_AUTOSAVE_ENABLED); | ||
_interval = registry::getValue<int>(RKEY_AUTOSAVE_INTERVAL) * 60; | ||
|
||
// Start the timer with the new interval | ||
if (_enabled) | ||
{ | ||
startTimer(); | ||
} | ||
} | ||
|
||
void AutoSaveTimer::startTimer() | ||
{ | ||
if (!_timer) return; | ||
|
||
_timer->Start(static_cast<int>(_interval * 1000)); | ||
} | ||
|
||
void AutoSaveTimer::stopTimer() | ||
{ | ||
if (!_timer) return; | ||
|
||
_timer->Stop(); | ||
} | ||
|
||
void AutoSaveTimer::onIntervalReached(wxTimerEvent& ev) | ||
{ | ||
if (_enabled && GlobalAutoSaver().runAutosaveCheck()) | ||
{ | ||
// Stop the timer before saving | ||
stopTimer(); | ||
|
||
GlobalAutoSaver().performAutosave(); | ||
|
||
// Re-start the timer after saving has finished | ||
startTimer(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <wx/timer.h> | ||
#include <sigc++/trackable.h> | ||
|
||
namespace map | ||
{ | ||
|
||
/** | ||
* Timer object which repeatedly triggers the automatic map save algorithms | ||
* in the configured intervals. | ||
*/ | ||
class AutoSaveTimer final : | ||
public wxEvtHandler, | ||
public sigc::trackable | ||
{ | ||
private: | ||
// TRUE, if autosaving is enabled | ||
bool _enabled; | ||
|
||
// The autosave interval stored in seconds | ||
unsigned long _interval; | ||
|
||
// The timer object that triggers the callback | ||
wxSharedPtr<wxTimer> _timer; | ||
|
||
public: | ||
AutoSaveTimer(); | ||
~AutoSaveTimer(); | ||
|
||
void initialise(); | ||
|
||
// Starts/stops the autosave "countdown" | ||
void startTimer(); | ||
void stopTimer(); | ||
|
||
private: | ||
void registryKeyChanged(); | ||
void onIntervalReached(wxTimerEvent& ev); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.