00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef KPTMAP_H
00021 #define KPTMAP_H
00022
00023
00024 #include <qmap.h>
00025 #include <qdatetime.h>
00026 #include <qstring.h>
00027 #include <qpair.h>
00028 #include <qvaluelist.h>
00029
00030 #include <kdebug.h>
00031
00032 namespace KPlato
00033 {
00034
00035 namespace Map {
00036 enum State { None=0, NonWorking=1, Working=2 };
00037 }
00038
00039 typedef QMap<QString, int> DateMapType;
00040 class DateMap : public DateMapType
00041 {
00042 public:
00043 DateMap() {}
00044 virtual ~DateMap() {}
00045
00046 virtual bool contains(QDate date) const { return DateMapType::contains(date.toString(Qt::ISODate)); }
00047
00048 void insert(QString date, int state=Map::NonWorking) {
00049
00050 if (state == Map::None)
00051 DateMapType::remove(date);
00052 else
00053 DateMapType::insert(date, state);
00054 }
00055 void insert(QDate date, int state=Map::NonWorking) { insert(date.toString(Qt::ISODate), state); }
00056
00057 void remove(QDate date) {
00058
00059 DateMapType::remove(date.toString(Qt::ISODate));
00060 }
00061
00062 int state(QString date) {
00063 DateMapType::iterator it = find(date);
00064 if (it == end()) return 0;
00065 else return it.data();
00066 }
00067 int state(QDate date) { return state(date.toString(Qt::ISODate)); }
00068
00069 bool operator==(const DateMap &m) const {
00070 return keys() == m.keys() && values() == m.values();
00071 }
00072 bool operator!=(const DateMap &m) const {
00073 return keys() != m.keys() || values() != m.values();
00074 }
00075
00076
00077 void toggle(QString date, int state=Map::NonWorking) {
00078
00079 if (DateMapType::contains(date))
00080 DateMapType::remove(date);
00081 else
00082 DateMapType::insert(date, state);
00083 }
00084 void toggle(QDate date, int state=Map::NonWorking) { return toggle(date.toString(Qt::ISODate)); }
00085 void toggleClear(QString date, int state=Map::NonWorking) {
00086
00087 bool s = DateMapType::contains(date);
00088 clear();
00089 if (!s) insert(date, state);
00090 }
00091 void toggleClear(QDate date, int state=Map::NonWorking) { toggleClear(date.toString(Qt::ISODate)); }
00092 };
00093
00094 typedef QMap<int, int> IntMapType;
00095 class IntMap : public IntMapType
00096 {
00097 public:
00098 IntMap() {}
00099 virtual ~IntMap() {}
00100
00101 void insert(int key, int state=Map::NonWorking) {
00102 if (state == Map::None)
00103 IntMapType::remove(key);
00104 else
00105 IntMapType::insert(key, state); }
00106
00107 virtual int state(int key) {
00108 IntMapType::iterator it = IntMapType::find(key);
00109 if (it == IntMapType::end()) return 0;
00110 else return it.data();
00111 }
00112
00113 bool operator==(const IntMap &m) const {
00114 return keys() == m.keys() && values() == m.values();
00115 }
00116 bool operator!=(const IntMap &m) const {
00117 return keys() != m.keys() || values() != m.values();
00118 }
00119
00120
00121 void toggle(int key, int state=Map::NonWorking) { IntMapType::contains(key) ? remove(key) : insert(key, state); }
00122 void toggleClear(int key, int state=Map::NonWorking) {
00123 bool s =contains(key);
00124 clear();
00125 if (!s) insert(key, state);
00126 }
00127 };
00128
00129 class WeekMap : public IntMap
00130 {
00131 public:
00132 bool contains(int week, int year) { return IntMap::contains(week*10000 + year); }
00133 bool contains(QPair<int,int> week) { return contains(week.first, week.second); }
00134
00135 void insert(int week, int year, int state=Map::NonWorking) {
00136 if (week < 1 || week > 53) { kdError()<<k_funcinfo<<"Illegal week number: "<<week<<endl; return; }
00137 IntMap::insert(week*10000 + year, state);
00138 }
00139 void insert(QPair<int,int> week, int state=Map::NonWorking) { insert(week.first, week.second, state); }
00140
00141 void insert(WeekMap::iterator it, int state) { insert(week(it.key()), state); }
00142
00143 void remove(QPair<int,int> week) { IntMap::remove(week.first*10000 + week.second); }
00144
00145 static QPair<int, int> week(int key) { return QPair<int, int>(key/10000, key%10000); }
00146
00147 int state(QPair<int, int> week) { return IntMap::state(week.first*10000 + week.second); }
00148 int state(int week, int year) { return state(QPair<int, int>(week, year)); }
00149
00150 void toggle(QPair<int,int> week, int state=Map::NonWorking) {
00151 if (week.first < 1 || week.first > 53) { kdError()<<k_funcinfo<<"Illegal week number: "<<week.first<<endl; return; }
00152 IntMap::toggle(week.first*10000 + week.second, state);
00153 }
00154 void toggleClear(QPair<int,int> week, int state=Map::NonWorking) {
00155 if (week.first < 1 || week.first > 53) { kdError()<<k_funcinfo<<"Illegal week number: "<<week.first<<endl; return; }
00156 IntMap::toggleClear(week.first*10000 + week.second, state);
00157 }
00158 };
00159
00160 }
00161
00162 #endif