Start to implement "Search through options"

This commit is contained in:
YuSanka 2020-03-13 22:37:18 +01:00
parent 3fd6c8a2ff
commit e188893c28
7 changed files with 397 additions and 5 deletions

View File

@ -150,6 +150,8 @@ set(SLIC3R_GUI_SOURCES
GUI/DoubleSlider.hpp
GUI/ObjectDataViewModel.cpp
GUI/ObjectDataViewModel.hpp
GUI/SearchComboBox.cpp
GUI/SearchComboBox.hpp
Utils/Http.cpp
Utils/Http.hpp
Utils/FixModelByWin10.cpp

View File

@ -82,6 +82,7 @@
#include "../Utils/UndoRedo.hpp"
#include "../Utils/Thread.hpp"
#include "RemovableDriveManager.hpp"
#include "SearchComboBox.hpp"
#include <wx/glcanvas.h> // Needs to be last because reasons :-/
#include "WipeTowerDialog.hpp"
@ -705,6 +706,7 @@ struct Sidebar::priv
wxBoxSizer *sizer_params;
FreqChangedParams *frequently_changed_parameters{ nullptr };
SearchComboBox *search_cb{ nullptr };
ObjectList *object_list{ nullptr };
ObjectManipulation *object_manipulation{ nullptr };
ObjectSettings *object_settings{ nullptr };
@ -831,6 +833,10 @@ Sidebar::Sidebar(Plater *parent)
p->frequently_changed_parameters = new FreqChangedParams(p->scrolled);
p->sizer_params->Add(p->frequently_changed_parameters->get_sizer(), 0, wxEXPAND | wxTOP | wxBOTTOM, wxOSX ? 1 : margin_5);
// Search combobox
p->search_cb = new SearchComboBox(p->scrolled);
p->sizer_params->Add(p->search_cb, 0, wxEXPAND | wxTOP | wxBOTTOM, wxOSX ? 1 : margin_5);
// Object List
p->object_list = new ObjectList(p->scrolled);
p->sizer_params->Add(p->object_list->get_sizer(), 1, wxEXPAND);
@ -1341,6 +1347,23 @@ bool Sidebar::is_multifilament()
return p->combos_filament.size() > 1;
}
static std::vector<SearchInput> get_search_inputs(ConfigOptionMode mode)
{
std::vector<SearchInput> ret {};
auto& tabs_list = wxGetApp().tabs_list;
auto print_tech = wxGetApp().preset_bundle->printers.get_selected_preset().printer_technology();
for (auto tab : tabs_list)
if (tab->supports_printer_technology(print_tech))
ret.emplace_back(SearchInput{tab->get_config(), tab->type(), mode});
return ret;
}
void Sidebar::update_search_list()
{
p->search_cb->init(get_search_inputs(m_mode));
}
void Sidebar::update_mode()
{
@ -1348,6 +1371,7 @@ void Sidebar::update_mode()
update_reslice_btn_tooltip();
update_mode_sizer();
update_search_list();
wxWindowUpdateLocker noUpdates(this);
@ -3650,13 +3674,17 @@ void Plater::priv::on_select_preset(wxCommandEvent &evt)
// update plater with new config
wxGetApp().plater()->on_config_change(wxGetApp().preset_bundle->full_config());
if (preset_type == Preset::TYPE_PRINTER) {
/* Settings list can be changed after printer preset changing, so
* update all settings items for all item had it.
* Furthermore, Layers editing is implemented only for FFF printers
* and for SLA presets they should be deleted
*/
if (preset_type == Preset::TYPE_PRINTER)
wxGetApp().obj_list()->update_object_list_by_printer_technology();
// print technology could be changed, so we should to update a search list
sidebar->update_search_list();
}
}
void Plater::priv::on_slicing_update(SlicingStatusEvent &evt)

View File

@ -125,6 +125,7 @@ public:
bool show_export_removable(bool show) const;
bool is_multifilament();
void update_mode();
void update_search_list();
std::vector<PresetComboBox*>& combos_filament();
private:

View File

