1 /* This file is part of the Calligra project
2  * Copyright (c) 2008 Dag Andersen <danders@get2net.dk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifndef KPTNODECHARTMODEL_H
21 #define KPTNODECHARTMODEL_H
22 
23 #include "planmodels_export.h"
24 
25 #include "kptitemmodelbase.h"
26 
27 #include "kpteffortcostmap.h"
28 
29 #include <QSortFilterProxyModel>
30 
31 #include "kptdebug.h"
32 
33 #include <KChartGlobal>
34 
35 namespace KPlato
36 {
37 
38 class Resource;
39 class Project;
40 class ScheduleManager;
41 class Node;
42 
43 class PLANMODELS_EXPORT ChartProxyModel : public QSortFilterProxyModel
44 {
45     Q_OBJECT
46 public:
QSortFilterProxyModel(parent)47     explicit ChartProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
48 
49     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override {
50         //if (role == Qt::DisplayRole && orientation == Qt::Vertical) debugPlan<<"fetch:"<<orientation<<section<<mapToSource(index(0, section)).column()<<m_rejects;
51         return QSortFilterProxyModel::headerData(section, orientation, role);
52     }
53 
54     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
55         if (role == Qt::DisplayRole && ! m_zerocolumns.isEmpty()) {
56             int column = mapToSource(index).column();
57             if (m_zerocolumns.contains(column) ) {
58                 //debugPlan<<"zero:"<<index.column()<<mapToSource(index).column();
59                 return QVariant();
60             }
61         }
62         //if (role == Qt::DisplayRole) debugPlan<<"fetch:"<<index.column()<<mapToSource(index).column()<<m_rejects;
63         QVariant v = QSortFilterProxyModel::data(index, role);
64         //if (role == Qt::DisplayRole) debugPlan<<index.row()<<","<<index.column()<<"("<<columnCount()<<")"<<v;
65         return v;
66     }
setRejectColumns(const QList<int> & columns)67     void setRejectColumns(const QList<int> &columns) { beginResetModel(); m_rejects = columns; endResetModel(); }
rejectColumns()68     QList<int> rejectColumns() const { return m_rejects; }
setZeroColumns(const QList<int> & columns)69     void setZeroColumns(const QList<int> &columns) { m_zerocolumns = columns; }
zeroColumns()70     QList<int> zeroColumns() const { return m_zerocolumns; }
71 
reset()72     void reset() { beginResetModel(); endResetModel(); }
73 
74 protected:
filterAcceptsColumn(int source_column,const QModelIndex &)75     bool filterAcceptsColumn (int source_column, const QModelIndex &/*source_parent */) const override {
76         //debugPlan<<this<<source_column<<m_rejects<<(! m_rejects.contains(source_column));
77         return ! m_rejects.contains(source_column);
78     }
79 
80 private:
81     QList<int> m_rejects;
82     QList<int> m_zerocolumns;
83 };
84 
85 class PLANMODELS_EXPORT ChartItemModel : public ItemModelBase
86 {
87     Q_OBJECT
88 public:
89     enum Properties {
90         BCWSCost,
91         BCWPCost,
92         ACWPCost,
93         BCWSEffort,
94         BCWPEffort,
95         ACWPEffort,
96         SPICost,
97         CPICost,
98         SPIEffort,
99         CPIEffort
100     };
101     Q_ENUM(Properties)
102     const QMetaEnum columnMap() const override;
103 
104     explicit ChartItemModel(QObject *parent = 0);
105 
106 
107 //    virtual Qt::ItemFlags flags(const QModelIndex & index) const;
108 
109     QModelIndex parent(const QModelIndex & index) const override;
110     QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const override;
111 
112     int columnCount(const QModelIndex & parent = QModelIndex()) const override;
113     int rowCount(const QModelIndex & parent = QModelIndex()) const override;
114 
115     QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
116 
117     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
118 
119 
bcwp()120     const EffortCostMap &bcwp() const { return m_bcws; }
acwp()121     const EffortCostMap &acwp() const { return m_acwp; }
122 
123     void setProject(Project *project) override;
124 
125     void setNodes(const QList<Node*> &nodes);
126     void addNode(Node *node);
127     void clearNodes();
128     QDate startDate() const;
129     QDate endDate() const;
130     void calculate();
131 
132     void setLocalizeValues(bool on);
133 
134     int rowForDate(const QDate &date) const;
135 
136 public Q_SLOTS:
137     void setScheduleManager(KPlato::ScheduleManager *sm) override;
138     void slotNodeRemoved(KPlato::Node *node);
139     void slotNodeChanged(KPlato::Node *node);
140     void slotResourceChanged(KPlato::Resource *resource);
141     void slotResourceRemoved(const KPlato::Resource *resource);
142 
143     void slotSetScheduleManager(KPlato::ScheduleManager *sm);
144 
145 protected:
146     double bcwsEffort(int day) const;
147     double bcwpEffort(int day) const;
148     double acwpEffort(int day) const;
149     double bcwsCost(int day) const;
150     double bcwpCost(int day) const;
151     double acwpCost(int day) const;
152     double spiEffort(int day) const;
153     double cpiEffort(int day) const;
154     double spiCost(int day) const;
155     double cpiCost(int day) const;
156 
157 protected:
158     QList<Node*> m_nodes;
159     EffortCostMap m_bcws;
160     EffortCostMap m_acwp;
161     bool m_localizeValues;
162 };
163 
164 class PLANMODELS_EXPORT PerformanceDataCurrentDateModel : public QAbstractProxyModel
165 {
166     Q_OBJECT
167 public:
168     explicit PerformanceDataCurrentDateModel(QObject *parent = nullptr);
169 
170     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
171     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
172     QModelIndex parent(const QModelIndex &idx) const override;
173     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
174     QVariant data(const QModelIndex &proxyIndex, int role = Qt::DisplayRole) const override;
175     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
176 
177     QModelIndex mapToSource(const QModelIndex &idx) const override;
178     QModelIndex mapFromSource(const QModelIndex &idx) const override;
179 
180     Project *project() const;
181     ScheduleManager *scheduleManager() const;
182     bool isReadWrite() const;
183 
184     void setNodes(const QList<Node*> &nodes);
185     void addNode(Node *node);
186     void clearNodes();
187 
188     QDate startDate() const;
189     QDate endDate() const;
190 
191 public Q_SLOTS:
192     virtual void setProject(KPlato::Project *project);
193     virtual void setScheduleManager(KPlato::ScheduleManager *sm);
194     virtual void setReadWrite(bool rw);
195 
196 };
197 
198 } //namespace KPlato
199 
200 #endif
201