KiCad PCB EDA Suite
Loading...
Searching...
No Matches
background_jobs_monitor.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef BACKGROUND_JOBS_MONITOR_H
22#define BACKGROUND_JOBS_MONITOR_H
23
24#include <kicommon.h>
26#include <atomic>
27#include <functional>
28#include <memory>
29#include <mutex>
30#include <shared_mutex>
31#include <vector>
32
34class wxString;
35class KISTATUSBAR;
36struct BACKGROUND_JOB;
40class wxWindow;
41class wxCloseEvent;
42class wxWindowDestroyEvent;
43
45{
46public:
48 const std::shared_ptr<BACKGROUND_JOB>& aJob );
49
50 void SetTitle( const wxString& aTitle ) override
51 {
52 }
53
54 void Report( const wxString& aMessage ) override;
55
56 void Cancel() { m_cancelled.store( true ); }
57
58 void AdvancePhase() override;
59
60 void SetNumPhases( int aNumPhases ) override;
61
62 void SetCurrentProgress( double aProgress ) override;
63
64private:
65 bool updateUI() override;
66
68 std::shared_ptr<BACKGROUND_JOB> m_job;
69 wxString m_title;
70 wxString m_report;
71};
72
73
75{
76public:
80 wxString GetStatus() const
81 {
82 std::lock_guard<std::mutex> lock( m_statusMutex );
83 return m_status;
84 }
85
86 void SetStatus( const wxString& aStatus )
87 {
88 std::lock_guard<std::mutex> lock( m_statusMutex );
89 m_status = aStatus;
90 }
91
92 wxString m_name;
93 std::shared_ptr<BACKGROUND_JOB_REPORTER> m_reporter;
94
95 std::atomic<int> m_maxProgress;
96 std::atomic<int> m_currentProgress;
97
98private:
99 mutable std::mutex m_statusMutex;
100 wxString m_status;
101};
102
103
105{
108
109public:
111
117 std::shared_ptr<BACKGROUND_JOB> Create( const wxString& aName );
118
122 void Remove( std::shared_ptr<BACKGROUND_JOB> job );
123
127 void ShowList( wxWindow* aParent, wxPoint aPos );
128
132 void RegisterStatusBar( KISTATUSBAR* aStatusBar );
133
137 void UnregisterStatusBar( KISTATUSBAR* aStatusBar );
138
139private:
143 void onListWindowClosed( wxCloseEvent& aEvent );
144
148 void onListWindowDestroyed( wxWindowDestroyEvent& aEvent );
149
155 void removeShownDialog( wxObject* aWindow );
156
160 void jobUpdated( std::shared_ptr<BACKGROUND_JOB> aJob );
161
167 std::vector<std::shared_ptr<BACKGROUND_JOB>> m_jobs;
168 std::vector<BACKGROUND_JOB_LIST*> m_shownDialogs;
169
170 std::vector<KISTATUSBAR*> m_statusBars;
171
173 mutable std::shared_mutex m_mutex;
174};
175
176#endif
void UnregisterStatusBar(KISTATUSBAR *aStatusBar)
Removes status bar from handling.
std::vector< std::shared_ptr< BACKGROUND_JOB > > m_jobs
Holds a reference to all active background jobs Access to this vector should be protected by locks si...
void ShowList(wxWindow *aParent, wxPoint aPos)
Shows the background job list.
std::shared_mutex m_mutex
Mutex to protect access to m_jobs, m_shownDialogs and m_statusBars.
std::shared_ptr< BACKGROUND_JOB > Create(const wxString &aName)
Creates a background job with the given name.
void onListWindowDestroyed(wxWindowDestroyEvent &aEvent)
Handles a list window that its parent destroys without a close event.
void jobUpdated(std::shared_ptr< BACKGROUND_JOB > aJob)
Handles job status updates, intended to be called by BACKGROUND_JOB_REPORTER only.
void Remove(std::shared_ptr< BACKGROUND_JOB > job)
Removes the given background job from any lists and frees it.
void onListWindowClosed(wxCloseEvent &aEvent)
Handles removing the shown list window from our list of shown windows.
std::vector< KISTATUSBAR * > m_statusBars
void removeShownDialog(wxObject *aWindow)
Removes a list window from m_shownDialogs.
std::vector< BACKGROUND_JOB_LIST * > m_shownDialogs
void RegisterStatusBar(KISTATUSBAR *aStatusBar)
Add a status bar for handling.
std::shared_ptr< BACKGROUND_JOB > m_job
BACKGROUND_JOBS_MONITOR * m_monitor
BACKGROUND_JOB_REPORTER(BACKGROUND_JOBS_MONITOR *aMonitor, const std::shared_ptr< BACKGROUND_JOB > &aJob)
void SetTitle(const wxString &aTitle) override
Change the title displayed on the window caption.
KISTATUSBAR is a wxStatusBar suitable for Kicad manager.
Definition kistatusbar.h:50
virtual bool updateUI()=0
virtual void AdvancePhase() override
Use the next available virtual zone of the dialog progress bar.
virtual void SetCurrentProgress(double aProgress) override
Set the progress value to aProgress (0..1).
virtual void Report(const wxString &aMessage) override
Display aMessage in the progress bar dialog.
void SetNumPhases(int aNumPhases) override
Set the number of phases.
A progress reporter interface for use in multi-threaded environments.
#define KICOMMON_API
Definition kicommon.h:27
std::atomic< int > m_currentProgress
std::shared_ptr< BACKGROUND_JOB_REPORTER > m_reporter
std::atomic< int > m_maxProgress
void SetStatus(const wxString &aStatus)
wxString GetStatus() const
Worker threads write the status and the UI reads it so it needs a lock.