Files
OrcaSlicer-bambulab/xs/src/slic3r/ProgressIndicator.hpp
T

72 lines
1.8 KiB
C++
Raw Normal View History

2018-06-28 18:47:18 +02:00
#ifndef IPROGRESSINDICATOR_HPP
#define IPROGRESSINDICATOR_HPP
#include <string>
#include <functional>
2018-07-03 10:22:55 +02:00
#include "Strings.hpp"
2018-06-28 18:47:18 +02:00
namespace Slic3r {
/**
* @brief Generic progress indication interface.
*/
2018-08-29 18:02:10 +02:00
class ProgressIndicator {
2018-06-28 18:47:18 +02:00
public:
2018-08-30 11:40:06 +02:00
using CancelFn = std::function<void(void)>; // Cancel function signature.
2018-06-28 18:47:18 +02:00
private:
float state_ = .0f, max_ = 1.f, step_;
CancelFn cancelfunc_ = [](){};
public:
2018-08-29 18:02:10 +02:00
inline virtual ~ProgressIndicator() {}
2018-06-28 18:47:18 +02:00
/// Get the maximum of the progress range.
float max() const { return max_; }
/// Get the current progress state
float state() const { return state_; }
2018-08-30 11:40:06 +02:00
/// Set the maximum of the progress range
2018-06-28 18:47:18 +02:00
virtual void max(float maxval) { max_ = maxval; }
/// Set the current state of the progress.
virtual void state(float val) { state_ = val; }
/**
2018-08-30 11:40:06 +02:00
* @brief Number of states int the progress. Can be used instead of giving a
2018-06-28 18:47:18 +02:00
* maximum value.
*/
virtual void states(unsigned statenum) {
step_ = max_ / statenum;
}
/// Message shown on the next status update.
2018-07-03 10:22:55 +02:00
virtual void message(const string&) = 0;
2018-06-28 18:47:18 +02:00
2018-08-30 11:40:06 +02:00
/// Title of the operation.
2018-07-03 10:22:55 +02:00
virtual void title(const string&) = 0;
2018-06-28 18:47:18 +02:00
2018-08-30 11:40:06 +02:00
/// Formatted message for the next status update. Works just like sprintf.
2018-07-03 10:22:55 +02:00
virtual void message_fmt(const string& fmt, ...);
2018-06-28 18:47:18 +02:00
/// Set up a cancel callback for the operation if feasible.
2018-08-30 11:40:06 +02:00
virtual void on_cancel(CancelFn func = CancelFn()) { cancelfunc_ = func; }
2018-06-28 18:47:18 +02:00
/**
* Explicitly shut down the progress indicator and call the associated
* callback.
*/
virtual void cancel() { cancelfunc_(); }
2018-08-30 11:40:06 +02:00
/// Convenience function to call message and status update in one function.
2018-07-03 10:22:55 +02:00
void update(float st, const string& msg) {
2018-06-28 18:47:18 +02:00
message(msg); state(st);
}
};
}
#endif // IPROGRESSINDICATOR_HPP