KiCad PCB EDA Suite
WX_PROGRESS_REPORTER Class Reference

Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of work status. More...

#include <wx_progress_reporters.h>

Inheritance diagram for WX_PROGRESS_REPORTER:
PROGRESS_REPORTER_BASE PROGRESS_REPORTER

Public Member Functions

 WX_PROGRESS_REPORTER (wxWindow *aParent, const wxString &aTitle, int aNumPhases, bool aCanAbort=true, bool aReserveSpaceForMessage=true)
 The PROGRESS_REPORTER will stay on top of aParent. More...
 
 ~WX_PROGRESS_REPORTER ()
 
void SetTitle (const wxString &aTitle) override
 Change the title displayed on the window caption. More...
 
void SetNumPhases (int aNumPhases) override
 Set the number of phases. More...
 
void AddPhases (int aNumPhases) override
 
virtual void BeginPhase (int aPhase) override
 Initialize the aPhase virtual zone of the dialog progress bar. More...
 
virtual void AdvancePhase () override
 Use the next available virtual zone of the dialog progress bar. More...
 
virtual void AdvancePhase (const wxString &aMessage) override
 Use the next available virtual zone of the dialog progress bar and updates the message. More...
 
virtual void Report (const wxString &aMessage) override
 Display aMessage in the progress bar dialog. More...
 
virtual void SetCurrentProgress (double aProgress) override
 Set the progress value to aProgress (0..1). More...
 
void SetMaxProgress (int aMaxProgress) override
 Fix the value that gives the 100 percent progress bar length (inside the current virtual zone). More...
 
void AdvanceProgress () override
 Increment the progress bar length (inside the current virtual zone). More...
 
bool KeepRefreshing (bool aWait=false) override
 Update the UI dialog. More...
 
bool IsCancelled () const override
 

Protected Member Functions

int currentProgress () const
 

Protected Attributes

wxString m_rptMessage
 
bool m_msgChanged
 
std::mutex m_mutex
 
std::atomic_int m_phase
 
std::atomic_int m_numPhases
 
std::atomic_int m_progress
 
std::atomic_int m_maxProgress
 
std::atomic_bool m_cancelled
 

Private Member Functions

bool updateUI () override
 

Detailed Description

Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of work status.

See also
PROGRESS_REPORTER.

Definition at line 44 of file wx_progress_reporters.h.

Constructor & Destructor Documentation

◆ WX_PROGRESS_REPORTER()

WX_PROGRESS_REPORTER::WX_PROGRESS_REPORTER ( wxWindow *  aParent,
const wxString &  aTitle,
int  aNumPhases,
bool  aCanAbort = true,
bool  aReserveSpaceForMessage = true 
)

The PROGRESS_REPORTER will stay on top of aParent.

The style is wxPD_AUTO_HIDE | wxPD_CAN_ABORT | wxPD_ELAPSED_TIME.

Parameters
aParentis the wxDialog of Frame that manage this.
aTitleis the dialog progress title
aNumPhasesis the number of "virtual sections" of the progress bar aNumPhases = 1 is the usual progress bar aNumPhases = n creates n virtual progress bar zones: a 0 to 100 percent width of a virtual zone fills 0 to 1/n progress bar full size of the nth virtual zone index
aCanAbortis true if the abort button should be shown
aReserveSpaceForMessagewill ensure that the dialog is laid out for status messages, preventing layout issues on Windows when reporting a message after the initial layout

Definition at line 32 of file wx_progress_reporters.cpp.

34  :
35  PROGRESS_REPORTER_BASE( aNumPhases ),
36  wxProgressDialog( aTitle, ( aReserveSpaceForMessage ? wxT( " " ) : wxT( "" ) ), 1, aParent,
37  // wxPD_APP_MODAL | // Don't use; messes up OSX when called from
38  // quasi-modal dialog
39  wxPD_AUTO_HIDE | // *MUST* use; otherwise wxWidgets will spin
40  // up another event loop on completion which
41  // causes all sorts of grief
42  ( aCanAbort ? wxPD_CAN_ABORT : 0 ) |
43  wxPD_ELAPSED_TIME )
44 #if wxCHECK_VERSION( 3, 1, 0 )
45  ,
46  m_appProgressIndicator( aParent )
47 #endif
48 {
49 #if wxCHECK_VERSION( 3, 1, 0 )
50  // wxAppProgressIndicator doesn't like value > max, ever. However there are some risks
51  // with multithreaded setting of those values making a mess
52  // the cop out is just to set the progress to "indeterminate"
53  m_appProgressIndicator.Pulse();
54 #endif
55 }
PROGRESS_REPORTER_BASE(int aNumPhases)

◆ ~WX_PROGRESS_REPORTER()

WX_PROGRESS_REPORTER::~WX_PROGRESS_REPORTER ( )

Definition at line 58 of file wx_progress_reporters.cpp.

59 {
60 }

Member Function Documentation

◆ AddPhases()

