Skip to content

Commit

Permalink
Add a preference for the backup interval and for enabling backups
Browse files Browse the repository at this point in the history
Bug: #392
  • Loading branch information
cameronwhite committed Oct 31, 2024
1 parent b1dec72 commit a241998
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 23 deletions.
20 changes: 17 additions & 3 deletions source/app/autobackup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "documentmanager.h"
#include "paths.h"
#include "settings.h"
#include "settingsmanager.h"

#include <actions/undomanager.h>
#include <formats/powertab/powertabexporter.h>
Expand Down Expand Up @@ -100,7 +102,7 @@ backupThread()
}

AutoBackup::AutoBackup(const DocumentManager &document_manager, const UndoManager &undo_manager,
QObject *parent)
SettingsManager &settings_manager, QObject *parent)
: QObject(parent),
myDocumentManager(document_manager),
myUndoManager(undo_manager),
Expand All @@ -109,8 +111,9 @@ AutoBackup::AutoBackup(const DocumentManager &document_manager, const UndoManage
{
connect(myTimer.get(), &QTimer::timeout, this, &AutoBackup::startBackup);

static constexpr int interval_ms = 5000; // TODO - configure via preferences.
myTimer->start(interval_ms);
updateTimerSettings(settings_manager);
mySettingsListener =
settings_manager.subscribeToChanges([&]() { updateTimerSettings(settings_manager); });
}

AutoBackup::~AutoBackup()
Expand All @@ -125,6 +128,17 @@ AutoBackup::~AutoBackup()
myWorkerThread.join();
}

void
AutoBackup::updateTimerSettings(const SettingsManager &settings_manager)
{
auto settings = settings_manager.getReadHandle();

if (settings->get(Settings::BackupEnabled))
myTimer->start(1000 * settings->get(Settings::BackupInterval));
else
myTimer->stop();
}

void
AutoBackup::startBackup()
{
Expand Down
9 changes: 7 additions & 2 deletions source/app/autobackup.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,33 @@
#define APP_AUTOBACKUP_H

#include <QObject>
#include <memory>
#include <thread>
#include <boost/signals2/signal.hpp>
#include <memory>
#include <thread>

class DocumentManager;
class QTimer;
class SettingsManager;
class UndoManager;

class AutoBackup : public QObject
{
public:
AutoBackup(const DocumentManager &document_manager, const UndoManager &undo_manager,
SettingsManager &settings_manager,
QObject *parent = nullptr);
~AutoBackup();

private:
void updateTimerSettings(const SettingsManager &settings_manager);
void startBackup();

const DocumentManager &myDocumentManager;
const UndoManager &myUndoManager;

std::unique_ptr<QTimer> myTimer;
std::thread myWorkerThread;
boost::signals2::scoped_connection mySettingsListener;
};

#endif
Expand Down
3 changes: 2 additions & 1 deletion source/app/powertabeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ PowerTabEditor::PowerTabEditor()
myDocumentManager(std::make_unique<DocumentManager>()),
myFileFormatManager(std::make_unique<FileFormatManager>(*mySettingsManager)),
myUndoManager(std::make_unique<UndoManager>()),
myAutoBackup(std::make_unique<AutoBackup>(*myDocumentManager, *myUndoManager, this)),
myAutoBackup(std::make_unique<AutoBackup>(*myDocumentManager, *myUndoManager,
*mySettingsManager, this)),
myTuningDictionary(std::make_unique<TuningDictionary>()),
myIsPlaying(false),
myRecentFiles(nullptr),
Expand Down
5 changes: 4 additions & 1 deletion source/app/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ const Setting<int> DefaultInstrumentPreset(
"app/default_instrument_preset", Midi::MIDI_PRESET_ACOUSTIC_GUITAR_STEEL);

const Setting<Tuning> DefaultTuning("app/default_tuning", Tuning());
}

const Setting<bool> BackupEnabled("app/backup_enabled", true);
const Setting<int> BackupInterval("app/backup_interval", 30);
} // namespace Settings

Tuning SettingValueConverter<Tuning>::from(const SettingsTree::SettingValue &v)
{
Expand Down
3 changes: 3 additions & 0 deletions source/app/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ namespace Settings
extern const Setting<std::string> DefaultInstrumentName;
extern const Setting<int> DefaultInstrumentPreset;
extern const Setting<Tuning> DefaultTuning;

extern const Setting<bool> BackupEnabled;
extern const Setting<int> BackupInterval;
}

