kplato

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