Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add OptionsModel getOption/setOption methods #600

Merged
merged 1 commit into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions src/qt/optionsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,29 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const
{
if(role == Qt::EditRole)
{
QSettings settings;
switch(index.row())
return getOption(OptionID(index.row()));
}
return QVariant();
}

// write QSettings values
bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
bool successful = true; /* set to false on parse error */
if(role == Qt::EditRole)
{
successful = setOption(OptionID(index.row()), value);
}

Q_EMIT dataChanged(index, index);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR but we actually don't need to call dataChanged if nothing changed inside setOption (which can happen if the new value is the same as the stored one or if something bad happened internally, like a bad parsing or a impossibility to store the new value).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine as is.


return successful;
}

QVariant OptionsModel::getOption(OptionID option) const
{
QSettings settings;
switch (option) {
case StartAtStartup:
return GUIUtil::GetStartOnSystemStartup();
case ShowTrayIcon:
Expand Down Expand Up @@ -411,18 +431,13 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const
return QVariant();
}
}
return QVariant();
}

// write QSettings values
bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, int role)
bool OptionsModel::setOption(OptionID option, const QVariant& value)
{
bool successful = true; /* set to false on parse error */
if(role == Qt::EditRole)
{
QSettings settings;
switch(index.row())
{

switch (option) {
case StartAtStartup:
successful = GUIUtil::SetStartOnSystemStartup(value.toBool());
break;
Expand Down Expand Up @@ -585,9 +600,6 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
default:
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it should set successful = false? Or maybe just drop the default cases and let the compiler complain about unhandled cases?

}
}

Q_EMIT dataChanged(index, index);

return successful;
}
Expand Down
2 changes: 2 additions & 0 deletions src/qt/optionsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class OptionsModel : public QAbstractListModel
int rowCount(const QModelIndex & parent = QModelIndex()) const override;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) override;
QVariant getOption(OptionID option) const;
bool setOption(OptionID option, const QVariant& value);
/** Updates current unit in memory, settings and emits displayUnitChanged(new_unit) signal */
void setDisplayUnit(const QVariant& new_unit);

Expand Down