Browse Source

Simplify the saving & loading of a setting

Remove excessive usage of constexpr.
adaptive-webui-19844
Chocobo1 7 years ago
parent
commit
eb72b9ca7d
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 32
      src/gui/addnewtorrentdialog.cpp
  2. 6
      src/gui/addnewtorrentdialog.h

32
src/gui/addnewtorrentdialog.cpp

@ -44,7 +44,6 @@
#include "base/net/downloadmanager.h" #include "base/net/downloadmanager.h"
#include "base/preferences.h" #include "base/preferences.h"
#include "base/settingsstorage.h" #include "base/settingsstorage.h"
#include "base/settingvalue.h"
#include "base/torrentfileguard.h" #include "base/torrentfileguard.h"
#include "base/unicodestrings.h" #include "base/unicodestrings.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
@ -79,8 +78,8 @@ namespace
} }
} }
constexpr int AddNewTorrentDialog::minPathHistoryLength; const int AddNewTorrentDialog::minPathHistoryLength;
constexpr int AddNewTorrentDialog::maxPathHistoryLength; const int AddNewTorrentDialog::maxPathHistoryLength;
AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inParams, QWidget *parent) AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inParams, QWidget *parent)
: QDialog(parent) : QDialog(parent)
@ -186,30 +185,21 @@ void AddNewTorrentDialog::setTopLevel(bool value)
int AddNewTorrentDialog::savePathHistoryLength() int AddNewTorrentDialog::savePathHistoryLength()
{ {
return savePathHistoryLengthSetting(); const int defaultHistoryLength = 8;
const int value = settings()->loadValue(KEY_SAVEPATHHISTORYLENGTH, defaultHistoryLength).toInt();
return qBound(minPathHistoryLength, value, maxPathHistoryLength);
} }
void AddNewTorrentDialog::setSavePathHistoryLength(int value) void AddNewTorrentDialog::setSavePathHistoryLength(int value)
{ {
Q_ASSERT(value >= minPathHistoryLength); const int clampedValue = qBound(minPathHistoryLength, value, maxPathHistoryLength);
Q_ASSERT(value <= maxPathHistoryLength);
const int oldValue = savePathHistoryLength(); const int oldValue = savePathHistoryLength();
if (oldValue != value) { if (clampedValue == oldValue)
savePathHistoryLengthSetting() = value; return;
settings()->storeValue(KEY_SAVEPATHHISTORY,
QStringList(settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList().mid(0, value)));
}
}
CachedSettingValue<int> &AddNewTorrentDialog::savePathHistoryLengthSetting() settings()->storeValue(KEY_SAVEPATHHISTORYLENGTH, clampedValue);
{ settings()->storeValue(KEY_SAVEPATHHISTORY
const int defaultHistoryLength = 8; , QStringList(settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList().mid(0, clampedValue)));
static CachedSettingValue<int> setting(KEY_SAVEPATHHISTORYLENGTH.toUtf8().constData(), defaultHistoryLength,
[](int v)
{
return std::max(minPathHistoryLength, std::min(maxPathHistoryLength, v));
});
return setting;
} }
void AddNewTorrentDialog::loadState() void AddNewTorrentDialog::loadState()

6
src/gui/addnewtorrentdialog.h

@ -51,15 +51,14 @@ namespace Ui
class PropListDelegate; class PropListDelegate;
class TorrentContentFilterModel; class TorrentContentFilterModel;
class TorrentFileGuard; class TorrentFileGuard;
template <typename T> class CachedSettingValue;
class AddNewTorrentDialog : public QDialog class AddNewTorrentDialog : public QDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
static constexpr int minPathHistoryLength = 0; static const int minPathHistoryLength = 0;
static constexpr int maxPathHistoryLength = 99; static const int maxPathHistoryLength = 99;
~AddNewTorrentDialog(); ~AddNewTorrentDialog();
@ -103,7 +102,6 @@ private:
void setupTreeview(); void setupTreeview();
void setCommentText(const QString &str) const; void setCommentText(const QString &str) const;
void setSavePath(const QString &newPath); void setSavePath(const QString &newPath);
static CachedSettingValue<int> &savePathHistoryLengthSetting();
void showEvent(QShowEvent *event) override; void showEvent(QShowEvent *event) override;

Loading…
Cancel
Save