#include "my_slider.hpp" #include #include #include double MySlider::get_value() { return value; } void MySlider::update(bool force) { if (update_occurring) { return; } update_occurring = true; std::string value_str = to_string_with_decimals(value, digits_after_decimal); try { value = std::stod(value_str); } catch (std::out_of_range) { } catch (std::invalid_argument) { } if (text_editing) { edit_btn.set_icon_name("document-save-symbolic"); } else { edit_btn.set_icon_name("edit-symbolic"); } std::vector classes; classes.push_back("precise-slider"); if (text_editing) { classes.push_back("text-editing"); } if (get_hexpand()) { classes.push_back("hexpand"); } set_css_classes(classes); if (min_value <= max_value) { slider.set_range(min_value, max_value); } prefix_label.set_text(prefix); value_label.set_text(value_str.c_str()); suffix_label.set_text(suffix); text_entry.set_visible(text_editing); value_label.set_visible(!text_editing); slider.set_visible(!text_editing); if (force || text_editing) { update_slider(value); } if (force || !text_editing) { update_text_entry(value_str); } update_occurring = false; } void MySlider::set_value(double value, bool force) { this->value = value; update(force); } Glib::ustring MySlider::get_prefix() { return prefix; } void MySlider::set_prefix(Glib::ustring value) { prefix = value; } Glib::ustring MySlider::get_suffix() { return suffix; } void MySlider::set_suffix(Glib::ustring value) { suffix = value; } unsigned MySlider::get_digits_after_decimal() { return digits_after_decimal; } void MySlider::set_digits_after_decimal(unsigned value) { digits_after_decimal = value; slider.set_digits(digits_after_decimal); } void MySlider::update_text_entry(std::string text) { text_entry.delete_text(0, text_entry.get_text_length()); int pos = 0; text_entry.insert_text(text.c_str(), text.size(), pos); } void MySlider::update_slider(double value) { slider.set_value(value); } double MySlider::get_min_value() { return min_value; } void MySlider::set_min_value(double value) { min_value = value; } double MySlider::get_max_value() { return max_value; } void MySlider::set_max_value(double value) { max_value = value; } MySlider::MySlider() { min_value = 0; max_value = 1; value = 0; text_valid = false; text_editing = false; update_occurring = false; prefix = ""; suffix = ""; digits_after_decimal = 0; prefix_label.set_css_classes({"prefix-label"}); prefix_label.insert_at_end(*this); value_label.set_css_classes({"value-label"}); value_label.insert_at_end(*this); slider.set_css_classes({"imprecise-slider"}); text_entry.set_css_classes({"precise-number-editor"}); slider.set_hexpand(); slider.set_size_request(100); text_entry.set_hexpand(); text_entry.insert_at_end(*this); suffix_label.set_css_classes({"suffix-label"}); suffix_label.insert_at_end(*this); slider.insert_at_end(*this); edit_btn.insert_at_end(*this); edit_btn.set_css_classes({"edit-btn"}); edit_btn.signal_clicked().connect([this]() { update(); text_editing = !text_editing; update(); }); text_entry.property_width_chars().set_value(0); text_entry.signal_changed().connect([this]() { try { value = std::stod(text_entry.get_text().c_str()); text_valid = true; } catch(std::out_of_range) { text_valid = false; } catch (std::invalid_argument) { text_valid = false; } if (text_valid) { value_changed.emit(value); } update(); }); slider.signal_value_changed().connect([this]() { value = slider.get_value(); value_changed.emit(value); update(); }); update(true); }