Files
OrcaSlicer-KX/src/slic3r/GUI/Widgets/ImageSwitchButton.cpp
SoftFever 38d5ccb29f Merge branch 'master-remote' into feature/1.5
Signed-off-by: SoftFever <softfeverever@gmail.com>

# Conflicts:
#	bbl/i18n/BambuStudio.pot
#	bbl/i18n/de/BambuStudio_de.po
#	bbl/i18n/en/BambuStudio_en.po
#	bbl/i18n/es/BambuStudio_es.po
#	bbl/i18n/fr/BambuStudio_fr.po
#	bbl/i18n/hu/BambuStudio_hu.po
#	bbl/i18n/it/BambuStudio_it.po
#	bbl/i18n/nl/BambuStudio_nl.po
#	bbl/i18n/sv/BambuStudio_sv.po
#	bbl/i18n/zh_cn/BambuStudio_zh_CN.po
#	deps/Boost/Boost.cmake
#	deps/wxWidgets/wxWidgets.cmake
#	resources/config.json
#	resources/i18n/de/BambuStudio.mo
#	resources/i18n/en/BambuStudio.mo
#	resources/i18n/es/BambuStudio.mo
#	resources/i18n/fr/BambuStudio.mo
#	resources/i18n/hu/BambuStudio.mo
#	resources/i18n/it/BambuStudio.mo
#	resources/i18n/nl/BambuStudio.mo
#	resources/i18n/sv/BambuStudio.mo
#	resources/i18n/zh_cn/BambuStudio.mo
#	resources/images/tips_arrow.svg
#	resources/profiles/Anycubic.json
#	resources/profiles/Anycubic/filament/Anycubic Generic ABS.json
#	resources/profiles/Anycubic/filament/Anycubic Generic ASA.json
#	resources/profiles/Anycubic/filament/Anycubic Generic PA-CF.json
#	resources/profiles/Anycubic/filament/Anycubic Generic PA.json
#	resources/profiles/Anycubic/filament/Anycubic Generic PC.json
#	resources/profiles/Anycubic/filament/Anycubic Generic PETG.json
#	resources/profiles/Anycubic/filament/Anycubic Generic PLA-CF.json
#	resources/profiles/Anycubic/filament/Anycubic Generic PLA.json
#	resources/profiles/Anycubic/filament/Anycubic Generic PVA.json
#	resources/profiles/Anycubic/filament/Anycubic Generic TPU.json
#	resources/profiles/Anycubic/filament/fdm_filament_common.json
#	resources/profiles/Anycubic/machine/Anycubic 4Max Pro 0.4 nozzle.json
#	resources/profiles/Anycubic/machine/Anycubic 4Max Pro.json
#	resources/profiles/Anycubic/process/0.20mm Standard @4MaxPro.json
#	resources/profiles/Anycubic/process/fdm_process_common.json
#	resources/profiles/BBL.json
#	resources/profiles/BBL/machine/Bambu Lab P1P 0.2 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab P1P 0.4 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab P1P 0.6 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab P1P 0.8 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab X1 0.2 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab X1 0.4 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab X1 0.6 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab X1 0.8 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab X1 Carbon 0.2 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab X1 Carbon 0.4 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab X1 Carbon 0.6 nozzle.json
#	resources/profiles/BBL/machine/Bambu Lab X1 Carbon 0.8 nozzle.json
#	resources/profiles/BBL/machine/fdm_bbl_3dp_001_common.json
#	resources/profiles/Voron.json
#	resources/web/data/text.js
#	resources/web/image/printer/Anycubic 4Max Pro_cover.png
#	src/BambuStudio.cpp
#	src/libslic3r/GCode.cpp
#	src/libslic3r/GCode.hpp
#	src/libslic3r/GCode/GCodeProcessor.cpp
#	src/libslic3r/GCodeWriter.hpp
#	src/libslic3r/PerimeterGenerator.cpp
#	src/libslic3r/PresetBundle.cpp
#	src/libslic3r/Print.cpp
#	src/libslic3r/Print.hpp
#	src/libslic3r/PrintConfig.cpp
#	src/libslic3r/PrintConfig.hpp
#	src/libslic3r/PrintObject.cpp
#	src/slic3r/GUI/AMSMaterialsSetting.cpp
#	src/slic3r/GUI/AMSMaterialsSetting.hpp
#	src/slic3r/GUI/AmsMappingPopup.cpp
#	src/slic3r/GUI/AmsMappingPopup.hpp
#	src/slic3r/GUI/Auxiliary.cpp
#	src/slic3r/GUI/BackgroundSlicingProcess.cpp
#	src/slic3r/GUI/ConfigManipulation.cpp
#	src/slic3r/GUI/DeviceManager.cpp
#	src/slic3r/GUI/DeviceManager.hpp
#	src/slic3r/GUI/ExtrusionCalibration.cpp
#	src/slic3r/GUI/GCodeViewer.cpp
#	src/slic3r/GUI/GCodeViewer.hpp
#	src/slic3r/GUI/GUI_App.cpp
#	src/slic3r/GUI/IMSlider.cpp
#	src/slic3r/GUI/Jobs/PrintJob.cpp
#	src/slic3r/GUI/Jobs/PrintJob.hpp
#	src/slic3r/GUI/Jobs/SendJob.cpp
#	src/slic3r/GUI/Jobs/SendJob.hpp
#	src/slic3r/GUI/MainFrame.cpp
#	src/slic3r/GUI/MainFrame.hpp
#	src/slic3r/GUI/MediaPlayCtrl.cpp
#	src/slic3r/GUI/OptionsGroup.cpp
#	src/slic3r/GUI/PhysicalPrinterDialog.cpp
#	src/slic3r/GUI/Plater.cpp
#	src/slic3r/GUI/PrintHostDialogs.cpp
#	src/slic3r/GUI/Printer/BambuTunnel.h
#	src/slic3r/GUI/Printer/PrinterFileSystem.cpp
#	src/slic3r/GUI/Printer/gstbambusrc.c
#	src/slic3r/GUI/Printer/gstbambusrc.h
#	src/slic3r/GUI/ReleaseNote.cpp
#	src/slic3r/GUI/ReleaseNote.hpp
#	src/slic3r/GUI/SelectMachine.cpp
#	src/slic3r/GUI/SendToPrinter.cpp
#	src/slic3r/GUI/SetBedTypeDialog.cpp
#	src/slic3r/GUI/StatusPanel.cpp
#	src/slic3r/GUI/StatusPanel.hpp
#	src/slic3r/GUI/Tab.cpp
#	src/slic3r/GUI/Widgets/AMSControl.cpp
#	src/slic3r/GUI/Widgets/AMSControl.hpp
#	src/slic3r/GUI/Widgets/ImageSwitchButton.cpp
#	src/slic3r/GUI/Widgets/Label.cpp
#	src/slic3r/GUI/WipeTowerDialog.cpp
#	src/slic3r/Utils/Process.cpp
#	src/slic3r/Utils/bambu_networking.hpp
#	version.inc
2023-03-08 00:08:26 +08:00

