SPH
ProgressPanel.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "system/Statistics.h"
4 #include "thread/CheckFunction.h"
5 #include <wx/panel.h>
6 
8 
9 class ProgressPanel : public wxPanel {
10 private:
11  std::string name;
12 
13  struct {
14  float progress = 0.f;
15  std::string simulationTime;
16  std::string eta;
17  bool finished = false;
18  } stat;
19 
20 public:
21  ProgressPanel(wxWindow* parent)
22  : wxPanel(parent, wxID_ANY) {
23  this->Connect(wxEVT_PAINT, wxPaintEventHandler(ProgressPanel::onPaint));
24  }
25 
26  void onRunStart(const std::string& className, const std::string& instanceName) {
28  name = instanceName + " (" + className + ")";
29  this->reset();
30  this->Refresh();
31  }
32 
33  void onRunEnd() {
34  stat.finished = true;
35  }
36 
37  void update(const Statistics& stats) {
39  this->reset();
40 
41  stat.progress = float(stats.getOr<Float>(StatisticsId::RELATIVE_PROGRESS, 0._f));
42 
43  if (stats.has(StatisticsId::WALLCLOCK_TIME)) {
44  const int64_t wallclock = stats.get<int>(StatisticsId::WALLCLOCK_TIME);
45  stat.simulationTime = "Elapsed time: " + getFormattedTime(wallclock);
46 
47  if (stat.progress > 0.05_f) {
48  stat.eta = "Estimated remaining: " +
49  getFormattedTime(int64_t(wallclock * (1._f / stat.progress - 1._f)));
50  }
51  }
52 
53  this->Refresh();
54  }
55 
56 private:
57  void reset() {
58  stat.progress = 0._f;
59  stat.eta = "";
60  stat.simulationTime = "";
61  stat.finished = false;
62  }
63 
64  void onPaint(wxPaintEvent& UNUSED(evt)) {
65  wxPaintDC dc(this);
66  wxSize size = dc.GetSize();
67  constexpr int padding = 25;
68  wxRect rect(wxPoint(padding, 0), wxSize(size.x - 2 * padding, size.y));
69 
70  wxBrush brush = *wxBLACK_BRUSH;
71  const bool isLightTheme = Rgba(dc.GetBackground().GetColour()).intensity() > 0.5f;
72  if (stat.finished) {
73  brush.SetColour(wxColour(40, 150, 40));
74  } else {
75  brush.SetColour(isLightTheme ? wxColour(160, 160, 200) : wxColour(100, 100, 120));
76  }
77  dc.SetBrush(brush);
78  dc.DrawRectangle(wxPoint(0, 0), wxSize(int(stat.progress * size.x), size.y));
79 
80  wxFont font = dc.GetFont();
81  dc.SetFont(font.Bold());
82  dc.DrawLabel(name, rect, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
83 
84  dc.SetFont(font);
85  dc.DrawLabel(stat.eta, rect, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
86  dc.DrawLabel(stat.simulationTime, rect, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
87  }
88 };
89 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Helper functions to check the internal consistency of the code.
@ NO_THROW
Function cannot throw exceptions.
@ MAIN_THREAD
Function can only be executed from main thread.
#define CHECK_FUNCTION(flags)
Definition: CheckFunction.h:40
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Statistics gathered and periodically displayed during the run.
@ WALLCLOCK_TIME
Current wallclock duration of the simulation.
String getFormattedTime(const String &format)
Utility functions.
Definition: String.cpp:128
std::string eta
Definition: ProgressPanel.h:16
std::string simulationTime
Definition: ProgressPanel.h:15
void update(const Statistics &stats)
Definition: ProgressPanel.h:37
ProgressPanel(wxWindow *parent)
Definition: ProgressPanel.h:21
void onRunStart(const std::string &className, const std::string &instanceName)
Definition: ProgressPanel.h:26
Definition: Color.h:8
float intensity() const
Returns the average intensity of the color.
Definition: Color.h:115
Object holding various statistics about current run.
Definition: Statistics.h:22
TValue get(const StatisticsId idx) const
Returns value of a statistic.
Definition: Statistics.h:88
TValue getOr(const StatisticsId idx, const TValue &other) const
Returns value of a statistic, or a given value if the statistic is not stored.
Definition: Statistics.h:98
bool has(const StatisticsId idx) const
Checks if the object contains a statistic with given ID.
Definition: Statistics.h:44