-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
background_jobs_monitor.cpp
373 lines (293 loc) · 10.9 KB
/
background_jobs_monitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 Mark Roszko <[email protected]>
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <unordered_map>
#include <wx/gauge.h>
#include <wx/frame.h>
#include <wx/panel.h>
#include <wx/settings.h>
#include <wx/scrolwin.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/string.h>
#include <background_jobs_monitor.h>
#include <widgets/kistatusbar.h>
class BACKGROUND_JOB_PANEL : public wxPanel
{
public:
BACKGROUND_JOB_PANEL( wxWindow* aParent, std::shared_ptr<BACKGROUND_JOB> aJob ) :
wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxSize( -1, 75 ),
wxBORDER_SIMPLE ),
m_job( aJob )
{
SetSizeHints( wxDefaultSize, wxDefaultSize );
wxBoxSizer* mainSizer;
mainSizer = new wxBoxSizer( wxVERTICAL );
SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) );
m_stName = new wxStaticText( this, wxID_ANY, aJob->m_name );
m_stName->Wrap( -1 );
m_stName->SetFont( wxFont( wxNORMAL_FONT->GetPointSize(), wxFONTFAMILY_DEFAULT,
wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxEmptyString ) );
mainSizer->Add( m_stName, 0, wxALL | wxEXPAND, 1 );
m_stStatus = new wxStaticText( this, wxID_ANY, aJob->m_status, wxDefaultPosition,
wxDefaultSize, 0 );
m_stStatus->Wrap( -1 );
mainSizer->Add( m_stStatus, 0, wxALL | wxEXPAND, 1 );
m_progress = new wxGauge( this, wxID_ANY, aJob->m_maxProgress, wxDefaultPosition, wxDefaultSize,
wxGA_HORIZONTAL );
m_progress->SetValue( 0 );
mainSizer->Add( m_progress, 0, wxALL | wxEXPAND, 1 );
SetSizer( mainSizer );
Layout();
UpdateFromJob();
}
void UpdateFromJob()
{
m_stStatus->SetLabelText( m_job->m_status );
m_progress->SetValue( m_job->m_currentProgress );
m_progress->SetRange( m_job->m_maxProgress );
}
private:
wxGauge* m_progress;
wxStaticText* m_stName;
wxStaticText* m_stStatus;
std::shared_ptr<BACKGROUND_JOB> m_job;
};
class BACKGROUND_JOB_LIST : public wxFrame
{
public:
BACKGROUND_JOB_LIST( wxWindow* parent, const wxPoint& pos ) :
wxFrame( parent, wxID_ANY, _( "Background Jobs" ), pos, wxSize( 300, 150 ),
wxFRAME_NO_TASKBAR | wxBORDER_SIMPLE )
{
SetSizeHints( wxDefaultSize, wxDefaultSize );
wxBoxSizer* bSizer1;
bSizer1 = new wxBoxSizer( wxVERTICAL );
m_scrolledWindow = new wxScrolledWindow( this, wxID_ANY, wxDefaultPosition,
wxSize( -1, -1 ), wxVSCROLL );
m_scrolledWindow->SetScrollRate( 5, 5 );
m_contentSizer = new wxBoxSizer( wxVERTICAL );
m_scrolledWindow->SetSizer( m_contentSizer );
m_scrolledWindow->Layout();
m_contentSizer->Fit( m_scrolledWindow );
bSizer1->Add( m_scrolledWindow, 1, wxEXPAND | wxALL, 0 );
Bind( wxEVT_KILL_FOCUS, &BACKGROUND_JOB_LIST::onFocusLoss, this );
SetSizer( bSizer1 );
Layout();
SetFocus();
}
void onFocusLoss( wxFocusEvent& aEvent )
{
Close( true );
aEvent.Skip();
}
void Add( std::shared_ptr<BACKGROUND_JOB> aJob )
{
BACKGROUND_JOB_PANEL* panel = new BACKGROUND_JOB_PANEL( m_scrolledWindow, aJob );
m_contentSizer->Add( panel, 0, wxEXPAND | wxALL, 2 );
m_scrolledWindow->Layout();
m_contentSizer->Fit( m_scrolledWindow );
// call this at this window otherwise the child panels dont resize width properly
Layout();
m_jobPanels[aJob] = panel;
}
void Remove( std::shared_ptr<BACKGROUND_JOB> aJob )
{
auto it = m_jobPanels.find( aJob );
if( it != m_jobPanels.end() )
{
BACKGROUND_JOB_PANEL* panel = m_jobPanels[aJob];
m_contentSizer->Detach( panel );
panel->Destroy();
m_jobPanels.erase( it );
}
}
void UpdateJob( std::shared_ptr<BACKGROUND_JOB> aJob )
{
auto it = m_jobPanels.find( aJob );
if( it != m_jobPanels.end() )
{
BACKGROUND_JOB_PANEL* panel = m_jobPanels[aJob];
panel->UpdateFromJob();
}
}
private:
wxScrolledWindow* m_scrolledWindow;
wxBoxSizer* m_contentSizer;
std::unordered_map<std::shared_ptr<BACKGROUND_JOB>, BACKGROUND_JOB_PANEL*> m_jobPanels;
};
BACKGROUND_JOB_REPORTER::BACKGROUND_JOB_REPORTER( BACKGROUND_JOBS_MONITOR* aMonitor,
std::shared_ptr<BACKGROUND_JOB> aJob ) :
PROGRESS_REPORTER_BASE( 1 ),
m_monitor( aMonitor ), m_job( aJob )
{
}
bool BACKGROUND_JOB_REPORTER::updateUI()
{
return true;
}
void BACKGROUND_JOB_REPORTER::Report( const wxString& aMessage )
{
m_job->m_status = aMessage;
m_monitor->jobUpdated( m_job );
}
void BACKGROUND_JOB_REPORTER::SetNumPhases( int aNumPhases )
{
PROGRESS_REPORTER_BASE::SetNumPhases( aNumPhases );
m_job->m_maxProgress = m_numPhases;
m_monitor->jobUpdated( m_job );
}
void BACKGROUND_JOB_REPORTER::AdvancePhase()
{
PROGRESS_REPORTER_BASE::AdvancePhase();
m_job->m_currentProgress = m_phase;
m_monitor->jobUpdated( m_job );
}
BACKGROUND_JOBS_MONITOR::BACKGROUND_JOBS_MONITOR()
{
}
std::shared_ptr<BACKGROUND_JOB> BACKGROUND_JOBS_MONITOR::Create( const wxString& aName )
{
std::shared_ptr<BACKGROUND_JOB> job = std::make_shared<BACKGROUND_JOB>();
job->m_name = aName;
job->m_reporter = std::make_shared<BACKGROUND_JOB_REPORTER>( this, job );
std::lock_guard<std::shared_mutex> lock( m_mutex );
m_jobs.push_back( job );
if( m_shownDialogs.size() > 0 )
{
// update dialogs
for( BACKGROUND_JOB_LIST* list : m_shownDialogs )
{
list->CallAfter(
[=]()
{
list->Add( job );
} );
}
}
return job;
}
void BACKGROUND_JOBS_MONITOR::Remove( std::shared_ptr<BACKGROUND_JOB> aJob )
{
if( m_shownDialogs.size() > 0 )
{
// update dialogs
for( BACKGROUND_JOB_LIST* list : m_shownDialogs )
{
list->CallAfter(
[=]()
{
list->Remove( aJob );
} );
}
}
std::lock_guard<std::shared_mutex> lock( m_mutex );
m_jobs.erase( std::remove_if( m_jobs.begin(), m_jobs.end(),
[&]( std::shared_ptr<BACKGROUND_JOB> job )
{
return job == aJob;
} ) );
if( m_jobs.size() == 0 )
{
for( KISTATUSBAR* statusBar : m_statusBars )
{
statusBar->CallAfter(
[=]()
{
statusBar->HideBackgroundProgressBar();
statusBar->SetBackgroundStatusText( wxT( "" ) );
} );
}
}
}
void BACKGROUND_JOBS_MONITOR::onListWindowClosed( wxCloseEvent& aEvent )
{
BACKGROUND_JOB_LIST* evtWindow = dynamic_cast<BACKGROUND_JOB_LIST*>( aEvent.GetEventObject() );
m_shownDialogs.erase( std::remove_if( m_shownDialogs.begin(), m_shownDialogs.end(),
[&]( BACKGROUND_JOB_LIST* dialog )
{
return dialog == evtWindow;
} ) );
aEvent.Skip();
}
void BACKGROUND_JOBS_MONITOR::ShowList( wxWindow* aParent, wxPoint aPos )
{
BACKGROUND_JOB_LIST* list = new BACKGROUND_JOB_LIST( aParent, aPos );
std::shared_lock<std::shared_mutex> lock( m_mutex, std::try_to_lock );
for( std::shared_ptr<BACKGROUND_JOB> job : m_jobs )
{
list->Add( job );
}
lock.unlock();
m_shownDialogs.push_back( list );
list->Bind( wxEVT_CLOSE_WINDOW, &BACKGROUND_JOBS_MONITOR::onListWindowClosed, this );
// correct the position
wxSize windowSize = list->GetSize();
list->SetPosition( aPos - windowSize );
list->Show();
}
void BACKGROUND_JOBS_MONITOR::jobUpdated( std::shared_ptr<BACKGROUND_JOB> aJob )
{
std::shared_lock<std::shared_mutex> lock( m_mutex, std::try_to_lock );
// this method is called from the reporters from potentially other threads
// we have to guard ui calls with CallAfter
if( m_jobs.size() > 0 )
{
//for now, we go and update the status bar if its the first job in the vector
if( m_jobs.front() == aJob )
{
// update all status bar entries
for( KISTATUSBAR* statusBar : m_statusBars )
{
statusBar->CallAfter(
[=]()
{
statusBar->ShowBackgroundProgressBar();
statusBar->SetBackgroundProgress( aJob->m_currentProgress );
statusBar->SetBackgroundProgressMax( aJob->m_maxProgress );
statusBar->SetBackgroundStatusText( aJob->m_status );
} );
}
}
}
for( BACKGROUND_JOB_LIST* list : m_shownDialogs )
{
list->CallAfter(
[=]()
{
list->UpdateJob( aJob );
} );
}
}
void BACKGROUND_JOBS_MONITOR::RegisterStatusBar( KISTATUSBAR* aStatusBar )
{
m_statusBars.push_back( aStatusBar );
}
void BACKGROUND_JOBS_MONITOR::UnregisterStatusBar( KISTATUSBAR* aStatusBar )
{
m_statusBars.erase( std::remove_if( m_statusBars.begin(), m_statusBars.end(),
[&]( KISTATUSBAR* statusBar )
{
return statusBar == aStatusBar;
} ) );
}