From d60fac42d69a8bfc74d35d62970f3360c2f7585c Mon Sep 17 00:00:00 2001 From: YuSanka Date: Wed, 13 Dec 2017 14:45:10 +0100 Subject: [PATCH] Start filling the Print's Tab, using @lordofhyphens's Optionsgroup --- xs/src/slic3r/GUI/ConfigExceptions.hpp | 15 + xs/src/slic3r/GUI/Field.cpp | 42 ++ xs/src/slic3r/GUI/Field.hpp | 137 ++++++ xs/src/slic3r/GUI/GUI.cpp | 29 +- xs/src/slic3r/GUI/OptionsGroup.cpp | 207 ++++----- xs/src/slic3r/GUI/OptionsGroup.hpp | 235 ++++++---- xs/src/slic3r/GUI/OptionsGroup/Field.cpp | 36 -- xs/src/slic3r/GUI/OptionsGroup/Field.hpp | 95 ---- xs/src/slic3r/GUI/Tab.cpp | 545 +++++++++++++++++++---- xs/src/slic3r/GUI/Tab.h | 135 +++--- xs/src/slic3r/GUI/Widget.hpp | 6 +- 11 files changed, 1000 insertions(+), 482 deletions(-) create mode 100644 xs/src/slic3r/GUI/ConfigExceptions.hpp create mode 100644 xs/src/slic3r/GUI/Field.cpp create mode 100644 xs/src/slic3r/GUI/Field.hpp delete mode 100644 xs/src/slic3r/GUI/OptionsGroup/Field.cpp delete mode 100644 xs/src/slic3r/GUI/OptionsGroup/Field.hpp diff --git a/xs/src/slic3r/GUI/ConfigExceptions.hpp b/xs/src/slic3r/GUI/ConfigExceptions.hpp new file mode 100644 index 000000000..9038d3445 --- /dev/null +++ b/xs/src/slic3r/GUI/ConfigExceptions.hpp @@ -0,0 +1,15 @@ +#include +namespace Slic3r { + +class ConfigError : public std::runtime_error { +using std::runtime_error::runtime_error; +}; + +namespace GUI { + +class ConfigGUITypeError : public ConfigError { +using ConfigError::ConfigError; +}; +} + +} diff --git a/xs/src/slic3r/GUI/Field.cpp b/xs/src/slic3r/GUI/Field.cpp new file mode 100644 index 000000000..af89b01b1 --- /dev/null +++ b/xs/src/slic3r/GUI/Field.cpp @@ -0,0 +1,42 @@ +#include "GUI.hpp"//"slic3r_gui.hpp" +#include "Field.hpp" + +namespace Slic3r { namespace GUI { + + void Field::_on_kill_focus(wxFocusEvent& event) { + // Without this, there will be nasty focus bugs on Windows. + // Also, docs for wxEvent::Skip() say "In general, it is recommended to skip all + // non-command events to allow the default handling to take place." + event.Skip(1); + + // call the registered function if it is available +//! if (on_kill_focus) +//! on_kill_focus(opt_id); + } + void Field::_on_change(wxCommandEvent& event) { + std::cerr << "calling Field::_on_change \n"; +//! if (on_change != nullptr && !disable_change_event) +//! on_change(opt_id, "A"); + } + void TextCtrl::BUILD() { + auto size = wxSize(wxDefaultSize); + if (opt.height >= 0) size.SetHeight(opt.height); + if (opt.width >= 0) size.SetWidth(opt.width); + + auto temp = new wxTextCtrl(parent, wxID_ANY, wxString(""), wxDefaultPosition, size, (opt.multiline ? wxTE_MULTILINE : 0)); //! new wxTextCtrl(parent, wxID_ANY, wxString(opt.default_value->getString()), wxDefaultPosition, size, (opt.multiline ? wxTE_MULTILINE : 0)); + + if (opt.tooltip.length() > 0) { temp->SetToolTip(opt.tooltip); } + + temp->Bind(wxEVT_TEXT, ([=](wxCommandEvent e) { _on_change(e); }), temp->GetId()); + temp->Bind(wxEVT_KILL_FOCUS, ([this](wxFocusEvent e) { _on_kill_focus(e); }), temp->GetId()); + + // recast as a wxWindow to fit the calling convention + window = dynamic_cast(temp); + + } + + void TextCtrl::enable() { (dynamic_cast(window))->Enable(); (dynamic_cast(window))->SetEditable(1); } + void TextCtrl::disable() { dynamic_cast(window)->Disable(); dynamic_cast(window)->SetEditable(0); } + void TextCtrl::set_tooltip(const wxString& tip) { } +}} + diff --git a/xs/src/slic3r/GUI/Field.hpp b/xs/src/slic3r/GUI/Field.hpp new file mode 100644 index 000000000..c5bf74256 --- /dev/null +++ b/xs/src/slic3r/GUI/Field.hpp @@ -0,0 +1,137 @@ +#ifndef SLIC3R_GUI_FIELD_HPP +#define SLIC3R_GUI_FIELD_HPP + +#include +#ifndef WX_PRECOMP + #include +#endif + +#include +#include +#include + +#include "../../libslic3r/libslic3r.h" +#include "../../libslic3r/Config.hpp" + +//#include "slic3r_gui.hpp" +#include "GUI.hpp" + +#if SLIC3R_CPPVER==11 + // C++14 has make_unique, C++11 doesn't. This is really useful so we're going to steal it. + template + std::unique_ptr make_unique(Args&&... args) + { + std::unique_ptr ret (new T(std::forward(args)...)); + return ret; + } +#endif + +namespace Slic3r { namespace GUI { + +class Field; +using t_field = std::unique_ptr; + +class Field { + protected: + // factory function to defer and enforce creation of derived type. + virtual void PostInitialize() { BUILD(); } + + /// Finish constructing the Field's wxWidget-related properties, including setting its own sizer, etc. + virtual void BUILD() = 0; + + /// Call the attached on_kill_focus method. + void _on_kill_focus(wxFocusEvent& event); + /// Call the attached on_change method. + void _on_change(wxCommandEvent& event); + + public: + + /// parent wx item, opportunity to refactor (probably not necessary - data duplication) + wxWindow* parent {nullptr}; + + /// Function object to store callback passed in from owning object. +//! t_kill_focus on_kill_focus {nullptr}; + + /// Function object to store callback passed in from owning object. +//! t_change on_change {nullptr}; + + bool disable_change_event {false}; + + /// Copy of ConfigOption for deduction purposes + const ConfigOptionDef opt {ConfigOptionDef()}; + const t_config_option_key opt_id;//! {""}; + + /// Sets a value for this control. + /// subclasses should overload with a specific version + /// Postcondition: Method does not fire the on_change event. + virtual void set_value(boost::any value) = 0; + + /// Gets a boost::any representing this control. + /// subclasses should overload with a specific version + virtual boost::any get_value() = 0; + + virtual void enable() = 0; + virtual void disable() = 0; + + /// Fires the enable or disable function, based on the input. + inline void toggle(bool en) { en ? enable() : disable(); } + + virtual void set_tooltip(const wxString& tip) = 0; + + + Field(const ConfigOptionDef& opt, const t_config_option_key& id) : opt(opt), opt_id(id) {}; + Field(wxWindow* parent, const ConfigOptionDef& opt, const t_config_option_key& id) : parent(parent), opt(opt), opt_id(id) {}; + + /// If you don't know what you are getting back, check both methods for nullptr. + virtual wxSizer* getSizer() { return nullptr; } + virtual wxWindow* getWindow() { return nullptr; } + + + /// Factory method for generating new derived classes. + template + static t_field Create(wxWindow* parent, const ConfigOptionDef& opt, const t_config_option_key& id) // interface for creating shared objects + { + auto p = std::make_unique(parent, opt, id); + p->PostInitialize(); + return p; + } + +}; + +/// Convenience function, accepts a const reference to t_field and checks to see whether +/// or not both wx pointers are null. +inline bool is_bad_field(const t_field& obj) { return obj->getSizer() == nullptr && obj->getWindow() == nullptr; } + +/// Covenience function to determine whether this field is a valid window field. +inline bool is_window_field(const t_field& obj) { return !is_bad_field(obj) && obj->getWindow() != nullptr; } + +/// Covenience function to determine whether this field is a valid sizer field. +inline bool is_sizer_field(const t_field& obj) { return !is_bad_field(obj) && obj->getSizer() != nullptr; } + +class TextCtrl : public Field { + using Field::Field; +public: + void BUILD(); + wxWindow* window {nullptr}; + + + virtual void set_value(std::string value) { + dynamic_cast(window)->SetValue(wxString(value)); + } + virtual void set_value(boost::any value) { + dynamic_cast(window)->SetValue(boost::any_cast(value)); + } + + boost::any get_value() { return boost::any(dynamic_cast(window)->GetValue()); } + + virtual void enable(); + virtual void disable(); + virtual wxWindow* getWindow() { return window; } + void set_tooltip(const wxString& tip); + +}; + + +#endif +}} + diff --git a/xs/src/slic3r/GUI/GUI.cpp b/xs/src/slic3r/GUI/GUI.cpp index cbd84f6db..e1292bd3a 100644 --- a/xs/src/slic3r/GUI/GUI.cpp +++ b/xs/src/slic3r/GUI/GUI.cpp @@ -9,6 +9,15 @@ #import #elif _WIN32 #include +// Undefine min/max macros incompatible with the standard library +// For example, std::numeric_limits::max() +// produces some weird errors +#ifdef min +#undef min +#endif +#ifdef max +#undef max +#endif #include "boost/nowide/convert.hpp" #pragma comment(lib, "user32.lib") #endif @@ -20,6 +29,7 @@ #include #include #include +#include #include "Tab.h" @@ -181,8 +191,25 @@ void add_debug_menu(wxMenuBar *menu) // void create_preset_tab(const char *name) { - CTabPrint* panel = new CTabPrint(g_wxTabPanel, name/*, someParams*/); + CTabPrint* panel = new CTabPrint(g_wxTabPanel, name); + panel->create_preset_tab(); g_wxTabPanel->AddPage(panel, name); + + //!------------Exp + // parse all command line options into a DynamicConfig +/* + DynamicPrintConfig print_config; +//! const DynamicPrintConfig &print_config = preset_bundle.prints .get_edited_preset().config; + + auto vsizer = new wxBoxSizer(wxVERTICAL); + this->SetSizer(vsizer); + auto optgroup = GUI::ConfigOptionsGroup(this, "Custom GCode", &print_config); + optgroup.on_change = ON_CHANGE(= , {}); + vsizer->Add(optgroup.sizer, 0, wxEXPAND | wxALL, 10); + + optgroup.append_single_option_line(GUI::Option(*(config.def->get("before_layer_gcode")), "before_layer_gcode")); +*/ //!------------Exp + } } } diff --git a/xs/src/slic3r/GUI/OptionsGroup.cpp b/xs/src/slic3r/GUI/OptionsGroup.cpp index 6dd278913..1c7124710 100644 --- a/xs/src/slic3r/GUI/OptionsGroup.cpp +++ b/xs/src/slic3r/GUI/OptionsGroup.cpp @@ -1,124 +1,125 @@ #include "OptionsGroup.hpp" -#include "OptionsGroup/Field.hpp" -#include "Config.hpp" +#include "ConfigExceptions.hpp" -// Translate the ifdef -#ifdef __WXOSX__ - #define wxOSX true -#else - #define wxOSX false -#endif - -#define BORDER(a, b) ((wxOSX ? a : b)) +#include +#include namespace Slic3r { namespace GUI { +const t_field& OptionsGroup::build_field(const Option& opt) { + return build_field(opt.opt_id, opt.opt); +} +const t_field& OptionsGroup::build_field(const t_config_option_key& id) { + const ConfigOptionDef& opt = options.at(id); + return build_field(id, opt); +} -void OptionsGroup::BUILD() { - if (staticbox) { - wxStaticBox* box = new wxStaticBox(_parent, -1, title); - _sizer = new wxStaticBoxSizer(box, wxVERTICAL); - } else { - _sizer = new wxBoxSizer(wxVERTICAL); +const t_field& OptionsGroup::build_field(const t_config_option_key& id, const ConfigOptionDef& opt) { + // Check the gui_type field first, fall through + // is the normal type. + if (opt.gui_type.compare("select") == 0) { + } else if (opt.gui_type.compare("select_open") == 0) { + } else if (opt.gui_type.compare("color") == 0) { + } else if (opt.gui_type.compare("f_enum_open") == 0 || + opt.gui_type.compare("i_enum_open") == 0 || + opt.gui_type.compare("i_enum_closed") == 0) { + } else if (opt.gui_type.compare("slider") == 0) { + } else if (opt.gui_type.compare("i_spin") == 0) { // Spinctrl + } else { + switch (opt.type) { + case coFloatOrPercent: + case coPercent: + case coFloat: + case coString: +//! fields.emplace(id, STDMOVE(TextCtrl::Create(_parent, opt,id))); + break; + case coNone: break; + default: + break;//! throw ConfigGUITypeError(""); break; + } } - size_t num_columns = 1; - if (label_width != 0) ++num_columns; - if (extra_column != 0) ++num_columns; - - _grid_sizer = new wxFlexGridSizer(0, num_columns, 0, 0); - _grid_sizer->SetFlexibleDirection(wxHORIZONTAL); - _grid_sizer->AddGrowableCol(label_width > 0); - _sizer->Add(_grid_sizer, 0, wxEXPAND | wxALL, BORDER(0,5)); + // Grab a reference to fields for convenience + const t_field& field = fields[id]; +//! field->on_change = [this](std::string id, boost::any val) { }; + field->parent = parent(); + // assign function objects for callbacks, etc. + return field; } void OptionsGroup::append_line(const Line& line) { - if (line.has_sizer() || (line.has_widget() && line.full_width)) { - wxASSERT(line.sizer() != nullptr); - _sizer->Add( (line.has_sizer() ? line.sizer() : line.widget().sizer()), 0, wxEXPAND | wxALL, BORDER(0, 15)); - return; + if (line.sizer != nullptr || (line.widget != nullptr && line.full_width > 0)){ + if (line.sizer != nullptr) { + sizer->Add(line.sizer, 0, wxEXPAND | wxALL, wxOSX ? 0 : 15); + return; + } + if (line.widget != nullptr) { + sizer->Add(line.widget(_parent), 0, wxEXPAND | wxALL, wxOSX ? 0 : 15); + return; + } } - wxSizer* grid_sizer = _grid_sizer; - // If we have an extra column, build it. - // If there's a label, build it. + + auto grid_sizer = _grid_sizer; + + // Build a label if we have it if (label_width != 0) { - wxStaticText* label = new wxStaticText(_parent, -1, (line.label) + ":", wxDefaultPosition); - label->Wrap(label_width); - if (wxIsEmpty(line.tooltip())) { label->SetToolTip(line.tooltip()); } - grid_sizer->Add(label, 0, wxALIGN_CENTER_VERTICAL, 0); + auto label = new wxStaticText(parent(), wxID_ANY, line.label , wxDefaultPosition, wxSize(label_width, -1)); + label->SetFont(label_font); + label->Wrap(label_width); // avoid a Linux/GTK bug + grid_sizer->Add(label, 0, wxALIGN_CENTER_VERTICAL,0); + if (line.label_tooltip.compare("") != 0) + label->SetToolTip(line.label_tooltip); } - // If we have a widget, add it to the sizer - if (line.has_widget()) { - grid_sizer->Add(line.widget().sizer(), 0, wxEXPAND | wxALL, BORDER(0,15)); + + // If there's a widget, build it and add the result to the sizer. + if (line.widget != nullptr) { + auto wgt = line.widget(parent()); + grid_sizer->Add(wgt, 0, wxEXPAND | wxALL, wxOSX ? 0 : 15); return; } - // If we have a single option with no sidetext just add it directly to the grid sizer - if (line.options().size() == 1) { - const ConfigOptionDef& opt = line.options()[0]; - if (line.extra_widgets().size() && !wxIsEmpty(opt.sidetext) && line.extra_widgets().size() == 0) { - Field* field = _build_field(opt); - if (field != nullptr) { - if (field->has_sizer()) { - grid_sizer->Add(field->sizer(), 0, (opt.full_width ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0); - } else if (field->has_window()) { - grid_sizer->Add(field->window(), 0, (opt.full_width ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0); - } - } - } + + + // if we have a single option with no sidetext just add it directly to the grid sizer + auto option_set = line.get_options(); + if (option_set.size() == 1 && option_set.front().opt.sidetext.size() == 0 && + option_set.front().side_widget == nullptr && line.get_extra_widgets().size() == 0) { + const auto& option = option_set.front(); + const auto& field = build_field(option); + std::cerr << "single option, no sidetext.\n"; + std::cerr << "field parent is not null?: " << (field->parent != nullptr) << "\n"; + + if (is_window_field(field)) + grid_sizer->Add(field->getWindow(), 0, (option.opt.full_width ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0); + if (is_sizer_field(field)) + grid_sizer->Add(field->getSizer(), 0, (option.opt.full_width ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0); + return; } - // Otherwise, there's more than one option or a single option with sidetext -- make - // a horizontal sizer to arrange things. - wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); + + // if we're here, we have more than one option or a single option with sidetext + // so we need a horizontal sizer to arrange these things + auto sizer = new wxBoxSizer(wxHORIZONTAL); grid_sizer->Add(sizer, 0, 0, 0); - for (auto& option : line.options()) { - // add label if any - if (!wxIsEmpty(option.label)) { - wxStaticText* field_label = new wxStaticText(_parent, -1, __(option.label) + ":", wxDefaultPosition, wxDefaultSize); - sizer->Add(field_label, 0, wxALIGN_CENTER_VERTICAL,0); - } - - // add field - Field* field = _build_field(option); - if (field != nullptr) { - if (field->has_sizer()) { - sizer->Add(field->sizer(), 0, (option.full_width ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0); - } else if (field->has_window()) { - sizer->Add(field->window(), 0, (option.full_width ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0); - } - } - - if (!wxIsEmpty(option.sidetext)) { - } - // !!! side_widget !!! find out the purpose -// if (option.side_widget.valid()) { -// sizer->Add(option.side_widget.sizer(), 0, wxLEFT | wxALIGN_CENTER_VERTICAL, 1); -// } - if (&option != &line.options().back()) { - sizer->AddSpacer(4); - } - - // add side text if any - // add side widget if any - } - // Append extra sizers - for (auto& widget : line.extra_widgets()) { - _sizer->Add(widget.sizer(), 0, wxLEFT | wxALIGN_CENTER_VERTICAL, 4); + for (auto opt : option_set) { + } + + +} +Line OptionsGroup::create_single_option_line(const Option& option) const { + Line retval {option.opt.label, option.opt.tooltip}; + Option tmp(option); + tmp.opt.label = std::string(""); + retval.append_option(tmp); + return retval; } -Field* OptionsGroup::_build_field(const ConfigOptionDef& opt) { - Field* built_field = nullptr; - switch (opt.type) { - case coString: - { - printf("Making new textctrl\n"); - TextCtrl* temp = new TextCtrl(_parent, opt); - printf("recasting textctrl\n"); - built_field = dynamic_cast(temp); - } - break; - default: - break; - } - return built_field; +//! void OptionsGroup::_on_change(t_config_option_key id, config_value value) { +//! if (on_change != nullptr) +//! on_change(id, value); +//! } + +void OptionsGroup::_on_kill_focus (t_config_option_key id) { + // do nothing. } -} } + + +}} diff --git a/xs/src/slic3r/GUI/OptionsGroup.hpp b/xs/src/slic3r/GUI/OptionsGroup.hpp index f2db0304e..328f721d8 100644 --- a/xs/src/slic3r/GUI/OptionsGroup.hpp +++ b/xs/src/slic3r/GUI/OptionsGroup.hpp @@ -1,109 +1,148 @@ -#ifndef OPTIONSGROUP_HPP -#define OPTIONSGROUP_HPP +#include +#include +#include +//#include -#include #include -#include "wxinit.h" -#include "Widget.hpp" -#include "OptionsGroup/Field.hpp" -#include "Config.hpp" -namespace Slic3r { -class ConfigOptionDef; -namespace GUI { +#include +#include "libslic3r/Config.hpp" +#include "libslic3r/PrintConfig.hpp" +#include "libslic3r/libslic3r.h" -/// Enumeration class to provide flags for these GUI hints. -/// they resolve to hex numbers to permit boolean masking. -enum class GUI_Type { - i_enum_open = 0x1, - f_enum_open = 0x2, - select_open = 0x4 +#include "Field.hpp" +//#include "slic3r_gui.hpp" +#include "GUI.hpp" + +// Translate the ifdef +#ifdef __WXOSX__ + #define wxOSX true +#else + #define wxOSX false +#endif + +#define BORDER(a, b) ((wxOSX ? a : b)) + +namespace Slic3r { namespace GUI { + +/// Widget type describes a function object that returns a wxWindow (our widget) and accepts a wxWidget (parent window). +using widget_t = std::function; +using column_t = std::function; + +class StaticText; + +/// Wraps a ConfigOptionDef and adds function object for creating a side_widget. +struct Option { + ConfigOptionDef opt {ConfigOptionDef()}; + t_config_option_key opt_id;//! {""}; + widget_t side_widget {nullptr}; + bool readonly {false}; + + Option(const ConfigOptionDef& _opt, t_config_option_key id) : opt(_opt), opt_id(id) {}; +}; +using t_option = std::unique_ptr