Debian Codegraph @

src/plugins/schedulers/tj/PlanTJPlugin.cpp

raw
1 /* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2009, 2011 Dag Andersen <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 // clazy:excludeall=qstring-arg
8 #include "PlanTJPlugin.h"
9
10 #include "PlanTJScheduler.h"
11
12 #include "kptproject.h"
13 #include "kptschedule.h"
14
15 #include "kptdebug.h"
16
17 #include <KLocalizedString>
18 #include <KPluginFactory>
19
20 #include <QApplication>
21
22 #ifndef PLAN_NOPLUGIN
23 K_PLUGIN_CLASS_WITH_JSON(PlanTJPlugin, "plantjscheduler.json")
24 #endif
25
26 using namespace KPlato;
27
28 PlanTJPlugin::PlanTJPlugin(QObject * parent, const QVariantList &)
29 : KPlato::SchedulerPlugin(parent)
30 {
31 m_granularities << (long unsigned int) 5 * 60 * 1000
32 << (long unsigned int) 15 * 60 * 1000
33 << (long unsigned int) 30 * 60 * 1000
34 << (long unsigned int) 60 * 60 * 1000;
35 }
36
37 PlanTJPlugin::~PlanTJPlugin()
38 {
39 }
40
41 QString PlanTJPlugin::description() const
42 {
43 return xi18nc("@info:whatsthis", "<title>TaskJuggler Scheduler</title>"
44 "<para>This is a slightly modified version of the scheduler used in TaskJuggler."
45 " It has been enhanced to handle resource units.</para>"
46 "<para>Scheduling backwards is simulated by scheduling all tasks as late as possible.</para>"
47 "<para><note>Plan does not utilize all of its functionality.</note></para>"
48 );
49 }
50
51 int PlanTJPlugin::capabilities() const
52 {
53 return SchedulerPlugin::AvoidOverbooking | SchedulerPlugin::ScheduleForward | SchedulerPlugin::ScheduleBackward | SchedulerPlugin::ScheduleInParallel;
54 }
55
56 ulong PlanTJPlugin::currentGranularity() const
57 {
58 ulong v = m_granularities.value(m_granularityIndex);
59 return qMax(v, (ulong)300000); // minimum 5 min
60 }
61
62 void PlanTJPlugin::calculate(KPlato::Project &project, KPlato::ScheduleManager *sm, bool nothread)
63 {
64 for (SchedulerThread *j : std::as_const(m_jobs)) {
65 if (j->manager() == sm) {
66 return;
67 }
68 }
69 sm->setScheduling(true);
70
71 PlanTJScheduler *job = new PlanTJScheduler(&project, sm, currentGranularity());
72 m_jobs << job;
73 connect(job, &KPlato::SchedulerThread::jobFinished, this, &PlanTJPlugin::slotFinished);
74
75 project.changed(sm);
76
77 connect(this, SIGNAL(sigCalculationStarted(KPlato::Project*,KPlato::ScheduleManager*)), &project, SIGNAL(sigCalculationStarted(KPlato::Project*,KPlato::ScheduleManager*)));
78 connect(this, SIGNAL(sigCalculationFinished(KPlato::Project*,KPlato::ScheduleManager*)), &project, SIGNAL(sigCalculationFinished(KPlato::Project*,KPlato::ScheduleManager*)));
79
80 connect(job, &KPlato::SchedulerThread::maxProgressChanged, sm, &KPlato::ScheduleManager::setMaxProgress);
81 connect(job, &KPlato::SchedulerThread::progressChanged, sm, &KPlato::ScheduleManager::setProgress);
82
83 if (nothread) {
84 job->doRun();
85 } else {
86 job->start();
87 }
88 }
89
90 void PlanTJPlugin::stopAllCalculations()
91 {
92 for (SchedulerThread *s : std::as_const(m_jobs)) {
93 stopCalculation(s);
94 }
95 }
96
97 void PlanTJPlugin::stopCalculation(SchedulerThread *sch)
98 {
99 if (sch) {
100 //FIXME: this should just call stopScheduling() and let the job finish "normally"
101 disconnect(sch, &KPlato::SchedulerThread::jobFinished, this, &PlanTJPlugin::slotFinished);
102 sch->stopScheduling();
103 // wait max 20 seconds.
104 sch->mainManager()->setCalculationResult(ScheduleManager::CalculationStopped);
105 if (! sch->wait(20000)) {
106 sch->deleteLater();
107 m_jobs.removeAt(m_jobs.indexOf(sch));
108 } else {
109 slotFinished(sch);
110 }
111 }
112 }
113
114 void PlanTJPlugin::slotStarted(SchedulerThread *job)
115 {
116 // debugPlan<<"PlanTJPlugin::slotStarted:";
117 Q_EMIT sigCalculationStarted(job->mainProject(), job->mainManager());
118 }
119
120 void PlanTJPlugin::slotFinished(SchedulerThread *j)
121 {
122 PlanTJScheduler *job = static_cast<PlanTJScheduler*>(j);
123 Project *mp = job->mainProject();
124 ScheduleManager *sm = job->mainManager();
125 //debugPlan<<"PlanTJPlugin::slotFinished:"<<mp<<sm<<job->isStopped();
126 if (job->isStopped()) {
127 sm->setCalculationResult(ScheduleManager::CalculationCanceled);
128 } else {
129 updateLog(job);
130 if (job->result > 0) {
131 sm->setCalculationResult(ScheduleManager::CalculationError);
132 } else {
133 Project *tp = job->project();
134 ScheduleManager *tm = job->manager();
135 updateProject(tp, tm, mp, sm);
136 sm->setCalculationResult(ScheduleManager::CalculationDone);
137 }
138 }
139 sm->setScheduling(false);
140
141 m_jobs.removeAt(m_jobs.indexOf(job));
142 if (m_jobs.isEmpty()) {
143 m_synctimer.stop();
144 }
145 Q_EMIT sigCalculationFinished(mp, sm);
146
147 disconnect(this, &PlanTJPlugin::sigCalculationStarted, mp, &KPlato::Project::sigCalculationStarted);
148 disconnect(this, &PlanTJPlugin::sigCalculationFinished, mp, &KPlato::Project::sigCalculationFinished);
149
150 job->deleteLater();
151 }
152
153 void PlanTJPlugin::schedule(SchedulingContext &context)
154 {
155 PlanTJScheduler *job = new PlanTJScheduler(currentGranularity());
156 m_jobs << job;
157 connect(job, &PlanTJScheduler::progressChanged, this, &PlanTJPlugin::progressChanged);
158 context.scheduleInParallel = scheduleInParallel();
159 job->schedule(context);
160 m_jobs.clear();
161 delete job;
162 }
163
164 #include "PlanTJPlugin.moc"