23
23
#include "kplatokernel_export.h"
25
#include "kptschedule.h"
27
#include "KoXmlReader.h"
41
class SchedulerThread;
32
43
class ScheduleManager;
45
class XMLLoaderObject;
48
SchedulerPlugin is the base class for project calculation plugins.
50
Sub-class SchedulerThread to do the actual calculation, then re-implement calculate()
51
to calculate the project, and slotFinished() to fetch the result into your project.
53
There is two ways to show progress:
55
<li> Connect the SchedulerThread::maxProgressChanged() to ScheduleManager::setMaxProgress() and
56
and SchedulerThread::progressChanged() to ScheduleManager::setProgress().
57
Note that too many progress signals too often may choke the ui thread.
58
<li> Start the m_synctimer. This will fetch progress and log messages every 500 ms (by default).
61
When the thread has finished scheduling, data can be fetched from its temporary project
62
into the real project by calling the updateProject() method.
34
64
class KPLATOKERNEL_EXPORT SchedulerPlugin : public QObject
52
77
/// Comment is normally set by the plugin loader, from Comment in the desktop file
53
78
void setComment( const QString &name );
80
/// Stop calculation of the schedule @p sm. Current result may be used.
81
void stopCalculation( ScheduleManager *sm );
82
/// Terminate calculation of the schedule @p sm. No results will be available.
83
void haltCalculation( ScheduleManager *sm );
85
/// Stop calculation of the scheduling @p job. Current result may be used.
86
virtual void stopCalculation( SchedulerThread *job );
87
/// Terminate calculation of the scheduling @p job. No results will be available.
88
virtual void haltCalculation( SchedulerThread *job );
90
/// Calculate the project
91
virtual void calculate( Project &project, ScheduleManager *sm, bool nothread = false ) = 0;
94
virtual void slotSyncData();
97
void updateProject( const Project *tp, const ScheduleManager *tm, Project *mp, ScheduleManager *sm ) const;
98
void updateNode( const Node *tn, Node *mn, long sid, XMLLoaderObject &status ) const;
99
void updateAppointments( const Project *tp, const ScheduleManager *tm, Project *mp, ScheduleManager *sm, XMLLoaderObject &status ) const;
101
void updateProgress();
103
void updateLog( SchedulerThread *job );
107
QList<SchedulerThread*> m_jobs;
115
SchedulerThread is a basic class used to implement project calculation in a separate thread.
116
The scheduling thread is meant to run on a private copy of the project to avoid that the ui thread
117
changes the data while calculations are going on.
119
The constructor creates a KoXmlDocument m_pdoc of the project that can be used to
120
create a private project. This should be done in the reimplemented run() method.
122
When the calculations are done the signal jobFinished() is emitted. This can be used to
123
fetch data from the private calculated project into the actual project.
125
To track progress, the progress() method should be called from the ui thread with
126
an apropriate interval to avoid overload of the ui thread.
127
The progressChanged() signal may also be used but note that async signal handling are very slow
128
so it may affect the ui threads performance too much.
130
class KPLATOKERNEL_EXPORT SchedulerThread : public QThread
134
SchedulerThread( Project *project, ScheduleManager *manager, QObject *parent );
137
Project *mainProject() const { return m_mainproject; }
138
ScheduleManager *mainManager() const { return m_mainmanager; }
140
Project *project() const;
141
ScheduleManager *manager() const;
143
/// Run with no thread
146
/// The scheduling is stopping
147
bool isStopped() const { return m_stopScheduling; }
148
/// The scheduling is halting
149
bool isHalted() const { return m_haltScheduling; }
151
int maxProgress() const;
152
int progress() const;
153
QList<Schedule::Log> log();
155
/// Save the @p project into @p document
156
static void saveProject( Project *project, QDomDocument &document );
157
/// Load the @p project from @p document
158
static bool loadProject( Project *project, const KoXmlDocument &document );
162
void jobStarted( SchedulerThread *job );
164
void jobFinished( SchedulerThread *job );
166
/// Maximum progress value has changed
167
void maxProgressChanged( int value, ScheduleManager *sm = 0 );
168
/// Progress has changed
169
void progressChanged( int value, ScheduleManager *sm = 0 );
172
/// Stop scheduling. Result may still be used.
173
virtual void stopScheduling();
174
/// Halt scheduling. Discard result.
175
virtual void haltScheduling();
179
virtual void slotStarted();
180
virtual void slotFinished();
182
void setMaxProgress( int );
183
void setProgress( int );
185
void slotAddLog( Schedule::Log log );
188
/// Re-implement to do the job
189
virtual void run() {}
192
/// The actual project to be calculated. Not accessed outside constructor.
193
Project *m_mainproject;
194
/// The actual schedule manager to be calculated. Not accessed outside constructor.
195
ScheduleManager *m_mainmanager;
196
/// The schedule manager identity
197
QString m_mainmanagerId;
199
/// The temporary project
201
mutable QMutex m_projectMutex;
202
/// The temporary schedule manager
203
ScheduleManager *m_manager;
204
mutable QMutex m_managerMutex;
206
bool m_stopScheduling; /// Stop asap, preliminary result may be used
207
bool m_haltScheduling; /// Stop and discrad result. Delete yourself.
209
KoXmlDocument m_pdoc;
212
mutable QMutex m_maxprogressMutex;
214
mutable QMutex m_progressMutex;
215
QList<Schedule::Log> m_logs;
216
mutable QMutex m_logMutex;
60
219
} //namespace KPlato