2020-04-03 08:01:23 +00:00
|
|
|
#include "Search.hpp"
|
2020-03-13 21:37:18 +00:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <string>
|
2020-05-01 06:57:24 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2020-03-13 21:37:18 +00:00
|
|
|
#include <boost/optional.hpp>
|
2020-05-01 06:57:24 +00:00
|
|
|
#include <boost/nowide/convert.hpp>
|
2020-03-13 21:37:18 +00:00
|
|
|
|
|
|
|
#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-05-04 19:22:56 +00:00
|
|
|
static const std::vector<std::wstring>& NameByType()
|
2020-03-31 20:46:12 +00:00
|
|
|
{
|
2020-05-04 19:22:56 +00:00
|
|
|
static std::vector<std::wstring> data;
|
|
|
|
if (data.empty()) {
|
|
|
|
data.assign(Preset::TYPE_COUNT, std::wstring());
|
|
|
|
data[Preset::TYPE_PRINT ] = _L("Print" ).ToStdWstring();
|
|
|
|
data[Preset::TYPE_FILAMENT ] = _L("Filament" ).ToStdWstring();
|
|
|
|
data[Preset::TYPE_SLA_MATERIAL ] = _L("Material" ).ToStdWstring();
|
|
|
|
data[Preset::TYPE_SLA_PRINT ] = _L("Print" ).ToStdWstring();
|
|
|
|
data[Preset::TYPE_PRINTER ] = _L("Printer" ).ToStdWstring();
|
|
|
|
};
|
|
|
|
return data;
|
2020-03-13 21:37:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 18:45:57 +00:00
|
|
|
static char marker_by_type(Preset::Type type, PrinterTechnology pt)
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case Preset::TYPE_PRINT:
|
|
|
|
case Preset::TYPE_SLA_PRINT:
|
|
|
|
return ImGui::PrintIconMarker;
|
|
|
|
case Preset::TYPE_FILAMENT:
|
|
|
|
return ImGui::FilamentIconMarker;
|
|
|
|
case Preset::TYPE_SLA_MATERIAL:
|
|
|
|
return ImGui::MaterialIconMarker;
|
|
|
|
case Preset::TYPE_PRINTER:
|
|
|
|
return pt == ptSLA ? ImGui::PrinterSlaIconMarker : ImGui::PrinterIconMarker;
|
|
|
|
default:
|
|
|
|
return ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-30 10:03:49 +00:00
|
|
|
*label_ = marked_label.c_str();
|
|
|
|
*tooltip_ = tooltip.c_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;
|
2020-05-04 19:22:56 +00:00
|
|
|
wxString suffix_local;
|
|
|
|
if (gc.category == "Machine limits") {
|
2020-04-16 07:59:12 +00:00
|
|
|
suffix = opt_key.back()=='1' ? L("Stealth") : L("Normal");
|
2020-05-04 19:22:56 +00:00
|
|
|
suffix_local = " " + _(suffix);
|
|
|
|
suffix = " " + suffix;
|
|
|
|
}
|
2020-04-16 07:59:12 +00:00
|
|
|
|
|
|
|
if (!label.IsEmpty())
|
2020-05-01 06:57:24 +00:00
|
|
|
options.emplace_back(Option{ boost::nowide::widen(opt_key), type,
|
2020-05-04 19:22:56 +00:00
|
|
|
(label + suffix).ToStdWstring(), (_(label) + suffix_local).ToStdWstring(),
|
2020-05-01 06:57:24 +00:00
|
|
|
gc.group.ToStdWstring(), _(gc.group).ToStdWstring(),
|
|
|
|
gc.category.ToStdWstring(), _(gc.category).ToStdWstring() });
|
2020-04-16 07:59:12 +00:00
|
|
|
};
|
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)
|
2020-05-06 18:45:57 +00:00
|
|
|
// ! It's very important to use "#". opt_key#n is a real option key used in GroupAndCategory
|
|
|
|
emplace(opt_key + "#" + std::to_string(i), label);
|
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
|
2020-05-01 06:57:24 +00:00
|
|
|
static std::wstring mark_string(const std::wstring &str, const std::vector<uint16_t> &matches)
|
2020-03-31 20:46:12 +00:00
|
|
|
{
|
2020-05-01 06:57:24 +00:00
|
|
|
std::wstring out;
|
|
|
|
if (matches.empty())
|
|
|
|
out = str;
|
|
|
|
else {
|
|
|
|
out.reserve(str.size() * 2);
|
|
|
|
if (matches.front() > 0)
|
|
|
|
out += str.substr(0, matches.front());
|
|
|
|
for (size_t i = 0;;) {
|
|
|
|
// Find the longest string of successive indices.
|
|
|
|
size_t j = i + 1;
|
|
|
|
while (j < matches.size() && matches[j] == matches[j - 1] + 1)
|
|
|
|
++ j;
|
|
|
|
out += ImGui::ColorMarkerStart;
|
|
|
|
out += str.substr(matches[i], matches[j - 1] - matches[i] + 1);
|
|
|
|
out += ImGui::ColorMarkerEnd;
|
|
|
|
if (j == matches.size()) {
|
|
|
|
out += str.substr(matches[j - 1] + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out += str.substr(matches[j - 1] + 1, matches[j] - matches[j - 1] - 1);
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
2020-03-31 20:46:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 09:01:14 +00:00
|
|
|
bool OptionsSearcher::search()
|
|
|
|
{
|
|
|
|
return search(search_line, true);
|
|
|
|
}
|
|
|
|
|
2020-05-04 19:22:56 +00:00
|
|
|
static bool fuzzy_match(const std::wstring &search_pattern, const std::wstring &label, int& out_score, std::vector<uint16_t> &out_matches)
|
|
|
|
{
|
|
|
|
uint16_t matches[fts::max_matches + 1]; // +1 for the stopper
|
|
|
|
int score;
|
|
|
|
if (fts::fuzzy_match(search_pattern.c_str(), label.c_str(), score, matches)) {
|
|
|
|
size_t cnt = 0;
|
|
|
|
for (; matches[cnt] != fts::stopper; ++cnt);
|
|
|
|
out_matches.assign(matches, matches + cnt);
|
|
|
|
out_score = score;
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-05-04 19:22:56 +00:00
|
|
|
std::wstring sep = L" : ";
|
2020-03-31 20:46:12 +00:00
|
|
|
|
2020-05-06 18:45:57 +00:00
|
|
|
auto get_label = [this, &sep](const Option& opt)
|
2020-04-14 15:54:15 +00:00
|
|
|
{
|
2020-05-04 19:22:56 +00:00
|
|
|
std::wstring out;
|
2020-05-06 18:45:57 +00:00
|
|
|
out += marker_by_type(opt.type, printer_technology);
|
2020-05-04 19:22:56 +00:00
|
|
|
const std::wstring *prev = nullptr;
|
|
|
|
for (const std::wstring * const s : {
|
|
|
|
view_params.category ? &opt.category_local : nullptr,
|
|
|
|
view_params.group ? &opt.group_local : nullptr,
|
|
|
|
&opt.label_local })
|
|
|
|
if (s != nullptr && (prev == nullptr || *prev != *s)) {
|
2020-05-06 18:45:57 +00:00
|
|
|
// if (! out.empty())
|
|
|
|
if (out.size()>2)
|
2020-05-04 19:22:56 +00:00
|
|
|
out += sep;
|
|
|
|
out += *s;
|
|
|
|
prev = s;
|
|
|
|
}
|
|
|
|
return out;
|
2020-04-14 15:54:15 +00:00
|
|
|
};
|
|
|
|
|
2020-05-06 18:45:57 +00:00
|
|
|
auto get_label_english = [this, &sep](const Option& opt)
|
2020-04-20 19:46:16 +00:00
|
|
|
{
|
2020-05-04 19:22:56 +00:00
|
|
|
std::wstring out;
|
2020-05-06 18:45:57 +00:00
|
|
|
out += marker_by_type(opt.type, printer_technology);
|
2020-05-04 19:22:56 +00:00
|
|
|
const std::wstring*prev = nullptr;
|
|
|
|
for (const std::wstring * const s : {
|
|
|
|
view_params.category ? &opt.category : nullptr,
|
|
|
|
view_params.group ? &opt.group : nullptr,
|
|
|
|
&opt.label })
|
|
|
|
if (s != nullptr && (prev == nullptr || *prev != *s)) {
|
2020-05-06 18:45:57 +00:00
|
|
|
// if (! out.empty())
|
|
|
|
if (out.size()>2)
|
2020-05-04 19:22:56 +00:00
|
|
|
out += sep;
|
|
|
|
out += *s;
|
|
|
|
prev = s;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2020-05-06 18:45:57 +00:00
|
|
|
auto get_tooltip = [this, &sep](const Option& opt)
|
2020-05-04 19:22:56 +00:00
|
|
|
{
|
2020-05-06 18:45:57 +00:00
|
|
|
return marker_by_type(opt.type, printer_technology) +
|
2020-04-20 19:46:16 +00:00
|
|
|
opt.category_local + sep +
|
|
|
|
opt.group_local + sep + opt.label_local;
|
|
|
|
};
|
|
|
|
|
2020-05-04 19:22:56 +00:00
|
|
|
std::vector<uint16_t> matches, matches2;
|
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-30 10:03:49 +00:00
|
|
|
std::string label = into_u8(get_label(opt));
|
2020-05-04 19:22:56 +00:00
|
|
|
found.emplace_back(FoundOption{ label, label, boost::nowide::narrow(get_tooltip(opt)), i, 0 });
|
2020-03-31 20:46:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-04 19:22:56 +00:00
|
|
|
std::wstring wsearch = boost::nowide::widen(search);
|
|
|
|
boost::trim_left(wsearch);
|
|
|
|
std::wstring label = get_label(opt);
|
|
|
|
std::wstring label_english = get_label_english(opt);
|
|
|
|
int score = std::numeric_limits<int>::min();
|
|
|
|
int score2;
|
|
|
|
matches.clear();
|
|
|
|
fuzzy_match(wsearch, label, score, matches);
|
|
|
|
if (fuzzy_match(wsearch, opt.opt_key, score2, matches2) && score2 > score) {
|
|
|
|
for (fts::pos_type &pos : matches2)
|
|
|
|
pos += label.size() + 1;
|
|
|
|
label += L"(" + opt.opt_key + L")";
|
|
|
|
append(matches, matches2);
|
|
|
|
score = score2;
|
|
|
|
}
|
2020-05-11 20:00:26 +00:00
|
|
|
if (view_params.english && fuzzy_match(wsearch, label_english, score2, matches2) && score2 > score) {
|
2020-05-04 19:22:56 +00:00
|
|
|
label = std::move(label_english);
|
|
|
|
matches = std::move(matches2);
|
|
|
|
score = score2;
|
|
|
|
}
|
|
|
|
if (score > std::numeric_limits<int>::min()) {
|
2020-05-11 20:00:26 +00:00
|
|
|
label = mark_string(label, matches);
|
|
|
|
label += L" [" + std::to_wstring(score) + L"]";// add score value
|
2020-05-04 19:22:56 +00:00
|
|
|
std::string label_u8 = into_u8(label);
|
|
|
|
std::string label_plain = label_u8;
|
|
|
|
boost::erase_all(label_plain, std::string(1, char(ImGui::ColorMarkerStart)));
|
|
|
|
boost::erase_all(label_plain, std::string(1, char(ImGui::ColorMarkerEnd)));
|
|
|
|
found.emplace_back(FoundOption{ label_plain, label_u8, boost::nowide::narrow(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-05-04 19:22:56 +00:00
|
|
|
|
2020-04-24 09:01:14 +00:00
|
|
|
if (search_line != search)
|
|
|
|
search_line = search;
|
|
|
|
|
2020-04-07 14:22:03 +00:00
|
|
|
return true;
|
2020-03-28 18:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
OptionsSearcher::OptionsSearcher()
|
|
|
|
{
|
|
|
|
search_dialog = new SearchDialog(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionsSearcher::~OptionsSearcher()
|
|
|
|
{
|
|
|
|
if (search_dialog)
|
|
|
|
search_dialog->Destroy();
|
|
|
|
}
|
|
|
|
|
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);
|
2020-04-30 10:03:49 +00:00
|
|
|
if (n >= 0 && n < int(wxListBox::GetCount()))
|
2020-04-14 15:54:15 +00:00
|
|
|
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-16 13:49:40 +00:00
|
|
|
|
|
|
|
//------------------------------------------
|
2020-04-22 13:46:23 +00:00
|
|
|
// SearchDialog
|
2020-04-16 13:49:40 +00:00
|
|
|
//------------------------------------------
|
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
SearchDialog::SearchDialog(OptionsSearcher* searcher)
|
|
|
|
: GUI::DPIDialog(NULL, wxID_ANY, _L("Search"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER),
|
|
|
|
searcher(searcher)
|
2020-04-16 13:49:40 +00:00
|
|
|
{
|
2020-04-22 13:46:23 +00:00
|
|
|
SetFont(GUI::wxGetApp().normal_font());
|
|
|
|
wxColour bgr_clr = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
|
|
|
|
SetBackgroundColour(bgr_clr);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
default_string = _L("Type here to search");
|
|
|
|
int border = 10;
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
search_line = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
// wxWANTS_CHARS style is neede for process Enter key press
|
|
|
|
search_list = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxSize(em_unit() * 40, em_unit() * 30), 0, NULL, wxWANTS_CHARS);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
wxBoxSizer* check_sizer = new wxBoxSizer(wxHORIZONTAL);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
check_category = new wxCheckBox(this, wxID_ANY, _L("Category"));
|
|
|
|
check_group = new wxCheckBox(this, wxID_ANY, _L("Group"));
|
2020-05-07 16:39:38 +00:00
|
|
|
if (GUI::wxGetApp().is_localized())
|
|
|
|
check_english = new wxCheckBox(this, wxID_ANY, _L("Search in English"));
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
wxStdDialogButtonSizer* cancel_btn = this->CreateStdDialogButtonSizer(wxCANCEL);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
check_sizer->Add(check_category, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, border);
|
2020-05-07 16:39:38 +00:00
|
|
|
check_sizer->Add(check_group, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, border);
|
|
|
|
if (GUI::wxGetApp().is_localized())
|
|
|
|
check_sizer->Add(check_english, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, border);
|
2020-04-22 13:46:23 +00:00
|
|
|
check_sizer->AddStretchSpacer(border);
|
|
|
|
check_sizer->Add(cancel_btn, 0, wxALIGN_CENTER_VERTICAL);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
topSizer->Add(search_line, 0, wxEXPAND | wxLEFT | wxTOP | wxRIGHT, border);
|
|
|
|
topSizer->Add(search_list, 1, wxEXPAND | wxLEFT | wxTOP | wxRIGHT, border);
|
|
|
|
topSizer->Add(check_sizer, 0, wxEXPAND | wxALL, border);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
search_line->Bind(wxEVT_TEXT, &SearchDialog::OnInputText, this);
|
|
|
|
search_line->Bind(wxEVT_LEFT_UP, &SearchDialog::OnLeftUpInTextCtrl, this);
|
|
|
|
// process wxEVT_KEY_DOWN to navigate inside search_list, if ArrowUp/Down was pressed
|
|
|
|
search_line->Bind(wxEVT_KEY_DOWN,&SearchDialog::OnKeyDown, this);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
search_list->Bind(wxEVT_MOTION, &SearchDialog::OnMouseMove, this);
|
|
|
|
search_list->Bind(wxEVT_LEFT_UP, &SearchDialog::OnMouseClick, this);
|
|
|
|
search_list->Bind(wxEVT_KEY_DOWN,&SearchDialog::OnKeyDown, this);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-05-07 16:39:38 +00:00
|
|
|
if (GUI::wxGetApp().is_localized())
|
|
|
|
check_english ->Bind(wxEVT_CHECKBOX, &SearchDialog::OnCheck, this);
|
2020-04-22 13:46:23 +00:00
|
|
|
check_category->Bind(wxEVT_CHECKBOX, &SearchDialog::OnCheck, this);
|
|
|
|
check_group ->Bind(wxEVT_CHECKBOX, &SearchDialog::OnCheck, this);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
this->Bind(wxEVT_LISTBOX, &SearchDialog::OnSelect, this);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
SetSizer(topSizer);
|
|
|
|
topSizer->SetSizeHints(this);
|
|
|
|
}
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
void SearchDialog::Popup(wxPoint position /*= wxDefaultPosition*/)
|
|
|
|
{
|
|
|
|
const std::string& line = searcher->search_string();
|
|
|
|
search_line->SetValue(line.empty() ? default_string : from_u8(line));
|
|
|
|
search_line->SetFocus();
|
|
|
|
search_line->SelectAll();
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
update_list();
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
const OptionViewParameters& params = searcher->view_params;
|
|
|
|
check_category->SetValue(params.category);
|
|
|
|
check_group->SetValue(params.group);
|
2020-05-06 18:45:57 +00:00
|
|
|
check_english->SetValue(params.english);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
this->SetPosition(position);
|
|
|
|
this->ShowModal();
|
|
|
|
}
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
void SearchDialog::ProcessSelection(int selection)
|
|
|
|
{
|
|
|
|
if (selection < 0)
|
|
|
|
return;
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
GUI::wxGetApp().sidebar().jump_to_option(selection);
|
|
|
|
this->EndModal(wxID_CLOSE);
|
|
|
|
}
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
void SearchDialog::OnInputText(wxCommandEvent&)
|
|
|
|
{
|
|
|
|
search_line->SetInsertionPointEnd();
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
wxString input_string = search_line->GetValue();
|
|
|
|
if (input_string == default_string)
|
|
|
|
input_string.Clear();
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-24 09:01:14 +00:00
|
|
|
searcher->search(into_u8(input_string));
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
update_list();
|
|
|
|
}
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
void SearchDialog::OnLeftUpInTextCtrl(wxEvent& event)
|
|
|
|
{
|
|
|
|
if (search_line->GetValue() == default_string)
|
|
|
|
search_line->SetValue("");
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
event.Skip();
|
2020-04-16 13:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
void SearchDialog::OnMouseMove(wxMouseEvent& event)
|
2020-04-16 13:49:40 +00:00
|
|
|
{
|
2020-04-22 13:46:23 +00:00
|
|
|
wxPoint pt = wxGetMousePosition() - search_list->GetScreenPosition();
|
|
|
|
int selection = search_list->HitTest(pt);
|
|
|
|
search_list->Select(selection);
|
2020-04-16 13:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
void SearchDialog::OnMouseClick(wxMouseEvent&)
|
2020-04-16 13:49:40 +00:00
|
|
|
{
|
2020-04-22 13:46:23 +00:00
|
|
|
int selection = search_list->GetSelection();
|
|
|
|
search_list->SetSelection(wxNOT_FOUND);
|
|
|
|
|
|
|
|
wxCommandEvent event(wxEVT_LISTBOX, search_list->GetId());
|
|
|
|
event.SetInt(selection);
|
|
|
|
event.SetEventObject(search_list);
|
|
|
|
ProcessEvent(event);
|
2020-04-16 13:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
void SearchDialog::OnSelect(wxCommandEvent& event)
|
2020-04-16 13:49:40 +00:00
|
|
|
{
|
2020-04-22 13:46:23 +00:00
|
|
|
int selection = event.GetSelection();
|
|
|
|
ProcessSelection(selection);
|
2020-04-16 13:49:40 +00:00
|
|
|
}
|
2020-04-22 13:46:23 +00:00
|
|
|
|
|
|
|
void SearchDialog::update_list()
|
2020-04-16 13:49:40 +00:00
|
|
|
{
|
2020-04-22 13:46:23 +00:00
|
|
|
search_list->Clear();
|
|
|
|
|
|
|
|
const std::vector<FoundOption>& filters = searcher->found_options();
|
|
|
|
for (const FoundOption& item : filters)
|
2020-05-06 18:45:57 +00:00
|
|
|
search_list->Append(from_u8(item.label).Remove(0, 1));
|
2020-04-16 13:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
void SearchDialog::OnKeyDown(wxKeyEvent& event)
|
2020-04-16 13:49:40 +00:00
|
|
|
{
|
2020-04-22 13:46:23 +00:00
|
|
|
int key = event.GetKeyCode();
|
|
|
|
|
|
|
|
// change selected item in the list
|
|
|
|
if (key == WXK_UP || key == WXK_DOWN)
|
|
|
|
{
|
|
|
|
int selection = search_list->GetSelection();
|
|
|
|
|
|
|
|
if (key == WXK_UP && selection > 0)
|
|
|
|
selection--;
|
|
|
|
if (key == WXK_DOWN && selection < int(search_list->GetCount() - 1))
|
|
|
|
selection++;
|
|
|
|
|
|
|
|
search_list->Select(selection);
|
|
|
|
// This function could be called from search_line,
|
|
|
|
// So, for the next correct navigation, set focus on the search_list
|
|
|
|
search_list->SetFocus();
|
|
|
|
}
|
|
|
|
// process "Enter" pressed
|
|
|
|
else if (key == WXK_NUMPAD_ENTER || key == WXK_RETURN)
|
|
|
|
ProcessSelection(search_list->GetSelection());
|
|
|
|
else
|
|
|
|
event.Skip(); // !Needed to have EVT_CHAR generated as well
|
2020-04-16 13:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
void SearchDialog::OnCheck(wxCommandEvent& event)
|
2020-04-16 13:49:40 +00:00
|
|
|
{
|
2020-04-22 13:46:23 +00:00
|
|
|
OptionViewParameters& params = searcher->view_params;
|
2020-05-06 18:45:57 +00:00
|
|
|
params.english = check_english->GetValue();
|
2020-04-22 13:46:23 +00:00
|
|
|
params.category = check_category->GetValue();
|
|
|
|
params.group = check_group->GetValue();
|
|
|
|
|
2020-04-24 09:01:14 +00:00
|
|
|
searcher->search();
|
2020-04-22 13:46:23 +00:00
|
|
|
update_list();
|
2020-04-16 13:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
void SearchDialog::on_dpi_changed(const wxRect& suggested_rect)
|
2020-04-16 13:49:40 +00:00
|
|
|
{
|
2020-04-22 13:46:23 +00:00
|
|
|
const int& em = em_unit();
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
msw_buttons_rescale(this, em, { wxID_CANCEL });
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
const wxSize& size = wxSize(40 * em, 30 * em);
|
|
|
|
SetMinSize(size);
|
2020-04-16 13:49:40 +00:00
|
|
|
|
2020-04-22 13:46:23 +00:00
|
|
|
Fit();
|
|
|
|
Refresh();
|
2020-04-16 13:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-13 15:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Slic3r::GUI
|