template <>
Expand Down
10 changes: 10 additions & 0 deletions source/dialogs/preferencesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ PreferencesDialog::PreferencesDialog(QWidget *parent,

ui->systemSpacingSpinBox->setRange(0, 100);

ui->backupIntervalSpinBox->setRange(5, 1000);

loadCurrentSettings();
}

Expand Down Expand Up @@ -140,6 +142,9 @@ void PreferencesDialog::loadCurrentSettings()
ui->defaultPresetComboBox->setCurrentIndex(
settings->get(Settings::DefaultInstrumentPreset));

ui->enableAutoBackupCheckBox->setChecked(settings->get(Settings::BackupEnabled));
ui->backupIntervalSpinBox->setValue(settings->get(Settings::BackupInterval));

ui->defaultTuningClickButton->setToolTip(tr("Click to adjust tuning."));
myDefaultTuning = settings->get(Settings::DefaultTuning);
ui->defaultTuningClickButton->setText(
Expand Down Expand Up @@ -196,6 +201,11 @@ void PreferencesDialog::accept()
settings->set(Settings::SystemSpacing,
ui->systemSpacingSpinBox->value());

settings->set(Settings::BackupEnabled,
ui->enableAutoBackupCheckBox->isChecked());
settings->set(Settings::BackupInterval,
ui->backupIntervalSpinBox->value());

settings->set(Settings::DefaultInstrumentName,
ui->defaultInstrumentNameLineEdit->text().toStdString());

Expand Down
65 changes: 49 additions & 16 deletions source/dialogs/preferencesdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<class>PreferencesDialog</class>
<widget class="QDialog" name="PreferencesDialog">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
<enum>Qt::WindowModality::ApplicationModal</enum>
</property>
<property name="geometry">
<rect>
Expand All @@ -23,10 +23,10 @@
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::North</enum>
<enum>QTabWidget::TabPosition::North</enum>
</property>
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="midiTab">
<attribute name="title">
Expand All @@ -42,7 +42,7 @@
<item>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
<enum>QFormLayout::FieldGrowthPolicy::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="midiOutputDeviceLabel">
Expand Down Expand Up @@ -83,7 +83,7 @@
<item row="3" column="0">
<widget class="QLabel" name="playNotesWhileEditing">
<property name="text">
<string>Play Notes While Editing</string>
<string>Play Notes While Editing:</string>
</property>
</widget>
</item>
Expand All @@ -104,7 +104,7 @@
<item>
<layout class="QFormLayout" name="formLayout_2">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
<enum>QFormLayout::FieldGrowthPolicy::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="metronomeEnabledLabel">
Expand Down Expand Up @@ -166,7 +166,7 @@
<item>
<layout class="QFormLayout" name="formLayout_4">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
<enum>QFormLayout::FieldGrowthPolicy::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="countInEnabledLabel">
Expand All @@ -180,7 +180,7 @@
<string>Enabled:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
<set>Qt::AlignmentFlag::AlignRight|Qt::AlignmentFlag::AlignTrailing|Qt::AlignmentFlag::AlignVCenter</set>
</property>
</widget>
</item>
Expand Down Expand Up @@ -276,14 +276,47 @@
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="systemSpacingLabel">
<property name="text">
<string>System Spacing:</string>
</property>
</widget>
<widget class="QLabel" name="systemSpacingLabel">
<property name="text">
<string>System Spacing:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="systemSpacingSpinBox"/>
<widget class="QSpinBox" name="systemSpacingSpinBox"/>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_5">
<property name="title">
<string>Backup</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_8">
<item>
<layout class="QFormLayout" name="formLayout_6">
<item row="0" column="0">
<widget class="QLabel" name="enableAutoBackupLabel">
<property name="text">
<string>Enable Auto Backup:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QCheckBox" name="enableAutoBackupCheckBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="backupIntervalLabel">
<property name="text">
<string>Backup Interval (seconds):</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="backupIntervalSpinBox"/>
</item>
</layout>
</item>
Expand Down Expand Up @@ -348,10 +381,10 @@
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok</set>
</property>
</widget>
</item>
Expand Down

0 comments on commit a241998

Please sign in to comment.