Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ProgressStatusBar.cpp « GUI « slic3r « src - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 363e34cb2b6962719c2df1817de959687f5014f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#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_();
        m_perl_cancel_callback.call();
        cancelbutton_->Hide();
    });
}

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);
    }
}

int ProgressStatusBar::get_range() const
{
    return prog_->GetRange();
}

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(wxFrame *frame)
{
    wxFrame* mf = frame? frame : GUI::get_main_frame();
    mf->SetStatusBar(self);
}

void ProgressStatusBar::set_status_text(const wxString& txt)
{
    self->SetStatusText(wxString::FromUTF8(txt.c_str()));
}

void ProgressStatusBar::show_cancel_button()
{
    cancelbutton_->Show();
}

void ProgressStatusBar::hide_cancel_button()
{
    cancelbutton_->Hide();
}

}