371 lines
9.6 KiB
C++

#include "ImageSwitchButton.hpp"
#include "Label.hpp"
#include "StaticBox.hpp"
#include "../wxExtensions.hpp"
#include <wx/dcgraph.h>
BEGIN_EVENT_TABLE(ImageSwitchButton, StaticBox)
EVT_LEFT_DOWN(ImageSwitchButton::mouseDown)
EVT_ENTER_WINDOW(ImageSwitchButton::mouseEnterWindow)
EVT_LEAVE_WINDOW(ImageSwitchButton::mouseLeaveWindow)
EVT_LEFT_UP(ImageSwitchButton::mouseReleased)
EVT_PAINT(ImageSwitchButton::paintEvent)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(FanSwitchButton, StaticBox)
EVT_LEFT_DOWN(FanSwitchButton::mouseDown)
EVT_ENTER_WINDOW(FanSwitchButton::mouseEnterWindow)
EVT_LEAVE_WINDOW(FanSwitchButton::mouseLeaveWindow)
EVT_LEFT_UP(FanSwitchButton::mouseReleased)
EVT_PAINT(FanSwitchButton::paintEvent)
END_EVENT_TABLE()
static const wxColour DEFAULT_HOVER_COL = wxColour(0, 150, 136);
static const wxColour DEFAULT_PRESS_COL = wxColour(238, 238, 238);
ImageSwitchButton::ImageSwitchButton(wxWindow *parent, ScalableBitmap &img_on, ScalableBitmap &img_off, long style)
: text_color(std::make_pair(0x6B6B6B, (int) StateColor::Disabled), std::make_pair(*wxBLACK, (int) StateColor::Normal))
{
radius = 0;
m_padding = 0;
m_on = img_on;
m_off = img_off;
background_color = StateColor(std::make_pair(*wxWHITE, (int) StateColor::Disabled), std::make_pair(DEFAULT_PRESS_COL, (int) StateColor::Pressed),
std::make_pair(*wxWHITE, (int) StateColor::Normal));
border_color = StateColor(std::make_pair(*wxWHITE, (int) StateColor::Disabled), std::make_pair(DEFAULT_HOVER_COL, (int) StateColor::Focused),
std::make_pair(DEFAULT_HOVER_COL, (int) StateColor::Hovered), std::make_pair(*wxWHITE, (int) StateColor::Normal));
StaticBox::Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style);
messureSize();
Refresh();
}
void ImageSwitchButton::SetLabels(wxString const &lbl_on, wxString const &lbl_off)
{
labels[0] = lbl_on;
labels[1] = lbl_off;
auto fina_txt = GetValue() ? labels[0] : labels[1];
SetToolTip(fina_txt);
messureSize();
Refresh();
}
void ImageSwitchButton::SetImages(ScalableBitmap &img_on, ScalableBitmap &img_off)
{
m_on = img_on;
m_off = img_off;
messureSize();
Refresh();
}
void ImageSwitchButton::SetTextColor(StateColor const &color)
{
text_color = color;
state_handler.update_binds();
messureSize();
Refresh();
}
void ImageSwitchButton::SetValue(bool value)
{
m_on_off = value;
messureSize();
Refresh();
}
void ImageSwitchButton::SetPadding(int padding)
{
m_padding = padding;
messureSize();
Refresh();
}
void ImageSwitchButton::messureSize()
{
wxClientDC dc(this);
dc.SetFont(GetFont());
textSize = dc.GetTextExtent(GetValue() ? labels[0] : labels[1]);
}
void ImageSwitchButton::paintEvent(wxPaintEvent &evt)
{
wxPaintDC dc(this);
render(dc);
}
void ImageSwitchButton::render(wxDC& dc)
{
StaticBox::render(dc);
int states = state_handler.states();
wxSize size = GetSize();
wxSize szIcon;
wxSize szContent = textSize;
ScalableBitmap &icon = GetValue() ? m_on : m_off;
int content_height = icon.GetBmpHeight() + textSize.y + m_padding;
wxPoint pt = wxPoint((size.x - icon.GetBmpWidth()) / 2, (size.y - content_height) / 2);
if (icon.bmp().IsOk()) {
dc.DrawBitmap(icon.bmp(), pt);
pt.y += m_padding + icon.GetBmpHeight();
}
pt.x = (size.x - textSize.x) / 2;
dc.SetFont(GetFont());
if (!IsEnabled())
dc.SetTextForeground(text_color.colorForStates(StateColor::Disabled));
else
dc.SetTextForeground(text_color.colorForStates(states));
auto fina_txt = GetValue() ? labels[0] : labels[1];
if (dc.GetTextExtent(fina_txt).x > size.x) {
wxString forment_txt = wxEmptyString;
for (auto i = 0; i < fina_txt.length(); i++) {
forment_txt = fina_txt.SubString(0, i) + "...";
if (dc.GetTextExtent(forment_txt).x > size.x) {
pt.x = (size.x - dc.GetTextExtent(forment_txt).x) / 2;
dc.DrawText(forment_txt, pt);
break;
}
}
} else {
dc.DrawText(fina_txt, pt);
}
}
void ImageSwitchButton::Rescale()
{
messureSize();
}
void ImageSwitchButton::mouseDown(wxMouseEvent &event)
{
event.Skip();
pressedDown = true;
SetFocus();
CaptureMouse();
}
void ImageSwitchButton::mouseReleased(wxMouseEvent &event)
{
event.Skip();
if (pressedDown) {
pressedDown = false;
ReleaseMouse();
m_on_off = !m_on_off;
Refresh();
sendButtonEvent();
}
}
void ImageSwitchButton::mouseEnterWindow(wxMouseEvent &event)
{
if (!hover) {
hover = true;
Refresh();
}
}
void ImageSwitchButton::mouseLeaveWindow(wxMouseEvent &event)
{
if (hover) {
hover = false;
Refresh();
}
}
void ImageSwitchButton::sendButtonEvent()
{
wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
event.SetEventObject(this);
GetEventHandler()->ProcessEvent(event);
}
FanSwitchButton::FanSwitchButton(wxWindow* parent, ScalableBitmap& img_on, ScalableBitmap& img_off, long style)
: text_color(std::make_pair(0x6B6B6B, (int)StateColor::Disabled), std::make_pair(*wxBLACK, (int)StateColor::Normal))
{
radius = 0;
m_padding = 0;
m_speed = 0;
m_on = img_on;
m_off = img_off;
background_color = StateColor(std::make_pair(*wxWHITE, (int)StateColor::Disabled), std::make_pair(DEFAULT_PRESS_COL, (int)StateColor::Pressed),
std::make_pair(*wxWHITE, (int)StateColor::Normal));
border_color = StateColor(std::make_pair(*wxWHITE, (int)StateColor::Disabled), std::make_pair(DEFAULT_HOVER_COL, (int)StateColor::Focused),
std::make_pair(DEFAULT_HOVER_COL, (int)StateColor::Hovered), std::make_pair(*wxWHITE, (int)StateColor::Normal));
StaticBox::Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style);
messureSize();
Refresh();
}
void FanSwitchButton::SetLabels(wxString const& lbl_on, wxString const& lbl_off)
{
labels[0] = lbl_on;
labels[1] = lbl_off;
auto fina_txt = GetValue() ? labels[0] : labels[1];
SetToolTip(fina_txt);
messureSize();
Refresh();
}
void FanSwitchButton::SetImages(ScalableBitmap& img_on, ScalableBitmap& img_off)
{
m_on = img_on;
m_off = img_off;
messureSize();
Refresh();
}
void FanSwitchButton::SetTextColor(StateColor const& color)
{
text_color = color;
state_handler.update_binds();
messureSize();
Refresh();
}
void FanSwitchButton::SetValue(bool value)
{
m_on_off = value;
messureSize();
Refresh();
}
void FanSwitchButton::SetPadding(int padding)
{
m_padding = padding;
messureSize();
Refresh();
}
void FanSwitchButton::messureSize()
{
wxClientDC dc(this);
dc.SetFont(GetFont());
textSize = dc.GetTextExtent(GetValue() ? labels[0] : labels[1]);
}
void FanSwitchButton::paintEvent(wxPaintEvent& evt)
{
wxPaintDC dc(this);
render(dc);
}
void FanSwitchButton::render(wxDC& dc)
{
StaticBox::render(dc);
int states = state_handler.states();
wxSize size = GetSize();
wxSize szIcon;
wxSize szContent = textSize;
ScalableBitmap& icon = GetValue() ? m_on : m_off;
//int content_height = icon.GetBmpHeight() + textSize.y + m_padding;
int content_height = m_padding;
wxPoint pt = wxPoint((size.x - icon.GetBmpWidth()) / 2, (size.y - content_height) / 2);
pt.x = (size.x - textSize.x) / 2;
dc.SetFont(GetFont());
if (!IsEnabled())
dc.SetTextForeground(text_color.colorForStates(StateColor::Disabled));
else
dc.SetTextForeground(text_color.colorForStates(states));
auto fina_txt = GetValue() ? labels[0] : labels[1];
if (dc.GetTextExtent(fina_txt).x > size.x) {
wxString forment_txt = wxEmptyString;
for (auto i = 0; i < fina_txt.length(); i++) {
forment_txt = fina_txt.SubString(0, i) + "...";
if (dc.GetTextExtent(forment_txt).x > size.x) {
pt.x = (size.x - dc.GetTextExtent(forment_txt).x) / 2;
dc.DrawText(forment_txt, wxPoint(pt.x, content_height));
break;
}
}
}
else {
dc.DrawText(fina_txt, wxPoint(pt.x, content_height));
}
pt = wxPoint((size.x - icon.GetBmpWidth()) / 2, content_height + textSize.y);
if (icon.bmp().IsOk()) {
dc.DrawBitmap(icon.bmp(), pt);
pt.y += m_padding + icon.GetBmpHeight();
}
auto speed = wxString::Format("%d%%", m_speed);
dc.SetFont(GetFont());
if (!IsEnabled())
dc.SetTextForeground(text_color.colorForStates(StateColor::Disabled));
else
dc.SetTextForeground(text_color.colorForStates(states));
pt.x = (size.x - dc.GetTextExtent(speed).x) / 2;
pt.y += FromDIP(1);
dc.DrawText(speed, pt);
}
void FanSwitchButton::Rescale()
{
messureSize();
}
void FanSwitchButton::setFanValue(int val)
{
m_speed = val;
Refresh();
}
void FanSwitchButton::mouseDown(wxMouseEvent& event)
{
event.Skip();
pressedDown = true;
SetFocus();
CaptureMouse();
}
void FanSwitchButton::mouseReleased(wxMouseEvent& event)
{
event.Skip();
if (pressedDown) {
pressedDown = false;
ReleaseMouse();
//m_on_off = !m_on_off;
Refresh();
sendButtonEvent();
}
}
void FanSwitchButton::mouseEnterWindow(wxMouseEvent& event)
{
if (!hover) {
hover = true;
Refresh();
}
}
void FanSwitchButton::mouseLeaveWindow(wxMouseEvent& event)
{
if (hover) {
hover = false;
Refresh();
}
}
void FanSwitchButton::sendButtonEvent()
{
wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
event.SetEventObject(this);
GetEventHandler()->ProcessEvent(event);
}