kplato

kptcalendaredit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Dag Andersen <danders@get2net.dk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation;
7 version 2 of the License.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kptcalendaredit.h"
21#include "kptproject.h"
22#include "kptcalendar.h"
23#include "kptcalendarpanel.h"
24#include "kptmap.h"
25#include "intervalitem.h"
26
27#include <tqbuttongroup.h>
28#include <tqheader.h>
29#include <tqpushbutton.h>
30#include <tqradiobutton.h>
31#include <tqcombobox.h>
32#include <tqlabel.h>
33#include <tqtextedit.h>
34#include <tqlineedit.h>
35#include <tqdatetimeedit.h>
36#include <tqdatetime.h>
37#include <tqtabwidget.h>
38#include <tqtextbrowser.h>
39
40#include <tdelocale.h>
41
42#include <tdeabc/addressee.h>
43#include <tdeabc/addresseedialog.h>
44
45#include <tqmap.h>
46
47#include <kdebug.h>
48
49namespace KPlato
50{
51
52CalendarEdit::CalendarEdit (TQWidget *parent, const char */*name*/)
53 : CalendarEditBase(parent),
54 m_calendar(0)
55 {
56
57 clear();
58 intervalList->header()->setStretchEnabled(true, 0);
59 intervalList->setShowSortIndicator(true);
60 intervalList->setSorting(0);
61
62 connect (calendarPanel, TQ_SIGNAL(dateChanged(TQDate)), TQ_SLOT(slotDateSelected(TQDate)));
63 connect (calendarPanel, TQ_SIGNAL(weekdaySelected(int)), TQ_SLOT(slotWeekdaySelected(int)));
64 connect(calendarPanel, TQ_SIGNAL(selectionCleared()), TQ_SLOT(slotSelectionCleared()));
65
66 connect (state, TQ_SIGNAL(activated(int)), TQ_SLOT(slotStateActivated(int)));
67 connect (bClear, TQ_SIGNAL(clicked()), TQ_SLOT(slotClearClicked()));
68 connect (bAddInterval, TQ_SIGNAL(clicked()), TQ_SLOT(slotAddIntervalClicked()));
69
70 connect (bApply, TQ_SIGNAL(clicked()), TQ_SLOT(slotApplyClicked()));
71}
72
73void CalendarEdit::slotStateActivated(int id) {
74 //kdDebug()<<k_funcinfo<<"id="<<id<<endl;
75 if (id == 0) { // undefined
76 startTime->setEnabled(false);
77 endTime->setEnabled(false);
78 bClear->setEnabled(false);
79 bAddInterval->setEnabled(false);
80 intervalList->setEnabled(false);
81 bApply->setEnabled(true);
82 } else if (id == 1) { // non working
83 startTime->setEnabled(false);
84 endTime->setEnabled(false);
85 bClear->setEnabled(false);
86 bAddInterval->setEnabled(false);
87 intervalList->setEnabled(false);
88 bApply->setEnabled(true);
89 } else if (id == 2) { //working
90 startTime->setEnabled(true);
91 endTime->setEnabled(true);
92 bClear->setEnabled(true);
93 bAddInterval->setEnabled(true);
94 intervalList->setEnabled(true);
95 bApply->setEnabled(intervalList->firstChild());
96 }
97}
98
99void CalendarEdit::slotClearClicked() {
100 //kdDebug()<<k_funcinfo<<endl;
101 intervalList->clear();
102 bApply->setEnabled(false);
103}
104void CalendarEdit::slotAddIntervalClicked() {
105 //kdDebug()<<k_funcinfo<<endl;
106 intervalList->insertItem(new IntervalItem(intervalList, startTime->time(), endTime->time()));
107 bApply->setEnabled(true);
108}
109
110//NOTE: enum Map::State must match combobox state!
111void CalendarEdit::slotApplyClicked() {
112 //kdDebug()<<k_funcinfo<<"("<<m_calendar<<")"<<endl;
113 DateMap dates = calendarPanel->selectedDates();
114 for(DateMap::iterator it = dates.begin(); it != dates.end(); ++it) {
115 TQDate date = TQDate::fromString(it.key(), TQt::ISODate);
116 //kdDebug()<<k_funcinfo<<"Date: "<<date<<endl;
117 CalendarDay *calDay = m_calendar->findDay(date);
118 if (!calDay) {
119 calDay = new CalendarDay(date);
120 m_calendar->addDay(calDay);
121 }
122 calDay->setState(state->currentItem()); //NOTE!!
123 calDay->clearIntervals();
124 if (calDay->state() == Map::Working) {
125 for (TQListViewItem *item = intervalList->firstChild(); item; item = item->nextSibling()) {
126 //kdDebug()<<k_funcinfo<<"Adding interval: "<<static_cast<IntervalItem *>(item)->interval().first.toString()<<"-"<<static_cast<IntervalItem *>(item)->interval().second.toString()<<endl;
127 calDay->addInterval(static_cast<IntervalItem *>(item)->interval());
128 }
129 }
130 }
131
132 IntMap weekdays = calendarPanel->selectedWeekdays();
133 for(IntMap::iterator it = weekdays.begin(); it != weekdays.end(); ++it) {
134 //kdDebug()<<k_funcinfo<<"weekday="<<it.key()<<endl;
135 CalendarDay *weekday = m_calendar->weekday(it.key()-1);
136 weekday->setState(state->currentItem());//NOTE!!
137 weekday->clearIntervals();
138 if (weekday->state() == Map::Working) {
139 for (TQListViewItem *item = intervalList->firstChild(); item; item = item->nextSibling()) {
140 //kdDebug()<<k_funcinfo<<"Adding interval: "<<static_cast<IntervalItem *>(item)->interval().first.toString()<<"-"<<static_cast<IntervalItem *>(item)->interval().second.toString()<<endl;
141 weekday->addInterval(static_cast<IntervalItem *>(item)->interval());
142 }
143 }
144 }
145
146 calendarPanel->markSelected(state->currentItem()); //NOTE!!
147 emit applyClicked();
148 slotCheckAllFieldsFilled();
149}
150
151void CalendarEdit::slotCheckAllFieldsFilled() {
152 //kdDebug()<<k_funcinfo<<endl;
153 if (state->currentItem() == 0 /*undefined*/ ||
154 state->currentItem() == 1 /*Non-working*/||
155 (state->currentItem() == 2 /*Working*/ && intervalList->firstChild()))
156 {
157 emit obligatedFieldsFilled(true);
158 }
159 else if (state->currentItem() == 2 && !intervalList->firstChild())
160 {
161 emit obligatedFieldsFilled(false);
162 }
163}
164
165void CalendarEdit::setCalendar(Calendar *cal) {
166 m_calendar = cal;
167 clear();
168 calendarPanel->setCalendar(cal);
169}
170
171void CalendarEdit::clear() {
172 clearPanel();
173 clearEditPart();
174}
175
176void CalendarEdit::clearPanel() {
177 calendarPanel->clear();
178}
179
180void CalendarEdit::clearEditPart() {
181 day->setEnabled(true);
182 intervalList->clear();
183 intervalList->setEnabled(false);
184 startTime->setEnabled(false);
185 startTime->setTime(TQTime(8, 0, 0)); //FIXME
186 endTime->setEnabled(false);
187 endTime->setTime(TQTime(16, 0, 0)); //FIXME
188
189 bAddInterval->setEnabled(false);
190 bClear->setEnabled(false);
191 bApply->setEnabled(false);
192 state->setEnabled(false);
193}
194
195void CalendarEdit::slotDateSelected(TQDate date) {
196 if (m_calendar == 0)
197 return;
198 //kdDebug()<<k_funcinfo<<"("<<date.toString()<<")"<<endl;
199 clearEditPart();
200 state->clear();
201 state->insertItem(i18n("Undefined"));
202 state->insertItem(i18n("Non-working"));
203 state->insertItem(i18n("Working"));
204
205 CalendarDay *calDay = m_calendar->findDay(date);
206 state->setEnabled(true);
207 if (calDay) {
208 TQPtrListIterator<TQPair<TQTime, TQTime> > it = calDay->workingIntervals();
209 for (; it.current(); ++it) {
210 IntervalItem *item = new IntervalItem(intervalList, it.current()->first, it.current()->second);
211 intervalList->insertItem(item);
212 }
213 if (calDay->state() == Map::Working) {
214 //kdDebug()<<k_funcinfo<<"("<<date.toString()<<") is workday"<<endl;
215 state->setCurrentItem(2);
216 slotStateActivated(2);
217 bApply->setEnabled(calDay->workingIntervals().count() > 0);
218 } else if (calDay->state() == Map::NonWorking){
219 //kdDebug()<<k_funcinfo<<"("<<date.toString()<<") is holiday"<<endl;
220 state->setCurrentItem(1);
221 slotStateActivated(1);
222 bApply->setEnabled(true);
223 } else {
224 //kdDebug()<<k_funcinfo<<"("<<date.toString()<<")=none"<<endl;
225 state->setCurrentItem(0);
226 slotStateActivated(0);
227 bApply->setEnabled(true);
228 }
229 } else {
230 // default
231 state->setCurrentItem(0);
232 slotStateActivated(0);
233 bApply->setEnabled(true);
234 }
235}
236
237void CalendarEdit::slotWeekdaySelected(int day_/* 1..7 */) {
238 if (m_calendar == 0 || day_ < 1 || day_ > 7) {
239 kdError()<<k_funcinfo<<"No calendar or weekday ("<<day_<<") not defined!"<<endl;
240 return;
241 }
242 //kdDebug()<<k_funcinfo<<"("<<day_<<")"<<endl;
243 clearEditPart();
244 CalendarDay *calDay = m_calendar->weekday(day_-1); // 0..6
245 if (!calDay) {
246 kdError()<<k_funcinfo<<"Weekday ("<<day_<<") not defined!"<<endl;
247 return;
248 }
249 state->clear();
250 state->insertItem(i18n("Undefined"));
251 state->insertItem(i18n("Non-working"));
252 state->insertItem(i18n("Working"));
253 TQPtrListIterator<TQPair<TQTime, TQTime> > it = calDay->workingIntervals();
254 for (; it.current(); ++it) {
255 IntervalItem *item = new IntervalItem(intervalList, it.current()->first, it.current()->second);
256 intervalList->insertItem(item);
257 }
258 state->setEnabled(true);
259 if (calDay->state() == Map::Working) {
260 //kdDebug()<<k_funcinfo<<"("<<day_<<")=workday"<<endl;
261 state->setCurrentItem(2);
262 slotStateActivated(2);
263 bApply->setEnabled(calDay->workingIntervals().count() > 0);
264 } else if (calDay->state() == Map::NonWorking) {
265 //kdDebug()<<k_funcinfo<<"("<<day_<<")=Holiday"<<endl;
266 state->setCurrentItem(1);
267 slotStateActivated(1);
268 bApply->setEnabled(true);
269 } else {
270 //kdDebug()<<k_funcinfo<<"("<<day_<<")=none"<<endl;
271 state->setCurrentItem(0);
272 slotStateActivated(0);
273 bApply->setEnabled(true);
274 }
275}
276
277void CalendarEdit::slotSelectionCleared() {
278 clearEditPart();
279}
280
281} //KPlato namespace
282
283#include "kptcalendaredit.moc"