2020-04-03 08:01:23 +00:00
|
|
|
#include "Search.hpp"
|
2020-03-13 21:37:18 +00:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <string>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
|
|
|
#include "libslic3r/PrintConfig.hpp"
|
|
|
|
#include "GUI_App.hpp"
|
|
|
|
#include "Tab.hpp"
|
|
|
|
#include "PresetBundle.hpp"
|
|
|
|
|
2020-03-25 09:24:43 +00:00
|
|
|
#define FTS_FUZZY_MATCH_IMPLEMENTATION
|
|
|
|
#include "fts_fuzzy_match.h"
|
|
|
|
|
2020-03-31 20:46:12 +00:00
|
|
|
#include "imgui/imconfig.h"
|
|
|
|
|
2020-03-13 21:37:18 +00:00
|
|
|
using boost::optional;
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
using GUI::from_u8;
|
|
|
|
using GUI::into_u8;
|
|
|
|
|
|
|
|
namespace Search {
|
2020-03-25 09:24:43 +00:00
|
|
|
|
2020-04-20 19:46:16 +00:00
|
|
|
static std::map<Preset::Type, std::string> NameByType = {
|
|
|
|
{ Preset::TYPE_PRINT, L("Print") },
|
|
|
|
{ Preset::TYPE_FILAMENT, L("Filament") },
|
|
|
|
{ Preset::TYPE_SLA_MATERIAL, L("Material") },
|
|
|
|
{ Preset::TYPE_SLA_PRINT, L("Print") },
|
|
|
|
{ Preset::TYPE_PRINTER, L("Printer") }
|
|
|
|
};
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
FMFlag Option::fuzzy_match_simple(char const * search_pattern) const
|
|
|
|
{
|
|
|
|
return fts::fuzzy_match_simple(search_pattern, label_local.utf8_str()) ? fmLabelLocal :
|
|
|
|
fts::fuzzy_match_simple(search_pattern, group_local.utf8_str()) ? fmGroupLocal :
|
|
|
|
fts::fuzzy_match_simple(search_pattern, category_local.utf8_str()) ? fmCategoryLocal :
|
|
|
|
fts::fuzzy_match_simple(search_pattern, opt_key.c_str()) ? fmOptKey :
|
|
|
|
fts::fuzzy_match_simple(search_pattern, label.utf8_str()) ? fmLabel :
|
|
|
|
fts::fuzzy_match_simple(search_pattern, group.utf8_str()) ? fmGroup :
|
|
|
|
fts::fuzzy_match_simple(search_pattern, category.utf8_str()) ? fmCategory : fmUndef ;
|
2020-03-25 09:24:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
FMFlag Option::fuzzy_match_simple(const wxString& search) const
|
2020-03-25 09:24:43 +00:00
|
|
|
{
|
|
|
|
char const* search_pattern = search.utf8_str();
|
2020-03-31 20:46:12 +00:00
|
|
|
return fuzzy_match_simple(search_pattern);
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
FMFlag Option::fuzzy_match_simple(const std::string& search) const
|
2020-03-31 20:46:12 +00:00
|
|
|
{
|
|
|
|
char const* search_pattern = search.c_str();
|
|
|
|
return fuzzy_match_simple(search_pattern);
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
FMFlag Option::fuzzy_match(char const* search_pattern, int& outScore) const
|
2020-03-31 20:46:12 +00:00
|
|
|
{
|
2020-04-13 15:55:38 +00:00
|
|
|
FMFlag flag = fmUndef;
|
|
|
|
int score;
|
|
|
|
|
|
|
|
if (fts::fuzzy_match(search_pattern, label_local.utf8_str(), score) && outScore < score) {
|
|
|
|
outScore = score; flag = fmLabelLocal ; }
|
|
|
|
if (fts::fuzzy_match(search_pattern, group_local.utf8_str(), score) && outScore < score) {
|
|
|
|
outScore = score; flag = fmGroupLocal ; }
|
|
|
|
if (fts::fuzzy_match(search_pattern, category_local.utf8_str(), score) && outScore < score) {
|
|
|
|
outScore = score; flag = fmCategoryLocal; }
|
|
|
|
if (fts::fuzzy_match(search_pattern, opt_key.c_str(), score) && outScore < score) {
|
|
|
|
outScore = score; flag = fmOptKey ; }
|
|
|
|
if (fts::fuzzy_match(search_pattern, label.utf8_str(), score) && outScore < score) {
|
|
|
|
outScore = score; flag = fmLabel ; }
|
|
|
|
if (fts::fuzzy_match(search_pattern, group.utf8_str(), score) && outScore < score) {
|
|
|
|
outScore = score; flag = fmGroup ; }
|
|
|
|
if (fts::fuzzy_match(search_pattern, category.utf8_str(), score) && outScore < score) {
|
|
|
|
outScore = score; flag = fmCategory ; }
|
|
|
|
|
|
|
|
return flag;
|
2020-03-13 21:37:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
FMFlag Option::fuzzy_match(const wxString& search, int& outScore) const
|
2020-03-31 20:46:12 +00:00
|
|
|
{
|
|
|
|
char const* search_pattern = search.utf8_str();
|
|
|
|
return fuzzy_match(search_pattern, outScore);
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
FMFlag Option::fuzzy_match(const std::string& search, int& outScore) const
|
2020-03-31 20:46:12 +00:00
|
|
|
{
|
|
|
|
char const* search_pattern = search.c_str();
|
|
|
|
return fuzzy_match(search_pattern, outScore);
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
void FoundOption::get_label(const char** out_text) const
|
2020-03-28 18:39:24 +00:00
|
|
|
{
|
|
|
|
*out_text = label.utf8_str();
|
|
|
|
}
|
2020-03-13 21:37:18 +00:00
|
|
|
|
2020-04-20 19:46:16 +00:00
|
|
|
void FoundOption::get_marked_label_and_tooltip(const char** label_, const char** tooltip_) const
|
2020-04-03 08:01:23 +00:00
|
|
|
{
|
2020-04-20 19:46:16 +00:00
|
|
|
*label_ = marked_label.utf8_str();
|
|
|
|
*tooltip_ = tooltip.utf8_str();
|
2020-04-03 08:01:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 21:37:18 +00:00
|
|
|
template<class T>
|
2020-04-16 07:59:12 +00:00
|
|
|
//void change_opt_key(std::string& opt_key, DynamicPrintConfig* config)
|
|
|
|
void change_opt_key(std::string& opt_key, DynamicPrintConfig* config, int& cnt)
|
2020-03-13 21:37:18 +00:00
|
|
|
{
|
|
|
|
T* opt_cur = static_cast<T*>(config->option(opt_key));
|
2020-04-16 07:59:12 +00:00
|
|
|
cnt = opt_cur->values.size();
|
|
|
|
return;
|
|
|
|
|
2020-03-13 21:37:18 +00:00
|
|
|
if (opt_cur->values.size() > 0)
|
|
|
|
opt_key += "#" + std::to_string(0);
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
void OptionsSearcher::append_options(DynamicPrintConfig* config, Preset::Type type, ConfigOptionMode mode)
|
2020-03-13 21:37:18 +00:00
|
|
|
{
|
2020-04-16 07:59:12 +00:00
|
|
|
auto emplace = [this, type](const std::string opt_key, const wxString& label)
|
|
|
|
{
|
|
|
|
const GroupAndCategory& gc = groups_and_categories[opt_key];
|
|
|
|
if (gc.group.IsEmpty() || gc.category.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
wxString suffix;
|
|
|
|
if (gc.category == "Machine limits")
|
|
|
|
suffix = opt_key.back()=='1' ? L("Stealth") : L("Normal");
|
|
|
|
|
|
|
|
if (!label.IsEmpty())
|
|
|
|
options.emplace_back(Option{ opt_key, type,
|
|
|
|
label+ " " + suffix, _(label)+ " " + _(suffix),
|
|
|
|
gc.group, _(gc.group),
|
|
|
|
gc.category, _(gc.category) });
|
|
|
|
};
|
2020-04-13 15:55:38 +00:00
|
|
|
|
2020-03-13 21:37:18 +00:00
|
|
|
for (std::string opt_key : config->keys())
|
|
|
|
{
|
|
|
|
const ConfigOptionDef& opt = config->def()->options.at(opt_key);
|
|
|
|
if (opt.mode > mode)
|
|
|
|
continue;
|
|
|
|
|
2020-04-16 07:59:12 +00:00
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
if ( (type == Preset::TYPE_SLA_MATERIAL || type == Preset::TYPE_PRINTER) && opt_key != "bed_shape")
|
2020-03-13 21:37:18 +00:00
|
|
|
switch (config->option(opt_key)->type())
|
|
|
|
{
|
2020-04-16 07:59:12 +00:00
|
|
|
case coInts: change_opt_key<ConfigOptionInts >(opt_key, config, cnt); break;
|
|
|
|
case coBools: change_opt_key<ConfigOptionBools >(opt_key, config, cnt); break;
|
|
|
|
case coFloats: change_opt_key<ConfigOptionFloats >(opt_key, config, cnt); break;
|
|
|
|
case coStrings: change_opt_key<ConfigOptionStrings >(opt_key, config, cnt); break;
|
|
|
|
case coPercents:change_opt_key<ConfigOptionPercents >(opt_key, config, cnt); break;
|
|
|
|
case coPoints: change_opt_key<ConfigOptionPoints >(opt_key, config, cnt); break;
|
2020-03-13 21:37:18 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
wxString label = opt.full_label.empty() ? opt.label : opt.full_label;
|
|
|
|
|
2020-04-16 07:59:12 +00:00
|
|
|
if (cnt == 0)
|
|
|
|
emplace(opt_key, label);
|
|
|
|
else
|
|
|
|
for (int i = 0; i < cnt; ++i)
|
|
|
|
emplace(opt_key + "#" + std::to_string(i), label);
|
|
|
|
|
|
|
|
/*const GroupAndCategory& gc = groups_and_categories[opt_key];
|
|
|
|
if (gc.group.IsEmpty() || gc.category.IsEmpty())
|
|
|
|
continue;
|
2020-03-13 21:37:18 +00:00
|
|
|
|
2020-03-28 18:39:24 +00:00
|
|
|
if (!label.IsEmpty())
|
2020-04-13 15:55:38 +00:00
|
|
|
options.emplace_back(Option{opt_key, type,
|
|
|
|
label, _(label),
|
|
|
|
gc.group, _(gc.group),
|
2020-04-16 07:59:12 +00:00
|
|
|
gc.category, _(gc.category) });*/
|
2020-03-13 21:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:46:12 +00:00
|
|
|
// Wrap a string with ColorMarkerStart and ColorMarkerEnd symbols
|
|
|
|
static wxString wrap_string(const wxString& str)
|
|
|
|
{
|
|
|
|
return wxString::Format("%c%s%c", ImGui::ColorMarkerStart, str, ImGui::ColorMarkerEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark a string using ColorMarkerStart and ColorMarkerEnd symbols
|
|
|
|
static void mark_string(wxString& str, const wxString& search_str)
|
|
|
|
{
|
|
|
|
// Try to find whole search string
|
|
|
|
if (str.Replace(search_str, wrap_string(search_str), false) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Try to find whole capitalized search string
|
|
|
|
wxString search_str_capitalized = search_str.Capitalize();
|
|
|
|
if (str.Replace(search_str_capitalized, wrap_string(search_str_capitalized), false) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// if search string is just a one letter now, there is no reason to continue
|
|
|
|
if (search_str.Len()==1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Split a search string for two strings (string without last letter and last letter)
|
|
|
|
// and repeat a function with new search strings
|
|
|
|
mark_string(str, search_str.SubString(0, search_str.Len() - 2));
|
|
|
|
mark_string(str, search_str.Last());
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear marked string from a redundant use of ColorMarkers
|
|
|
|
static void clear_marked_string(wxString& str)
|
|
|
|
{
|
|
|
|
// Check if the string has a several ColorMarkerStart in a row and replace them to only one, if any
|
|
|
|
wxString delete_string = wxString::Format("%c%c", ImGui::ColorMarkerStart, ImGui::ColorMarkerStart);
|
2020-04-03 12:29:57 +00:00
|
|
|
str.Replace(delete_string, ImGui::ColorMarkerStart, true);
|
|
|
|
// If there were several ColorMarkerStart in a row, it means there should be a several ColorMarkerStop in a row,
|
|
|
|
// replace them to only one
|
|
|
|
delete_string = wxString::Format("%c%c", ImGui::ColorMarkerEnd, ImGui::ColorMarkerEnd);
|
|
|
|
str.Replace(delete_string, ImGui::ColorMarkerEnd, true);
|
2020-03-31 20:46:12 +00:00
|
|
|
|
|
|
|
// And we should to remove redundant ColorMarkers, if they are in "End, Start" sequence in a row
|
|
|
|
delete_string = wxString::Format("%c%c", ImGui::ColorMarkerEnd, ImGui::ColorMarkerStart);
|
|
|
|
str.Replace(delete_string, wxEmptyString, true);
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
bool OptionsSearcher::search(const std::string& search, bool force/* = false*/)
|
2020-03-25 09:24:43 +00:00
|
|
|
{
|
2020-04-07 14:22:03 +00:00
|
|
|
if (search_line == search && !force)
|
|
|
|
return false;
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
found.clear();
|
2020-03-28 18:39:24 +00:00
|
|
|
|
|
|
|
bool full_list = search.empty();
|
2020-04-20 19:46:16 +00:00
|
|
|
wxString sep = " : ";
|
2020-03-31 20:46:12 +00:00
|
|
|
|
2020-04-20 19:46:16 +00:00
|
|
|
auto get_label = [this, sep](const Option& opt)
|
2020-04-14 15:54:15 +00:00
|
|
|
{
|
|
|
|
wxString label;
|
2020-04-20 19:46:16 +00:00
|
|
|
if (view_params.type)
|
|
|
|
label += _(NameByType[opt.type]) + sep;
|
|
|
|
if (view_params.category)
|
|
|
|
label += opt.category_local + sep;
|
|
|
|
if (view_params.group)
|
|
|
|
label += opt.group_local + sep;
|
2020-04-14 15:54:15 +00:00
|
|
|
label += opt.label_local;
|
|
|
|
return label;
|
|
|
|
};
|
|
|
|
|
2020-04-20 19:46:16 +00:00
|
|
|
auto get_tooltip = [this, sep](const Option& opt)
|
|
|
|
{
|
|
|
|
return _(NameByType[opt.type]) + sep +
|
|
|
|
opt.category_local + sep +
|
|
|
|
opt.group_local + sep + opt.label_local;
|
|
|
|
};
|
|
|
|
|
2020-03-31 20:46:12 +00:00
|
|
|
for (size_t i=0; i < options.size(); i++)
|
|
|
|
{
|
2020-04-13 15:55:38 +00:00
|
|
|
const Option &opt = options[i];
|
2020-03-31 20:46:12 +00:00
|
|
|
if (full_list) {
|
2020-04-14 15:54:15 +00:00
|
|
|
wxString label = get_label(opt);
|
2020-04-20 19:46:16 +00:00
|
|
|
found.emplace_back(FoundOption{ label, label, get_tooltip(opt), i, 0 });
|
2020-03-31 20:46:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int score = 0;
|
2020-04-13 15:55:38 +00:00
|
|
|
|
|
|
|
FMFlag fuzzy_match_flag = opt.fuzzy_match(search, score);
|
|
|
|
if (fuzzy_match_flag != fmUndef)
|
2020-03-31 20:46:12 +00:00
|
|
|
{
|
2020-04-20 19:46:16 +00:00
|
|
|
wxString label = get_label(opt);
|
2020-04-14 15:54:15 +00:00
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
if ( fuzzy_match_flag == fmLabel ) label += "(" + opt.label + ")";
|
|
|
|
else if (fuzzy_match_flag == fmGroup ) label += "(" + opt.group + ")";
|
|
|
|
else if (fuzzy_match_flag == fmCategory) label += "(" + opt.category + ")";
|
|
|
|
else if (fuzzy_match_flag == fmOptKey ) label += "(" + opt.opt_key + ")";
|
|
|
|
|
|
|
|
wxString marked_label = label;
|
2020-04-03 08:01:23 +00:00
|
|
|
mark_string(marked_label, from_u8(search));
|
|
|
|
clear_marked_string(marked_label);
|
2020-03-31 20:46:12 +00:00
|
|
|
|
2020-04-20 19:46:16 +00:00
|
|
|
found.emplace_back(FoundOption{ label, marked_label, get_tooltip(opt), i, score });
|
2020-03-31 20:46:12 +00:00
|
|
|
}
|
2020-03-25 09:24:43 +00:00
|
|
|
}
|
2020-03-28 18:39:24 +00:00
|
|
|
|
|
|
|
if (!full_list)
|
2020-04-13 15:55:38 +00:00
|
|
|
sort_found();
|
2020-04-07 14:22:03 +00:00
|
|
|
|
|
|
|
search_line = search;
|
|
|
|
return true;
|
2020-03-28 18:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
void OptionsSearcher::init(std::vector<InputInfo> input_values)
|
2020-03-28 18:39:24 +00:00
|
|
|
{
|
2020-04-13 15:55:38 +00:00
|
|
|
options.clear();
|
2020-03-28 18:39:24 +00:00
|
|
|
for (auto i : input_values)
|
|
|
|
append_options(i.config, i.type, i.mode);
|
|
|
|
sort_options();
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
search(search_line, true);
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:59:12 +00:00
|
|
|
void OptionsSearcher::apply(DynamicPrintConfig* config, Preset::Type type, ConfigOptionMode mode)
|
|
|
|
{
|
|
|
|
if (options.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
options.erase(std::remove_if(options.begin(), options.end(), [type](Option opt) {
|
|
|
|
return opt.type == type;
|
|
|
|
}), options.end());
|
|
|
|
|
|
|
|
append_options(config, type, mode);
|
|
|
|
|
|
|
|
sort_options();
|
|
|
|
|
|
|
|
search(search_line, true);
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
const Option& OptionsSearcher::get_option(size_t pos_in_filter) const
|
|
|
|
{
|
|
|
|
assert(pos_in_filter != size_t(-1) && found[pos_in_filter].option_idx != size_t(-1));
|
|
|
|
return options[found[pos_in_filter].option_idx];
|
2020-03-28 18:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
void OptionsSearcher::add_key(const std::string& opt_key, const wxString& group, const wxString& category)
|
2020-03-28 18:39:24 +00:00
|
|
|
{
|
2020-04-13 15:55:38 +00:00
|
|
|
groups_and_categories[opt_key] = GroupAndCategory{group, category};
|
2020-03-25 09:24:43 +00:00
|
|
|
}
|
2020-04-03 08:01:23 +00:00
|
|
|
|
|
|
|
|
2020-04-14 15:54:15 +00:00
|
|
|
//------------------------------------------
|
|
|
|
// SearchComboPopup
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
void SearchComboPopup::Init()
|
|
|
|
{
|
|
|
|
this->Bind(wxEVT_MOTION, &SearchComboPopup::OnMouseMove, this);
|
|
|
|
this->Bind(wxEVT_LEFT_UP, &SearchComboPopup::OnMouseClick, this);
|
|
|
|
this->Bind(wxEVT_KEY_DOWN, &SearchComboPopup::OnKeyDown, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchComboPopup::Create(wxWindow* parent)
|
|
|
|
{
|
|
|
|
return wxListBox::Create(parent, 1, wxPoint(0, 0), wxDefaultSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchComboPopup::SetStringValue(const wxString& s)
|
|
|
|
{
|
|
|
|
int n = wxListBox::FindString(s);
|
|
|
|
if (n >= 0 && n < wxListBox::GetCount())
|
|
|
|
wxListBox::Select(n);
|
|
|
|
|
|
|
|
// save a combo control's string
|
|
|
|
m_input_string = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchComboPopup::ProcessSelection(int selection)
|
|
|
|
{
|
|
|
|
wxCommandEvent event(wxEVT_LISTBOX, GetId());
|
|
|
|
event.SetInt(selection);
|
|
|
|
event.SetEventObject(this);
|
|
|
|
ProcessEvent(event);
|
|
|
|
|
|
|
|
Dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchComboPopup::OnMouseMove(wxMouseEvent& event)
|
|
|
|
{
|
|
|
|
wxPoint pt = wxGetMousePosition() - this->GetScreenPosition();
|
|
|
|
int selection = this->HitTest(pt);
|
|
|
|
wxListBox::Select(selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchComboPopup::OnMouseClick(wxMouseEvent&)
|
|
|
|
{
|
|
|
|
int selection = wxListBox::GetSelection();
|
|
|
|
SetSelection(wxNOT_FOUND);
|
|
|
|
ProcessSelection(selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchComboPopup::OnKeyDown(wxKeyEvent& event)
|
|
|
|
{
|
|
|
|
int key = event.GetKeyCode();
|
|
|
|
|
|
|
|
// change selected item in the list
|
|
|
|
if (key == WXK_UP || key == WXK_DOWN)
|
|
|
|
{
|
|
|
|
int selection = wxListBox::GetSelection();
|
|
|
|
|
|
|
|
if (key == WXK_UP && selection > 0)
|
|
|
|
selection--;
|
|
|
|
if (key == WXK_DOWN && selection < int(wxListBox::GetCount() - 1))
|
|
|
|
selection++;
|
|
|
|
|
|
|
|
wxListBox::Select(selection);
|
|
|
|
}
|
|
|
|
// send wxEVT_LISTBOX event if "Enter" was pushed
|
|
|
|
else if (key == WXK_NUMPAD_ENTER || key == WXK_RETURN)
|
|
|
|
ProcessSelection(wxListBox::GetSelection());
|
|
|
|
else
|
|
|
|
event.Skip(); // !Needed to have EVT_CHAR generated as well
|
|
|
|
}
|
|
|
|
|
2020-04-03 08:01:23 +00:00
|
|
|
//------------------------------------------
|
|
|
|
// SearchCtrl
|
|
|
|
//------------------------------------------
|
|
|
|
|
2020-04-07 18:34:09 +00:00
|
|
|
SearchCtrl::SearchCtrl(wxWindow* parent) :
|
2020-04-13 15:55:38 +00:00
|
|
|
wxComboCtrl(parent, wxID_ANY, _L("Type here to search"), wxDefaultPosition, wxSize(25 * GUI::wxGetApp().em_unit(), -1), wxTE_PROCESS_ENTER)
|
2020-04-03 08:01:23 +00:00
|
|
|
{
|
2020-04-04 17:25:14 +00:00
|
|
|
default_string = _L("Type here to search");
|
|
|
|
|
2020-04-07 18:34:09 +00:00
|
|
|
this->UseAltPopupWindow();
|
2020-04-04 17:25:14 +00:00
|
|
|
|
|
|
|
wxBitmap bmp_norm = create_scaled_bitmap("search_gray");
|
|
|
|
wxBitmap bmp_hov = create_scaled_bitmap("search");
|
2020-04-07 18:34:09 +00:00
|
|
|
this->SetButtonBitmaps(bmp_norm, true, bmp_hov, bmp_hov, bmp_norm);
|
2020-04-05 20:11:45 +00:00
|
|
|
|
2020-04-07 14:22:03 +00:00
|
|
|
popupListBox = new SearchComboPopup();
|
2020-04-05 20:11:45 +00:00
|
|
|
|
|
|
|
// It is important to call SetPopupControl() as soon as possible
|
2020-04-07 18:34:09 +00:00
|
|
|
this->SetPopupControl(popupListBox);
|
2020-04-04 17:25:14 +00:00
|
|
|
|
2020-04-07 18:34:09 +00:00
|
|
|
this->Bind(wxEVT_TEXT, &SearchCtrl::OnInputText, this);
|
|
|
|
this->Bind(wxEVT_TEXT_ENTER, &SearchCtrl::PopupList, this);
|
|
|
|
this->Bind(wxEVT_COMBOBOX_DROPDOWN, &SearchCtrl::PopupList, this);
|
2020-04-16 07:59:12 +00:00
|
|
|
|
2020-04-07 18:34:09 +00:00
|
|
|
this->GetTextCtrl()->Bind(wxEVT_LEFT_UP, &SearchCtrl::OnLeftUpInTextCtrl, this);
|
|
|
|
popupListBox->Bind(wxEVT_LISTBOX, &SearchCtrl::OnSelect, this);
|
2020-04-03 08:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchCtrl::OnInputText(wxCommandEvent& )
|
|
|
|
{
|
|
|
|
if (prevent_update)
|
|
|
|
return;
|
2020-04-04 17:25:14 +00:00
|
|
|
|
2020-04-07 18:34:09 +00:00
|
|
|
this->GetTextCtrl()->SetInsertionPointEnd();
|
2020-04-07 14:22:03 +00:00
|
|
|
|
2020-04-07 18:34:09 +00:00
|
|
|
wxString input_string = this->GetValue();
|
2020-04-07 14:22:03 +00:00
|
|
|
if (input_string == default_string)
|
|
|
|
input_string.Clear();
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
GUI::wxGetApp().sidebar().get_search_line() = into_u8(input_string);
|
2020-04-07 14:22:03 +00:00
|
|
|
|
|
|
|
editing = true;
|
2020-04-13 15:55:38 +00:00
|
|
|
GUI::wxGetApp().sidebar().search_and_apply_tab_search_lines();
|
2020-04-07 14:22:03 +00:00
|
|
|
editing = false;
|
2020-04-03 08:01:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 17:25:14 +00:00
|
|
|
void SearchCtrl::PopupList(wxCommandEvent& e)
|
2020-04-03 08:01:23 +00:00
|
|
|
{
|
2020-04-13 15:55:38 +00:00
|
|
|
update_list(GUI::wxGetApp().sidebar().get_searcher().found_options());
|
2020-04-08 07:47:33 +00:00
|
|
|
if (e.GetEventType() == wxEVT_TEXT_ENTER)
|
|
|
|
this->Popup();
|
|
|
|
|
2020-04-04 17:25:14 +00:00
|
|
|
e.Skip();
|
2020-04-03 08:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchCtrl::set_search_line(const std::string& line)
|
|
|
|
{
|
|
|
|
prevent_update = true;
|
2020-04-07 18:34:09 +00:00
|
|
|
this->SetValue(line.empty() && !editing ? default_string : from_u8(line));
|
2020-04-03 08:01:23 +00:00
|
|
|
prevent_update = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchCtrl::msw_rescale()
|
|
|
|
{
|
2020-04-13 15:55:38 +00:00
|
|
|
wxSize size = wxSize(25 * GUI::wxGetApp().em_unit(), -1);
|
2020-04-03 08:01:23 +00:00
|
|
|
// Set rescaled min height to correct layout
|
2020-04-07 18:34:09 +00:00
|
|
|
this->SetMinSize(size);
|
2020-04-04 17:25:14 +00:00
|
|
|
|
2020-04-07 18:34:09 +00:00
|
|
|
wxBitmap bmp_norm = create_scaled_bitmap("search_gray");
|
|
|
|
wxBitmap bmp_hov = create_scaled_bitmap("search");
|
|
|
|
this->SetButtonBitmaps(bmp_norm, true, bmp_hov, bmp_hov, bmp_norm);
|
2020-04-03 08:01:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:34:09 +00:00
|
|
|
void SearchCtrl::OnSelect(wxCommandEvent& event)
|
2020-04-04 17:25:14 +00:00
|
|
|
{
|
2020-04-07 18:34:09 +00:00
|
|
|
int selection = event.GetSelection();
|
2020-04-07 17:09:33 +00:00
|
|
|
if (selection < 0)
|
|
|
|
return;
|
2020-04-04 17:25:14 +00:00
|
|
|
|
2020-04-07 17:09:33 +00:00
|
|
|
prevent_update = true;
|
2020-04-13 15:55:38 +00:00
|
|
|
GUI::wxGetApp().sidebar().jump_to_option(selection);
|
2020-04-04 17:25:14 +00:00
|
|
|
prevent_update = false;
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
void SearchCtrl::update_list(const std::vector<FoundOption>& filters)
|
2020-04-05 20:11:45 +00:00
|
|
|
{
|
2020-04-16 07:59:12 +00:00
|
|
|
if (!filters.empty() && popupListBox->GetCount() == filters.size() &&
|
2020-04-07 14:22:03 +00:00
|
|
|
popupListBox->GetString(0) == filters[0].label &&
|
|
|
|
popupListBox->GetString(popupListBox->GetCount()-1) == filters[filters.size()-1].label)
|
|
|
|
return;
|
2020-04-05 20:11:45 +00:00
|
|
|
|
2020-04-07 14:22:03 +00:00
|
|
|
popupListBox->Clear();
|
2020-04-13 15:55:38 +00:00
|
|
|
for (const FoundOption& item : filters)
|
2020-04-07 14:22:03 +00:00
|
|
|
popupListBox->Append(item.label);
|
2020-04-05 20:11:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:22:03 +00:00
|
|
|
void SearchCtrl::OnLeftUpInTextCtrl(wxEvent &event)
|
2020-04-04 17:25:14 +00:00
|
|
|
{
|
2020-04-07 18:34:09 +00:00
|
|
|
if (this->GetValue() == default_string)
|
|
|
|
this->SetValue("");
|
2020-04-07 14:22:03 +00:00
|
|
|
|
2020-04-04 17:25:14 +00:00
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
2020-04-16 13:49:40 +00:00
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// PopupSearchList
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
PopupSearchList::PopupSearchList(wxWindow* parent) :
|
|
|
|
wxPopupTransientWindow(parent, /*wxSTAY_ON_TOP*/wxWANTS_CHARS | wxBORDER_NONE)
|
|
|
|
{
|
|
|
|
panel = new wxPanel(this, wxID_ANY);
|
|
|
|
|
|
|
|
text = new wxTextCtrl(panel, 1);
|
|
|
|
list = new wxListBox(panel, 2, wxDefaultPosition, wxSize(GUI::wxGetApp().em_unit() * 40, -1));
|
|
|
|
check = new wxCheckBox(panel, 3, "Group");
|
|
|
|
|
|
|
|
wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
|
|
|
|
text->Bind(wxEVT_MOUSE_CAPTURE_CHANGED, [](wxEvent& e) {
|
|
|
|
int i = 0; });
|
|
|
|
|
|
|
|
// text->Bind(wxEVT_LEFT_DOWN, [this](wxEvent& e) {
|
|
|
|
text->Bind(wxEVT_LEFT_UP, [this](wxEvent& e) {
|
|
|
|
text->SetValue("mrrrrrty");
|
|
|
|
});
|
|
|
|
|
|
|
|
text->Bind(wxEVT_MOTION, [this](wxMouseEvent& evt)
|
|
|
|
{
|
|
|
|
wxPoint pt = wxGetMousePosition() - text->GetScreenPosition();
|
|
|
|
long pos;
|
|
|
|
text->HitTest(pt, &pos);
|
|
|
|
|
|
|
|
if (pos == wxTE_HT_UNKNOWN)
|
|
|
|
return;
|
|
|
|
|
|
|
|
list->SetSelection(wxNOT_FOUND);
|
|
|
|
text->SetSelection(0, pos);
|
|
|
|
});
|
|
|
|
|
|
|
|
text->Bind(wxEVT_TEXT, [this](wxCommandEvent& e)
|
|
|
|
{
|
|
|
|
text->SetSelection(0, 3);
|
|
|
|
});
|
|
|
|
|
|
|
|
this->Bind(wxEVT_KEY_DOWN, [this](wxKeyEvent& event) {
|
|
|
|
int key = event.GetKeyCode();
|
|
|
|
|
|
|
|
// change selected item in the list
|
|
|
|
if (key == WXK_UP || key == WXK_DOWN)
|
|
|
|
{
|
|
|
|
int selection = list->GetSelection();
|
|
|
|
|
|
|
|
if (key == WXK_UP && selection > 0)
|
|
|
|
selection--;
|
|
|
|
if (key == WXK_DOWN && selection < int(list->GetCount() - 1))
|
|
|
|
selection++;
|
|
|
|
|
|
|
|
list->Select(selection);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
event.Skip(); // !Needed to have EVT_CHAR generated as well
|
|
|
|
});
|
|
|
|
|
|
|
|
this->Bind(wxEVT_CHAR, [this](wxKeyEvent& e) {
|
|
|
|
int key = e.GetKeyCode();
|
|
|
|
wxChar symbol = e.GetUnicodeKey();
|
|
|
|
search_str += symbol;
|
|
|
|
|
|
|
|
text->SetValue(search_str);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
list->Append("One");
|
|
|
|
list->Append("Two");
|
|
|
|
list->Append("Three");
|
|
|
|
|
|
|
|
list->Bind(wxEVT_LISTBOX, [this](wxCommandEvent& evt)
|
|
|
|
{
|
|
|
|
int selection = list->GetSelection();
|
|
|
|
});
|
|
|
|
|
|
|
|
list->Bind(wxEVT_LEFT_UP, [this](wxMouseEvent& evt)
|
|
|
|
{
|
|
|
|
int selection = list->GetSelection();
|
|
|
|
list->SetSelection(wxNOT_FOUND);
|
|
|
|
|
|
|
|
wxCommandEvent event(wxEVT_LISTBOX, list->GetId());
|
|
|
|
event.SetInt(selection);
|
|
|
|
event.SetEventObject(this);
|
|
|
|
ProcessEvent(event);
|
|
|
|
|
|
|
|
Dismiss();
|
|
|
|
});
|
|
|
|
|
|
|
|
list->Bind(wxEVT_MOTION, [this](wxMouseEvent& evt)
|
|
|
|
{
|
|
|
|
wxPoint pt = wxGetMousePosition() - list->GetScreenPosition();
|
|
|
|
int selection = list->HitTest(pt);
|
|
|
|
list->Select(selection);
|
|
|
|
});
|
|
|
|
|
|
|
|
list->Bind(wxEVT_KEY_DOWN, [this](wxKeyEvent& event) {
|
|
|
|
int key = event.GetKeyCode();
|
|
|
|
|
|
|
|
// change selected item in the list
|
|
|
|
if (key == WXK_UP || key == WXK_DOWN)
|
|
|
|
{
|
|
|
|
int selection = list->GetSelection();
|
|
|
|
|
|
|
|
if (key == WXK_UP && selection > 0)
|
|
|
|
selection--;
|
|
|
|
if (key == WXK_DOWN && selection < int(list->GetCount() - 1))
|
|
|
|
selection++;
|
|
|
|
|
|
|
|
list->Select(selection);
|
|
|
|
}
|
|
|
|
// send wxEVT_LISTBOX event if "Enter" was pushed
|
|
|
|
else if (key == WXK_NUMPAD_ENTER || key == WXK_RETURN)
|
|
|
|
{
|
|
|
|
int selection = list->GetSelection();
|
|
|
|
|
|
|
|
wxCommandEvent event(wxEVT_LISTBOX, list->GetId());
|
|
|
|
event.SetInt(selection);
|
|
|
|
event.SetEventObject(this);
|
|
|
|
ProcessEvent(event);
|
|
|
|
|
|
|
|
Dismiss();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
event.Skip(); // !Needed to have EVT_CHAR generated as well
|
|
|
|
});
|
|
|
|
|
|
|
|
topSizer->Add(text, 0, wxEXPAND | wxALL, 2);
|
|
|
|
topSizer->Add(list, 0, wxEXPAND | wxALL, 2);
|
|
|
|
topSizer->Add(check, 0, wxEXPAND | wxALL, 2);
|
|
|
|
|
|
|
|
panel->SetSizer(topSizer);
|
|
|
|
|
|
|
|
topSizer->Fit(panel);
|
|
|
|
SetClientSize(panel->GetSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupSearchList::Popup(wxWindow* WXUNUSED(focus))
|
|
|
|
{
|
|
|
|
wxPopupTransientWindow::Popup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupSearchList::OnDismiss()
|
|
|
|
{
|
|
|
|
wxPopupTransientWindow::OnDismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PopupSearchList::ProcessLeftDown(wxMouseEvent& event)
|
|
|
|
{
|
|
|
|
return wxPopupTransientWindow::ProcessLeftDown(event);
|
|
|
|
}
|
|
|
|
bool PopupSearchList::Show(bool show)
|
|
|
|
{
|
|
|
|
return wxPopupTransientWindow::Show(show);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupSearchList::OnSize(wxSizeEvent& event)
|
|
|
|
{
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupSearchList::OnSetFocus(wxFocusEvent& event)
|
|
|
|
{
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupSearchList::OnKillFocus(wxFocusEvent& event)
|
|
|
|
{
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// SearchCtrl
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
SearchButton::SearchButton(wxWindow* parent) :
|
|
|
|
ScalableButton(parent, wxID_ANY, "search")
|
|
|
|
{
|
|
|
|
popup_win = new PopupSearchList(parent);
|
|
|
|
this->Bind(wxEVT_BUTTON, &SearchButton::PopupSearch, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchButton::PopupSearch(wxCommandEvent& e)
|
|
|
|
{
|
|
|
|
// popup_win->update_list(wxGetApp().sidebar().get_search_list().filters);
|
|
|
|
wxPoint pos = this->ClientToScreen(wxPoint(0, 0));
|
|
|
|
wxSize sz = wxSize(GUI::wxGetApp().em_unit()*40, -1);
|
|
|
|
pos.x -= sz.GetWidth();
|
|
|
|
pos.y += this->GetSize().y;
|
|
|
|
popup_win->Position(pos, sz);
|
|
|
|
popup_win->Popup();
|
|
|
|
e.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Slic3r::GUI
|