void PROGRESS_REPORTER_BASE::AddPhases ( int  aNumPhases)
overridevirtualinherited

Implements PROGRESS_REPORTER.

Definition at line 97 of file progress_reporter_base.cpp.

98 {
99  m_numPhases += aNumPhases;
100 }

References PROGRESS_REPORTER_BASE::m_numPhases.

◆ AdvancePhase() [1/2]

void PROGRESS_REPORTER_BASE::AdvancePhase ( )
overridevirtualinherited

Use the next available virtual zone of the dialog progress bar.

Implements PROGRESS_REPORTER.

Definition at line 50 of file progress_reporter_base.cpp.

51 {
52  m_phase.fetch_add( 1 );
53  m_progress.store( 0 );
54 }

References PROGRESS_REPORTER_BASE::m_phase, and PROGRESS_REPORTER_BASE::m_progress.

Referenced by DIALOG_ERC::AdvancePhase(), PROGRESS_REPORTER_BASE::AdvancePhase(), DIALOG_DRC::AdvancePhase(), and DIALOG_ERC::testErc().

◆ AdvancePhase() [2/2]

void PROGRESS_REPORTER_BASE::AdvancePhase ( const wxString &  aMessage)
overridevirtualinherited

Use the next available virtual zone of the dialog progress bar and updates the message.

Implements PROGRESS_REPORTER.

Reimplemented in DIALOG_DRC, and DIALOG_ERC.

Definition at line 57 of file progress_reporter_base.cpp.

58 {
59  AdvancePhase();
60  Report( aMessage );
61 }
virtual void AdvancePhase() override
Use the next available virtual zone of the dialog progress bar.
virtual void Report(const wxString &aMessage) override
Display aMessage in the progress bar dialog.

References PROGRESS_REPORTER_BASE::AdvancePhase(), and PROGRESS_REPORTER_BASE::Report().

◆ AdvanceProgress()

void PROGRESS_REPORTER_BASE::AdvanceProgress ( )
overridevirtualinherited

Increment the progress bar length (inside the current virtual zone).

Implements PROGRESS_REPORTER.

Definition at line 85 of file progress_reporter_base.cpp.

86 {
87  m_progress.fetch_add( 1 );
88 }

References PROGRESS_REPORTER_BASE::m_progress.

◆ BeginPhase()

void PROGRESS_REPORTER_BASE::BeginPhase ( int  aPhase)
overridevirtualinherited

Initialize the aPhase virtual zone of the dialog progress bar.

Implements PROGRESS_REPORTER.

Definition at line 43 of file progress_reporter_base.cpp.

44 {
45  m_phase.store( aPhase );
46  m_progress.store( 0 );
47 }

References PROGRESS_REPORTER_BASE::m_phase, and PROGRESS_REPORTER_BASE::m_progress.

◆ currentProgress()

int PROGRESS_REPORTER_BASE::currentProgress ( ) const
protectedinherited

Definition at line 103 of file progress_reporter_base.cpp.

104 {
105  double current = ( 1.0 / (double) m_numPhases ) *
106  ( (double) m_phase + ( (double) m_progress.load() / (double) m_maxProgress ) );
107 
108  return (int)( current * 1000 );
109 }

References PROGRESS_REPORTER_BASE::m_maxProgress, PROGRESS_REPORTER_BASE::m_numPhases, PROGRESS_REPORTER_BASE::m_phase, and PROGRESS_REPORTER_BASE::m_progress.

Referenced by updateUI(), and GAUGE_PROGRESS_REPORTER::updateUI().

◆ IsCancelled()

bool PROGRESS_REPORTER_BASE::IsCancelled ( ) const
inlineoverridevirtualinherited

Implements PROGRESS_REPORTER.

Definition at line 108 of file progress_reporter_base.h.

108 { return m_cancelled; }

References PROGRESS_REPORTER_BASE::m_cancelled.

◆ KeepRefreshing()

bool PROGRESS_REPORTER_BASE::KeepRefreshing ( bool  aWait = false)
overridevirtualinherited

Update the UI dialog.

Warning
This should only be called from the main thread.
Returns
false if the user clicked Cancel.

Implements PROGRESS_REPORTER.

Definition at line 112 of file progress_reporter_base.cpp.

113 {
114  if( aWait )
115  {
116  while( m_progress.load() < m_maxProgress && m_maxProgress > 0 )
117  {
118  if( !updateUI() )
119  {
120  m_cancelled.store( true );
121  return false;
122  }
123 
124  wxMilliSleep( 20 );
125  }
126 
127  return true;
128  }
129  else
130  {
131  if( !updateUI() )
132  {
133  m_cancelled.store( true );
134  return false;
135  }
136 
137  return true;
138  }
139 }
virtual bool updateUI()=0

References PROGRESS_REPORTER_BASE::m_cancelled, PROGRESS_REPORTER_BASE::m_maxProgress, PROGRESS_REPORTER_BASE::m_progress, and PROGRESS_REPORTER_BASE::updateUI().

