kplato

kpteffortcostmap.h
1/* This file is part of the KDE project
2 Copyright (C) 2005 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;
7 version 2 of the License.
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 KPTEFFORTCOST_H
21#define KPTEFFORTCOST_H
22
23#include <tqdatetime.h>
24#include <tqmap.h>
25
26#include "kptduration.h"
27
28#include <kdebug.h>
29
30namespace KPlato
31{
32
33class EffortCost
34{
35public:
36 EffortCost()
37 : m_effort(Duration::zeroDuration),
38 m_cost(0)
39 {}
40 EffortCost(const Duration &effort, const double cost)
41 : m_effort(effort),
42 m_cost(cost) {
43 //kdDebug()<<k_funcinfo<<endl;
44 }
45 ~EffortCost() {
46 //kdDebug()<<k_funcinfo<<endl;
47 }
48 Duration effort() const { return m_effort; }
49 double cost() const { return m_cost; }
50 void setCost(double cost) { m_cost = cost; }
51 void add(const Duration &effort, const double cost) {
52 m_effort += effort;
53 m_cost += cost;
54 }
55 EffortCost &operator+=(const EffortCost &ec) {
56 add(ec.effort(), ec.cost());
57 return *this;
58 }
59private:
60 Duration m_effort;
61 double m_cost;
62};
63typedef TQMap<TQDate, EffortCost> EffortCostDayMap;
64class EffortCostMap
65{
66public:
67 EffortCostMap()
68 : m_days() {
69 //kdDebug()<<k_funcinfo<<endl;
70 }
71 ~EffortCostMap() {
72 //kdDebug()<<k_funcinfo<<endl;
73 m_days.clear();
74 }
75
76 EffortCost effortCost(const TQDate &date) const {
77 EffortCost ec;
78 if (!date.isValid()) {
79 kdError()<<k_funcinfo<<"Date not valid"<<endl;
80 return ec;
81 }
82 EffortCostDayMap::const_iterator it = m_days.find(date);
83 if (it != m_days.end())
84 ec = it.data();
85 return ec;
86 }
87 void insert(const TQDate &date, const Duration &effort, const double cost) {
88 if (!date.isValid()) {
89 kdError()<<k_funcinfo<<"Date not valid"<<endl;
90 return;
91 }
92 m_days.insert(date, EffortCost(effort, cost));
93 }
98 EffortCost &add(const TQDate &date, const Duration &effort, const double cost) {
99 return add(date, EffortCost(effort, cost));
100 }
105 EffortCost &add(const TQDate &date, const EffortCost &ec) {
106 if (!date.isValid()) {
107 kdError()<<k_funcinfo<<"Date not valid"<<endl;
108 return zero();
109 }
110 //kdDebug()<<k_funcinfo<<date.toString()<<endl;
111 return m_days[date] += ec;
112 }
113
114 bool isEmpty() const {
115 return m_days.isEmpty();
116 }
117
118 EffortCostDayMap days() const { return m_days; }
119
120 EffortCostMap &operator+=(const EffortCostMap &ec) {
121 //kdDebug()<<k_funcinfo<<"me="<<m_days.count()<<" ec="<<ec.days().count()<<endl;
122 if (ec.isEmpty()) {
123 return *this;
124 }
125 if (isEmpty()) {
126 m_days = ec.days();
127 return *this;
128 }
129 EffortCostDayMap::const_iterator it;
130 for(it = ec.days().constBegin(); it != ec.days().constEnd(); ++it) {
131 add(it.key(), it.data());
132 }
133 return *this;
134 }
135 EffortCost &effortCostOnDate(const TQDate &date) {
136 return m_days[date];
137 }
139 double cost(const TQDate &date, int num=7) {
140 double r=0.0;
141 for (int i=0; i < num; ++i) {
142 r += costOnDate(date.addDays(i));
143 }
144 return r;
145 }
146 double costOnDate(const TQDate &date) const {
147 if (!date.isValid()) {
148 kdError()<<k_funcinfo<<"Date not valid"<<endl;
149 return 0.0;
150 }
151 if (m_days.contains(date)) {
152 return m_days[date].cost();
153 }
154 return 0.0;
155 }
156 Duration effortOnDate(const TQDate &date) const {
157 if (!date.isValid()) {
158 kdError()<<k_funcinfo<<"Date not valid"<<endl;
160 }
161 if (m_days.contains(date)) {
162 return m_days[date].effort();
163 }
165 }
166 double totalCost() const {
167 double cost = 0.0;
168 EffortCostDayMap::const_iterator it;
169 for(it = m_days.constBegin(); it != m_days.constEnd(); ++it) {
170 cost += it.data().cost();
171 }
172 return cost;
173 }
174 Duration totalEffort() const {
175 Duration eff;
176 EffortCostDayMap::const_iterator it;
177 for(it = m_days.constBegin(); it != m_days.constEnd(); ++it) {
178 eff += it.data().effort();
179 }
180 return eff;
181 }
182
183private:
184 EffortCost &zero() { return m_zero; }
185
186private:
187 EffortCost m_zero;
188 EffortCostDayMap m_days;
189};
190
191} //namespace KPlato
192
193#endif
static const Duration zeroDuration
This is useful for occasions where we need a zero duration.