summaryrefslogtreecommitdiffstats
path: root/kplato/kptcalendaredit.cc
blob: ab2df8bd062b6a28d9e8a5afbcece63ad3a21efd (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* This file is part of the KDE project
   Copyright (C) 2004 Dag Andersen <[email protected]>

   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.
*/

#include "kptcalendaredit.h"
#include "kptproject.h"
#include "kptcalendar.h"
#include "kptcalendarpanel.h"
#include "kptmap.h"
#include "intervalitem.h"

#include <tqbuttongroup.h>
#include <tqheader.h>
#include <tqpushbutton.h>
#include <tqradiobutton.h>
#include <tqcombobox.h>
#include <tqlabel.h>
#include <tqtextedit.h>
#include <tqlineedit.h>
#include <tqdatetimeedit.h>
#include <tqdatetime.h>
#include <tqtabwidget.h>
#include <tqtextbrowser.h>

#include <klocale.h>

#include <kabc/addressee.h>
#include <kabc/addresseedialog.h>

#include <tqmap.h>

#include <kdebug.h>

namespace KPlato
{

CalendarEdit::CalendarEdit (TQWidget *parent, const char */*name*/)
    : CalendarEditBase(parent),
      m_calendar(0)
 {

    clear();
    intervalList->header()->setStretchEnabled(true, 0);
    intervalList->setShowSortIndicator(true);
    intervalList->setSorting(0);

    connect (calendarPanel, TQT_SIGNAL(dateChanged(TQDate)), TQT_SLOT(slotDateSelected(TQDate)));
    connect (calendarPanel, TQT_SIGNAL(weekdaySelected(int)), TQT_SLOT(slotWeekdaySelected(int)));
    connect(calendarPanel, TQT_SIGNAL(selectionCleared()), TQT_SLOT(slotSelectionCleared()));

    connect (state, TQT_SIGNAL(activated(int)), TQT_SLOT(slotStateActivated(int)));
    connect (bClear, TQT_SIGNAL(clicked()), TQT_SLOT(slotClearClicked()));
    connect (bAddInterval, TQT_SIGNAL(clicked()), TQT_SLOT(slotAddIntervalClicked()));

    connect (bApply, TQT_SIGNAL(clicked()), TQT_SLOT(slotApplyClicked()));
}

void CalendarEdit::slotStateActivated(int id) {
    //kdDebug()<<k_funcinfo<<"id="<<id<<endl;
    if (id == 0) { // undefined
        startTime->setEnabled(false);
        endTime->setEnabled(false);
        bClear->setEnabled(false);
        bAddInterval->setEnabled(false);
        intervalList->setEnabled(false);
        bApply->setEnabled(true);
    } else if (id == 1) { // non working
        startTime->setEnabled(false);
        endTime->setEnabled(false);
        bClear->setEnabled(false);
        bAddInterval->setEnabled(false);
        intervalList->setEnabled(false);
        bApply->setEnabled(true);
    } else if (id == 2) { //working
        startTime->setEnabled(true);
        endTime->setEnabled(true);
        bClear->setEnabled(true);
        bAddInterval->setEnabled(true);
        intervalList->setEnabled(true);
        bApply->setEnabled(intervalList->firstChild());
    }
}

void CalendarEdit::slotClearClicked() {
    //kdDebug()<<k_funcinfo<<endl;
    intervalList->clear();
    bApply->setEnabled(false);
}
void CalendarEdit::slotAddIntervalClicked() {
    //kdDebug()<<k_funcinfo<<endl;
    intervalList->insertItem(new IntervalItem(intervalList, startTime->time(), endTime->time()));
    bApply->setEnabled(true);
}

//NOTE: enum Map::State must match combobox state!
void CalendarEdit::slotApplyClicked() {
    //kdDebug()<<k_funcinfo<<"("<<m_calendar<<")"<<endl;
    DateMap dates = calendarPanel->selectedDates();
    for(DateMap::iterator it = dates.begin(); it != dates.end(); ++it) {
        TQDate date = TQDate::fromString(it.key(), Qt::ISODate);
        //kdDebug()<<k_funcinfo<<"Date: "<<date<<endl;
        CalendarDay *calDay = m_calendar->findDay(date);
        if (!calDay) {
            calDay = new CalendarDay(date);
            m_calendar->addDay(calDay);
        }
        calDay->setState(state->currentItem()); //NOTE!!
        calDay->clearIntervals();
        if (calDay->state() == Map::Working) {
            for (TQListViewItem *item = intervalList->firstChild(); item; item = item->nextSibling()) {
                //kdDebug()<<k_funcinfo<<"Adding interval: "<<static_cast<IntervalItem *>(item)->interval().first.toString()<<"-"<<static_cast<IntervalItem *>(item)->interval().second.toString()<<endl;
                calDay->addInterval(static_cast<IntervalItem *>(item)->interval());
            }
        }
    }

    IntMap weekdays = calendarPanel->selectedWeekdays();
    for(IntMap::iterator it = weekdays.begin(); it != weekdays.end(); ++it) {
        //kdDebug()<<k_funcinfo<<"weekday="<<it.key()<<endl;
        CalendarDay *weekday = m_calendar->weekday(it.key()-1);
        weekday->setState(state->currentItem());//NOTE!!
        weekday->clearIntervals();
        if (weekday->state() == Map::Working) {
            for (TQListViewItem *item = intervalList->firstChild(); item; item = item->nextSibling()) {
                //kdDebug()<<k_funcinfo<<"Adding interval: "<<static_cast<IntervalItem *>(item)->interval().first.toString()<<"-"<<static_cast<IntervalItem *>(item)->interval().second.toString()<<endl;
                weekday->addInterval(static_cast<IntervalItem *>(item)->interval());
            }
        }
    }

    calendarPanel->markSelected(state->currentItem()); //NOTE!!
    emit applyClicked();
    slotCheckAllFieldsFilled();
}

void CalendarEdit::slotCheckAllFieldsFilled() {
    //kdDebug()<<k_funcinfo<<endl;
    if (state->currentItem() == 0 /*undefined*/ ||
        state->currentItem() == 1 /*Non-working*/||
        (state->currentItem() == 2 /*Working*/ && intervalList->firstChild()))
    {
        emit obligatedFieldsFilled(true);
    }
    else if (state->currentItem() == 2 && !intervalList->firstChild())
    {
        emit obligatedFieldsFilled(false);
    }
}

void CalendarEdit::setCalendar(Calendar *cal) {
    m_calendar = cal;
    clear();
    calendarPanel->setCalendar(cal);
}

void CalendarEdit::clear() {
    clearPanel();
    clearEditPart();
}

void CalendarEdit::clearPanel() {
    calendarPanel->clear();
}

void CalendarEdit::clearEditPart() {
    day->setEnabled(true);
    intervalList->clear();
    intervalList->setEnabled(false);
    startTime->setEnabled(false);
    startTime->setTime(TQTime(8, 0, 0)); //FIXME
    endTime->setEnabled(false);
    endTime->setTime(TQTime(16, 0, 0)); //FIXME

    bAddInterval->setEnabled(false);
    bClear->setEnabled(false);
    bApply->setEnabled(false);
    state->setEnabled(false);
}

void CalendarEdit::slotDateSelected(TQDate date) {
    if (m_calendar == 0)
        return;
    //kdDebug()<<k_funcinfo<<"("<<date.toString()<<")"<<endl;
    clearEditPart();
    state->clear();
    state->insertItem(i18n("Undefined"));
    state->insertItem(i18n("Non-working"));
    state->insertItem(i18n("Working"));

    CalendarDay *calDay = m_calendar->findDay(date);
    state->setEnabled(true);
    if (calDay) {
        TQPtrListIterator<TQPair<TQTime, TQTime> > it = calDay->workingIntervals();
        for (; it.current(); ++it) {
            IntervalItem *item = new IntervalItem(intervalList, it.current()->first, it.current()->second);
            intervalList->insertItem(item);
        }
        if (calDay->state() == Map::Working) {
            //kdDebug()<<k_funcinfo<<"("<<date.toString()<<") is workday"<<endl;
            state->setCurrentItem(2);
            slotStateActivated(2);
            bApply->setEnabled(calDay->workingIntervals().count() > 0);
        } else if (calDay->state() == Map::NonWorking){
            //kdDebug()<<k_funcinfo<<"("<<date.toString()<<") is holiday"<<endl;
            state->setCurrentItem(1);
            slotStateActivated(1);
            bApply->setEnabled(true);
        } else  {
            //kdDebug()<<k_funcinfo<<"("<<date.toString()<<")=none"<<endl;
            state->setCurrentItem(0);
            slotStateActivated(0);
            bApply->setEnabled(true);
        }
    } else {
        // default
        state->setCurrentItem(0);
        slotStateActivated(0);
        bApply->setEnabled(true);
    }
}

void CalendarEdit::slotWeekdaySelected(int day_/* 1..7 */) {
    if (m_calendar == 0 || day_ < 1 || day_ > 7) {
        kdError()<<k_funcinfo<<"No calendar or weekday ("<<day_<<") not defined!"<<endl;
        return;
    }
    //kdDebug()<<k_funcinfo<<"("<<day_<<")"<<endl;
    clearEditPart();
    CalendarDay *calDay = m_calendar->weekday(day_-1); // 0..6
    if (!calDay) {
        kdError()<<k_funcinfo<<"Weekday ("<<day_<<") not defined!"<<endl;
        return;
    }
    state->clear();
    state->insertItem(i18n("Undefined"));
    state->insertItem(i18n("Non-working"));
    state->insertItem(i18n("Working"));
    TQPtrListIterator<TQPair<TQTime, TQTime> > it = calDay->workingIntervals();
    for (; it.current(); ++it) {
        IntervalItem *item = new IntervalItem(intervalList, it.current()->first, it.current()->second);
        intervalList->insertItem(item);
    }
    state->setEnabled(true);
    if (calDay->state() == Map::Working) {
        //kdDebug()<<k_funcinfo<<"("<<day_<<")=workday"<<endl;
        state->setCurrentItem(2);
        slotStateActivated(2);
        bApply->setEnabled(calDay->workingIntervals().count() > 0);
    } else if (calDay->state() == Map::NonWorking) {
        //kdDebug()<<k_funcinfo<<"("<<day_<<")=Holiday"<<endl;
        state->setCurrentItem(1);
        slotStateActivated(1);
        bApply->setEnabled(true);
    } else {
        //kdDebug()<<k_funcinfo<<"("<<day_<<")=none"<<endl;
        state->setCurrentItem(0);
        slotStateActivated(0);
        bApply->setEnabled(true);
    }
}

void CalendarEdit::slotSelectionCleared() {
    clearEditPart();
}

}  //KPlato namespace

#include "kptcalendaredit.moc"