◆ Report()

void PROGRESS_REPORTER_BASE::Report ( const wxString &  aMessage)
overridevirtualinherited

Display aMessage in the progress bar dialog.

Implements PROGRESS_REPORTER.

Reimplemented in DIALOG_ERC.

Definition at line 64 of file progress_reporter_base.cpp.

65 {
66  std::lock_guard<std::mutex> guard( m_mutex );
67  m_rptMessage = aMessage;
68  m_msgChanged = true;
69 }

References PROGRESS_REPORTER_BASE::m_msgChanged, PROGRESS_REPORTER_BASE::m_mutex, and PROGRESS_REPORTER_BASE::m_rptMessage.

Referenced by PROGRESS_REPORTER_BASE::AdvancePhase().

◆ SetCurrentProgress()

void PROGRESS_REPORTER_BASE::SetCurrentProgress ( double  aProgress)
overridevirtualinherited

Set the progress value to aProgress (0..1).

Implements PROGRESS_REPORTER.

Reimplemented in CONSOLE_PROGRESS_REPORTER.

Definition at line 78 of file progress_reporter_base.cpp.

79 {
80  m_maxProgress.store( 1000 );
81  m_progress.store( (int) ( aProgress * 1000.0 ) );
82 }

References PROGRESS_REPORTER_BASE::m_maxProgress, and PROGRESS_REPORTER_BASE::m_progress.

Referenced by DIALOG_ERC::AdvancePhase(), DIALOG_DRC::AdvancePhase(), and CONSOLE_PROGRESS_REPORTER::SetCurrentProgress().

◆ SetMaxProgress()

void PROGRESS_REPORTER_BASE::SetMaxProgress ( int  aMaxProgress)
overridevirtualinherited

Fix the value that gives the 100 percent progress bar length (inside the current virtual zone).

Implements PROGRESS_REPORTER.

Definition at line 72 of file progress_reporter_base.cpp.

73 {
74  m_maxProgress.store( aMaxProgress );
75 }

References PROGRESS_REPORTER_BASE::m_maxProgress.

◆ SetNumPhases()

void PROGRESS_REPORTER_BASE::SetNumPhases ( int  aNumPhases)
overridevirtualinherited

Set the number of phases.

Implements PROGRESS_REPORTER.

Definition at line 91 of file progress_reporter_base.cpp.

92 {
93  m_numPhases = aNumPhases;
94 }

References PROGRESS_REPORTER_BASE::m_numPhases.

◆ SetTitle()

void WX_PROGRESS_REPORTER::SetTitle ( const wxString &  aTitle)
inlineoverridevirtual

Change the title displayed on the window caption.

Reimplemented from PROGRESS_REPORTER_BASE.

Definition at line 69 of file wx_progress_reporters.h.

70  {
71  wxProgressDialog::SetTitle( aTitle );
72  }

◆ updateUI()

bool WX_PROGRESS_REPORTER::updateUI ( )
overrideprivatevirtual

Implements PROGRESS_REPORTER_BASE.

Definition at line 63 of file wx_progress_reporters.cpp.

64 {
65  int cur = currentProgress();
66 
67  if( cur < 0 || cur > 1000 )
68  cur = 0;
69 
70  bool msgChanged = false;
71  wxString message;
72 
73  {
74  std::lock_guard<std::mutex> guard( m_mutex );
75  message = m_rptMessage;
76  msgChanged = m_msgChanged;
77  m_msgChanged = false;
78  }
79 
80  SetRange( 1000 );
81  bool diag = wxProgressDialog::Update( cur, message );
82 
83  if( msgChanged )
84  Fit();
85 
86  return diag;
87 }

References PROGRESS_REPORTER_BASE::currentProgress(), PROGRESS_REPORTER_BASE::m_msgChanged, PROGRESS_REPORTER_BASE::m_mutex, and PROGRESS_REPORTER_BASE::m_rptMessage.

Member Data Documentation

◆ m_cancelled

std::atomic_bool PROGRESS_REPORTER_BASE::m_cancelled
protectedinherited

◆ m_maxProgress

◆ m_msgChanged

bool PROGRESS_REPORTER_BASE::m_msgChanged
protectedinherited

Definition at line 116 of file progress_reporter_base.h.

Referenced by PROGRESS_REPORTER_BASE::Report(), and updateUI().

◆ m_mutex

std::mutex PROGRESS_REPORTER_BASE::m_mutex
mutableprotectedinherited

Definition at line 118 of file progress_reporter_base.h.

Referenced by PROGRESS_REPORTER_BASE::Report(), and updateUI().

◆ m_numPhases

std::atomic_int PROGRESS_REPORTER_BASE::m_numPhases
protectedinherited

◆ m_phase

std::atomic_int PROGRESS_REPORTER_BASE::m_phase
protectedinherited

◆ m_progress

◆ m_rptMessage

wxString PROGRESS_REPORTER_BASE::m_rptMessage
protectedinherited

The documentation for this class was generated from the following files: