PrusaSlicer-NonPlainar/src/slic3r/GUI/Search.hpp

173 lines
5 KiB
C++
Raw Normal View History

#ifndef slic3r_SearchComboBox_hpp_
#define slic3r_SearchComboBox_hpp_
#include <vector>
#include <wx/panel.h>
#include <wx/sizer.h>
//#include <wx/bmpcbox.h>
#include <wx/popupwin.h>
#include <wx/listctrl.h>
2020-04-04 17:25:14 +00:00
#include <wx/combo.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
{
2020-04-07 14:22:03 +00:00
std::string search_line;
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; }
wxString label;
std::string opt_key;
wxString category;
Preset::Type type {Preset::TYPE_INVALID};
// wxString grope;
bool fuzzy_match_simple(char const *search_pattern) const;
bool fuzzy_match_simple(const wxString& search) const;
bool fuzzy_match_simple(const std::string &search) const;
bool fuzzy_match(char const *search_pattern, int &outScore);
bool fuzzy_match(const wxString &search, int &outScore);
bool fuzzy_match(const std::string &search, int &outScore);
};
std::vector<Option> options {};
struct Filter {
wxString label;
wxString marked_label;
size_t option_idx {0};
int outScore {0};
void get_label(const char** out_text) const;
void get_marked_label(const char** out_text) const;
};
std::vector<Filter> filters {};
void clear_options() { options.clear(); }
void clear_filters() { filters.clear(); }
void init(std::vector<SearchInput> input_values);
void append_options(DynamicPrintConfig* config, Preset::Type type, ConfigOptionMode mode);
2020-04-07 14:22:03 +00:00
bool apply_filters(const std::string& search, bool force = false);
void sort_options() {
std::sort(options.begin(), options.end(), [](const Option& o1, const Option& o2) {
return o1.label < o2.label; });
}
void sort_filters() {
std::sort(filters.begin(), filters.end(), [](const Filter& f1, const Filter& f2) {
return f1.outScore > f2.outScore; });
};
size_t options_size() const { return options.size(); }
size_t filters_size() const { return filters.size(); }
size_t size() const { return filters_size(); }
const Filter& operator[](const size_t pos) const noexcept { return filters[pos]; }
const Option& get_option(size_t pos_in_filter) const;
};
2020-04-04 17:25:14 +00:00
class SearchComboPopup : public wxListBox, public wxComboPopup
{
public:
// Initialize member variables
virtual void Init()
{
this->Bind(wxEVT_MOTION, &SearchComboPopup::OnMouseMove, this);
this->Bind(wxEVT_LEFT_UP, &SearchComboPopup::OnMouseClick, this);
}
2020-04-04 17:25:14 +00:00
// Create popup control
virtual bool Create(wxWindow* parent)
{
return wxListBox::Create(parent, 1, wxPoint(0, 0), wxDefaultSize);
}
// Return pointer to the created control
virtual wxWindow* GetControl() { return this; }
// Translate string into a list selection
virtual void 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;
}
// Get list selection as a string
virtual wxString GetStringValue() const
{
// we shouldn't change a combo control's string
return m_input_string;
}
// Do mouse hot-tracking (which is typical in list popups)
void OnMouseMove(wxMouseEvent& event)
{
wxPoint pt = wxGetMousePosition() - this->GetScreenPosition();
int selection = this->HitTest(pt);
wxListBox::Select(selection);
2020-04-04 17:25:14 +00:00
}
// On mouse left up, set the value and close the popup
void OnMouseClick(wxMouseEvent& WXUNUSED(event))
{
int selection = wxListBox::GetSelection();
SetSelection(wxNOT_FOUND);
wxCommandEvent event(wxEVT_LISTBOX, GetId());
event.SetInt(selection);
event.SetEventObject(this);
ProcessEvent(event);
2020-04-04 17:25:14 +00:00
Dismiss();
}
protected:
wxString m_input_string;
};
2020-04-07 18:34:09 +00:00
class SearchCtrl : public wxComboCtrl
{
2020-04-07 18:34:09 +00:00
SearchComboPopup* popupListBox {nullptr};
2020-04-04 17:25:14 +00:00
bool prevent_update{ false };
2020-04-04 17:25:14 +00:00
wxString default_string;
2020-04-07 14:22:03 +00:00
bool editing {false};
void PopupList(wxCommandEvent& event);
void OnInputText(wxCommandEvent& event);
2020-04-04 17:25:14 +00:00
void OnSelect(wxCommandEvent& event);
2020-04-07 14:22:03 +00:00
void OnLeftUpInTextCtrl(wxEvent& event);
2020-04-04 17:25:14 +00:00
public:
SearchCtrl(wxWindow* parent);
2020-04-07 18:34:09 +00:00
~SearchCtrl() {}
void set_search_line(const std::string& search_line);
void msw_rescale();
2020-04-04 17:25:14 +00:00
void update_list(std::vector<SearchOptions::Filter>& filters);
};
2020-04-04 17:25:14 +00:00
}}
#endif //slic3r_SearchComboBox_hpp_