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: e74ff3958bb2
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: 4c7a2ace0e4f
Choose a head ref
  • 2 commits
  • 6 files changed
  • 2 contributors

Commits on Oct 29, 2020

  1. Implemented color preferences

    nshcat committed Oct 29, 2020
    Copy the full SHA
    0ab9dd5 View commit details
  2. Merge pull request #261 from azonenberg/kathi_dev

    Implemented color preferences
    azonenberg authored Oct 29, 2020
    Copy the full SHA
    4c7a2ac View commit details
40 changes: 40 additions & 0 deletions src/glscopeclient/Preference.cpp
Original file line number Diff line number Diff line change
@@ -93,6 +93,27 @@ bool Preference::GetBool() const
return GetValueRaw<bool>();
}

Gdk::Color Preference::GetColor() const
{
if(m_type != PreferenceType::Color)
throw runtime_error("Preference type mismatch");

const auto& value = GetValueRaw<impl::Color>();
Gdk::Color color{};
color.set_red(value.m_r);
color.set_green(value.m_g);
color.set_blue(value.m_b);
return color;
}

const impl::Color& Preference::GetColorRaw() const
{
if(m_type != PreferenceType::Color)
throw runtime_error("Preference type mismatch");

return GetValueRaw<impl::Color>();
}

double Preference::GetReal() const
{
if(m_type != PreferenceType::Real)
@@ -135,6 +156,8 @@ string Preference::ToString() const
return GetBool() ? "true" : "false";
case PreferenceType::Real:
return to_string(GetReal());
case PreferenceType::Color:
return "Color";
default:
throw runtime_error("tried to retrieve value from preference in moved-from state");
}
@@ -162,6 +185,10 @@ void Preference::MoveFrom(Preference& other)
case PreferenceType::String:
Construct<string>(move(other.GetValueRaw<string>()));
break;

case PreferenceType::Color:
Construct<impl::Color>(move(other.GetValueRaw<impl::Color>()));
break;

default:
break;
@@ -194,6 +221,19 @@ void Preference::SetString(string value)
Construct<string>(move(value));
}

void Preference::SetColor(const Gdk::Color& color)
{
CleanUp();
impl::Color clr{ color.get_red(), color.get_green(), color.get_blue() };
Construct<impl::Color>(move(clr));
}

void Preference::SetColorRaw(const impl::Color& color)
{
CleanUp();
Construct<impl::Color>(color);
}

impl::PreferenceBuilder Preference::New(std::string identifier, std::string label, std::string description, bool defaultValue)
{
return impl::PreferenceBuilder(
39 changes: 34 additions & 5 deletions src/glscopeclient/Preference.h
Original file line number Diff line number Diff line change
@@ -39,6 +39,11 @@
#include <string>
#include <type_traits>
#include <utility>
#include <cstdint>

#include <giomm.h>
#include <gtkmm.h>

#include "Unit.h"

constexpr std::size_t max(std::size_t a, std::size_t b)
@@ -51,12 +56,24 @@ enum class PreferenceType
Boolean,
String,
Real,
Color,
None // Only for moved-from values
};

namespace impl
{
class PreferenceBuilder;

struct Color
{
Color(std::uint16_t r, std::uint16_t g, std::uint16_t b)
: m_r{r}, m_g{g}, m_b{b}
{

}

std::uint16_t m_r, m_g, m_b;
};
}

class Preference
@@ -65,8 +82,8 @@ class Preference

private:
using PreferenceValue = typename std::aligned_union<
max(max(sizeof(bool), sizeof(double)), sizeof(std::string)),
bool, std::string, double
max(sizeof(impl::Color), max(max(sizeof(bool), sizeof(double)), sizeof(std::string))),
bool, std::string, double, impl::Color
>::type;

public:
@@ -98,6 +115,13 @@ class Preference
{
new (&m_value) double(defaultValue);
}

Preference(std::string identifier, std::string label, std::string description, const Gdk::Color& defaultValue)
: m_identifier{std::move(identifier)}, m_label{std::move(label)}, m_description{std::move(description)},
m_type{PreferenceType::Color}
{
new (&m_value) impl::Color(defaultValue.get_red(), defaultValue.get_green(), defaultValue.get_blue());
}

~Preference()
{
@@ -109,7 +133,8 @@ class Preference
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);

static impl::PreferenceBuilder New(std::string identifier, std::string label, std::string description, const Gdk::Color& defaultValue);

public:
Preference(const Preference&) = delete;
Preference(Preference&& other)
@@ -135,11 +160,15 @@ class Preference
const std::string& GetString() const;
std::string ToString() const;
bool GetIsVisible() const;

Gdk::Color GetColor() const;
const impl::Color& GetColorRaw() const;

void SetBool(bool value);
void SetReal(double value);
void SetString(std::string value);

void SetColor(const Gdk::Color& value);
void SetColorRaw(const impl::Color& value);

bool HasUnit();
Unit& GetUnit();

38 changes: 38 additions & 0 deletions src/glscopeclient/PreferenceDialog.cpp
Original file line number Diff line number Diff line change
@@ -96,6 +96,30 @@ void PreferencePage::CreateWidgets()
break;
}

