~ubuntu-branches/ubuntu/oneiric/koffice/oneiric-updates

« back to all changes in this revision

Viewing changes to kplato/libs/kernel/kptschedulerplugin.h

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* This file is part of the KDE project
2
 
  Copyright (C) 2009 Dag Andersen <danders@get2net.dk>
 
2
  Copyright (C) 2009, 2010 Dag Andersen <danders@get2net.dk>
3
3
 
4
4
  This library is free software; you can redistribute it and/or
5
5
  modify it under the terms of the GNU Library General Public
22
22
 
23
23
#include "kplatokernel_export.h"
24
24
 
 
25
#include "kptschedule.h"
 
26
 
 
27
#include "KoXmlReader.h"
 
28
 
25
29
#include <QObject>
26
30
#include <QString>
 
31
#include <QMutex>
 
32
#include <QThread>
 
33
#include <QTimer>
 
34
 
 
35
class QDomDocument;
 
36
class KLocale;
27
37
 
28
38
namespace KPlato
29
39
{
30
40
 
 
41
class SchedulerThread;
31
42
class Project;
32
43
class ScheduleManager;
33
 
 
 
44
class Node;
 
45
class XMLLoaderObject;
 
46
 
 
47
/**
 
48
 SchedulerPlugin is the base class for project calculation plugins.
 
49
 
 
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.
 
52
 
 
53
 There is two ways to show progress:
 
54
 <ul>
 
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).
 
59
 </ul>
 
60
 
 
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.
 
63
*/
34
64
class KPLATOKERNEL_EXPORT SchedulerPlugin : public QObject
35
65
{
36
66
    Q_OBJECT
38
68
    SchedulerPlugin(QObject *parent);
39
69
    virtual ~SchedulerPlugin();
40
70
 
41
 
    /// Stop calculation of the schedule @p sm
42
 
    virtual void stopCalculation( ScheduleManager *sm ) {Q_UNUSED(sm);}
43
 
    /// Calculate the project
44
 
    virtual void calculate( Project &project, ScheduleManager *sm, bool nothread = false ) = 0;
45
 
 
46
71
    /// Localized name
47
72
    QString name() const;
48
73
    /// Name is normally set by the plugin loader, from Name in the desktop file
52
77
    /// Comment is normally set by the plugin loader, from Comment in the desktop file
53
78
    void setComment( const QString &name );
54
79
 
 
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 );
 
84
    
 
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 );
 
89
    
 
90
    /// Calculate the project
 
91
    virtual void calculate( Project &project, ScheduleManager *sm, bool nothread = false ) = 0;
 
92
 
 
93
protected slots:
 
94
    virtual void slotSyncData();
 
95
 
 
96
protected:
 
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;
 
100
 
 
101
    void updateProgress();
 
102
    void updateLog();
 
103
    void updateLog( SchedulerThread *job );
 
104
 
 
105
protected:
 
106
    QTimer m_synctimer;
 
107
    QList<SchedulerThread*> m_jobs;
 
108
 
55
109
private:
56
110
    class Private;
57
111
    Private *d;
58
112
};
59
113
 
 
114
/**
 
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.
 
118
 
 
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.
 
121
 
 
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.
 
124
 
 
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.
 
129
*/
 
130
class KPLATOKERNEL_EXPORT SchedulerThread : public QThread
 
131
{
 
132
    Q_OBJECT
 
133
public:
 
134
    SchedulerThread( Project *project, ScheduleManager *manager, QObject *parent );
 
135
    ~SchedulerThread();
 
136
 
 
137
    Project *mainProject() const { return m_mainproject; }
 
138
    ScheduleManager *mainManager() const { return m_mainmanager; }
 
139
    
 
140
    Project *project() const;
 
141
    ScheduleManager *manager() const;
 
142
 
 
143
    /// Run with no thread
 
144
    void doRun();
 
145
    
 
146
    /// The scheduling is stopping
 
147
    bool isStopped() const { return m_stopScheduling; }
 
148
    /// The scheduling is halting
 
149
    bool isHalted() const { return m_haltScheduling; }
 
150
 
 
151
    int maxProgress() const;
 
152
    int progress() const;
 
153
    QList<Schedule::Log> log();
 
154
 
 
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 );
 
159
 
 
160
signals:
 
161
    /// Job has started
 
162
    void jobStarted( SchedulerThread *job );
 
163
    /// Job is finished
 
164
    void jobFinished( SchedulerThread *job );
 
165
 
 
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 );
 
170
 
 
171
public slots:
 
172
    /// Stop scheduling. Result may still be used.
 
173
    virtual void stopScheduling();
 
174
    /// Halt scheduling. Discard result.
 
175
    virtual void haltScheduling();
 
176
 
 
177
 
 
178
protected slots:
 
179
    virtual void slotStarted();
 
180
    virtual void slotFinished();
 
181
 
 
182
    void setMaxProgress( int );
 
183
    void setProgress( int );
 
184
 
 
185
    void slotAddLog( Schedule::Log log );
 
186
 
 
187
protected:
 
188
    /// Re-implement to do the job
 
189
    virtual void run() {}
 
190
 
 
191
protected:
 
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;
 
198
    
 
199
    /// The temporary project
 
200
    Project *m_project;
 
201
    mutable QMutex m_projectMutex;
 
202
    /// The temporary schedule manager
 
203
    ScheduleManager *m_manager;
 
204
    mutable QMutex m_managerMutex;
 
205
 
 
206
    bool m_stopScheduling; /// Stop asap, preliminary result may be used
 
207
    bool m_haltScheduling; /// Stop and discrad result. Delete yourself.
 
208
    
 
209
    KoXmlDocument m_pdoc;
 
210
 
 
211
    int m_maxprogress;
 
212
    mutable QMutex m_maxprogressMutex;
 
213
    int m_progress;
 
214
    mutable QMutex m_progressMutex;
 
215
    QList<Schedule::Log> m_logs;
 
216
    mutable QMutex m_logMutex;
 
217
};
 
218
 
60
219
} //namespace KPlato
61
220
 
62
221
#endif