@ -0,0 +1,211 @@
#include "SearchComboBox.hpp"
#include <cstddef>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <regex>
#include <future>
#include <boost/algorithm/string.hpp>
#include <boost/optional.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/log/trivial.hpp>
#include <wx/sizer.h>
#include <wx/bmpcbox.h>
#include "libslic3r/PrintConfig.hpp"
#include "GUI_App.hpp"
#include "Tab.hpp"
#include "PresetBundle.hpp"
using boost::optional;
namespace Slic3r {
namespace GUI {
bool SearchOptions::Option::containes(const wxString& search_) const
{
wxString search = search_.Lower();
wxString label_ = label.Lower();
wxString category_ = category.Lower();
return (opt_key.find(into_u8(search)) != std::string::npos ||
label_.Find(search) != wxNOT_FOUND ||
category_.Find(search) != wxNOT_FOUND);
/* */
auto search_str = into_u8(search);
auto pos = opt_key.find(into_u8(search));
bool in_opt_key = pos != std::string::npos;
bool in_label = label_.Find(search) != wxNOT_FOUND;
bool in_category = category_.Find(search) != wxNOT_FOUND;
if (in_opt_key || in_label || in_category)
return true;
return false;
}
template<class T>
void change_opt_key(std::string& opt_key, DynamicPrintConfig* config)
{
T* opt_cur = static_cast<T*>(config->option(opt_key));
if (opt_cur->values.size() > 0)
opt_key += "#" + std::to_string(0);
}
void SearchOptions::append_options(DynamicPrintConfig* config, Preset::Type type, ConfigOptionMode mode)
{
for (std::string opt_key : config->keys())
{
const ConfigOptionDef& opt = config->def()->options.at(opt_key);
if (opt.mode > mode)
continue;
if (type == Preset::TYPE_SLA_MATERIAL || type == Preset::TYPE_PRINTER)
switch (config->option(opt_key)->type())
{
case coInts: change_opt_key<ConfigOptionInts >(opt_key, config); break;
case coBools: change_opt_key<ConfigOptionBools >(opt_key, config); break;
case coFloats: change_opt_key<ConfigOptionFloats >(opt_key, config); break;
case coStrings: change_opt_key<ConfigOptionStrings >(opt_key, config); break;
case coPercents:change_opt_key<ConfigOptionPercents >(opt_key, config); break;
case coPoints: change_opt_key<ConfigOptionPoints >(opt_key, config); break;
default: break;
}
wxString label;
if (!opt.category.empty())
label += _(opt.category) + " : ";
label += _(opt.full_label.empty() ? opt.label : opt.full_label);
options.emplace(Option{ opt_key, label, opt.category, type });
}
}
SearchComboBox::SearchComboBox(wxWindow *parent) :
wxBitmapComboBox(parent, wxID_ANY, "", wxDefaultPosition, wxSize(25 * wxGetApp().em_unit(), -1)),
em_unit(wxGetApp().em_unit())
{
SetFont(wxGetApp().normal_font());
default_search_line = search_line = _(L("Search through options")) + dots;
bmp = ScalableBitmap(this, "search");
#ifdef _WIN32
// Workaround for ignoring CBN_EDITCHANGE events, which are processed after the content of the combo box changes, so that
// the index of the item inside CBN_EDITCHANGE may no more be valid.
// EnableTextChangedEvents(false);
#endif /* _WIN32 */
Bind(wxEVT_COMBOBOX, [this](wxCommandEvent &evt) {
auto selected_item = this->GetSelection();
SearchOptions::Option* opt = reinterpret_cast<SearchOptions::Option*>(this->GetClientData(selected_item));
wxGetApp().get_tab(opt->type)->activate_option(opt->opt_key, opt->category);
evt.StopPropagation();
SuppressUpdate su(this);
this->SetValue(search_line);
});
Bind(wxEVT_TEXT, [this](wxCommandEvent &e) {
if (prevent_update)
return;
if (this->IsTextEmpty())
{
return;
}
if (search_line != this->GetValue()) {
update_combobox();
search_line = this->GetValue();
}
});
Bind(wxEVT_KILL_FOCUS, [this](wxEvent & e) {
e.Skip();
SuppressUpdate su(this);
this->SetValue(search_line.IsEmpty() ? default_search_line : search_line);
});
}
SearchComboBox::~SearchComboBox()
{
}
void SearchComboBox::msw_rescale()
{
em_unit = wxGetApp().em_unit();
wxSize size = wxSize(25 * em_unit, -1);
// Set rescaled min height to correct layout
this->SetMinSize(size);
// Set rescaled size
this->SetSize(size);
update_combobox();
}
void SearchComboBox::init(DynamicPrintConfig* config, Preset::Type type, ConfigOptionMode mode)
{
search_list.clear();
search_list.append_options(config, type, mode);
update_combobox();
}
void SearchComboBox::init(std::vector<SearchInput> input_values)
{
search_list.clear();
for (auto i : input_values)
search_list.append_options(i.config, i.type, i.mode);
update_combobox();
}
void SearchComboBox::update_combobox()
{
wxString search_str = this->GetValue();
if (search_str.IsEmpty() || search_str == default_search_line)
// add whole options list to the controll
append_all_items();
else
append_items(search_str);
}
void SearchComboBox::append_all_items()
{
this->Clear();
for (const SearchOptions::Option& item : search_list.options)
if (!item.label.IsEmpty())
append(item.label, (void*)&item);
SuppressUpdate su(this);
this->SetValue(default_search_line);
}
void SearchComboBox::append_items(const wxString& search)
{
this->Clear();
auto cmp = [](SearchOptions::Option* o1, SearchOptions::Option* o2) { return o1->label > o2->label; };
std::set<SearchOptions::Option*, decltype(cmp)> ret(cmp);
for (const SearchOptions::Option& option : search_list.options)
if (option.containes(search))
append(option.label, (void*)&option);
this->Popup();
SuppressUpdate su(this);
this->SetValue(search);
this->SetInsertionPointEnd();
}
}} // namespace Slic3r::GUI

