From 32061b6772fb516efc1ba0e043d29f6da0005a48 Mon Sep 17 00:00:00 2001 From: "xin.zhang" Date: Tue, 6 May 2025 11:06:50 +0800 Subject: [PATCH] ENH: update the print options layout jira: [STUDIO-11972] Change-Id: Idb15ad586d6c517f6f65ea940719d3c99230ab60 (cherry picked from commit 572721071356ecb69eff4c89c19c5ec3e37aa12c) --- resources/images/print_options_bg.svg | 18 ++- src/slic3r/GUI/SelectMachine.cpp | 177 +++++++++++++++++++------- src/slic3r/GUI/SelectMachine.hpp | 49 ++++--- 3 files changed, 180 insertions(+), 64 deletions(-) diff --git a/resources/images/print_options_bg.svg b/resources/images/print_options_bg.svg index 12363aabd0..f3bfcbd02c 100644 --- a/resources/images/print_options_bg.svg +++ b/resources/images/print_options_bg.svg @@ -1,4 +1,16 @@ - - - + + + + + + + + + + + + + + + diff --git a/src/slic3r/GUI/SelectMachine.cpp b/src/slic3r/GUI/SelectMachine.cpp index cc5b5e4850..e7dec30d83 100644 --- a/src/slic3r/GUI/SelectMachine.cpp +++ b/src/slic3r/GUI/SelectMachine.cpp @@ -4547,8 +4547,6 @@ SelectMachineDialog::~SelectMachineDialog() m_printoption_title = new Label(this, title); m_printoption_title->SetFont(Label::Head_13); - //m_printoption_title->SetBackgroundColour(0xF8F8F8); - m_printoption_title->SetToolTip(tips); m_printoption_item = new PrintOptionItem(this, m_ops, param); m_printoption_item->SetFont(Label::Body_13); @@ -4560,6 +4558,8 @@ SelectMachineDialog::~SelectMachineDialog() SetSizer(sizer); Layout(); Fit(); + + update_tooltip(tips); } void PrintOption::OnPaint(wxPaintEvent &event) @@ -4628,6 +4628,7 @@ void PrintOption::update_options(std::vector ops, const wxString &tips) void PrintOption::update_tooltip(const wxString &tips) { if (m_printoption_title->GetToolTipText() != tips) { m_printoption_title->SetToolTip(tips); } + if (m_printoption_item->GetToolTipText() != tips) { m_printoption_item->SetToolTip(tips); } } std::string PrintOption::getValue() @@ -4648,68 +4649,152 @@ int PrintOption::getValueInt() } } -PrintOptionItem::PrintOptionItem(wxWindow *parent, std::vector ops, std::string param) - : ComboBox(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(parent->FromDIP(70), parent->FromDIP(24)), 0, nullptr, wxCB_READONLY) +#define PRINT_OPT_WIDTH FromDIP(44) +PrintOptionItem::PrintOptionItem(wxWindow* parent, std::vector ops, std::string param) + : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize) { - Bind(wxEVT_COMBOBOX, &PrintOptionItem::on_combobox_changed, this); +#ifdef __WINDOWS__ + SetDoubleBuffered(true); +#endif //__WINDOWS__ + + m_ops = ops; m_param = param; - update_options(ops); + SetBackgroundColour(PRINT_OPT_ITEM_BG_GRAY); + + Bind(wxEVT_PAINT, &PrintOptionItem::OnPaint, this); + auto width = ops.size() * PRINT_OPT_WIDTH + FromDIP(8); + auto height = FromDIP(22) + FromDIP(8); + SetMinSize(wxSize(width, height)); + SetMaxSize(wxSize(width, height)); + Bind(wxEVT_ENTER_WINDOW, [this](auto& e) { SetCursor(wxCURSOR_HAND); }); + Bind(wxEVT_LEAVE_WINDOW, [this](auto& e) { SetCursor(wxCURSOR_ARROW); }); + Bind(wxEVT_LEFT_DOWN, &PrintOptionItem::on_left_down, this); + + m_selected_bk = ScalableBitmap(this, "print_options_bg", 22); } -void PrintOptionItem::update_options(std::vector ops) { - if (m_ops != ops) { - m_ops = ops; - - ComboBox::Clear(); - selected_key.clear(); - for (const auto &entry : m_ops) { Append(entry.value); } - - Layout(); - Fit(); - } +void PrintOptionItem::OnPaint(wxPaintEvent& event) +{ + wxPaintDC dc(this); + doRender(dc); } -void PrintOptionItem::on_combobox_changed(wxCommandEvent &evt) { - const auto &new_key = get_key(evt.GetString()); - setValue(new_key); -} +void PrintOptionItem::render(wxDC& dc) +{ +#ifdef __WXMSW__ + wxSize size = GetSize(); + wxMemoryDC memdc; + wxBitmap bmp(size.x, size.y); + memdc.SelectObject(bmp); + memdc.Blit({ 0, 0 }, size, &dc, { 0, 0 }); -wxString PrintOptionItem::get_display_str(const std::string &key) const { - for (const auto &entry : m_ops) { - if (entry.key == key) { return entry.value; } + { + wxGCDC dc2(memdc); + doRender(dc2); } - return wxEmptyString; + memdc.SelectObject(wxNullBitmap); + dc.DrawBitmap(bmp, 0, 0); +#else + doRender(dc); +#endif } -std::string PrintOptionItem::get_key(const wxString &display_val) const { - for (const auto &entry : m_ops) { - if (entry.value == display_val) { return entry.key; } +void PrintOptionItem::on_left_down(wxMouseEvent& evt) +{ + if (!m_enable) + { + return; } - return std::string(); + auto pos = ClientToScreen(evt.GetPosition()); + auto rect = ClientToScreen(wxPoint(0, 0)); + auto select_size = GetSize().x / m_ops.size(); + + int i = 0; + for (const auto& entry : m_ops) + { + auto left_edge = rect.x + i * select_size; + auto right_edge = rect.x + (i + 1) * select_size; + + if (pos.x > left_edge && pos.x < right_edge) + { + selected_key = entry.key; + } + i++; + } + + wxCommandEvent event(EVT_SWITCH_PRINT_OPTION); + event.SetString(selected_key); + event.SetEventObject(GetParent()); + wxPostEvent(GetParent(), event); + + Refresh(); } -void PrintOptionItem::setValue(std::string value) { - if (selected_key != value) { - selected_key = value; - ComboBox::SetStringSelection(get_display_str(value)); +void PrintOptionItem::doRender(wxDC& dc) +{ + auto size = GetSize(); + dc.SetPen(wxPen(*wxTRANSPARENT_PEN)); + dc.SetBrush(GetBackgroundColour()); + dc.DrawRoundedRectangle(0, 0, size.x, size.y, FromDIP(5)); - if (!m_param.empty()) { - AppConfig *config = wxGetApp().app_config; - if (selected_key == "auto") { - config->set_str("print", m_param, "2"); - } else if (selected_key == "on") { - config->set_str("print", m_param, "1"); - } else if (selected_key == "off") { - config->set_str("print", m_param, "0"); - } + auto left = FromDIP(4); + + int selected = 0; + for (const auto& entry : m_ops) + { + if (entry.key == selected_key) + { + break; + } + selected++; + } + + /*selected*/ + auto selected_left = selected * PRINT_OPT_WIDTH + FromDIP(4); + dc.DrawBitmap(m_selected_bk.bmp(), selected_left, (size.y - m_selected_bk.GetBmpHeight()) / 2); + + for (auto it = m_ops.begin(); it != m_ops.end(); ++it) + { + auto text_key = it->key; + auto text_value = it->value; + + if (text_key == selected_key) + { + + const wxColour& clr = wxGetApp().dark_mode() ? StateColor::darkModeColorFor("#00AE42") : "#00AE42"; + dc.SetPen(wxPen(clr)); + dc.SetTextForeground(clr); + + dc.SetFont(::Label::Head_13); + auto text_size = dc.GetTextExtent(text_value); + auto text_left = left + (PRINT_OPT_WIDTH - text_size.x) / 2; + auto text_top = (size.y - text_size.y) / 2; + dc.DrawText(text_value, wxPoint(text_left, text_top)); + } + else + { + const wxColour& clr = wxGetApp().dark_mode() ? StateColor::darkModeColorFor(*wxBLACK) : *wxBLACK; + dc.SetPen(wxPen(clr)); + dc.SetTextForeground(clr); + + dc.SetFont(::Label::Body_13); + auto text_size = dc.GetTextExtent(text_value); + auto text_left = left + (PRINT_OPT_WIDTH - text_size.x) / 2; + auto text_top = (size.y - text_size.y) / 2; + dc.DrawText(text_value, wxPoint(text_left, text_top)); } - wxCommandEvent event(EVT_SWITCH_PRINT_OPTION); - event.SetString(selected_key); - event.SetEventObject(GetParent()); - wxPostEvent(GetParent(), event); + left += PRINT_OPT_WIDTH; + } +} + +void PrintOptionItem::setValue(std::string value) +{ + if (selected_key != value) { + selected_key = value; + Refresh(); } } diff --git a/src/slic3r/GUI/SelectMachine.hpp b/src/slic3r/GUI/SelectMachine.hpp index 2b74aaae97..c9c8c44776 100644 --- a/src/slic3r/GUI/SelectMachine.hpp +++ b/src/slic3r/GUI/SelectMachine.hpp @@ -137,32 +137,51 @@ struct POItem bool operator==(const POItem &other) const { return key == other.key && value == other.value; } }; -class PrintOptionItem : public ComboBox +class PrintOptionItem : public wxPanel { - std::vector m_ops; - std::string selected_key; - std::string m_param; - public: - PrintOptionItem(wxWindow *parent, std::vector ops, std::string param = ""); + PrintOptionItem(wxWindow* parent, std::vector ops, std::string param = ""); ~PrintOptionItem() {}; public: - bool Enable(bool enable) override { return ComboBox::Enable(enable); } - void setValue(std::string value); std::string getValue() const { return selected_key; } + void update_options(std::vector ops) { + if (m_ops != ops) + { + m_ops = ops; + selected_key = ""; + auto width = ops.size() * FromDIP(56) + FromDIP(8); + auto height = FromDIP(22) + FromDIP(8); + SetMinSize(wxSize(width, height)); + SetMaxSize(wxSize(width, height)); + Refresh(); + } + }; - void msw_rescale() { ComboBox::Rescale();}; - void update_options(std::vector ops); + void enable(bool able) { + if (m_enable != able) + { + m_enable = able; + Refresh(); + } + } - bool CanBeFocused() const override { return false; } + void msw_rescale() { m_selected_bk.msw_rescale(); Refresh(); }; private: - void on_combobox_changed(wxCommandEvent &evt); + void OnPaint(wxPaintEvent& event); + void render(wxDC& dc); + void on_left_down(wxMouseEvent& evt); + void doRender(wxDC& dc); - wxString get_display_str(const std::string& key) const; - std::string get_key(const wxString &display_val) const; +private: + ScalableBitmap m_selected_bk; + std::vector m_ops; + std::string selected_key; + std::string m_param; + + bool m_enable = true; }; class PrintOption : public wxPanel @@ -178,7 +197,7 @@ public: ~PrintOption(){}; public: - void enable(bool en) { m_printoption_item->Enable(en); } + void enable(bool en) { m_printoption_item->enable(en); } void setValue(std::string value); std::string getValue();