Skip to content

Commit ad84f92

Browse files
authored
Merge pull request #945 from fredzo/fix-#297
Fix for #297 - Restore preferences default values
2 parents 85e6384 + 5c25927 commit ad84f92

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

src/ngscopeclient/Preference.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ bool Preference::HasUnit()
212212
return this->m_unit.GetType() != Unit::UNIT_COUNTS;
213213
}
214214

215+
void Preference::ResetToDefault()
216+
{
217+
m_value = std::move(m_defaultValue);
218+
}
219+
215220
Unit& Preference::GetUnit()
216221
{
217222
return this->m_unit;
@@ -302,6 +307,8 @@ void Preference::MoveFrom(Preference& other)
302307
break;
303308
}
304309
}
310+
// Copy default value
311+
m_defaultValue = std::move(other.m_defaultValue);
305312

306313
other.m_type = PreferenceType::None;
307314
}
@@ -393,6 +400,7 @@ impl::PreferenceBuilder Preference::Int(std::string identifier, int64_t defaultV
393400
{
394401
Preference pref(PreferenceType::Int, std::move(identifier));
395402
pref.Construct<int64_t>(defaultValue);
403+
new (&pref.m_defaultValue) int64_t(std::move(defaultValue));
396404

397405
return impl::PreferenceBuilder{ std::move(pref) };
398406
}
@@ -401,6 +409,7 @@ impl::PreferenceBuilder Preference::Real(std::string identifier, double defaultV
401409
{
402410
Preference pref(PreferenceType::Real, std::move(identifier));
403411
pref.Construct<double>(defaultValue);
412+
new (&pref.m_defaultValue) double(std::move(defaultValue));
404413

405414
return impl::PreferenceBuilder{ std::move(pref) };
406415
}
@@ -409,6 +418,7 @@ impl::PreferenceBuilder Preference::Bool(std::string identifier, bool defaultVal
409418
{
410419
Preference pref(PreferenceType::Boolean, std::move(identifier));
411420
pref.Construct<bool>(defaultValue);
421+
new (&pref.m_defaultValue) bool(std::move(defaultValue));
412422

413423
return impl::PreferenceBuilder{ std::move(pref) };
414424
}
@@ -417,6 +427,7 @@ impl::PreferenceBuilder Preference::String(std::string identifier, std::string d
417427
{
418428
Preference pref(PreferenceType::String, std::move(identifier));
419429
pref.Construct<std::string>(defaultValue);
430+
new (&pref.m_defaultValue) std::string(std::move(defaultValue));
420431

421432
return impl::PreferenceBuilder{ std::move(pref) };
422433
}
@@ -429,6 +440,11 @@ impl::PreferenceBuilder Preference::Color(std::string identifier, const ImU32& d
429440
static_cast<uint8_t>((defaultValue >> IM_COL32_G_SHIFT) & 0xff),
430441
static_cast<uint8_t>((defaultValue >> IM_COL32_B_SHIFT) & 0xff),
431442
static_cast<uint8_t>((defaultValue >> IM_COL32_A_SHIFT) & 0xff)));
443+
new (&pref.m_defaultValue) impl::Color(
444+
static_cast<uint8_t>((defaultValue >> IM_COL32_R_SHIFT) & 0xff),
445+
static_cast<uint8_t>((defaultValue >> IM_COL32_G_SHIFT) & 0xff),
446+
static_cast<uint8_t>((defaultValue >> IM_COL32_B_SHIFT) & 0xff),
447+
static_cast<uint8_t>((defaultValue >> IM_COL32_A_SHIFT) & 0xff));
432448

433449
return impl::PreferenceBuilder{ std::move(pref) };
434450
}
@@ -437,6 +453,7 @@ impl::PreferenceBuilder Preference::EnumRaw(std::string identifier, std::int64_t
437453
{
438454
Preference pref(PreferenceType::Enum, std::move(identifier));
439455
pref.Construct<std::int64_t>(defaultValue);
456+
new (&pref.m_defaultValue) std::int64_t(std::move(defaultValue));
440457

441458
return impl::PreferenceBuilder{ std::move(pref) };
442459
}
@@ -445,6 +462,7 @@ impl::PreferenceBuilder Preference::Font(std::string identifier, FontDescription
445462
{
446463
Preference pref(PreferenceType::Font, std::move(identifier));
447464
pref.Construct<FontDescription>(defaultValue);
465+
new (&pref.m_defaultValue) FontDescription(std::move(defaultValue));
448466

449467
return impl::PreferenceBuilder{ std::move(pref) };
450468
}

src/ngscopeclient/Preference.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class Preference
146146
std::string m_description;
147147
PreferenceType m_type;
148148
PreferenceValue m_value;
149+
PreferenceValue m_defaultValue;
149150
bool m_isVisible{true};
150151
Unit m_unit{Unit::UNIT_COUNTS};
151152
bool m_hasValue{false};
@@ -204,6 +205,7 @@ class Preference
204205
void SetLabel(std::string label);
205206
void SetDescription(std::string description);
206207
bool HasUnit();
208+
void ResetToDefault();
207209
Unit& GetUnit();
208210
const EnumMapping& GetMapping() const;
209211

src/ngscopeclient/PreferenceDialog.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,55 @@ void PreferenceDialog::FindFontFiles(const string& path)
110110
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
111111
// Rendering
112112

113+
bool PreferenceDialog::DefaultButton(const std::string& label, const std::string& id, bool centered)
114+
{
115+
float buttonWidth = ImGui::CalcTextSize(label.c_str()).x + ImGui::GetStyle().FramePadding.x * 2.0f;
116+
float availWidth = ImGui::GetContentRegionAvail().x;
117+
float cursorX = ImGui::GetCursorPosX();
118+
float xPos = centered ? (cursorX + (availWidth - buttonWidth)/2.0f) : (cursorX + availWidth - buttonWidth);
119+
ImGui::SetCursorPosX(xPos);
120+
ImGui::PushID(id.c_str());
121+
bool result = ImGui::Button(label.c_str());
122+
ImGui::PopID();
123+
ImGui::SameLine(cursorX);
124+
return result;
125+
}
126+
127+
void PreferenceDialog::OpenConfirmDialog(const std::string& title, const std::string& message, const std::string& identifier)
128+
{
129+
ImGui::OpenPopup((title + "###" + identifier).c_str());
130+
m_confirmDialogTitle = title;
131+
m_confirmDialogMessage = message;
132+
}
133+
134+
bool PreferenceDialog::RenderConfirmDialog(const std::string& identifier)
135+
{
136+
bool confirmed = false;
137+
if (ImGui::BeginPopupModal((m_confirmDialogTitle + "###" + identifier).c_str(), nullptr,ImGuiWindowFlags_AlwaysAutoResize))
138+
{
139+
ImGui::TextWrapped("%s", m_confirmDialogMessage.c_str());
140+
ImGui::Separator();
141+
float buttonWidth = ImGui::GetFontSize()*6;
142+
// OK button
143+
if (ImGui::Button("OK", ImVec2(buttonWidth, 0)))
144+
{
145+
confirmed = true;
146+
ImGui::CloseCurrentPopup();
147+
}
148+
ImGui::SameLine();
149+
// Cancel button
150+
if (ImGui::Button("Cancel", ImVec2(buttonWidth, 0)) || ImGui::IsKeyPressed(ImGuiKey_Escape))
151+
{
152+
ImGui::CloseCurrentPopup();
153+
}
154+
// Default focus on cancel buttun
155+
ImGui::SetItemDefaultFocus();
156+
ImGui::EndPopup();
157+
}
158+
return confirmed;
159+
}
160+
161+
113162
/**
114163
@brief Renders the dialog and handles UI events
115164
@@ -132,13 +181,39 @@ bool PreferenceDialog::DoRender()
132181
if(subCategory.IsVisible())
133182
{
134183
ImGui::PushID(identifier.c_str());
184+
if(DefaultButton("Default section",identifier))
185+
{
186+
OpenConfirmDialog("Reset to default","Reset all settings in this section to default?",identifier);
187+
}
188+
if(RenderConfirmDialog(identifier))
189+
{
190+
ResetCategoryToDefault(subCategory);
191+
}
135192
if(ImGui::CollapsingHeader(identifier.c_str()))
136193
ProcessCategory(subCategory);
137194
ImGui::PopID();
138195
}
139196
}
140197
}
141198

199+
ImGui::NewLine();
200+
if(DefaultButton("Default all Preferences","###resetAll",true))
201+
{
202+
OpenConfirmDialog("Reset to default","Reset all settings to default?","resetAll");
203+
}
204+
if(RenderConfirmDialog("resetAll"))
205+
{
206+
for(const auto& identifier: root.GetOrdering())
207+
{
208+
auto& node = children[identifier];
209+
210+
if(node->IsCategory())
211+
{
212+
auto& subCategory = node->AsCategory();
213+
ResetCategoryToDefault(subCategory);
214+
}
215+
}
216+
}
142217
return true;
143218
}
144219

@@ -159,6 +234,14 @@ void PreferenceDialog::ProcessCategory(PreferenceCategory& cat)
159234

160235
if(subCategory.IsVisible())
161236
{
237+
if(DefaultButton("Default category",identifier))
238+
{
239+
OpenConfirmDialog("Reset to default","Reset all settings in this category to default?",identifier);
240+
}
241+
if(RenderConfirmDialog(identifier))
242+
{
243+
ResetCategoryToDefault(subCategory);
244+
}
162245
if(ImGui::TreeNode(identifier.c_str()))
163246
{
164247
ImGui::PushID(identifier.c_str());
@@ -175,6 +258,34 @@ void PreferenceDialog::ProcessCategory(PreferenceCategory& cat)
175258
}
176259
}
177260

261+
/**
262+
@brief Reset all preferences in this category to default
263+
*/
264+
void PreferenceDialog::ResetCategoryToDefault(PreferenceCategory& cat)
265+
{
266+
auto& children = cat.GetChildren();
267+
for(const auto& identifier: cat.GetOrdering())
268+
{
269+
auto& node = children[identifier];
270+
271+
//Add child categories
272+
if(node->IsCategory())
273+
{
274+
auto& subCategory = node->AsCategory();
275+
ResetCategoryToDefault(subCategory);
276+
}
277+
278+
//Add preference widgets
279+
if(node->IsPreference())
280+
{
281+
auto& pref = node->AsPreference();
282+
pref.ResetToDefault();
283+
// Clear cache
284+
m_preferenceTemporaries.erase(pref.GetIdentifier());
285+
}
286+
}
287+
}
288+
178289
/**
179290
@brief Run the UI for a single preference
180291
*/
@@ -328,4 +439,12 @@ void PreferenceDialog::ProcessPreference(Preference& pref)
328439
}
329440

330441
HelpMarker(pref.GetDescription());
442+
label = "Default###" + pref.GetIdentifier() + "default";
443+
ImGui::SameLine();
444+
if(ImGui::Button(label.c_str()))
445+
{
446+
pref.ResetToDefault();
447+
// Clear cache
448+
m_preferenceTemporaries.erase(pref.GetIdentifier());
449+
}
331450
}

src/ngscopeclient/PreferenceDialog.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,20 @@ class PreferenceDialog : public Dialog
5252
protected:
5353
void ProcessCategory(PreferenceCategory& cat);
5454
void ProcessPreference(Preference& pref);
55+
bool DefaultButton(const std::string& label, const std::string& id, bool centered = false);
56+
void ResetCategoryToDefault(PreferenceCategory& cat);
57+
void OpenConfirmDialog(const std::string& title, const std::string& message, const std::string& identifier);
58+
bool RenderConfirmDialog(const std::string& identifier);
5559

5660
PreferenceManager& m_prefs;
5761

5862
std::vector<std::string> m_fontPaths;
5963
std::vector<std::string> m_fontShortNames;
6064
std::map<std::string, size_t> m_fontReverseMap;
6165

66+
std::string m_confirmDialogTitle;
67+
std::string m_confirmDialogMessage;
68+
6269
void FindFontFiles(const std::string& path);
6370

6471
//Temporary values for preferences that we're still configuring

0 commit comments

Comments
 (0)