View File

@ -0,0 +1,87 @@
#ifndef slic3r_SearchComboBox_hpp_
#define slic3r_SearchComboBox_hpp_
#include <memory>
#include <vector>
#include <boost/filesystem/path.hpp>
#include <wx/bmpcbox.h>
#include "Preset.hpp"
#include "wxExtensions.hpp"
namespace Slic3r {
namespace GUI {
struct SearchInput
{
DynamicPrintConfig* config {nullptr};
Preset::Type type {Preset::TYPE_INVALID};
ConfigOptionMode mode {comSimple};
};
class SearchOptions
{
public:
struct Option {
bool operator<(const Option& other) const { return other.label > this->label; }
bool operator>(const Option& other) const { return other.label < this->label; }
std::string opt_key;
wxString label;
wxString category;
Preset::Type type {Preset::TYPE_INVALID};
// wxString grope;
bool containes(const wxString& search) const;
};
std::set<Option> options {};
void clear() { options. clear(); }
void append_options(DynamicPrintConfig* config, Preset::Type type, ConfigOptionMode mode);
};
class SearchComboBox : public wxBitmapComboBox
{
class SuppressUpdate
{
SearchComboBox* m_cb;
public:
SuppressUpdate(SearchComboBox* cb) :
m_cb(cb) { m_cb->prevent_update = true ; }
~SuppressUpdate() { m_cb->prevent_update = false; }
};
public:
SearchComboBox(wxWindow *parent);
~SearchComboBox();
int append(const wxString& item, void* clientData) { return Append(item, bmp.bmp(), clientData); }
int append(const wxString& item, wxClientData* clientData) { return Append(item, bmp.bmp(), clientData); }
void append_all_items();
void append_items(const wxString& search);
void msw_rescale();
void init(DynamicPrintConfig* config, Preset::Type type, ConfigOptionMode mode);
void init(std::vector<SearchInput> input_values);
void update_combobox();
private:
SearchOptions search_list;
wxString default_search_line;
wxString search_line;
int em_unit;
bool prevent_update {false};
ScalableBitmap bmp;
};
}}
#endif //slic3r_SearchComboBox_hpp_

View File

