MeshLib
 
Loading...
Searching...
No Matches
MRProgressBar.h
Go to the documentation of this file.
1#pragma once
3#include <imgui.h>
4#include <functional>
5#include <atomic>
6#include <thread>
7
8namespace MR
9{
10
11// This class shows application progress bar for long operations
12// note! if class don't setup, then order and orderWithMainThreadPostProcessing methods call task directly
14{
15public:
16 using TaskWithMainThreadPostProcessing = std::function< std::function<void()>() >;
17 // this function should be called only once for each frame (it is called in MR::Menu (MR::RibbonMenu))
18 MRVIEWER_API static void setup( float scaling );
19 // call this function on frame end
20 MRVIEWER_API static void onFrameEnd();
21
22 // this shall be called in order to start concurrent task execution with progress bar display
23 MRVIEWER_API static void order(const char * name, const std::function<void()>& task, int taskCount = 1 );
24
25 // in this version the task returns a function to be executed in main thread
26 MRVIEWER_API static void orderWithMainThreadPostProcessing( const char* name, TaskWithMainThreadPostProcessing task, int taskCount = 1 );
27
29 MRVIEWER_API static void orderWithManualFinish( const char * name, std::function<void ()> task, int taskCount = 1 );
30
31 MRVIEWER_API static bool isCanceled();
32
33 MRVIEWER_API static bool isFinished();
34
35 MRVIEWER_API static float getProgress();
36
37 // returns time of last operation in seconds, returns -1.0f if no operation was performed
38 MRVIEWER_API static float getLastOperationTime();
39 // returns title of the last operation
40 MRVIEWER_API static const std::string& getLastOperationTitle();
41
42 // sets the current progress and returns false if the user has pressed Cancel button
43 MRVIEWER_API static bool setProgress(float p);
44
45 MRVIEWER_API static void nextTask();
46 MRVIEWER_API static void nextTask(const char * s);
47
48 MRVIEWER_API static void setTaskCount( int n );
49
50 // set the current task's name without auto-updating progress value
51 MRVIEWER_API static void forceSetTaskName( std::string taskName );
52 MRVIEWER_API static void resetTaskName();
53
54 MRVIEWER_API static void finish();
55
56 // returns true if progress bar was ordered and not finished
57 MRVIEWER_API static bool isOrdered();
58
59 // these callbacks allow canceling
60 MRVIEWER_API static bool callBackSetProgress(float p);
61 // these callbacks do not allow canceling
62 MRVIEWER_API static bool simpleCallBackSetProgress( float p );
63private:
64 static ProgressBar& instance_();
65
68
69 void initialize_();
70
71 // cover task execution with try catch block (in release only)
72 // if catches exception shows error in main thread overriding user defined main thread post-processing
73 bool tryRun_( const std::function<bool ()>& task );
74 bool tryRunWithSehHandler_( const std::function<bool ()>& task );
75
76 float lastOperationTimeSec_{ -1.0f };
77 Time operationStartTime_;
78 std::atomic<float> progress_;
79 std::atomic<int> currentTask_, taskCount_;
80 std::mutex mutex_;
81 std::string taskName_, title_;
82 bool overrideTaskName_{ false };
83
84 FrameRedrawRequest frameRequest_;
85
86 // parameter is needed for logging progress
87 std::atomic<int> percents_;
88
89 std::thread thread_;
90 std::function<void()> onFinish_;
91
92 // needed to be able to call progress bar from any point, not only from ImGui frame scope
93 struct DeferredInit
94 {
95 int taskCount;
96 std::string name;
97 std::function<void ()> postInit;
98 };
99 std::unique_ptr<DeferredInit> deferredInit_;
100
101 std::atomic<bool> allowCancel_;
102 std::atomic<bool> canceled_;
103 std::atomic<bool> finished_;
104 ImGuiID setupId_ = ImGuiID( -1 );
105
106 bool isOrdered_{ false };
107 bool isInit_{ false };
108 // this is needed to show full progress before closing
109 bool closeDialogNextFrame_{ false };
110};
111
112}
Definition MRProgressBar.h:14
static MRVIEWER_API bool isFinished()
static MRVIEWER_API void nextTask(const char *s)
std::function< std::function< void()>() > TaskWithMainThreadPostProcessing
Definition MRProgressBar.h:16
static MRVIEWER_API float getLastOperationTime()
static MRVIEWER_API const std::string & getLastOperationTitle()
static MRVIEWER_API void finish()
static MRVIEWER_API void orderWithMainThreadPostProcessing(const char *name, TaskWithMainThreadPostProcessing task, int taskCount=1)
static MRVIEWER_API float getProgress()
static MRVIEWER_API void resetTaskName()
static MRVIEWER_API void forceSetTaskName(std::string taskName)
static MRVIEWER_API void onFrameEnd()
static MRVIEWER_API bool callBackSetProgress(float p)
static MRVIEWER_API bool simpleCallBackSetProgress(float p)
static MRVIEWER_API bool isCanceled()
static MRVIEWER_API void order(const char *name, const std::function< void()> &task, int taskCount=1)
static MRVIEWER_API bool isOrdered()
static MRVIEWER_API void setTaskCount(int n)
static MRVIEWER_API void nextTask()
static MRVIEWER_API void setup(float scaling)
static MRVIEWER_API void orderWithManualFinish(const char *name, std::function< void()> task, int taskCount=1)
the task is spawned by the progress bar but the finish method is called from a callback
static MRVIEWER_API bool setProgress(float p)
Definition MRCameraOrientationPlugin.h:7
std::chrono::time_point< std::chrono::system_clock > Time
Definition MRAsyncTimer.h:17