From 551fc354393d48e419cc711f1866e73deb6738db Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 18 May 2018 14:24:54 +0800 Subject: [PATCH] Simplify the saving & loading of a setting Remove excessive usage of constexpr. --- src/gui/addnewtorrentdialog.cpp | 32 +++++++++++--------------------- src/gui/addnewtorrentdialog.h | 6 ++---- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 3d095ca91..76647f44d 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -46,7 +46,6 @@ #include "base/net/downloadmanager.h" #include "base/preferences.h" #include "base/settingsstorage.h" -#include "base/settingvalue.h" #include "base/torrentfileguard.h" #include "base/unicodestrings.h" #include "base/utils/fs.h" @@ -81,8 +80,8 @@ namespace } } -constexpr int AddNewTorrentDialog::minPathHistoryLength; -constexpr int AddNewTorrentDialog::maxPathHistoryLength; +const int AddNewTorrentDialog::minPathHistoryLength; +const int AddNewTorrentDialog::maxPathHistoryLength; AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inParams, QWidget *parent) : QDialog(parent) @@ -188,30 +187,21 @@ void AddNewTorrentDialog::setTopLevel(bool value) 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) { - Q_ASSERT(value >= minPathHistoryLength); - Q_ASSERT(value <= maxPathHistoryLength); + const int clampedValue = qBound(minPathHistoryLength, value, maxPathHistoryLength); const int oldValue = savePathHistoryLength(); - if (oldValue != value) { - savePathHistoryLengthSetting() = value; - settings()->storeValue(KEY_SAVEPATHHISTORY, - QStringList(settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList().mid(0, value))); - } -} + if (clampedValue == oldValue) + return; -CachedSettingValue &AddNewTorrentDialog::savePathHistoryLengthSetting() -{ - const int defaultHistoryLength = 8; - static CachedSettingValue setting(KEY_SAVEPATHHISTORYLENGTH.toUtf8().constData(), defaultHistoryLength, - [](int v) - { - return std::max(minPathHistoryLength, std::min(maxPathHistoryLength, v)); - }); - return setting; + settings()->storeValue(KEY_SAVEPATHHISTORYLENGTH, clampedValue); + settings()->storeValue(KEY_SAVEPATHHISTORY + , QStringList(settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList().mid(0, clampedValue))); } void AddNewTorrentDialog::loadState() diff --git a/src/gui/addnewtorrentdialog.h b/src/gui/addnewtorrentdialog.h index b52504232..c34cc5c65 100644 --- a/src/gui/addnewtorrentdialog.h +++ b/src/gui/addnewtorrentdialog.h @@ -51,15 +51,14 @@ namespace Ui class PropListDelegate; class TorrentContentFilterModel; class TorrentFileGuard; -template class CachedSettingValue; class AddNewTorrentDialog : public QDialog { Q_OBJECT public: - static constexpr int minPathHistoryLength = 0; - static constexpr int maxPathHistoryLength = 99; + static const int minPathHistoryLength = 0; + static const int maxPathHistoryLength = 99; ~AddNewTorrentDialog(); @@ -103,7 +102,6 @@ private: void setupTreeview(); void setCommentText(const QString &str) const; void setSavePath(const QString &newPath); - static CachedSettingValue &savePathHistoryLengthSetting(); void showEvent(QShowEvent *event) override;