Files
QIDISlicer/src/slic3r/GUI/PrinterWebView.hpp

318 lines
9.4 KiB
C++
Raw Normal View History

2023-06-10 10:14:12 +08:00
#ifndef slic3r_PrinterWebView_hpp_
#define slic3r_PrinterWebView_hpp_
#include "wx/artprov.h"
#include "wx/cmdline.h"
#include "wx/notifmsg.h"
#include "wx/settings.h"
#include "wx/webview.h"
#if wxUSE_WEBVIEW_EDGE
#include "wx/msw/webview_edge.h"
#endif
#include "wx/webviewarchivehandler.h"
#include "wx/webviewfshandler.h"
#include "wx/numdlg.h"
#include "wx/infobar.h"
#include "wx/filesys.h"
#include "wx/fs_arc.h"
#include "wx/fs_mem.h"
#include "wx/stdpaths.h"
#include <wx/panel.h>
#include <wx/tbarbase.h>
#include "wx/textctrl.h"
#include <wx/timer.h>
2023-09-11 11:10:12 +08:00
//B35
//B45
2023-09-11 11:10:12 +08:00
#if defined __linux__
#include <boost/log/trivial.hpp>
#include <wx/wx.h>
#include <thread>
#include <boost/format.hpp>
2023-09-11 11:10:12 +08:00
#endif
2023-11-27 11:29:19 +08:00
//B45
#include "PrintHostDialogs.hpp"
#include <wx/tokenzr.h>
2023-06-10 10:14:12 +08:00
namespace Slic3r {
namespace GUI {
2023-11-27 11:29:19 +08:00
class MachineListButton : public wxButton
{
public:
MachineListButton(wxWindow * parent,
wxWindowID id,
const wxString & label,
const wxString &fullname,
2023-11-27 11:29:19 +08:00
const wxPoint & pos = wxDefaultPosition,
const wxSize & size = wxDefaultSize,
long style = wxBORDER_DOUBLE,
const wxValidator &validator = wxDefaultValidator,
const wxString & name = wxButtonNameStr,
bool isSelected = false)
: wxButton(parent, id, label, pos, size, style, validator, name)
{
SetBackgroundColour(wxColour(30, 30, 32));
full_label = fullname;
2023-11-27 11:29:19 +08:00
m_isSelected = isSelected;
//if (isSelected)
// SetBackgroundColour(wxColour(100, 100, 105));
//else
// SetBackgroundColour(wxColour(67, 67, 71));
#if defined(__WIN32__) || defined(__WXMAC__)
SetBackgroundColour(wxColour(30, 30, 32));
#else
if (isSelected)
SetBackgroundColour(wxColour(100, 100, 105));
else
SetBackgroundColour(wxColour(67, 67, 71));
#endif
2023-11-27 11:29:19 +08:00
}
2023-12-04 14:10:31 +08:00
void SetLabel(const wxString &fullname) { full_label = fullname; }
2023-11-27 11:29:19 +08:00
wxString getLabel() { return full_label; }
wxString getIPLabel() { return m_ip_text; }
bool getIsQIDI() { return m_is_QIDI; }
2023-11-27 11:29:19 +08:00
void SetBitMap(const wxBitmap &bitmap)
{
m_bitmap = bitmap;
Refresh();
}
void SetNameText(const wxString &text)
{
m_name_text = text;
Refresh();
}
2023-12-04 14:10:31 +08:00
//B58
void SetIsQIDI(const bool &text)
{
m_is_QIDI = text;
//Refresh();
}
2023-12-04 14:10:31 +08:00
wxString GetNameText()
{
return m_name_text;
}
2023-11-27 11:29:19 +08:00
void SetIPText(const wxString &text)
{
m_ip_text = text;
Refresh();
}
void SetStateText(const wxString &text)
{
m_state_text = text;
Refresh();
}
void SetProgressText(const wxString &text)
{
m_progress_text = text;
Refresh();
}
void SetSelect(bool isselectd)
{
m_isSelected = isselectd;
#if defined(__WIN32__) || defined(__WXMAC__)
SetBackgroundColour(wxColour(30, 30, 32));
#else
if (m_isSelected)
SetBackgroundColour(wxColour(100, 100, 105));
else
SetBackgroundColour(wxColour(67, 67, 71));
#endif
2023-11-27 11:29:19 +08:00
Refresh();
}
2023-12-04 14:10:31 +08:00
bool GetSelected() { return m_isSelected;}
2023-11-27 11:29:19 +08:00
void SetSimpleMode(bool issimplemode)
{
m_isSimpleMode = issimplemode;
Refresh();
}
void SetClickHandler(const std::function<void(wxMouseEvent &)> &handler) { m_handlerl = handler; }
void PauseStatusThread() { m_pauseThread = true; }
void ResumeStatusThread() { m_pauseThread = false; }
void StopStatusThread()
{
m_stopThread = true;
if (m_statusThread.joinable()) {
m_statusThread.join();
}
}
void OnPaint(wxPaintEvent &event);
void OnSetFocus(wxFocusEvent &event);
void OnKillFocus(wxFocusEvent &event);
void OnKeyDown(wxKeyEvent &event);
void OnKeyUp(wxKeyEvent &event);
void OnMouseEnter(wxMouseEvent &event);
void OnMouseLeave(wxMouseEvent &event);
void OnMouseLeftDown(wxMouseEvent &event);
//void OnMouseLeftUp(wxMouseEvent &event);
//void OnClickHandler(wxCommandEvent &event);
2023-11-27 11:29:19 +08:00
void SetStatusThread(std::thread thread) { m_statusThread = std::move(thread); }
std::thread CreatThread(const wxString &buttonText, const wxString &ip, DynamicPrintConfig *cfg_t)
2023-11-27 11:29:19 +08:00
{
std::thread thread([this, buttonText,ip, cfg_t]() {
2023-11-27 11:29:19 +08:00
std::unique_ptr<PrintHost> printhost(PrintHost::get_print_host(cfg_t));
if (!printhost) {
BOOST_LOG_TRIVIAL(error) << ("Could not get a valid Printer Host reference");
return;
}
wxString msg;
std::string state = "standby";
float progress = 0;
int timeout_times = 0;
2023-11-27 11:29:19 +08:00
while (true) {
if (!m_pauseThread) {
state = printhost->get_status(msg);
if (state == "offline") {
BOOST_LOG_TRIVIAL(info) << boost::format("%1%Got state: %2%") % buttonText % state;
timeout_times += 1;
if (timeout_times>3)
m_pauseThread = true;
2023-11-27 11:29:19 +08:00
}
BOOST_LOG_TRIVIAL(info) << boost::format("%1%Got state: %2%") % buttonText % state;
if (m_state_text != state)
SetStateText(state);
2023-11-27 11:29:19 +08:00
if (state == "printing") {
timeout_times = 0;
progress = (printhost->get_progress(msg)) * 100;
2023-11-27 11:29:19 +08:00
int progressInt = static_cast<int>(progress);
SetProgressText(wxString::Format(wxT("(%d%%)"), progressInt));
2023-11-27 11:29:19 +08:00
BOOST_LOG_TRIVIAL(info) << boost::format("%1%Got progress: %2%") % buttonText % progress;
} else if (state == "standby")
timeout_times = 0;
}
2023-11-27 11:29:19 +08:00
if (m_stopThread)
break;
}
});
return thread;
}
private:
std::atomic<bool> m_stopThread{false};
std::atomic<bool> m_pauseThread{false};
bool m_isSimpleMode;
bool m_isSelected = false;
bool m_isHovered = false;
//bool m_isClicked = false;
2023-11-27 11:29:19 +08:00
std::thread m_statusThread;
//wxGraphicsContext *gc;
wxPaintDC * m_dc;
2023-11-27 11:29:19 +08:00
wxBitmap m_bitmap;
wxString full_label;
wxString m_name_text;
wxString m_ip_text;
wxString m_state_text;
wxString m_progress_text;
std::function<void(wxMouseEvent &)> m_handlerl;
//B58
bool m_is_QIDI;
2023-11-27 11:29:19 +08:00
wxDECLARE_EVENT_TABLE();
};
2023-06-10 10:14:12 +08:00
class PrinterWebView : public wxPanel {
public:
PrinterWebView(wxWindow *parent);
virtual ~PrinterWebView();
void load_url(wxString& url);
void UpdateState();
void OnClose(wxCloseEvent& evt);
2023-11-27 11:29:19 +08:00
//B45
void OnLeftButtonClick(wxCommandEvent &event);
void OnRightButtonClick(wxCommandEvent &event);
void OnCustomButtonClick(std::function<void(wxCommandEvent &)> m_handler, wxCommandEvent &event);
2023-12-04 14:10:31 +08:00
void OnAddButtonClick(wxCommandEvent &event);
void OnDeleteButtonClick(wxCommandEvent &event);
void OnEditButtonClick(wxCommandEvent &event);
2023-11-27 11:29:19 +08:00
void RunScript(const wxString &javascript);
//void OnScriptMessageReceived(wxWebViewEvent &event);
void OnScriptMessage(wxWebViewEvent &evt);
void UpdateLayout();
void OnScroll(wxScrollWinEvent &event);
void OnScrollup(wxScrollWinEvent &event);
void OnScrolldown(wxScrollWinEvent &event);
2023-11-27 11:29:19 +08:00
2023-12-04 14:10:31 +08:00
void SetUpdateHandler(const std::function<void(wxCommandEvent &)> &handler) { m_handlerl = handler; }
void SetDeleteHandler(const std::function<void(wxCommandEvent &)> &handler) { m_delete_handlerl = handler; }
2023-11-27 11:29:19 +08:00
//B45
//void SendRecentList(int images);
2023-11-27 11:29:19 +08:00
void SetButtons(std::vector<MachineListButton *> buttons);
2024-03-20 10:08:22 +08:00
//B55
void AddButton(const wxString & device_name,
const wxString & ip,
const wxString & machine_type,
const wxString & fullname,
bool isSelected,
2024-03-20 11:11:53 +08:00
bool isQIDI,
DynamicPrintConfig * cfg_t);
2023-11-27 11:29:19 +08:00
void DeleteButton();
void PauseButton();
void ResumeButton();
void StopAllThread();
2023-12-04 14:10:31 +08:00
void UnSelectedButton();
2023-11-27 11:29:19 +08:00
std::vector<MachineListButton *> GetButton() { return m_buttons; };
2023-06-10 10:14:12 +08:00
private:
2023-11-27 11:29:19 +08:00
//B45
2023-12-04 14:10:31 +08:00
wxBoxSizer *leftallsizer;
2023-11-27 11:29:19 +08:00
wxBoxSizer *leftsizer;
wxBoxSizer *topsizer;
bool m_isSimpleMode = false;
wxButton *arrow_button;
wxStaticText * text_static;
2023-11-27 11:29:19 +08:00
int height = 0;
wxString m_web;
2023-12-04 14:10:31 +08:00
std::function<void(wxCommandEvent &)> m_handlerl;
std::function<void(wxCommandEvent &)> m_delete_handlerl;
2023-11-27 11:29:19 +08:00
wxScrolledWindow * leftScrolledWindow;
2023-12-04 14:10:31 +08:00
wxPanel * leftPanel;
2023-11-27 11:29:19 +08:00
std::vector<MachineListButton *> m_buttons;
2023-06-10 10:14:12 +08:00
wxWebView* m_browser;
long m_zoomFactor;
// DECLARE_EVENT_TABLE()
};
2023-12-04 14:10:31 +08:00
2023-06-10 10:14:12 +08:00
} // GUI
} // Slic3r
#endif /* slic3r_Tab_hpp_ */