| 1 |
/* This file is part of the KDE project |
| 2 |
SPDX-FileCopyrightText: 2017 Dag Andersen <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
// clazy:excludeall=qstring-arg |
| 8 |
#include "planmodels_export.h" |
| 9 |
#include "ReportGenerator.h" |
| 10 |
#include "ReportGeneratorDebug.h" |
| 11 |
|
| 12 |
#include <kptproject.h> |
| 13 |
#include <kptschedule.h> |
| 14 |
#include <kptnodeitemmodel.h> |
| 15 |
#include <kpttaskstatusmodel.h> |
| 16 |
#include <kptitemmodelbase.h> |
| 17 |
#include <kptnodechartmodel.h> |
| 18 |
#include <kptschedulemodel.h> |
| 19 |
|
| 20 |
#include <KLocalizedString> |
| 21 |
|
| 22 |
#include <QFile> |
| 23 |
|
| 24 |
namespace KPlato |
| 25 |
{ |
| 26 |
|
| 27 |
QAbstractItemModel *translationModel() |
| 28 |
{ |
| 29 |
QList<QPair<QString, QString> > names; |
| 30 |
names << QPair<QString, QString>(QStringLiteral("Project"), i18n("Project")) |
| 31 |
<< QPair<QString, QString>(QStringLiteral("Manager"), i18n("Manager")) |
| 32 |
<< QPair<QString, QString>(QStringLiteral("Schedule"), i18n("Schedule")) |
| 33 |
<< QPair<QString, QString>(QStringLiteral("BCWS"), xi18nc("@title:column Budgeted Cost of Work Scheduled", "BCWS")) |
| 34 |
<< QPair<QString, QString>(QStringLiteral("BCWP"), xi18nc("@title:column Budgeted Cost of Work Performed", "BCWP")) |
| 35 |
<< QPair<QString, QString>(QStringLiteral("ACWP"), xi18nc("@title:column Actual Cost of Work Performed", "ACWP")) |
| 36 |
<< QPair<QString, QString>(QStringLiteral("SPI"), xi18nc("@title:column Schedule Performance Index", "SPI")) |
| 37 |
<< QPair<QString, QString>(QStringLiteral("CPI"), xi18nc("@title:column Cost Performance Index", "CPI")); |
| 38 |
|
| 39 |
QStandardItemModel *model = new QStandardItemModel(0, names.count()); |
| 40 |
for (int column = 0; column < names.count(); ++column) { |
| 41 |
model->setHeaderData(column, Qt::Horizontal, names.at(column).first, HeaderRole); |
| 42 |
model->setHeaderData(column, Qt::Horizontal, names.at(column).second); |
| 43 |
} |
| 44 |
return model; |
| 45 |
} |
| 46 |
|
| 47 |
QAbstractItemModel *projectModel() |
| 48 |
{ |
| 49 |
QList<QPair<QString, QString> > names; |
| 50 |
names << QPair<QString, QString>(QStringLiteral("Name"), i18n("Name")) |
| 51 |
<< QPair<QString, QString>(QStringLiteral("Manager"), i18n("Manager")) |
| 52 |
<< QPair<QString, QString>(QStringLiteral("BCWS Cost"), i18nc("Cost based Budgeted Cost of Work Scheduled", "BCWS Cost")) |
| 53 |
<< QPair<QString, QString>(QStringLiteral("BCWP Cost"), i18nc("Cost based Budgeted Cost of Work Performed", "BCWP Cost")) |
| 54 |
<< QPair<QString, QString>(QStringLiteral("ACWP Cost"), i18nc("Cost based Actual Cost of Work Performed", "ACWP Cost")) |
| 55 |
<< QPair<QString, QString>(QStringLiteral("SPI Cost"), i18nc("Cost based Schedule Performance Index", "SPI Cost")) |
| 56 |
<< QPair<QString, QString>(QStringLiteral("CPI Cost"), i18nc("Cost based Cost Performance Index", "CPI Cost")) |
| 57 |
<< QPair<QString, QString>(QStringLiteral("BCWS Effort"), i18nc("Effort based Budgeted Cost of Work Scheduled", "BCWS Effort")) |
| 58 |
<< QPair<QString, QString>(QStringLiteral("BCWP Effort"), i18nc("Effort based Budgeted Cost of Work Performed", "BCWP Effort")) |
| 59 |
<< QPair<QString, QString>(QStringLiteral("ACWP Effort"), i18nc("Effort based Actual Cost of Work Performed", "ACWP Effort")) |
| 60 |
<< QPair<QString, QString>(QStringLiteral("SPI Effort"), i18nc("Effort based Schedule Performance Index", "SPI Effort")) |
| 61 |
<< QPair<QString, QString>(QStringLiteral("CPI Effort"), i18nc("Effort based Cost Performance Index", "CPI Effort")); |
| 62 |
|
| 63 |
QStandardItemModel *model = new QStandardItemModel(0, names.count()); |
| 64 |
for (int column = 0; column < names.count(); ++column) { |
| 65 |
model->setHeaderData(column, Qt::Horizontal, names.at(column).first, HeaderRole); |
| 66 |
model->setHeaderData(column, Qt::Horizontal, names.at(column).second); |
| 67 |
} |
| 68 |
return model; |
| 69 |
} |
| 70 |
|
| 71 |
QAbstractItemModel *scheduleModel() |
| 72 |
{ |
| 73 |
ScheduleItemModel m; |
| 74 |
QStandardItemModel *model = new QStandardItemModel(0, m.columnCount()); |
| 75 |
for (int c = 0; c < m.columnCount(); ++c) { |
| 76 |
model->setHeaderData(c, Qt::Horizontal, m.headerData(c, Qt::Horizontal)); |
| 77 |
model->setHeaderData(c, Qt::Horizontal, m.headerData(c, Qt::Horizontal, Qt::EditRole), HeaderRole); |
| 78 |
} |
| 79 |
return model; |
| 80 |
} |
| 81 |
|
| 82 |
ReportGenerator::ReportGenerator() |
| 83 |
: m_project(nullptr) |
| 84 |
, m_manager(nullptr) |
| 85 |
, m_reportGenerator(nullptr) |
| 86 |
{ |
| 87 |
m_keys = QStringList() << QStringLiteral("table") << QStringLiteral("chart"); |
| 88 |
m_variables = QStringList() << QStringLiteral("project") << QStringLiteral("schedule"); |
| 89 |
|
| 90 |
m_basemodels |
| 91 |
<< new NodeItemModel() |
| 92 |
<< new TaskStatusItemModel() |
| 93 |
<< new ChartItemModel() |
| 94 |
<< new ScheduleItemModel() |
| 95 |
<< new NodeItemModel(); |
| 96 |
|
| 97 |
addDataModel(QStringLiteral("tasks"), m_basemodels.at(0), Qt::EditRole); |
| 98 |
addDataModel(QStringLiteral("taskstatus"), m_basemodels.at(1), Qt::EditRole); |
| 99 |
addDataModel(QStringLiteral("chart.project"), m_basemodels.at(2), Qt::EditRole); |
| 100 |
addDataModel(QStringLiteral("projects"), projectsModel(m_basemodels.at(4)), Qt::EditRole); |
| 101 |
addDataModel(QStringLiteral("schedules"), m_basemodels.at(3), Qt::EditRole); |
| 102 |
addDataModel(QStringLiteral("project"), projectModel(), HeaderRole); |
| 103 |
addDataModel(QStringLiteral("schedule"), scheduleModel(), HeaderRole); |
| 104 |
addDataModel(QStringLiteral("tr"), translationModel(), HeaderRole); |
| 105 |
|
| 106 |
// for (QAbstractItemModel *m : m_datamodels) { |
| 107 |
// const QString key = m_datamodels.key(m); |
| 108 |
// qInfo()<<key<<"columns:"<<m->columnCount(); |
| 109 |
// for (int i = 0; i < m->columnCount(); ++i) { |
| 110 |
// qInfo()<<'\t'<<i<<m->headerData(i, Qt::Horizontal, m_headerrole.value(key)).toString(); |
| 111 |
// } |
| 112 |
// } |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
ReportGenerator::~ReportGenerator() |
| 117 |
{ |
| 118 |
delete m_reportGenerator; |
| 119 |
} |
| 120 |
|
| 121 |
void ReportGenerator::setReportType(const QString &type) |
| 122 |
{ |
| 123 |
m_reportType = type; |
| 124 |
} |
| 125 |
|
| 126 |
void ReportGenerator::setTemplateFile(const QString &file) |
| 127 |
{ |
| 128 |
m_templateFile = file; |
| 129 |
} |
| 130 |
|
| 131 |
void ReportGenerator::setReportFile(const QString &file) |
| 132 |
{ |
| 133 |
m_reportFile = file; |
| 134 |
} |
| 135 |
|
| 136 |
void ReportGenerator::setProject(Project *project) |
| 137 |
{ |
| 138 |
m_project = project; |
| 139 |
} |
| 140 |
|
| 141 |
void ReportGenerator::setScheduleManager(ScheduleManager *manager) |
| 142 |
{ |
| 143 |
m_manager = manager; |
| 144 |
} |
| 145 |
|
| 146 |
ChartItemModel *ReportGenerator::findChartItemModel(QSortFilterProxyModel &model) |
| 147 |
{ |
| 148 |
for (QAbstractProxyModel *p = &model; p; p = qobject_cast<QAbstractProxyModel*>(p->sourceModel())) { |
| 149 |
ChartItemModel *c = qobject_cast<ChartItemModel*>(p->sourceModel()); |
| 150 |
if (c) { |
| 151 |
return c; |
| 152 |
} |
| 153 |
} |
| 154 |
return nullptr; |
| 155 |
} |
| 156 |
|
| 157 |
bool ReportGenerator::startsWith(const QStringList &keys, const QString &key) |
| 158 |
{ |
| 159 |
const auto lowerKey = key.toLower(); |
| 160 |
for (const QString &k : keys) { |
| 161 |
if (lowerKey.startsWith(k)) { |
| 162 |
return true; |
| 163 |
} |
| 164 |
} |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
QStringList ReportGenerator::trimmed(const QStringList &lst) |
| 169 |
{ |
| 170 |
QStringList rlst; |
| 171 |
for (const QString &s : lst) { |
| 172 |
QString r = s.trimmed(); |
| 173 |
if (!r.isEmpty()) { |
| 174 |
rlst << r; |
| 175 |
|
| 176 |
} |
| 177 |
} |
| 178 |
return rlst; |
| 179 |
} |
| 180 |
|
| 181 |
void initProjectModel(QAbstractItemModel *model, Project *project, ScheduleManager *sm) |
| 182 |
{ |
| 183 |
if (model->rowCount() == 0) { |
| 184 |
model->insertRow(0); |
| 185 |
} |
| 186 |
QModelIndex idx = model->index(0, 0); |
| 187 |
model->setData(idx, project->name()); |
| 188 |
idx = model->index(0, 1); |
| 189 |
model->setData(idx, project->leader()); |
| 190 |
|
| 191 |
PerformanceDataCurrentDateModel m(nullptr); |
| 192 |
m.setProject(project); |
| 193 |
m.setScheduleManager(sm); |
| 194 |
m.setNodes(QList<Node*>() << project); |
| 195 |
|
| 196 |
int col = 2; // column of BCWS Cost |
| 197 |
for (int r = 0; r < 2; ++r) { |
| 198 |
for (int c = 0; c < 5; ++c) { |
| 199 |
idx = model->index(0, col++); |
| 200 |
QModelIndex cidx = m.index(r, c); |
| 201 |
model->setData(idx, cidx.data()); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void initScheduleModel(QAbstractItemModel *model, Project *project, ScheduleManager *sm) |
| 207 |
{ |
| 208 |
ScheduleItemModel m; |
| 209 |
m.setProject(project); |
| 210 |
QModelIndex idx = m.index(sm); |
| 211 |
if (idx.isValid()) { |
| 212 |
if (model->rowCount() == 0) { |
| 213 |
model->insertRow(0); |
| 214 |
} |
| 215 |
for (QModelIndex i = idx; i.isValid(); i = i.sibling(i.row(), i.column() + 1)) { |
| 216 |
QModelIndex midx = model->index(0, i.column()); |
| 217 |
model->setData(midx, i.data()); |
| 218 |
dbgRGVariable<<model->headerData(midx.column(), Qt::Horizontal, HeaderRole).toString()<<'='<<i.data(Qt::UserRole+1).toString(); |
| 219 |
} |
| 220 |
} else dbgRGVariable<<"Could not find schedule"<<sm; |
| 221 |
dbgRGVariable<<model->rowCount()<<model->columnCount(); |
| 222 |
} |
| 223 |
|
| 224 |
QAbstractItemModel *ReportGenerator::projectsModel(ItemModelBase *base) const |
| 225 |
{ |
| 226 |
static_cast<NodeItemModel*>(base)->setShowProject(true); |
| 227 |
|
| 228 |
auto model = new QSortFilterProxyModel(); |
| 229 |
model->setSourceModel(base); |
| 230 |
model->setFilterRole(Qt::EditRole); |
| 231 |
model->setFilterFixedString(QString::number(Node::Type_Project)); |
| 232 |
model->setFilterKeyColumn(NodeModel::NodeType); |
| 233 |
return model; |
| 234 |
} |
| 235 |
|
| 236 |
void ReportGenerator::addDataModel(const QString &name, QAbstractItemModel *model, int role) |
| 237 |
{ |
| 238 |
model->setObjectName(name); |
| 239 |
m_datamodels[name] = model; |
| 240 |
m_headerrole[name] = role; |
| 241 |
} |
| 242 |
|
| 243 |
bool ReportGenerator::initiate() |
| 244 |
{ |
| 245 |
m_lastError.clear(); |
| 246 |
if (m_templateFile.isEmpty()) { |
| 247 |
m_lastError = i18n("Missing report template file"); |
| 248 |
return false; |
| 249 |
} |
| 250 |
if (m_reportFile.isEmpty()) { |
| 251 |
m_lastError = i18n("Missing report result file"); |
| 252 |
return false; |
| 253 |
} |
| 254 |
if (!QFile::exists(m_templateFile)) { |
| 255 |
m_lastError = i18n("Report template file does not exist"); |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
for (ItemModelBase *m : std::as_const(m_basemodels)) { |
| 260 |
m->setProject(m_project); |
| 261 |
m->setScheduleManager(m_manager); |
| 262 |
if (qobject_cast<ChartItemModel*>(m)) { |
| 263 |
static_cast<ChartItemModel*>(m)->setNodes(QList<Node*>() << m_project); |
| 264 |
dbgRGChart<<"chart:"<<m_project<<m_manager<<"set nodes"<<m_project; |
| 265 |
} |
| 266 |
} |
| 267 |
initProjectModel(m_datamodels[QStringLiteral("project")], m_project, m_manager); |
| 268 |
initScheduleModel(m_datamodels[QStringLiteral("schedule")], m_project, m_manager); |
| 269 |
static_cast<ScheduleItemModel*>(m_datamodels[QStringLiteral("schedules")])->setProject(m_project); |
| 270 |
return initiateInternal(); |
| 271 |
} |
| 272 |
|
| 273 |
bool ReportGenerator::initiateInternal() |
| 274 |
{ |
| 275 |
return true; |
| 276 |
} |
| 277 |
|
| 278 |
QString ReportGenerator::lastError() const |
| 279 |
{ |
| 280 |
if (m_reportGenerator) { |
| 281 |
return m_reportGenerator->lastError(); |
| 282 |
} |
| 283 |
return m_lastError; |
| 284 |
} |
| 285 |
|
| 286 |
} //namespace KPlato |