@ -114,6 +114,9 @@ void Tab::create_preset_tab()
// preset chooser
m_presets_choice = new PresetBitmapComboBox(panel, wxSize(35 * m_em_unit, -1));
// search combox
m_search_cb = new SearchComboBox(panel);
auto color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
//buttons
@ -186,13 +189,18 @@ void Tab::create_preset_tab()
m_hsizer->Add(m_btn_save_preset, 0, wxALIGN_CENTER_VERTICAL);
m_hsizer->AddSpacer(int(4 * scale_factor));
m_hsizer->Add(m_btn_delete_preset, 0, wxALIGN_CENTER_VERTICAL);
m_hsizer->AddSpacer(int(16 * scale_factor));
m_hsizer->AddSpacer(int(/*16*/8 * scale_factor));
m_hsizer->Add(m_btn_hide_incompatible_presets, 0, wxALIGN_CENTER_VERTICAL);
m_hsizer->AddSpacer(int(64 * scale_factor));
m_hsizer->AddSpacer(int(8 * scale_factor));
m_hsizer->Add(m_question_btn, 0, wxALIGN_CENTER_VERTICAL);
m_hsizer->AddSpacer(int(/*32*/16 * scale_factor));
m_hsizer->Add(m_undo_to_sys_btn, 0, wxALIGN_CENTER_VERTICAL);
m_hsizer->Add(m_undo_btn, 0, wxALIGN_CENTER_VERTICAL);
m_hsizer->AddSpacer(int(32 * scale_factor));
m_hsizer->Add(m_question_btn, 0, wxALIGN_CENTER_VERTICAL);
m_hsizer->AddSpacer(int(/*32*/16 * scale_factor));
m_hsizer->Add(m_search_cb, 0, wxALIGN_CENTER_VERTICAL);
m_hsizer->AddSpacer(int(16 * scale_factor));
// m_hsizer->AddSpacer(int(32 * scale_factor));
// m_hsizer->Add(m_question_btn, 0, wxALIGN_CENTER_VERTICAL);
// m_hsizer->AddStretchSpacer(32);
// StretchSpacer has a strange behavior under OSX, so
// There is used just additional sizer for m_mode_sizer with right alignment
@ -752,6 +760,9 @@ void Tab::update_mode()
update_visibility();
update_changed_tree_ui();
// update list of options for search
m_search_cb->init(m_config, type(), m_mode);
}
void Tab::update_visibility()
@ -778,6 +789,7 @@ void Tab::msw_rescale()
m_em_unit = wxGetApp().em_unit();
m_mode_sizer->msw_rescale();
m_search_cb->msw_rescale();
m_presets_choice->SetSize(35 * m_em_unit, -1);
m_treectrl->SetMinSize(wxSize(20 * m_em_unit, -1));
@ -820,6 +832,19 @@ Field* Tab::get_field(const t_config_option_key& opt_key, int opt_index/* = -1*/
return field;
}
Field* Tab::get_field(const t_config_option_key& opt_key, Page** selected_page, int opt_index/* = -1*/)
{
Field* field = nullptr;
for (auto page : m_pages) {
field = page->get_field(opt_key, opt_index);
if (field != nullptr) {
*selected_page = page.get();
return field;
}
}
return field;
}
// Set a key/value pair on this page. Return true if the value has been modified.
// Currently used for distributing extruders_count over preset pages of Slic3r::GUI::Tab::Printer
// after a preset is loaded.
@ -925,6 +950,40 @@ void Tab::update_wiping_button_visibility() {
}
}
void Tab::activate_option(const std::string& opt_key, const wxString& category)
{
Page* page {nullptr};
Field* field = get_field(opt_key, &page);
// for option, which doesn't have field but just a text or button
wxString page_title = (!field || !page) ? category : page->title();
auto cur_item = m_treectrl->GetFirstVisibleItem();
if (!cur_item || !m_treectrl->IsVisible(cur_item))
return;
while (cur_item) {
auto title = m_treectrl->GetItemText(cur_item);
if (page_title != title) {
cur_item = m_treectrl->GetNextVisible(cur_item);
continue;
}
m_treectrl->SelectItem(cur_item);
break;
}
// we should to activate a tab with searched option, if it doesn't.
wxNotebook* tap_panel = wxGetApp().tab_panel();
int page_id = tap_panel->FindPage(this);
if (tap_panel->GetSelection() != page_id)
tap_panel->SetSelection(page_id);
// focused selected field
if (field)
field->getWindow()->SetFocus();
}
// Call a callback to update the selection of presets on the plater:
// To update the content of the selection boxes,

View File

@ -33,6 +33,7 @@
#include "Event.hpp"
#include "wxExtensions.hpp"
#include "ConfigManipulation.hpp"
#include "SearchComboBox.hpp"
namespace Slic3r {
namespace GUI {
@ -121,6 +122,7 @@ protected:
std::string m_name;
const wxString m_title;
PresetBitmapComboBox* m_presets_choice;
SearchComboBox* m_search_cb;
ScalableButton* m_btn_save_preset;
ScalableButton* m_btn_delete_preset;
ScalableButton* m_btn_hide_incompatible_presets;
@ -299,6 +301,7 @@ public:
void update_visibility();
virtual void msw_rescale();
Field* get_field(const t_config_option_key& opt_key, int opt_index = -1) const;
Field* get_field(const t_config_option_key &opt_key, Page** selected_page, int opt_index = -1);
bool set_value(const t_config_option_key& opt_key, const boost::any& value);
wxSizer* description_line_widget(wxWindow* parent, ogStaticText** StaticText);
bool current_preset_is_dirty();
@ -310,6 +313,7 @@ public:
void on_value_change(const std::string& opt_key, const boost::any& value);
void update_wiping_button_visibility();
void activate_option(const std::string& opt_key, const wxString& category);
protected:
void create_line_with_widget(ConfigOptionsGroup* optgroup, const std::string& opt_key, widget_t widget);