kplato

kptmap.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 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 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 } // Map namespace
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         //kdDebug()<<k_funcinfo<<date<<"="<<state<<endl;
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         //kdDebug()<<k_funcinfo<<date.toString(Qt::ISODate)<<endl;
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     // boolean use
00077     void toggle(QString date, int state=Map::NonWorking) {
00078         //kdDebug()<<k_funcinfo<<date<<"="<<state<<endl;
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         //kdDebug()<<k_funcinfo<<date<<"="<<state<<endl;
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     // boolean use
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 }  //KPlato namespace
00161 
00162 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys