Skip to content
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
18 changes: 18 additions & 0 deletions src/ngscopeclient/Preference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ bool Preference::HasUnit()
return this->m_unit.GetType() != Unit::UNIT_COUNTS;
}

void Preference::ResetToDefault()
{
m_value = std::move(m_defaultValue);
}

Unit& Preference::GetUnit()
{
return this->m_unit;
Expand Down Expand Up @@ -302,6 +307,8 @@ void Preference::MoveFrom(Preference& other)
break;
}
}
// Copy default value
m_defaultValue = std::move(other.m_defaultValue);

other.m_type = PreferenceType::None;
}
Expand Down Expand Up @@ -393,6 +400,7 @@ impl::PreferenceBuilder Preference::Int(std::string identifier, int64_t defaultV
{
Preference pref(PreferenceType::Int, std::move(identifier));
pref.Construct<int64_t>(defaultValue);
new (&pref.m_defaultValue) int64_t(std::move(defaultValue));

return impl::PreferenceBuilder{ std::move(pref) };
}
Expand All @@ -401,6 +409,7 @@ impl::PreferenceBuilder Preference::Real(std::string identifier, double defaultV
{
Preference pref(PreferenceType::Real, std::move(identifier));
pref.Construct<double>(defaultValue);
new (&pref.m_defaultValue) double(std::move(defaultValue));

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

return impl::PreferenceBuilder{ std::move(pref) };
}
Expand All @@ -417,6 +427,7 @@ impl::PreferenceBuilder Preference::String(std::string identifier, std::string d
{
Preference pref(PreferenceType::String, std::move(identifier));
pref.Construct<std::string>(defaultValue);
new (&pref.m_defaultValue) std::string(std::move(defaultValue));

return impl::PreferenceBuilder{ std::move(pref) };
}
Expand All @@ -429,6 +440,11 @@ impl::PreferenceBuilder Preference::Color(std::string identifier, const ImU32& d
static_cast<uint8_t>((defaultValue >> IM_COL32_G_SHIFT) & 0xff),
static_cast<uint8_t>((defaultValue >> IM_COL32_B_SHIFT) & 0xff),
static_cast<uint8_t>((defaultValue >> IM_COL32_A_SHIFT) & 0xff)));
new (&pref.m_defaultValue) impl::Color(
static_cast<uint8_t>((defaultValue >> IM_COL32_R_SHIFT) & 0xff),
static_cast<uint8_t>((defaultValue >> IM_COL32_G_SHIFT) & 0xff),
static_cast<uint8_t>((defaultValue >> IM_COL32_B_SHIFT) & 0xff),
static_cast<uint8_t>((defaultValue >> IM_COL32_A_SHIFT) & 0xff));

return impl::PreferenceBuilder{ std::move(pref) };
}
Expand All @@ -437,6 +453,7 @@ impl::PreferenceBuilder Preference::EnumRaw(std::string identifier, std::int64_t
{
Preference pref(PreferenceType::Enum, std::move(identifier));
pref.Construct<std::int64_t>(defaultValue);
new (&pref.m_defaultValue) std::int64_t(std::move(defaultValue));

return impl::PreferenceBuilder{ std::move(pref) };
}
Expand All @@ -445,6 +462,7 @@ impl::PreferenceBuilder Preference::Font(std::string identifier, FontDescription
{
Preference pref(PreferenceType::Font, std::move(identifier));
pref.Construct<FontDescription>(defaultValue);
new (&pref.m_defaultValue) FontDescription(std::move(defaultValue));

return impl::PreferenceBuilder{ std::move(pref) };
}
Expand Down
2 changes: 2 additions & 0 deletions src/ngscopeclient/Preference.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class Preference
std::string m_description;
PreferenceType m_type;
PreferenceValue m_value;
PreferenceValue m_defaultValue;
bool m_isVisible{true};
Unit m_unit{Unit::UNIT_COUNTS};
bool m_hasValue{false};
Expand Down Expand Up @@ -204,6 +205,7 @@ class Preference
void SetLabel(std::string label);
void SetDescription(std::string description);
bool HasUnit();
void ResetToDefault();
Unit& GetUnit();
const EnumMapping& GetMapping() const;

Expand Down
119 changes: 119 additions & 0 deletions src/ngscopeclient/PreferenceDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,55 @@ void PreferenceDialog::FindFontFiles(const string& path)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rendering

bool PreferenceDialog::DefaultButton(const std::string& label, const std::string& id, bool centered)
{
float buttonWidth = ImGui::CalcTextSize(label.c_str()).x + ImGui::GetStyle().FramePadding.x * 2.0f;
float availWidth = ImGui::GetContentRegionAvail().x;
float cursorX = ImGui::GetCursorPosX();
float xPos = centered ? (cursorX + (availWidth - buttonWidth)/2.0f) : (cursorX + availWidth - buttonWidth);
ImGui::SetCursorPosX(xPos);
ImGui::PushID(id.c_str());
bool result = ImGui::Button(label.c_str());
ImGui::PopID();
ImGui::SameLine(cursorX);
return result;
}

void PreferenceDialog::OpenConfirmDialog(const std::string& title, const std::string& message, const std::string& identifier)
{
ImGui::OpenPopup((title + "###" + identifier).c_str());
m_confirmDialogTitle = title;
m_confirmDialogMessage = message;
}

bool PreferenceDialog::RenderConfirmDialog(const std::string& identifier)
{
bool confirmed = false;
if (ImGui::BeginPopupModal((m_confirmDialogTitle + "###" + identifier).c_str(), nullptr,ImGuiWindowFlags_AlwaysAutoResize))
{
ImGui::TextWrapped("%s", m_confirmDialogMessage.c_str());
ImGui::Separator();
float buttonWidth = ImGui::GetFontSize()*6;
// OK button
if (ImGui::Button("OK", ImVec2(buttonWidth, 0)))
{
confirmed = true;
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
// Cancel button
if (ImGui::Button("Cancel", ImVec2(buttonWidth, 0)) || ImGui::IsKeyPressed(ImGuiKey_Escape))
{
ImGui::CloseCurrentPopup();
}
// Default focus on cancel buttun
ImGui::SetItemDefaultFocus();
ImGui::EndPopup();
}
return confirmed;
}


/**
@brief Renders the dialog and handles UI events

Expand All @@ -132,13 +181,39 @@ bool PreferenceDialog::DoRender()
if(subCategory.IsVisible())
{
ImGui::PushID(identifier.c_str());
if(DefaultButton("Default section",identifier))
{
OpenConfirmDialog("Reset to default","Reset all settings in this section to default?",identifier);
}
if(RenderConfirmDialog(identifier))
{
ResetCategoryToDefault(subCategory);
}
if(ImGui::CollapsingHeader(identifier.c_str()))
ProcessCategory(subCategory);
ImGui::PopID();
}
}
}

ImGui::NewLine();
if(DefaultButton("Default all Preferences","###resetAll",true))
{
OpenConfirmDialog("Reset to default","Reset all settings to default?","resetAll");
}
if(RenderConfirmDialog("resetAll"))
{
for(const auto& identifier: root.GetOrdering())
{
auto& node = children[identifier];

if(node->IsCategory())
{
auto& subCategory = node->AsCategory();
ResetCategoryToDefault(subCategory);
}
}
}
return true;
}

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

if(subCategory.IsVisible())
{
if(DefaultButton("Default category",identifier))
{
OpenConfirmDialog("Reset to default","Reset all settings in this category to default?",identifier);
}
if(RenderConfirmDialog(identifier))
{
ResetCategoryToDefault(subCategory);
}
if(ImGui::TreeNode(identifier.c_str()))
{
ImGui::PushID(identifier.c_str());
Expand All @@ -175,6 +258,34 @@ void PreferenceDialog::ProcessCategory(PreferenceCategory& cat)
}
}

/**
@brief Reset all preferences in this category to default
*/
void PreferenceDialog::ResetCategoryToDefault(PreferenceCategory& cat)
{
auto& children = cat.GetChildren();
for(const auto& identifier: cat.GetOrdering())
{
auto& node = children[identifier];

//Add child categories
if(node->IsCategory())
{
auto& subCategory = node->AsCategory();
ResetCategoryToDefault(subCategory);
}

//Add preference widgets
if(node->IsPreference())
{
auto& pref = node->AsPreference();
pref.ResetToDefault();
// Clear cache
m_preferenceTemporaries.erase(pref.GetIdentifier());
}
}
}

/**
@brief Run the UI for a single preference
*/
Expand Down Expand Up @@ -328,4 +439,12 @@ void PreferenceDialog::ProcessPreference(Preference& pref)
}

HelpMarker(pref.GetDescription());
label = "Default###" + pref.GetIdentifier() + "default";
ImGui::SameLine();
if(ImGui::Button(label.c_str()))
{
pref.ResetToDefault();
// Clear cache
m_preferenceTemporaries.erase(pref.GetIdentifier());
}
}
7 changes: 7 additions & 0 deletions src/ngscopeclient/PreferenceDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ class PreferenceDialog : public Dialog
protected:
void ProcessCategory(PreferenceCategory& cat);
void ProcessPreference(Preference& pref);
bool DefaultButton(const std::string& label, const std::string& id, bool centered = false);
void ResetCategoryToDefault(PreferenceCategory& cat);
void OpenConfirmDialog(const std::string& title, const std::string& message, const std::string& identifier);
bool RenderConfirmDialog(const std::string& identifier);

PreferenceManager& m_prefs;

std::vector<std::string> m_fontPaths;
std::vector<std::string> m_fontShortNames;
std::map<std::string, size_t> m_fontReverseMap;

std::string m_confirmDialogTitle;
std::string m_confirmDialogMessage;

void FindFontFiles(const std::string& path);

//Temporary values for preferences that we're still configuring
Expand Down
Loading