Use Profile::PropertyMap in EditProfileDialog

This simplifies the code a bit.
wilder
Ahmad Samir 4 years ago
parent 6a341a520c
commit 364ece1de0
  1. 61
      src/widgets/EditProfileDialog.cpp
  2. 7
      src/widgets/EditProfileDialog.h

@ -233,7 +233,7 @@ void EditProfileDialog::save()
// to unpreview() // to unpreview()
const Profile::PropertyMap &map = _tempProfile->properties(); const Profile::PropertyMap &map = _tempProfile->properties();
for (const auto &[property, _] : map) { for (const auto &[property, _] : map) {
_previewedProperties.remove(property); _previewedProperties.erase(property);
} }
// Update the default profile if needed // Update the default profile if needed
@ -1022,34 +1022,26 @@ QSize EditProfileDialog::sizeHint() const
void EditProfileDialog::unpreviewAll() void EditProfileDialog::unpreviewAll()
{ {
Profile::PropertyMap map;
QHashIterator<int, QVariant> iter(_previewedProperties);
while (iter.hasNext()) {
iter.next();
map.insert({static_cast<Profile::Property>(iter.key()), iter.value()});
}
// undo any preview changes // undo any preview changes
if (!map.empty()) { if (!_previewedProperties.empty()) {
ProfileManager::instance()->changeProfile(_profile, map, false); ProfileManager::instance()->changeProfile(_profile, _previewedProperties, false);
} }
} }
void EditProfileDialog::unpreview(int property) void EditProfileDialog::unpreview(Profile::Property prop)
{ {
if (!_previewedProperties.contains(property)) { auto node = _previewedProperties.extract(prop);
if (!node) {
return; return;
} }
const Profile::PropertyMap map({{static_cast<Profile::Property>(property), _previewedProperties[property]}}); Profile::PropertyMap map;
map.insert(std::move(node));
ProfileManager::instance()->changeProfile(_profile, map, false); ProfileManager::instance()->changeProfile(_profile, map, false);
_previewedProperties.remove(property);
} }
void EditProfileDialog::preview(int property, const QVariant &value) void EditProfileDialog::preview(Profile::Property prop, const QVariant &value)
{ {
const Profile::PropertyMap map{{static_cast<Profile::Property>(property), value}};
const Profile::Ptr original = lookupProfile(); const Profile::Ptr original = lookupProfile();
// skip previews for profile groups if the profiles in the group // skip previews for profile groups if the profiles in the group
@ -1057,16 +1049,14 @@ void EditProfileDialog::preview(int property, const QVariant &value)
// //
// TODO - Save the original values for each profile and use to unpreview properties // TODO - Save the original values for each profile and use to unpreview properties
ProfileGroup::Ptr group = original->asGroup(); ProfileGroup::Ptr group = original->asGroup();
if (group && group->profiles().count() > 1 && original->property<QVariant>(static_cast<Profile::Property>(property)).isNull()) { if (group && group->profiles().count() > 1 && original->property<QVariant>(prop).isNull()) {
return; return;
} }
if (!_previewedProperties.contains(property)) { _previewedProperties.insert({prop, original->property<QVariant>(prop)});
_previewedProperties.insert(property, original->property<QVariant>(static_cast<Profile::Property>(property)));
}
// temporary change to color scheme // temporary change to color scheme
ProfileManager::instance()->changeProfile(_profile, map, false); ProfileManager::instance()->changeProfile(_profile, {{prop, value}}, false);
} }
void EditProfileDialog::previewColorScheme(const QModelIndex &index) void EditProfileDialog::previewColorScheme(const QModelIndex &index)
@ -1346,26 +1336,29 @@ void EditProfileDialog::updateTempProfileProperty(Profile::Property property, co
void EditProfileDialog::updateButtonApply() void EditProfileDialog::updateButtonApply()
{ {
bool userModified = false; auto filterFunc = [this](const auto &keyValue) {
const auto &[property, value] = keyValue;
const Profile::PropertyMap &map = _tempProfile->properties();
for (const auto &[property, value] : map) {
// for previewed property // for previewed property
if (_previewedProperties.contains(static_cast<int>(property))) { auto it = _previewedProperties.find(property);
if (value != _previewedProperties.value(static_cast<int>(property))) { if (it != _previewedProperties.cend()) {
userModified = true; if (value != it->second) {
break; return true;
} }
// for not-previewed property // for not-previewed property
// //
// for the Profile::KeyBindings property, if it's set in the _tempProfile // for the Profile::KeyBindings property, if it's set in the _tempProfile
// then the user opened the edit key bindings dialog and clicked // then the user opened the edit key bindings dialog and clicked
// OK, and could have add/removed a key bindings rule // OK, and could have add/removed a key bindings rule
} else if (property == Profile::KeyBindings || (value != _profile->property<QVariant>(property))) { } else if (property == Profile::KeyBindings //
userModified = true; || value != _profile->property<QVariant>(property)) {
break; return true;
} }
}
return false;
};
const Profile::PropertyMap &map = _tempProfile->properties();
bool userModified = std::any_of(map.cbegin(), map.cend(), filterFunc);
if (_generalUi->setAsDefaultButton->isChecked() != _isDefault) { if (_generalUi->setAsDefaultButton->isChecked() != _isDefault) {
userModified = true; userModified = true;

@ -272,8 +272,8 @@ private:
void showColorSchemeEditor(bool isNewScheme); void showColorSchemeEditor(bool isNewScheme);
void closeColorSchemeEditor(); void closeColorSchemeEditor();
void preview(int property, const QVariant &value); void preview(Profile::Property prop, const QVariant &value);
void unpreview(int property); void unpreview(Profile::Property prop);
void unpreviewAll(); void unpreviewAll();
void enableIfNonEmptySelection(QWidget *widget, QItemSelectionModel *selectionModel); void enableIfNonEmptySelection(QWidget *widget, QItemSelectionModel *selectionModel);
@ -344,8 +344,7 @@ private:
bool _isDefault = false; bool _isDefault = false;
QHash<int, QVariant> _previewedProperties; Profile::PropertyMap _previewedProperties;
ColorSchemeEditor *_colorDialog = nullptr; ColorSchemeEditor *_colorDialog = nullptr;
QDialogButtonBox *_buttonBox = nullptr; QDialogButtonBox *_buttonBox = nullptr;

Loading…
Cancel
Save