summaryrefslogtreecommitdiffstats
path: root/kplato/kptmap.h
blob: f21612c33a563897336d54b70f7237b566c2fdf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* This file is part of the KDE project
   Copyright (C) 2004 Dag Andersen <danders@get2net.dk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation;
   version 2 of the License.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef KPTMAP_H
#define KPTMAP_H


#include <tqmap.h>
#include <tqdatetime.h>
#include <tqstring.h>
#include <tqpair.h>
#include <tqvaluelist.h>

#include <kdebug.h>

namespace KPlato
{

namespace Map {
enum State { None=0, NonWorking=1, Working=2 };
} // Map namespace

typedef TQMap<TQString, int> DateMapType;
class DateMap : public DateMapType
{
public:
    DateMap() {}
    virtual ~DateMap() {}

    virtual bool tqcontains(TQDate date) const { return DateMapType::tqcontains(date.toString(Qt::ISODate)); }

    void insert(TQString date, int state=Map::NonWorking) {
        //kdDebug()<<k_funcinfo<<date<<"="<<state<<endl;
        if (state == Map::None)
            DateMapType::remove(date);
        else
            DateMapType::insert(date, state);
    }
    void insert(TQDate date, int state=Map::NonWorking) { insert(date.toString(Qt::ISODate), state); }

    void remove(TQDate date) {
        //kdDebug()<<k_funcinfo<<date.toString(Qt::ISODate)<<endl;
        DateMapType::remove(date.toString(Qt::ISODate));
    }

    int state(TQString date) {
        DateMapType::iterator it = tqfind(date);
        if (it == end()) return 0;
        else return it.data();
    }
    int state(TQDate date) { return state(date.toString(Qt::ISODate)); }

    bool operator==(const DateMap &m) const { 
        return keys() == m.keys() && values() == m.values(); 
    }
    bool operator!=(const DateMap &m) const { 
        return keys() != m.keys() || values() != m.values(); 
    }
    
    // boolean use
    void toggle(TQString date, int state=Map::NonWorking) {
        //kdDebug()<<k_funcinfo<<date<<"="<<state<<endl;
        if (DateMapType::tqcontains(date))
            DateMapType::remove(date);
        else
            DateMapType::insert(date, state);
    }
    void toggle(TQDate date, int state=Map::NonWorking) { return toggle(date.toString(Qt::ISODate)); }
    void toggleClear(TQString date, int state=Map::NonWorking) {
        //kdDebug()<<k_funcinfo<<date<<"="<<state<<endl;
        bool s = DateMapType::tqcontains(date);
        clear();
        if (!s) insert(date, state);
    }
    void toggleClear(TQDate date, int state=Map::NonWorking) { toggleClear(date.toString(Qt::ISODate)); }
};

typedef TQMap<int, int> IntMapType;
class IntMap : public IntMapType
{
public:
    IntMap() {}
    virtual ~IntMap() {}

    void insert(int key, int state=Map::NonWorking) {
        if (state == Map::None)
            IntMapType::remove(key);
        else
            IntMapType::insert(key, state); }

    virtual int state(int key) {
        IntMapType::iterator it = IntMapType::tqfind(key);
        if (it == IntMapType::end()) return 0;
        else return it.data();
    }

    bool operator==(const IntMap &m) const { 
        return keys() == m.keys() && values() == m.values(); 
    }
    bool operator!=(const IntMap &m) const { 
        return keys() != m.keys() || values() != m.values(); 
    }
    
    // boolean use
    void toggle(int key, int state=Map::NonWorking) { IntMapType::tqcontains(key) ? remove(key) : insert(key, state); }
    void toggleClear(int key, int state=Map::NonWorking) {
        bool s =tqcontains(key);
        clear();
        if (!s) insert(key, state);
    }
};

class WeekMap : public IntMap
{
public:
    bool tqcontains(int week, int year) { return IntMap::tqcontains(week*10000 + year); }
    bool tqcontains(TQPair<int,int> week) { return tqcontains(week.first,  week.second); }

    void insert(int week, int year, int state=Map::NonWorking) {
        if (week < 1 || week > 53) { kdError()<<k_funcinfo<<"Illegal week number: "<<week<<endl; return; }
        IntMap::insert(week*10000 + year, state);
    }
    void insert(TQPair<int,int> week, int state=Map::NonWorking) { insert(week.first, week.second, state); }

    void insert(WeekMap::iterator it, int state) { insert(week(it.key()), state); }

    void remove(TQPair<int,int> week) { IntMap::remove(week.first*10000 + week.second); }

    static TQPair<int, int> week(int key) { return TQPair<int, int>(key/10000, key%10000); }

    int state(TQPair<int, int> week) { return IntMap::state(week.first*10000 + week.second); }
    int state(int week, int year) { return state(TQPair<int, int>(week, year)); }

    void toggle(TQPair<int,int> week, int state=Map::NonWorking) {
        if (week.first < 1 || week.first > 53) { kdError()<<k_funcinfo<<"Illegal week number: "<<week.first<<endl; return; }
        IntMap::toggle(week.first*10000 + week.second, state);
    }
    void toggleClear(TQPair<int,int> week, int state=Map::NonWorking) {
        if (week.first < 1 || week.first > 53) { kdError()<<k_funcinfo<<"Illegal week number: "<<week.first<<endl; return; }
        IntMap::toggleClear(week.first*10000 + week.second, state);
    }
};

}  //KPlato namespace

#endif