Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ngscopeclient/scopehal-apps
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: aeac6656259e
Choose a base ref
...
head repository: ngscopeclient/scopehal-apps
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f762625718f3
Choose a head ref
  • 3 commits
  • 4 files changed
  • 2 contributors

Commits on Oct 27, 2020

  1. Added units to preference class

    nshcat committed Oct 27, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    45e684e View commit details
  2. Implemented units in interface

    nshcat committed Oct 27, 2020
    Copy the full SHA
    a1dab64 View commit details

Commits on Oct 28, 2020

  1. Merge pull request #259 from azonenberg/kathi_dev

    Implemented units in the preferences system and preferences GUI
    azonenberg authored Oct 28, 2020
    Copy the full SHA
    f762625 View commit details
Showing with 140 additions and 17 deletions.
  1. +65 −0 src/glscopeclient/Preference.cpp
  2. +43 −9 src/glscopeclient/Preference.h
  3. +28 −4 src/glscopeclient/PreferenceDialog.cpp
  4. +4 −4 src/glscopeclient/PreferenceManager.cpp
65 changes: 65 additions & 0 deletions src/glscopeclient/Preference.cpp
Original file line number Diff line number Diff line change
@@ -39,6 +39,32 @@

using namespace std;

namespace impl
{
PreferenceBuilder::PreferenceBuilder(Preference&& pref)
: m_pref{move(pref)}
{

}

PreferenceBuilder&& PreferenceBuilder::IsVisible(bool isVisible) &&
{
this->m_pref.m_isVisible = isVisible;
return move(*this);
}

PreferenceBuilder&& PreferenceBuilder::WithUnit(Unit::UnitType type) &&
{
this->m_pref.m_unit = Unit{ type };
return move(*this);
}

Preference&& PreferenceBuilder::Build() &&
{
return move(this->m_pref);
}
}

const string& Preference::GetIdentifier() const
{
return m_identifier;
@@ -75,6 +101,16 @@ double Preference::GetReal() const
return GetValueRaw<double>();
}

bool Preference::HasUnit()
{
return this->m_unit.GetType() != Unit::UNIT_COUNTS;
}

Unit& Preference::GetUnit()
{
return this->m_unit;
}

