F:/KPlato/koffice/kplato/kpteffortcostmap.h

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Dag Andersen <danders@get2net.dk>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation;
00007    version 2 of the License.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef KPTEFFORTCOST_H
00021 #define KPTEFFORTCOST_H
00022 
00023 #include <qdatetime.h>
00024 #include <QMap>
00025 
00026 #include "kptduration.h"
00027 
00028 #include <kdebug.h>
00029 
00030 namespace KPlato
00031 {
00032 
00033 class EffortCost
00034 {
00035 public:
00036     EffortCost()
00037         : m_effort(Duration::zeroDuration),
00038           m_cost(0)
00039     {}
00040     EffortCost(const Duration &effort, const double cost)
00041         : m_effort(effort),
00042           m_cost(cost) {
00043         //kDebug()<<k_funcinfo<<endl;
00044     }
00045     ~EffortCost() {
00046         //kDebug()<<k_funcinfo<<endl;
00047     }
00048     Duration effort() const { return m_effort; }
00049     double cost() const { return m_cost; }
00050     void setCost(double cost) { m_cost = cost; }
00051     void add(const Duration &effort, const double cost) {
00052         m_effort += effort;
00053         m_cost += cost;
00054     }
00055     EffortCost &operator+=(const EffortCost &ec) {
00056         add(ec.effort(), ec.cost());
00057         return *this;
00058     }
00059 private:
00060     Duration m_effort;
00061     double m_cost;
00062 };
00063 typedef QMap<QDate, EffortCost> EffortCostDayMap;
00064 class EffortCostMap
00065 {
00066 public:
00067     EffortCostMap()
00068         : m_days() {
00069         //kDebug()<<k_funcinfo<<endl; 
00070     }
00071     ~EffortCostMap() {
00072         //kDebug()<<k_funcinfo<<endl;
00073         m_days.clear();
00074     }
00075     
00076     EffortCost effortCost(const QDate &date) const {
00077         EffortCost ec;
00078         if (!date.isValid()) {
00079             kError()<<k_funcinfo<<"Date not valid"<<endl;
00080             return ec;
00081         }
00082         EffortCostDayMap::const_iterator it = m_days.find(date);
00083         if (it != m_days.end())
00084             ec = it.value();
00085         return ec;
00086     }
00087     void insert(const QDate &date, const Duration &effort, const double cost) {
00088         if (!date.isValid()) {
00089             kError()<<k_funcinfo<<"Date not valid"<<endl;
00090             return;
00091         }
00092         m_days.insert(date, EffortCost(effort, cost));
00093     }
00098     EffortCost &add(const QDate &date, const Duration &effort, const double cost) {
00099         return add(date, EffortCost(effort, cost));
00100     }
00105     EffortCost &add(const QDate &date, const EffortCost &ec) {
00106         if (!date.isValid()) {
00107             kError()<<k_funcinfo<<"Date not valid"<<endl;
00108             return zero();
00109         }
00110         //kDebug()<<k_funcinfo<<date.toString()<<endl;
00111         return m_days[date] += ec;
00112     }
00113     
00114     bool isEmpty() const {
00115         return m_days.isEmpty();
00116     }
00117     
00118     EffortCostDayMap days() const { return m_days; }
00119     
00120     EffortCostMap &operator+=(const EffortCostMap &ec) {
00121         //kDebug()<<k_funcinfo<<"me="<<m_days.count()<<" ec="<<ec.days().count()<<endl;
00122         if (ec.isEmpty()) {
00123             return *this;
00124         }
00125         if (isEmpty()) {
00126             m_days = ec.days();
00127             return *this;
00128         }
00129         EffortCostDayMap::const_iterator it;
00130         for(it = ec.days().constBegin(); it != ec.days().constEnd(); ++it) {
00131             add(it.key(), it.value());
00132         }
00133         return *this;
00134     }
00135     EffortCost &effortCostOnDate(const QDate &date) {
00136         return m_days[date];
00137     }
00139     double cost(const QDate &date, int num=7) {
00140         double r=0.0;
00141         for (int i=0; i < num; ++i) {
00142             r += costOnDate(date.addDays(i));
00143         }
00144         return r;
00145     }
00146     double costOnDate(const QDate &date) const {
00147         if (!date.isValid()) {
00148             kError()<<k_funcinfo<<"Date not valid"<<endl;
00149             return 0.0;
00150         }
00151         if (m_days.contains(date)) {
00152             return m_days[date].cost();
00153         }
00154         return 0.0;
00155     }
00156     Duration effortOnDate(const QDate &date) const {
00157         if (!date.isValid()) {
00158             kError()<<k_funcinfo<<"Date not valid"<<endl;
00159             return Duration::zeroDuration;
00160         }
00161         if (m_days.contains(date)) {
00162             return m_days[date].effort();
00163         }
00164         return Duration::zeroDuration;
00165     }
00166     double totalCost() const {
00167         double cost = 0.0;
00168         EffortCostDayMap::const_iterator it;
00169         for(it = m_days.constBegin(); it != m_days.constEnd(); ++it) {
00170             cost += it.value().cost();
00171         }
00172         return cost;
00173     }
00174     Duration totalEffort() const {
00175         Duration eff;
00176         EffortCostDayMap::const_iterator it;
00177         for(it = m_days.constBegin(); it != m_days.constEnd(); ++it) {
00178             eff += it.value().effort();
00179         }
00180         return eff;
00181     }
00182     
00183 private:
00184     EffortCost &zero() { return m_zero; }
00185     
00186 private:
00187     EffortCost m_zero;
00188     EffortCostDayMap m_days;
00189 };
00190 
00191 } //namespace KPlato
00192 
00193 #endif

Généré le Wed Nov 22 23:40:56 2006 pour KPlato par  doxygen 1.5.1-p1