2018-09-14 07:28:00 +00:00
|
|
|
#ifndef IPROGRESSINDICATOR_HPP
|
|
|
|
#define IPROGRESSINDICATOR_HPP
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Generic progress indication interface.
|
|
|
|
*/
|
|
|
|
class ProgressIndicator {
|
|
|
|
public:
|
|
|
|
using CancelFn = std::function<void(void)>; // Cancel function signature.
|
|
|
|
|
|
|
|
private:
|
2018-09-19 12:54:37 +00:00
|
|
|
float m_state = .0f, m_max = 1.f, m_step;
|
|
|
|
CancelFn m_cancelfunc = [](){};
|
2018-09-14 07:28:00 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
inline virtual ~ProgressIndicator() {}
|
|
|
|
|
|
|
|
/// Get the maximum of the progress range.
|
2018-09-19 12:54:37 +00:00
|
|
|
float max() const { return m_max; }
|
2018-09-14 07:28:00 +00:00
|
|
|
|
|
|
|
/// Get the current progress state
|
2018-09-19 12:54:37 +00:00
|
|
|
float state() const { return m_state; }
|
2018-09-14 07:28:00 +00:00
|
|
|
|
|
|
|
/// Set the maximum of the progress range
|
2018-09-19 12:54:37 +00:00
|
|
|
virtual void max(float maxval) { m_max = maxval; }
|
2018-09-14 07:28:00 +00:00
|
|
|
|
|
|
|
/// Set the current state of the progress.
|
2018-09-19 12:54:37 +00:00
|
|
|
virtual void state(float val) { m_state = val; }
|
2018-09-14 07:28:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Number of states int the progress. Can be used instead of giving a
|
|
|
|
* maximum value.
|
|
|
|
*/
|
|
|
|
virtual void states(unsigned statenum) {
|
2018-09-19 12:54:37 +00:00
|
|
|
m_step = m_max / statenum;
|
2018-09-14 07:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Message shown on the next status update.
|
2018-09-17 13:12:13 +00:00
|
|
|
virtual void message(const std::string&) = 0;
|
2018-09-14 07:28:00 +00:00
|
|
|
|
|
|
|
/// Title of the operation.
|
2018-09-17 13:12:13 +00:00
|
|
|
virtual void title(const std::string&) = 0;
|
2018-09-14 07:28:00 +00:00
|
|
|
|
|
|
|
/// Formatted message for the next status update. Works just like sprintf.
|
2018-09-17 13:12:13 +00:00
|
|
|
virtual void message_fmt(const std::string& fmt, ...);
|
2018-09-14 07:28:00 +00:00
|
|
|
|
|
|
|
/// Set up a cancel callback for the operation if feasible.
|
2018-09-19 12:54:37 +00:00
|
|
|
virtual void on_cancel(CancelFn func = CancelFn()) { m_cancelfunc = func; }
|
2018-09-14 07:28:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Explicitly shut down the progress indicator and call the associated
|
|
|
|
* callback.
|
|
|
|
*/
|
2018-09-19 12:54:37 +00:00
|
|
|
virtual void cancel() { m_cancelfunc(); }
|
2018-09-14 07:28:00 +00:00
|
|
|
|
|
|
|
/// Convenience function to call message and status update in one function.
|
2018-09-17 13:12:13 +00:00
|
|
|
void update(float st, const std::string& msg) {
|
2018-09-14 07:28:00 +00:00
|
|
|
message(msg); state(st);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // IPROGRESSINDICATOR_HPP
|