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

Aller à la documentation de ce fichier.
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>
00025 #include <qdatetime.h>
00026 #include <QString>
00027 #include <QPair>
00028 
00029 #include <kdebug.h>
00030 
00031 namespace KPlato
00032 {
00033 
00034 namespace Map {
00035 enum State { None=0, NonWorking=1, Working=2 };
00036 } // Map namespace
00037 
00038 typedef QMap<QString, int> DateMapType;
00039 class DateMap : public DateMapType
00040 {
00041 public:
00042     DateMap() {}
00043     virtual ~DateMap() {}
00044 
00045     virtual bool contains(QDate date) const { return DateMapType::contains(date.toString(Qt::ISODate)); }
00046 
00047     void insert(QString date, int state=Map::NonWorking) {
00048         //kDebug()<<k_funcinfo<<date<<"="<<state<<endl;
00049         if (state == Map::None)
00050             DateMapType::remove(date);
00051         else
00052             DateMapType::insert(date, state);
00053     }
00054     void insert(QDate date, int state=Map::NonWorking) { insert(date.toString(Qt::ISODate), state); }
00055 
00056     void remove(QDate date) {
00057         //kDebug()<<k_funcinfo<<date.toString(Qt::ISODate)<<endl;
00058         DateMapType::remove(date.toString(Qt::ISODate));
00059     }
00060 
00061     int state(QString date) {
00062         DateMapType::iterator it = find(date);
00063         if (it == end()) return 0;
00064         else return it.value();
00065     }
00066     int state(QDate date) { return state(date.toString(Qt::ISODate)); }
00067 
00068     bool operator==(const DateMap &m) const {
00069         return keys() == m.keys() && values() == m.values();
00070     }
00071     bool operator!=(const DateMap &m) const {
00072         return keys() != m.keys() || values() != m.values();
00073     }
00074 
00075     // boolean use
00076     void toggle(QString date, int state=Map::NonWorking) {
00077         //kDebug()<<k_funcinfo<<date<<"="<<state<<endl;
00078         if (DateMapType::contains(date))
00079             DateMapType::remove(date);
00080         else
00081             DateMapType::insert(date, state);
00082     }
00083     void toggle(QDate date, int state=Map::NonWorking) { return toggle(date.toString(Qt::ISODate), state); }
00084     void toggleClear(QString date, int state=Map::NonWorking) {
00085         //kDebug()<<k_funcinfo<<date<<"="<<state<<endl;
00086         bool s = DateMapType::contains(date);
00087         clear();
00088         if (!s) insert(date, state);
00089     }
00090     void toggleClear(QDate date, int state=Map::NonWorking) {
00091         toggleClear(date.toString(Qt::ISODate), state);
00092     }
00093 };
00094 
00095 typedef QMap<int, int> IntMapType;
00096 class IntMap : public IntMapType
00097 {
00098 public:
00099     IntMap() {}
00100     virtual ~IntMap() {}
00101 
00102     void insert(int key, int state=Map::NonWorking) {
00103         if (state == Map::None)
00104             IntMapType::remove(key);
00105         else
00106             IntMapType::insert(key, state); }
00107 
00108     virtual int state(int key) {
00109         IntMapType::iterator it = IntMapType::find(key);
00110         if (it == IntMapType::end()) return 0;
00111         else return it.value();
00112     }
00113 
00114     bool operator==(const IntMap &m) const {
00115         return keys() == m.keys() && values() == m.values();
00116     }
00117     bool operator!=(const IntMap &m) const {
00118         return keys() != m.keys() || values() != m.values();
00119     }
00120 
00121     // boolean use
00122     void toggle(int key, int state=Map::NonWorking) {
00123         if ( IntMapType::contains(key) )
00124             remove(key);
00125         else
00126             insert(key, state);
00127     }
00128     void toggleClear(int key, int state=Map::NonWorking) {
00129         bool s =contains(key);
00130         clear();
00131         if (!s) insert(key, state);
00132     }
00133 };
00134 
00135 class WeekMap : public IntMap
00136 {
00137 public:
00138     bool contains(int week, int year) { return IntMap::contains(week*10000 + year); }
00139     bool contains(QPair<int,int> week) { return contains(week.first,  week.second); }
00140 
00141     void insert(int week, int year, int state=Map::NonWorking) {
00142         if (week < 1 || week > 53) { kError()<<k_funcinfo<<"Illegal week number: "<<week<<endl; return; }
00143         IntMap::insert(week*10000 + year, state);
00144     }
00145     void insert(QPair<int,int> week, int state=Map::NonWorking) { insert(week.first, week.second, state); }
00146 
00147     void insert(WeekMap::iterator it, int state) { insert(week(it.key()), state); }
00148 
00149     void remove(QPair<int,int> week) { IntMap::remove(week.first*10000 + week.second); }
00150 
00151     static QPair<int, int> week(int key) { return QPair<int, int>(key/10000, key%10000); }
00152 
00153     int state(QPair<int, int> week) { return IntMap::state(week.first*10000 + week.second); }
00154     int state(int week, int year) { return state(QPair<int, int>(week, year)); }
00155 
00156     void toggle(QPair<int,int> week, int state=Map::NonWorking) {
00157         if (week.first < 1 || week.first > 53) { kError()<<k_funcinfo<<"Illegal week number: "<<week.first<<endl; return; }
00158         IntMap::toggle(week.first*10000 + week.second, state);
00159     }
00160     void toggleClear(QPair<int,int> week, int state=Map::NonWorking) {
00161         if (week.first < 1 || week.first > 53) { kError()<<k_funcinfo<<"Illegal week number: "<<week.first<<endl; return; }
00162         IntMap::toggleClear(week.first*10000 + week.second, state);
00163     }
00164 };
00165 
00166 }  //KPlato namespace
00167 
00168 #endif

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