case PreferenceType::Color:
{
auto row = unique_ptr<ColorRow>{ new ColorRow() };
row->m_identifier = preference.GetIdentifier();
row->m_label.set_label(preference.GetLabel());
row->m_label.set_halign(Gtk::ALIGN_START);
row->m_colorbutton.set_color(preference.GetColor());
row->m_colorbutton.set_halign(Gtk::ALIGN_CENTER);
row->m_label.set_tooltip_text(preference.GetDescription());
row->m_colorbutton.set_tooltip_text(preference.GetDescription());

if(!last)
attach(row->m_label, 0, 0, 1, 1);
else
attach_next_to(row->m_label, *last, Gtk::POS_BOTTOM, 1, 1);

attach_next_to(row->m_colorbutton, row->m_label, Gtk::POS_RIGHT, 1, 1);

last = &(row->m_label);
m_colorRows.push_back(std::move(row));

break;
}

case PreferenceType::Real:
case PreferenceType::String:
{
@@ -159,6 +183,20 @@ void PreferencePage::SaveChanges()

switch(preference.GetType())
{
case PreferenceType::Color:
{
// This will always succeed
const auto it = find_if(m_colorRows.begin(), m_colorRows.end(),
[&preference](const unique_ptr<ColorRow>& x) -> bool
{
return x->m_identifier == preference.GetIdentifier();
});

preference.SetColor((*it)->m_colorbutton.get_color());

break;
}

case PreferenceType::Boolean:
{
// This will always succeed
8 changes: 8 additions & 0 deletions src/glscopeclient/PreferenceDialog.h
Original file line number Diff line number Diff line change
@@ -57,6 +57,13 @@ struct StringRealRow
Gtk::Entry m_value;
};

struct ColorRow
{
std::string m_identifier;
Gtk::Label m_label;
Gtk::ColorButton m_colorbutton;
};

class PreferencePage : public Gtk::Grid
{

@@ -73,6 +80,7 @@ class PreferencePage : public Gtk::Grid
PreferenceCategory& m_category;
std::vector<std::unique_ptr<BooleanRow>> m_booleanRows;
std::vector<std::unique_ptr<StringRealRow>> m_stringRealRows;
std::vector<std::unique_ptr<ColorRow>> m_colorRows;
};


1 change: 1 addition & 0 deletions src/glscopeclient/PreferenceManager.cpp
Original file line number Diff line number Diff line change
@@ -100,6 +100,7 @@ void PreferenceManager::InitializeDefaults()
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));
testSettings.AddPreference(Preference("test_color", "Test color", "Some test color", Gdk::Color{}));
auto& miscSettings = debug.AddCategory("Misc");
miscSettings.AddPreference(Preference("misc_test_1", "Misc test real", "blabla", 13.37));
}
29 changes: 27 additions & 2 deletions src/glscopeclient/PreferenceTree.cpp
Original file line number Diff line number Diff line change
@@ -129,7 +129,22 @@ namespace internal

void PreferenceHolder::ToYAML(YAML::Node& node) const
{
node[this->m_identifier] = this->m_pref.ToString();
if(this->m_pref.GetType() == PreferenceType::Color)
{
YAML::Node child{ };

const auto& color = this->m_pref.GetColorRaw();

child["r"] = color.m_r;
child["g"] = color.m_g;
child["b"] = color.m_b;

node[this->m_identifier] = child;
}
else
{
node[this->m_identifier] = this->m_pref.ToString();
}
}

bool PreferenceHolder::IsVisible() const
@@ -156,7 +171,17 @@ namespace internal
case PreferenceType::String:
this->m_pref.SetString(n.as<string>());
break;


case PreferenceType::Color:
{
const auto n_r = n["r"].as<std::uint16_t>();
const auto n_g = n["g"].as<std::uint16_t>();
const auto n_b = n["b"].as<std::uint16_t>();

this->m_pref.SetColorRaw(impl::Color(n_r, n_g, n_b));
break;
}

default:
break;
}