DoubleSlider refactoring

This commit is contained in:
YuSanka 2019-09-05 11:47:04 +02:00
parent 1651c8db6e
commit 4263fa8dff
5 changed files with 29 additions and 37 deletions

View file

@ -850,7 +850,7 @@ void GLCanvas3D::LegendTexture::fill_color_print_legend_values(const GCodePrevie
std::vector<double> print_zs = canvas.get_current_print_zs(true);
for (auto cp_value : color_print_values)
{
auto lower_b = std::lower_bound(print_zs.begin(), print_zs.end(), cp_value);
auto lower_b = std::lower_bound(print_zs.begin(), print_zs.end(), cp_value - DoubleSlider::epsilon());
if (lower_b == print_zs.end())
continue;
@ -858,7 +858,10 @@ void GLCanvas3D::LegendTexture::fill_color_print_legend_values(const GCodePrevie
double current_z = *lower_b;
double previous_z = lower_b == print_zs.begin() ? 0.0 : *(--lower_b);
cp_legend_values.push_back(std::pair<double, double>(previous_z, current_z));
// to avoid duplicate values, check adding values
if (cp_legend_values.empty() ||
!(cp_legend_values.back().first == previous_z && cp_legend_values.back().second == current_z) )
cp_legend_values.push_back(std::pair<double, double>(previous_z, current_z));
}
}
}

View file