const std::string& Preference::GetString() const
{
if(m_type != PreferenceType::String)
@@ -111,6 +147,7 @@ void Preference::MoveFrom(Preference& other)
m_description = move(other.m_description);
m_label = move(other.m_label);
m_isVisible = move(other.m_isVisible);
m_unit = move(other.m_unit);

switch(other.m_type)
{
@@ -156,3 +193,31 @@ void Preference::SetString(string value)
CleanUp();
Construct<string>(move(value));
}

impl::PreferenceBuilder Preference::New(std::string identifier, std::string label, std::string description, bool defaultValue)
{
return impl::PreferenceBuilder(
Preference(std::move(identifier), std::move(label), std::move(description), defaultValue)
);
}

impl::PreferenceBuilder Preference::New(std::string identifier, std::string label, std::string description, double defaultValue)
{
return impl::PreferenceBuilder(
Preference(std::move(identifier), std::move(label), std::move(description), defaultValue)
);
}

impl::PreferenceBuilder Preference::New(std::string identifier, std::string label, std::string description, const char* defaultValue)
{
return impl::PreferenceBuilder(
Preference(std::move(identifier), std::move(label), std::move(description), defaultValue)
);
}

impl::PreferenceBuilder Preference::New(std::string identifier, std::string label, std::string description, std::string defaultValue)
{
return impl::PreferenceBuilder(
Preference(std::move(identifier), std::move(label), std::move(description), std::move(defaultValue))
);
}
52 changes: 43 additions & 9 deletions src/glscopeclient/Preference.h
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@
#include <string>
#include <type_traits>
#include <utility>
#include "Unit.h"

constexpr std::size_t max(std::size_t a, std::size_t b)
{
@@ -53,8 +54,15 @@ enum class PreferenceType
None // Only for moved-from values
};

namespace impl
{
class PreferenceBuilder;
}

class Preference
{
friend class impl::PreferenceBuilder;

private:
using PreferenceValue = typename std::aligned_union<
max(max(sizeof(bool), sizeof(double)), sizeof(std::string)),
@@ -63,30 +71,30 @@ class Preference

public:
// Taking string as value and then moving is intended
Preference(std::string identifier, std::string label, std::string description, bool defaultValue, bool isVisible = true)
Preference(std::string identifier, std::string label, std::string description, bool defaultValue)
: m_identifier{std::move(identifier)}, m_label{std::move(label)}, m_description{std::move(description)},
m_type{PreferenceType::Boolean}, m_isVisible{isVisible}
m_type{PreferenceType::Boolean}
{
new (&m_value) bool(defaultValue);
}

Preference(std::string identifier, std::string label, std::string description, std::string defaultValue, bool isVisible = true)
Preference(std::string identifier, std::string label, std::string description, std::string defaultValue)
: m_identifier{std::move(identifier)}, m_label{std::move(label)}, m_description{std::move(description)},
m_type{PreferenceType::String}, m_isVisible{isVisible}
m_type{PreferenceType::String}
{
new (&m_value) std::string(std::move(defaultValue));
}

Preference(std::string identifier, std::string label, std::string description, const char* defaultValue, bool isVisible = true)
Preference(std::string identifier, std::string label, std::string description, const char* defaultValue)
: m_identifier{std::move(identifier)}, m_label{std::move(label)}, m_description{std::move(description)},
m_type{PreferenceType::String}, m_isVisible{isVisible}
m_type{PreferenceType::String}
{
new (&m_value) std::string(defaultValue);
}

Preference(std::string identifier, std::string label, std::string description, double defaultValue, bool isVisible = true)
Preference(std::string identifier, std::string label, std::string description, double defaultValue)
: m_identifier{std::move(identifier)}, m_label{std::move(label)}, m_description{std::move(description)},
m_type{PreferenceType::Real}, m_isVisible{isVisible}
m_type{PreferenceType::Real}
{
new (&m_value) double(defaultValue);
}
@@ -95,6 +103,12 @@ class Preference
{
CleanUp();
}

public:
static impl::PreferenceBuilder New(std::string identifier, std::string label, std::string description, bool defaultValue);
static impl::PreferenceBuilder New(std::string identifier, std::string label, std::string description, double defaultValue);
static impl::PreferenceBuilder New(std::string identifier, std::string label, std::string description, const char* defaultValue);
static impl::PreferenceBuilder New(std::string identifier, std::string label, std::string description, std::string defaultValue);

public:
Preference(const Preference&) = delete;
@@ -126,6 +140,8 @@ class Preference
void SetReal(double value);
void SetString(std::string value);

bool HasUnit();
Unit& GetUnit();

private:
template<typename T>
@@ -156,7 +172,25 @@ class Preference
std::string m_description;
PreferenceType m_type;
PreferenceValue m_value;
bool m_isVisible;
bool m_isVisible{true};
Unit m_unit{Unit::UNIT_COUNTS};
};

namespace impl
{
class PreferenceBuilder
{
public:
PreferenceBuilder(Preference&& pref);

public:
PreferenceBuilder&& IsVisible(bool isVisible) &&;
PreferenceBuilder&& WithUnit(Unit::UnitType type) &&;
Preference&& Build() &&;

protected:
Preference m_pref;
};
}

#endif // Preference_h
32 changes: 28 additions & 4 deletions src/glscopeclient/PreferenceDialog.cpp
Original file line number Diff line number Diff line change
@@ -105,9 +105,25 @@ void PreferencePage::CreateWidgets()
row->m_label.set_halign(Gtk::ALIGN_START);
row->m_label.set_tooltip_text(preference.GetDescription());
row->m_value.set_tooltip_text(preference.GetDescription());

const auto text = (preference.GetType() == PreferenceType::Real) ?
to_string(preference.GetReal()) : preference.GetString();

std::string text;

if(preference.GetType() == PreferenceType::Real)
{
if(preference.HasUnit())
{
auto unit = preference.GetUnit();
text = unit.PrettyPrint(preference.GetReal());
}
else
{
text = to_string(preference.GetReal());
}
}
else
{
text = preference.GetString();
}

row->m_value.set_text(text);

@@ -173,7 +189,15 @@ void PreferencePage::SaveChanges()
{
try
{
preference.SetReal(stod(text));
if(preference.HasUnit())
{
auto unit = preference.GetUnit();
preference.SetReal(unit.ParseString(text));
}
else
{
preference.SetReal(stod(text));
}
}
catch(...)
{
8 changes: 4 additions & 4 deletions src/glscopeclient/PreferenceManager.cpp
Original file line number Diff line number Diff line change
@@ -88,20 +88,20 @@ void PreferenceManager::InitializeDefaults()
auto& instrument = this->m_treeRoot.AddCategory("Instrument");
auto& trans = instrument.AddCategory("Transports");
trans.AddPreference(Preference("test_string", "Test string", "First test value", "string"));
trans.AddPreference(Preference("test_real", "Test real", "Second test value", 42.09));
trans.AddPreference(Preference::New("test_real", "Test real", "Second test value", 42.09).WithUnit(Unit::UNIT_VOLTS).Build());
trans.AddPreference(Preference("test_bool", "Test boolean", "Third test value", true));
auto& decode = instrument.AddCategory("Decoders");
decode.AddPreference(Preference("test_string", "Test string", "First test value", "string"));
decode.AddPreference(Preference("test_real", "Test real", "Second test value", 42.09));
decode.AddPreference(Preference::New("test_real", "Test real", "Second test value", 42.09).WithUnit(Unit::UNIT_AMPS).Build());
decode.AddPreference(Preference("test_bool", "Test boolean", "Third test value", true));

auto& debug = this->m_treeRoot.AddCategory("Debug");
auto& testSettings = debug.AddCategory("Test Settings");
testSettings.AddPreference(Preference("test_string", "Test string", "First test value", "string"));
testSettings.AddPreference(Preference("test_real", "Test real", "Second test value", 42.09));
testSettings.AddPreference(Preference("test_bool", "Test boolean", "Third test value", true, false));
testSettings.AddPreference(Preference("test_bool", "Test boolean", "Third test value", true));
auto& miscSettings = debug.AddCategory("Misc");
miscSettings.AddPreference(Preference("misc_test_1", "Misc test real", "blabla", 13.37, false));
miscSettings.AddPreference(Preference("misc_test_1", "Misc test real", "blabla", 13.37));
}

PreferenceCategory& PreferenceManager::AllPreferences()