Added some logic to layers editor selection

This commit is contained in:
YuSanka 2019-06-12 16:28:25 +02:00
parent 26fb68ba45
commit 1694204687
4 changed files with 136 additions and 64 deletions

View File

@ -39,72 +39,83 @@ ObjectLayers::ObjectLayers(wxWindow* parent) :
m_bmp_add = ScalableBitmap(parent, "add_copies"); m_bmp_add = ScalableBitmap(parent, "add_copies");
} }
void ObjectLayers::select_editor(LayerRangeEditor* editor, const bool is_last_edited_range)
{
if (is_last_edited_range && m_selection_type == editor->type()) {
editor->SetFocus();
editor->SetInsertionPointEnd();
}
}
wxSizer* ObjectLayers::create_layer(const t_layer_height_range& range) wxSizer* ObjectLayers::create_layer(const t_layer_height_range& range)
{ {
const bool is_last_edited_range = range == m_last_edited_range; const bool is_last_edited_range = range == m_last_edited_range;
auto set_focus_fn = [range, this](const EditorType type)
{
m_last_edited_range = range;
m_selection_type = type;
};
// Add control for the "Min Z" // Add control for the "Min Z"
auto temp = new LayerRangeEditor(m_parent, double_to_string(range.first), auto editor = new LayerRangeEditor(m_parent, double_to_string(range.first), etMinZ,
[range, this](coordf_t min_z) set_focus_fn, [range, this](coordf_t min_z, bool enter_pressed)
{ {
if (fabs(min_z - range.first) < EPSILON) { if (fabs(min_z - range.first) < EPSILON || min_z > range.second) {
m_selection_type = sitUndef; m_selection_type = etUndef;
return false; // LayersList would not be updated/recreated return false;
} }
// data for next focusing // data for next focusing
m_last_edited_range = { min_z, range.second }; const t_layer_height_range& new_range = { min_z, range.second };
m_selection_type = sitMinZ; if (enter_pressed) {
m_last_edited_range = new_range;
m_selection_type = etMinZ;
}
wxGetApp().obj_list()->edit_layer_range(range, m_last_edited_range); return wxGetApp().obj_list()->edit_layer_range(range, new_range);
return true; // LayersList will be updated/recreated
}); });
if (is_last_edited_range && m_selection_type == sitMinZ) { select_editor(editor, is_last_edited_range);
temp->SetFocus(); m_grid_sizer->Add(editor);
temp->SetInsertionPointEnd();
}
m_grid_sizer->Add(temp);
// Add control for the "Max Z" // Add control for the "Max Z"
temp = new LayerRangeEditor(m_parent, double_to_string(range.second), editor = new LayerRangeEditor(m_parent, double_to_string(range.second), etMaxZ,
[range, this](coordf_t max_z) set_focus_fn, [range, this](coordf_t max_z, bool enter_pressed)
{ {
if (fabs(max_z - range.second) < EPSILON) { if (fabs(max_z - range.second) < EPSILON || range.first > max_z) {
m_selection_type = sitUndef; m_selection_type = etUndef;
return false; // LayersList would not be updated/recreated return false; // LayersList would not be updated/recreated
} }
// data for next focusing // data for next focusing
m_last_edited_range = { range.first, max_z }; const t_layer_height_range& new_range = { range.first, max_z };
m_selection_type = sitMaxZ; if (enter_pressed) {
m_last_edited_range = new_range;
m_selection_type = etMaxZ;
}
wxGetApp().obj_list()->edit_layer_range(range, m_last_edited_range); return wxGetApp().obj_list()->edit_layer_range(range, new_range);
return true; // LayersList will not be updated/recreated
}); });
if (is_last_edited_range && m_selection_type == sitMaxZ) { select_editor(editor, is_last_edited_range);
temp->SetFocus(); m_grid_sizer->Add(editor);
temp->SetInsertionPointEnd();
}
m_grid_sizer->Add(temp);
// Add control for the "Layer height" // Add control for the "Layer height"
temp = new LayerRangeEditor(m_parent, editor = new LayerRangeEditor(m_parent,
double_to_string(m_object->layer_config_ranges[range].option("layer_height")->getFloat()), double_to_string(m_object->layer_config_ranges[range].option("layer_height")->getFloat()),
[range, this](coordf_t layer_height) etLayerHeight, set_focus_fn, [range, this](coordf_t layer_height, bool)
{ {
wxGetApp().obj_list()->edit_layer_range(range, layer_height); return wxGetApp().obj_list()->edit_layer_range(range, layer_height);
return false; // LayersList would not be updated/recreated
}); });
select_editor(editor, is_last_edited_range);
auto sizer = new wxBoxSizer(wxHORIZONTAL); auto sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->Add(temp); sizer->Add(editor);
m_grid_sizer->Add(sizer); m_grid_sizer->Add(sizer);
return sizer; return sizer;
@ -193,34 +204,61 @@ void ObjectLayers::msw_rescale()
LayerRangeEditor::LayerRangeEditor( wxWindow* parent, LayerRangeEditor::LayerRangeEditor( wxWindow* parent,
const wxString& value, const wxString& value,
std::function<bool(coordf_t)> edit_fn EditorType type,
std::function<void(EditorType)> set_focus_fn,
std::function<bool(coordf_t, bool enter_pressed)> edit_fn
) : ) :
m_valid_value(value),
m_type(type),
wxTextCtrl(parent, wxID_ANY, value, wxDefaultPosition, wxTextCtrl(parent, wxID_ANY, value, wxDefaultPosition,
wxSize(8 * em_unit(parent), wxDefaultCoord), wxTE_PROCESS_ENTER) wxSize(8 * em_unit(parent), wxDefaultCoord), wxTE_PROCESS_ENTER)
{ {
this->SetFont(wxGetApp().normal_font()); this->SetFont(wxGetApp().normal_font());
this->Bind(wxEVT_TEXT_ENTER, ([this, edit_fn](wxEvent& e) this->Bind(wxEVT_TEXT_ENTER, [this, edit_fn](wxEvent&)
{ {
m_enter_pressed = true; m_enter_pressed = true;
// If LayersList wasn't updated/recreated, we can call wxEVT_KILL_FOCUS.Skip() // If LayersList wasn't updated/recreated, we can call wxEVT_KILL_FOCUS.Skip()
if ( !edit_fn(get_value()) ) if (m_type&etLayerHeight) {
if (!edit_fn(get_value(), true))
SetValue(m_valid_value);
else
m_valid_value = double_to_string(get_value());
m_call_kill_focus = true; m_call_kill_focus = true;
}), this->GetId()); }
else if (!edit_fn(get_value(), true)) {
SetValue(m_valid_value);
m_call_kill_focus = true;
}
}, this->GetId());
this->Bind(wxEVT_KILL_FOCUS, ([this, edit_fn](wxEvent& e) this->Bind(wxEVT_KILL_FOCUS, [this, edit_fn](wxFocusEvent& e)
{ {
if (!m_enter_pressed) { if (!m_enter_pressed) {
m_enter_pressed = false;
// If LayersList wasn't updated/recreated, we should call e.Skip() // If LayersList wasn't updated/recreated, we should call e.Skip()
if ( !edit_fn(get_value()) ) if (m_type & etLayerHeight) {
if (!edit_fn(get_value(), false))
SetValue(m_valid_value);
else
m_valid_value = double_to_string(get_value());
e.Skip(); e.Skip();
}
else if (!edit_fn(get_value(), false)) {
SetValue(m_valid_value);
e.Skip();
}
} }
else if (m_call_kill_focus) else if (m_call_kill_focus) {
m_call_kill_focus = false;
e.Skip(); e.Skip();
}), this->GetId()); }
}, this->GetId());
this->Bind(wxEVT_LEFT_DOWN, ([this, set_focus_fn](wxEvent& e)
{
set_focus_fn(m_type);
e.Skip();
}));
this->Bind(wxEVT_CHAR, ([this](wxKeyEvent& event) this->Bind(wxEVT_CHAR, ([this](wxKeyEvent& event)
{ {

View File

@ -19,18 +19,32 @@ class ConfigOptionsGroup;
typedef double coordf_t; typedef double coordf_t;
typedef std::pair<coordf_t, coordf_t> t_layer_height_range; typedef std::pair<coordf_t, coordf_t> t_layer_height_range;
enum EditorType
{
etUndef = 0,
etMinZ = 1,
etMaxZ = 2,
etLayerHeight = 4,
};
class LayerRangeEditor : public wxTextCtrl class LayerRangeEditor : public wxTextCtrl
{ {
bool m_enter_pressed { false }; bool m_enter_pressed { false };
bool m_call_kill_focus { false }; bool m_call_kill_focus { false };
wxString m_valid_value;
EditorType m_type;
public: public:
LayerRangeEditor( wxWindow* parent, LayerRangeEditor( wxWindow* parent,
const wxString& value = wxEmptyString, const wxString& value = wxEmptyString,
std::function<bool(coordf_t val)> edit_fn = [](coordf_t) {return false; } EditorType type = etUndef,
std::function<void(EditorType)> set_focus_fn = [](EditorType) {;},
std::function<bool(coordf_t, bool)> edit_fn = [](coordf_t, bool) {return false; }
); );
~LayerRangeEditor() {} ~LayerRangeEditor() {}
EditorType type() const {return m_type;}
private: private:
coordf_t get_value(); coordf_t get_value();
}; };
@ -43,19 +57,13 @@ class ObjectLayers : public OG_Settings
wxFlexGridSizer* m_grid_sizer; wxFlexGridSizer* m_grid_sizer;
t_layer_height_range m_last_edited_range; t_layer_height_range m_last_edited_range;
EditorType m_selection_type {etUndef};
enum SelectedItemType
{
sitUndef,
sitMinZ,
sitMaxZ,
sitLayerHeight,
} m_selection_type {sitUndef};
public: public:
ObjectLayers(wxWindow* parent); ObjectLayers(wxWindow* parent);
~ObjectLayers() {} ~ObjectLayers() {}
void select_editor(LayerRangeEditor* editor, const bool is_last_edited_range);
wxSizer* create_layer(const t_layer_height_range& range); // without_buttons wxSizer* create_layer(const t_layer_height_range& range); // without_buttons
void create_layers_list(); void create_layers_list();
void update_layers_list(); void update_layers_list();

View File

@ -2363,6 +2363,18 @@ void ObjectList::del_layer_range(const t_layer_height_range& range)
select_item(selectable_item); select_item(selectable_item);
} }
double get_min_layer_height(const int extruder_idx)
{
const DynamicPrintConfig& config = wxGetApp().preset_bundle->printers.get_edited_preset().config;
return config.opt_float("min_layer_height", extruder_idx <= 0 ? 0 : extruder_idx-1);
}
double get_max_layer_height(const int extruder_idx)
{
const DynamicPrintConfig& config = wxGetApp().preset_bundle->printers.get_edited_preset().config;
return config.opt_float("max_layer_height", extruder_idx <= 0 ? 0 : extruder_idx-1);
}
void ObjectList::add_layer_range_after_current(const t_layer_height_range& current_range) void ObjectList::add_layer_range_after_current(const t_layer_height_range& current_range)
{ {
const int obj_idx = get_selected_obj_idx(); const int obj_idx = get_selected_obj_idx();
@ -2393,13 +2405,14 @@ void ObjectList::add_layer_range_after_current(const t_layer_height_range& curre
if (current_range.second == next_range.first) if (current_range.second == next_range.first)
{ {
const auto old_config = ranges.at(next_range);
const coordf_t delta = (next_range.second - next_range.first); const coordf_t delta = (next_range.second - next_range.first);
if (delta < 0.05f) // next range division has no sense if (delta < get_min_layer_height(old_config.opt_int("extruder"))/*0.05f*/) // next range division has no sense
return; return;
const coordf_t midl_layer = next_range.first + 0.5f * delta; const coordf_t midl_layer = next_range.first + 0.5f * delta;
const auto old_config = ranges.at(next_range);
t_layer_height_range new_range = { midl_layer, next_range.second }; t_layer_height_range new_range = { midl_layer, next_range.second };
// delete old layer // delete old layer
@ -2450,21 +2463,32 @@ void ObjectList::add_layer_item(const t_layer_height_range& range,
select_item(m_objects_model->AddSettingsChild(layer_item)); select_item(m_objects_model->AddSettingsChild(layer_item));
} }
void ObjectList::edit_layer_range(const t_layer_height_range& range, coordf_t layer_height) bool ObjectList::edit_layer_range(const t_layer_height_range& range, coordf_t layer_height)
{ {
const int obj_idx = get_selected_obj_idx(); const int obj_idx = get_selected_obj_idx();
if (obj_idx < 0) return; if (obj_idx < 0)
return false;
t_layer_config_ranges& ranges = object(obj_idx)->layer_config_ranges; DynamicPrintConfig* config = &object(obj_idx)->layer_config_ranges[range];
if (fabs(layer_height - config->opt_float("layer_height")) < EPSILON)
return false;
DynamicPrintConfig* config = &ranges[range]; const int extruder_idx = config->opt_int("extruder");
config->set_key_value("layer_height", new ConfigOptionFloat(layer_height));
if (layer_height >= get_min_layer_height(extruder_idx) &&
layer_height <= get_max_layer_height(extruder_idx))
{
config->set_key_value("layer_height", new ConfigOptionFloat(layer_height));
return true;
}
return false;
} }
void ObjectList::edit_layer_range(const t_layer_height_range& range, const t_layer_height_range& new_range) bool ObjectList::edit_layer_range(const t_layer_height_range& range, const t_layer_height_range& new_range)
{ {
const int obj_idx = get_selected_obj_idx(); const int obj_idx = get_selected_obj_idx();
if (obj_idx < 0) return; if (obj_idx < 0) return false;
t_layer_config_ranges& ranges = object(obj_idx)->layer_config_ranges; t_layer_config_ranges& ranges = object(obj_idx)->layer_config_ranges;
@ -2484,6 +2508,8 @@ void ObjectList::edit_layer_range(const t_layer_height_range& range, const t_lay
// To update(recreate) layers sizer call select_item for LayerRoot item expand // To update(recreate) layers sizer call select_item for LayerRoot item expand
select_item(root_item); select_item(root_item);
Expand(root_item); Expand(root_item);
return true;
} }
void ObjectList::init_objects() void ObjectList::init_objects()

View File

@ -291,8 +291,8 @@ public:
void add_layer_item (const t_layer_height_range& range, void add_layer_item (const t_layer_height_range& range,
const wxDataViewItem layers_item, const wxDataViewItem layers_item,
const int layer_idx = -1); const int layer_idx = -1);
void edit_layer_range(const t_layer_height_range& range, coordf_t layer_height); bool edit_layer_range(const t_layer_height_range& range, coordf_t layer_height);
void edit_layer_range(const t_layer_height_range& range, bool edit_layer_range(const t_layer_height_range& range,
const t_layer_height_range& new_range); const t_layer_height_range& new_range);
void init_objects(); void init_objects();