1 /* This file is part of the KDE project
2    Copyright (C) 2004 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 KPTMAP_H
21 #define KPTMAP_H
22 
23 #include "plankernel_export.h"
24 #include "kptcalendar.h"
25 
26 #include <QMap>
27 #include <QString>
28 #include <QPair>
29 
30 
31 namespace KPlato
32 {
33 
34 typedef QMap<QString, int> DateMapType;
35 class PLANKERNEL_EXPORT DateMap : public DateMapType
36 {
37 public:
DateMap()38     DateMap() {}
~DateMap()39     virtual ~DateMap() {}
40 
contains(QDate date)41     virtual bool contains(QDate date) const { return DateMapType::contains(date.toString(Qt::ISODate)); }
42 
43     void insert(const QString &date, int state=CalendarDay::NonWorking) {
44         //debugPlan<<date<<"="<<state;
45         if (state == CalendarDay::None)
46             DateMapType::remove(date);
47         else
48             DateMapType::insert(date, state);
49     }
50     void insert(QDate date, int state=CalendarDay::NonWorking) { insert(date.toString(Qt::ISODate), state); }
51 
remove(QDate date)52     void remove(QDate date) {
53         //debugPlan<<date.toString(Qt::ISODate);
54         DateMapType::remove(date.toString(Qt::ISODate));
55     }
56 
state(const QString & date)57     int state(const QString &date) const {
58         DateMapType::ConstIterator it = find(date);
59         if (it == end()) return 0;
60         else return it.value();
61     }
state(QDate date)62     int state(QDate date) const { return state(date.toString(Qt::ISODate)); }
63 
64 //     bool operator==(const DateMap &m) const {
65 //         return keys() == m.keys() && values() == m.values();
66 //     }
67 //     bool operator!=(const DateMap &m) const {
68 //         return keys() != m.keys() || values() != m.values();
69 //     }
70 
71     // boolean use
72     void toggle(const QString &date, int state=CalendarDay::NonWorking) {
73         //debugPlan<<date<<"="<<state;
74         if (DateMapType::contains(date))
75             DateMapType::remove(date);
76         else
77             DateMapType::insert(date, state);
78     }
79     void toggle(QDate date, int state=CalendarDay::NonWorking) { return toggle(date.toString(Qt::ISODate), state); }
80     void toggleClear(const QString &date, int state=CalendarDay::NonWorking) {
81         //debugPlan<<date<<"="<<state;
82         bool s = DateMapType::contains(date);
83         clear();
84         if (!s) insert(date, state);
85     }
86     void toggleClear(QDate date, int state=CalendarDay::NonWorking) {
87         toggleClear(date.toString(Qt::ISODate), state);
88     }
89 };
90 
91 typedef QMap<int, int> IntMapType;
92 class PLANKERNEL_EXPORT IntMap : public IntMapType
93 {
94 public:
IntMap()95     IntMap() {}
~IntMap()96     virtual ~IntMap() {}
97 
98     void insert(int key, int state=CalendarDay::NonWorking) {
99         if (state == CalendarDay::None)
100             IntMapType::remove(key);
101         else
102             IntMapType::insert(key, state); }
103 
state(int key)104     virtual int state(int key) const {
105         IntMapType::ConstIterator it = IntMapType::find(key);
106         if (it == IntMapType::end()) return 0;
107         else return it.value();
108     }
109 
110 //     bool operator==(const IntMap &m) const {
111 //         return keys() == m.keys() && values() == m.values();
112 //     }
113 //     bool operator!=(const IntMap &m) const {
114 //         return keys() != m.keys() || values() != m.values();
115 //     }
116 
117     // boolean use
118     void toggle(int key, int state=CalendarDay::NonWorking) {
119         if (IntMapType::contains(key))
120             remove(key);
121         else
122             insert(key, state);
123     }
124     void toggleClear(int key, int state=CalendarDay::NonWorking) {
125         bool s =contains(key);
126         clear();
127         if (!s) insert(key, state);
128     }
129 };
130 
131 class PLANKERNEL_EXPORT WeekMap : public IntMap
132 {
133 public:
contains(int week,int year)134     bool contains(int week, int year) { return IntMap::contains(week*10000 + year); }
contains(QPair<int,int> week)135     bool contains(QPair<int, int> week) { return contains(week.first,  week.second); }
136 
137     void insert(int week, int year, int state=CalendarDay::NonWorking) {
138         if (week < 1 || week > 53) { errorPlan<<"Illegal week number: "<<week<<endl; return; }
139         IntMap::insert(week*10000 + year, state);
140     }
141     void insert(QPair<int, int> week, int state=CalendarDay::NonWorking) { insert(week.first, week.second, state); }
142 
insert(WeekMap::iterator it,int state)143     void insert(WeekMap::iterator it, int state) { insert(week(it.key()), state); }
144 
remove(QPair<int,int> week)145     void remove(QPair<int, int> week) { IntMap::remove(week.first*10000 + week.second); }
146 
week(int key)147     static QPair<int, int> week(int key) { return QPair<int, int>(key/10000, key%10000); }
148 
149     using IntMap::state;
state(QPair<int,int> week)150     int state(QPair<int, int> week) const { return IntMap::state(week.first*10000 + week.second); }
state(int week,int year)151     int state(int week, int year) const { return state(QPair<int, int>(week, year)); }
152 
153     void toggle(QPair<int, int> week, int state=CalendarDay::NonWorking) {
154         if (week.first < 1 || week.first > 53) { errorPlan<<"Illegal week number: "<<week.first<<endl; return; }
155         IntMap::toggle(week.first*10000 + week.second, state);
156     }
157     void toggleClear(QPair<int, int> week, int state=CalendarDay::NonWorking) {
158         if (week.first < 1 || week.first > 53) { errorPlan<<"Illegal week number: "<<week.first<<endl; return; }
159         IntMap::toggleClear(week.first*10000 + week.second, state);
160     }
161 };
162 
163 }  //KPlato namespace
164 
165 #endif
166