Almost working c++ status bar
Signed-off-by: tamasmeszaros <meszaros.q@gmail.com>
This commit is contained in:
156
xs/src/slic3r/GUI/ProgressStatusBar.cpp
Normal file
156
xs/src/slic3r/GUI/ProgressStatusBar.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
#include "ProgressStatusBar.hpp"
|
||||
|
||||
#include <wx/timer.h>
|
||||
#include <wx/gauge.h>
|
||||
#include <wx/button.h>
|
||||
#include <wx/statusbr.h>
|
||||
#include <wx/frame.h>
|
||||
|
||||
#include "GUI.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
ProgressStatusBar::ProgressStatusBar(wxWindow *parent, int id):
|
||||
self(new wxStatusBar(parent ? parent : GUI::get_main_frame(),
|
||||
id == -1? wxID_ANY : id)),
|
||||
timer_(new wxTimer(self)),
|
||||
prog_ (new wxGauge(self,
|
||||
wxGA_HORIZONTAL,
|
||||
100,
|
||||
wxDefaultPosition,
|
||||
wxDefaultSize)),
|
||||
cancelbutton_(new wxButton(self,
|
||||
-1,
|
||||
"Cancel",
|
||||
wxDefaultPosition,
|
||||
wxDefaultSize))
|
||||
{
|
||||
prog_->Hide();
|
||||
cancelbutton_->Hide();
|
||||
|
||||
|
||||
self->SetFieldsCount(3);
|
||||
int w[] = {-1, 150, 155};
|
||||
self->SetStatusWidths(3, w);
|
||||
|
||||
self->Bind(wxEVT_TIMER, [this](const wxTimerEvent&) {
|
||||
if (prog_->IsShown()) timer_->Stop();
|
||||
if(is_busy()) prog_->Pulse();
|
||||
});
|
||||
|
||||
self->Bind(wxEVT_SIZE, [this](wxSizeEvent& event){
|
||||
wxRect rect;
|
||||
self->GetFieldRect(1, rect);
|
||||
auto offset = 0;
|
||||
cancelbutton_->Move(rect.GetX() + offset, rect.GetY() + offset);
|
||||
cancelbutton_->SetSize(rect.GetWidth() - offset, rect.GetHeight());
|
||||
|
||||
self->GetFieldRect(2, rect);
|
||||
prog_->Move(rect.GetX() + offset, rect.GetY() + offset);
|
||||
prog_->SetSize(rect.GetWidth() - offset, rect.GetHeight());
|
||||
|
||||
event.Skip();
|
||||
});
|
||||
|
||||
cancelbutton_->Bind(wxEVT_BUTTON, [this](const wxCommandEvent&) {
|
||||
if(cancel_cb_) cancel_cb_();
|
||||
cancelbutton_->Hide();
|
||||
});
|
||||
}
|
||||
|
||||
//ProgressStatusBar::ProgressStatusBar(): ProgressStatusBar(nullptr, wxID_ANY)
|
||||
//{
|
||||
|
||||
//}
|
||||
|
||||
ProgressStatusBar::~ProgressStatusBar() {
|
||||
if(timer_->IsRunning()) timer_->Stop();
|
||||
}
|
||||
|
||||
int ProgressStatusBar::get_progress() const
|
||||
{
|
||||
return prog_->GetValue();
|
||||
}
|
||||
|
||||
void ProgressStatusBar::set_progress(int val)
|
||||
{
|
||||
if(!prog_->IsShown()) show_progress(true);
|
||||
|
||||
if(val == prog_->GetRange()) {
|
||||
prog_->SetValue(0);
|
||||
show_progress(false);
|
||||
} else {
|
||||
prog_->SetValue(val);
|
||||
}
|
||||
}
|
||||
|
||||
void ProgressStatusBar::set_range(int val)
|
||||
{
|
||||
if(val != prog_->GetRange()) {
|
||||
prog_->SetRange(val);
|
||||
}
|
||||
}
|
||||
|
||||
void ProgressStatusBar::show_progress(bool show)
|
||||
{
|
||||
prog_->Show(show);
|
||||
prog_->Pulse();
|
||||
}
|
||||
|
||||
void ProgressStatusBar::start_busy(int rate)
|
||||
{
|
||||
busy_ = true;
|
||||
show_progress(true);
|
||||
if (!timer_->IsRunning()) {
|
||||
timer_->Start(rate);
|
||||
}
|
||||
}
|
||||
|
||||
void ProgressStatusBar::stop_busy()
|
||||
{
|
||||
timer_->Stop();
|
||||
show_progress(false);
|
||||
prog_->SetValue(0);
|
||||
busy_ = false;
|
||||
}
|
||||
|
||||
void ProgressStatusBar::set_cancel_callback(ProgressStatusBar::CancelFn ccb) {
|
||||
cancel_cb_ = ccb;
|
||||
if(ccb) cancelbutton_->Show();
|
||||
else cancelbutton_->Hide();
|
||||
}
|
||||
|
||||
void ProgressStatusBar::run(int rate)
|
||||
{
|
||||
if(!timer_->IsRunning()) {
|
||||
timer_->Start(rate);
|
||||
}
|
||||
}
|
||||
|
||||
void ProgressStatusBar::Embed()
|
||||
{
|
||||
std::cout << "Embedding" << std::endl;
|
||||
wxFrame* mf = GUI::get_main_frame();
|
||||
std::cout << mf->GetName() << std::endl;
|
||||
std::cout << self->GetName() << std::endl;
|
||||
mf->SetStatusBar(self);
|
||||
}
|
||||
|
||||
void ProgressStatusBar::SetStatusText(std::string txt)
|
||||
{
|
||||
self->SetStatusText(txt);
|
||||
}
|
||||
|
||||
int ProgressStatusBar::GetId()
|
||||
{
|
||||
return self->GetId();
|
||||
}
|
||||
|
||||
int ProgressStatusBar::GetProgId()
|
||||
{
|
||||
return prog_->GetId();
|
||||
}
|
||||
|
||||
}
|
||||
61
xs/src/slic3r/GUI/ProgressStatusBar.hpp
Normal file
61
xs/src/slic3r/GUI/ProgressStatusBar.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#ifndef PROGRESSSTATUSBAR_HPP
|
||||
#define PROGRESSSTATUSBAR_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
class wxTimer;
|
||||
class wxGauge;
|
||||
class wxButton;
|
||||
class wxTimerEvent;
|
||||
class wxStatusBar;
|
||||
class wxWindow;
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
/**
|
||||
* @brief The ProgressStatusBar class is the widgets occupying the lower area
|
||||
* of the Slicer main window. It consists of a message area to the left and a
|
||||
* progress indication area to the right with an optional cancel button.
|
||||
*/
|
||||
class ProgressStatusBar {
|
||||
wxStatusBar *self; // we cheat! It should be the base class but: perl!
|
||||
wxTimer *timer_;
|
||||
wxGauge *prog_;
|
||||
wxButton *cancelbutton_;
|
||||
public:
|
||||
|
||||
/// Cancel callback function type
|
||||
using CancelFn = std::function<void()>;
|
||||
|
||||
ProgressStatusBar(wxWindow *parent = nullptr, int id = -1);
|
||||
~ProgressStatusBar();
|
||||
|
||||
int get_progress() const;
|
||||
void set_progress(int);
|
||||
void set_range(int = 100);
|
||||
void show_progress(bool);
|
||||
void start_busy(int = 100);
|
||||
void stop_busy();
|
||||
inline bool is_busy() const { return busy_; }
|
||||
void set_cancel_callback(CancelFn);
|
||||
void run(int rate);
|
||||
|
||||
// Temporary methods to satisfy Perl side
|
||||
void Embed();
|
||||
void SetStatusText(std::string txt);
|
||||
int GetId();
|
||||
int GetProgId();
|
||||
|
||||
private:
|
||||
bool busy_ = false;
|
||||
CancelFn cancel_cb_;
|
||||
};
|
||||
|
||||
namespace GUI {
|
||||
using Slic3r::ProgressStatusBar;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // PROGRESSSTATUSBAR_HPP
|
||||
Reference in New Issue
Block a user