looper/backends/ui/gtk/my_slider.cpp

117 lines
No EOL
3.1 KiB
C++

#include "my_slider.hpp"
#include <sstream>
#include <iomanip>
#include <util.hpp>
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 (force || text_editing) {
update_slider(value);
}
if (force || !text_editing) {
update_text_entry(value_str);
}
if (text_editing) {
edit_btn.set_icon_name("document-save-symbolic");
} else {
edit_btn.set_icon_name("edit-symbolic");
}
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);
update_occurring = false;
}
void MySlider::set_value(double value) {
this->value = value;
update(true);
}
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;
}
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() {
prefix_label.insert_at_end(*this);
value_label.insert_at_end(*this);
slider.set_hexpand(true);
text_entry.set_hexpand(true);
slider.insert_at_end(*this);
text_entry.insert_at_end(*this);
suffix_label.insert_at_end(*this);
edit_btn.insert_at_end(*this);
edit_btn.signal_clicked().connect([=]() {
update();
text_editing = !text_editing;
update();
});
text_entry.signal_changed().connect([=]() {
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([=]() {
value = slider.get_value();
value_changed.emit(value);
update();
});
update();
}