@ -640,9 +640,10 @@ void Preview::update_double_slider(const std::vector<double>& layers_z, bool kee
bool snap_to_min = force_sliders_full_range || m_slider->is_lower_at_min();
bool snap_to_max = force_sliders_full_range || m_slider->is_higher_at_max();
std::vector<std::pair<int, double>> values;
fill_slider_values(values, layers_z);
m_slider->SetSliderValues(values);
std::vector<double> &ticks_from_config = (wxGetApp().preset_bundle->project_config.option<ConfigOptionFloats>("colorprint_heights"))->values;
check_slider_values(ticks_from_config, layers_z);
m_slider->SetSliderValues(layers_z);
assert(m_slider->GetMinValue() == 0);
m_slider->SetMaxValue(layers_z.empty() ? 0 : layers_z.size() - 1);
@ -662,9 +663,6 @@ void Preview::update_double_slider(const std::vector<double>& layers_z, bool kee
}
m_slider->SetSelectionSpan(idx_low, idx_high);
const auto& config = wxGetApp().preset_bundle->project_config;
const std::vector<double> &ticks_from_config = (config.option<ConfigOptionFloats>("colorprint_heights"))->values;
m_slider->SetTicksValues(ticks_from_config);
bool color_print_enable = (wxGetApp().plater()->printer_technology() == ptFFF);
@ -676,26 +674,18 @@ void Preview::update_double_slider(const std::vector<double>& layers_z, bool kee
m_slider->EnableTickManipulation(color_print_enable);
}
void Preview::fill_slider_values(std::vector<std::pair<int, double>> &values,
void Preview::check_slider_values(std::vector<double>& ticks_from_config,
const std::vector<double> &layers_z)
{
values.clear();
for (int i = 0; i < (int)layers_z.size(); ++i)
{
values.push_back(std::pair<int, double>(i + 1, layers_z[i]));
}
// All ticks that would end up outside the slider range should be erased.
// TODO: this should be placed into more appropriate part of code,
// this function is e.g. not called when the last object is deleted
std::vector<double> &ticks_from_config = (wxGetApp().preset_bundle->project_config.option<ConfigOptionFloats>("colorprint_heights"))->values;
unsigned int old_size = ticks_from_config.size();
ticks_from_config.erase(std::remove_if(ticks_from_config.begin(), ticks_from_config.end(),
[values](double val)
[layers_z](double val)
{
return (values.back().second < val &&
// we can't ignore tick on last layer
fabs(values.back().second - val) > DoubleSlider::epsilon());
auto it = std::lower_bound(layers_z.begin(), layers_z.end(), val - DoubleSlider::epsilon());
return it == layers_z.end();
}),
ticks_from_config.end());
if (ticks_from_config.size() != old_size)

View file

@ -155,7 +155,7 @@ private:
// Create/Update/Reset double slider on 3dPreview
void create_double_slider();
void update_double_slider(const std::vector<double>& layers_z, bool keep_z_range = false);
void fill_slider_values(std::vector<std::pair<int, double>> &values,
void check_slider_values(std::vector<double> &ticks_from_config,
const std::vector<double> &layers_z);
void reset_double_slider();
// update DoubleSlider after keyDown in canvas

View file

@ -2192,9 +2192,9 @@ double DoubleSlider::get_double_value(const SelectedSlider& selection)
return 0.0;
if (m_values.size() <= m_higher_value) {
correct_higher_value();
return m_values.back().second;
return m_values.back();
}
return m_values[selection == ssLower ? m_lower_value : m_higher_value].second;
return m_values[selection == ssLower ? m_lower_value : m_higher_value];
}
std::vector<double> DoubleSlider::GetTicksValues() const
@ -2206,7 +2206,7 @@ std::vector<double> DoubleSlider::GetTicksValues() const
for (int tick : m_ticks) {
if (tick > val_size)
break;
values.push_back(m_values[tick].second);
values.push_back(m_values[tick]);
}
return values;
@ -2220,14 +2220,13 @@ void DoubleSlider::SetTicksValues(const std::vector<double>& heights)
const bool was_empty = m_ticks.empty();
m_ticks.clear();
unsigned int i = 0;
for (auto h : heights) {
while (i < m_values.size() && m_values[i].second - epsilon()/*1e-6*/ < h)
++i;
// don't miss last layer if it is
if (i == m_values.size() && fabs(m_values[i-1].second - h) > epsilon())
return;
m_ticks.insert(i-1);
auto it = std::lower_bound(m_values.begin(), m_values.end(), h - epsilon());
if (it == m_values.end())
continue;
m_ticks.insert(it-m_values.begin());
}
if (!was_empty && m_ticks.empty())
@ -2347,8 +2346,8 @@ wxString DoubleSlider::get_label(const SelectedSlider& selection) const
const wxString str = m_values.empty() ?
wxNumberFormatter::ToString(m_label_koef*value, 2, wxNumberFormatter::Style_None) :
wxNumberFormatter::ToString(m_values[value].second, 2, wxNumberFormatter::Style_None);
return wxString::Format("%s\n(%d)", str, m_values.empty() ? value : m_values[value].first);
wxNumberFormatter::ToString(m_values[value], 2, wxNumberFormatter::Style_None);
return wxString::Format("%s\n(%d)", str, m_values.empty() ? value : value+1);
}
void DoubleSlider::draw_thumb_text(wxDC& dc, const wxPoint& pos, const SelectedSlider& selection) const

View file

@ -729,8 +729,8 @@ public:
int GetMinValue() const { return m_min_value; }
int GetMaxValue() const { return m_max_value; }
double GetMinValueD() { return m_values.empty() ? 0. : m_values[m_min_value].second; }
double GetMaxValueD() { return m_values.empty() ? 0. : m_values[m_max_value].second; }
double GetMinValueD() { return m_values.empty() ? 0. : m_values[m_min_value]; }
double GetMaxValueD() { return m_values.empty() ? 0. : m_values[m_max_value]; }
int GetLowerValue() const { return m_lower_value; }
int GetHigherValue() const { return m_higher_value; }
int GetActiveValue() const;
@ -746,7 +746,7 @@ public:
void SetKoefForLabels(const double koef) {
m_label_koef = koef;
}
void SetSliderValues(const std::vector<std::pair<int, double>>& values) {
void SetSliderValues(const std::vector<double>& values) {
m_values = values;
}
void ChangeOneLayerLock();
@ -867,7 +867,7 @@ private:
std::vector<wxPen*> m_line_pens;
std::vector<wxPen*> m_segm_pens;
std::set<int> m_ticks;
std::vector<std::pair<int,double>> m_values;
std::vector<double> m_values;
};