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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/* This file is part of the KDE project
Copyright (C) 2004 - 2005 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.
*/
#include "kptcalendarlistdialog.h"
#include "kptproject.h"
#include "kptcalendar.h"
#include "kptcommand.h"
#include "kptpart.h"
#include <tqpushbutton.h>
#include <tqcombobox.h>
#include <tqheader.h>
#include <tqlabel.h>
#include <tqtextedit.h>
#include <tqlineedit.h>
#include <tqdatetimeedit.h>
#include <tqdatetime.h>
#include <tqtabwidget.h>
#include <tqtextbrowser.h>
#include <tdelocale.h>
#include <tdeabc/addressee.h>
#include <tdeabc/addresseedialog.h>
#include <kdebug.h>
namespace KPlato
{
class CalendarListViewItem : public TDEListViewItem
{
public:
CalendarListViewItem(CalendarListDialogImpl &pan, TQListView *lv, Calendar *cal, Calendar *orig=0)
: TDEListViewItem(lv, cal->name()), panel(pan) {
calendar = cal;
original = orig;
state = None;
base = 0;
setRenameEnabled(0, false);
}
~CalendarListViewItem() {
delete calendar;
}
enum State { None=0, New=1, Modified=2, Deleted=4 };
void setState(State s) { state |= s; }
Calendar *baseCalendar() {
if (state & Deleted) return 0;
return original ? original : calendar;
}
bool hasBaseCalendar(CalendarListViewItem *item) {
if (!base) return false;
return base == item || base->hasBaseCalendar(item);
}
KMacroCommand *buildCommand(Part *part, Project &p) {
KMacroCommand *macro=0;
if (state & New) {
if (macro == 0) macro = new KMacroCommand("");
//kdDebug()<<k_funcinfo<<"add: "<<calendar->name()<<" p="<<&p<<endl;
base ? calendar->setParent(base->baseCalendar()) : calendar->setParent(0);
macro->addCommand(new CalendarAddCmd(part, &p, calendar));
calendar = 0;
} else if (state & Modified) {
//kdDebug()<<k_funcinfo<<"modified: "<<calendar->name()<<endl;
if (original->name() != calendar->name()) {
if (macro == 0) macro = new KMacroCommand("");
macro->addCommand(new CalendarModifyNameCmd(part, original, calendar->name()));
}
Calendar *c = base ? base->baseCalendar() : 0;
if (c != original->parent()) {
if (macro == 0) macro = new KMacroCommand("");
macro->addCommand(new CalendarModifyParentCmd(part, original, c));
//kdDebug()<<k_funcinfo<<"Base modified: "<<c->name()<<endl;
}
//kdDebug()<<k_funcinfo<<"Check for days deleted: "<<calendar->name()<<endl;
TQPtrListIterator<CalendarDay> oit = original->days();
for (; oit.current(); ++oit) {
if (calendar->findDay(oit.current()->date()) == 0) {
if (macro == 0) macro = new KMacroCommand("");
macro->addCommand(new CalendarRemoveDayCmd(part, original, oit.current()->date()));
//kdDebug()<<k_funcinfo<<"Removed day"<<endl;
}
}
//kdDebug()<<k_funcinfo<<"Check for days added or modified: "<<calendar->name()<<endl;
TQPtrListIterator<CalendarDay> cit = calendar->days();
for (; cit.current(); ++cit) {
CalendarDay *day = original->findDay(cit.current()->date());
if (day == 0) {
if (macro == 0) macro = new KMacroCommand("");
// added
//kdDebug()<<k_funcinfo<<"Added day"<<endl;
macro->addCommand(new CalendarAddDayCmd(part, original, new CalendarDay(cit.current())));
} else if (*day != cit.current()) {
if (macro == 0) macro = new KMacroCommand("");
// modified
//kdDebug()<<k_funcinfo<<"Modified day"<<endl;
macro->addCommand(new CalendarModifyDayCmd(part, original, new CalendarDay(cit.current())));
}
}
//kdDebug()<<k_funcinfo<<"Check for weekdays modified: "<<calendar->name()<<endl;
CalendarDay *day = 0, *org = 0;
for (int i=0; i < 7; ++i) {
day = calendar->weekdays()->weekday(i);
org = original->weekdays()->weekday(i);
if (day && org) {
if (*org != *day) {
if (macro == 0) macro = new KMacroCommand("");
//kdDebug()<<k_funcinfo<<"Weekday["<<i<<"] modified"<<endl;
macro->addCommand(new CalendarModifyWeekdayCmd(part, original, i, new CalendarDay(day)));
}
} else if (day) {
// shouldn't happen: hmmm, add day to original??
kdError()<<k_funcinfo<<"Should always have 7 weekdays"<<endl;
} else if (org) {
// shouldn't happen: set org to default??
kdError()<<k_funcinfo<<"Should always have 7 weekdays"<<endl;
}
}
}
return macro;
}
Calendar *calendar;
Calendar *original;
CalendarListViewItem* base;
CalendarListDialogImpl &panel;
TQString oldText;
protected:
virtual void cancelRename(int col) {
//kdDebug()<<k_funcinfo<<endl;
if (col == 0 && oldText.isEmpty()) {
return;
}
panel.renameStopped(this);
TDEListViewItem::cancelRename(col);
setRenameEnabled(col, false);
}
private:
int state;
};
//----------------------------------------------------
CalendarListDialog::CalendarListDialog(Project &p, TQWidget *parent, const char *name)
: KDialogBase( Swallow, i18n("Calendar's Settings"), Ok|Cancel, Ok, parent, name, true, true),
project(p)
{
//kdDebug()<<k_funcinfo<<&p<<endl;
dia = new CalendarListDialogImpl(p, this);
TQPtrList<Calendar> list = p.calendars();
TQPtrListIterator<Calendar> it = list;
for (; it.current(); ++it) {
Calendar *c = new Calendar(it.current());
//c->setProject(&p);
new CalendarListViewItem(*dia, dia->calendarList, c, it.current());
}
dia->setBaseCalendars();
TQListViewItem *f = dia->calendarList->firstChild();
if (f) {
dia->calendarList->setSelected(f, true);
}
//kdDebug()<<"size="<<size().width()<<"x"<<size().height()<<" hint="<<sizeHint().width()<<"x"<<sizeHint().height()<<endl;
resize(TQSize(725, 388).expandedTo(minimumSizeHint()));
setMainWidget(dia);
enableButtonOK(false);
connect(dia, TQT_SIGNAL(enableButtonOk(bool)), TQT_SLOT(enableButtonOK(bool)));
}
KCommand *CalendarListDialog::buildCommand(Part *part) {
//kdDebug()<<k_funcinfo<<endl;
KMacroCommand *cmd = 0;
TQListViewItemIterator cit(dia->calendarList);
for (;cit.current(); ++cit) {
CalendarListViewItem *item = dynamic_cast<CalendarListViewItem *>(cit.current());
if (item) {
KMacroCommand *c = item->buildCommand(part, project);
if (c != 0) {
if (cmd == 0) cmd = new KMacroCommand("");
cmd->addCommand(c);
}
}
}
TQPtrListIterator<CalendarListViewItem> it = dia->deletedItems();
for (; it.current(); ++it) {
//kdDebug()<<k_funcinfo<<"deleted: "<<it.current()->calendar->name()<<endl;
if (it.current()->original) {
if (cmd == 0) cmd = new KMacroCommand("");
cmd->addCommand(new CalendarDeleteCmd(part, it.current()->original));
}
}
if (cmd) {
cmd->setName(i18n("Modify Calendars"));
}
return cmd;
}
void CalendarListDialog::slotOk() {
accept();
}
//--------------------------------------------------
CalendarListDialogImpl::CalendarListDialogImpl (Project &p, TQWidget *parent)
: CalendarListDialogBase(parent),
project(p),
m_renameItem(0) {
calendarList->header()->setStretchEnabled(true, 0);
calendarList->setShowSortIndicator(true);
calendarList->setSorting(0);
calendarList->setDefaultRenameAction(TQListView::Accept);
m_deletedItems.setAutoDelete(true);
calendar->setEnabled(false);
slotSelectionChanged();
connect(calendar, TQT_SIGNAL(obligatedFieldsFilled(bool)), TQT_SLOT(slotEnableButtonOk(bool)));
connect(calendar, TQT_SIGNAL(applyClicked()), TQT_SLOT(slotCalendarModified()));
connect(bDelete, TQT_SIGNAL(clicked()), TQT_SLOT(slotDeleteClicked()));
connect(bAdd, TQT_SIGNAL(clicked()), TQT_SLOT(slotAddClicked()));
//connect(editName, TQT_SIGNAL(returnPressed()), TQT_SLOT(slotAddClicked()));
connect(calendarList, TQT_SIGNAL(selectionChanged()), TQT_SLOT(slotSelectionChanged()));
connect(calendarList, TQT_SIGNAL(doubleClicked(TQListViewItem*, const TQPoint&, int)), TQT_SLOT(slotListDoubleClicked(TQListViewItem*, const TQPoint&, int)));
connect(calendarList, TQT_SIGNAL(itemRenamed(TQListViewItem*, int)), TQT_SLOT(slotItemRenamed(TQListViewItem*, int)));
connect (baseCalendar, TQT_SIGNAL(activated(int)), TQT_SLOT(slotBaseCalendarActivated(int)));
// Internal rename stuff
connect(this, TQT_SIGNAL(renameStarted(TQListViewItem*, int)), TQT_SLOT(slotRenameStarted(TQListViewItem*, int)));
connect(this, TQT_SIGNAL(startRename(TQListViewItem*, int)), TQT_SLOT(slotStartRename(TQListViewItem*, int)));
connect(this, TQT_SIGNAL(selectionChanged()), TQT_SLOT(slotSelectionChanged()));
}
void CalendarListDialogImpl::setBaseCalendars() {
TQListViewItemIterator it(calendarList);
for (;it.current(); ++it) {
CalendarListViewItem *item = dynamic_cast<CalendarListViewItem *>(it.current());
if (item) {
item->base = findItem(item->calendar->parent());
}
}
}
void CalendarListDialogImpl::slotEnableButtonOk(bool on) {
emit enableButtonOk(on);
}
void CalendarListDialogImpl::slotBaseCalendarActivated(int id) {
CalendarListViewItem *item = dynamic_cast<CalendarListViewItem*>(calendarList->selectedItem());
if (item) {
item->base = baseCalendarList.at(id);
item->setState(CalendarListViewItem::Modified);
slotEnableButtonOk(true);
} else {
kdError()<<k_funcinfo<<"No CalendarListViewItem"<<endl;
}
}
void CalendarListDialogImpl::slotSelectionChanged() {
//kdDebug()<<k_funcinfo<<endl;
TQListViewItem *item = calendarList->selectedItem();
bDelete->setEnabled((bool)item);
bAdd->setEnabled(true);
slotSelectionChanged(item);
}
void CalendarListDialogImpl::slotSelectionChanged(TQListViewItem *listItem) {
//kdDebug()<<k_funcinfo<<endl;
baseCalendarList.clear();
baseCalendar->clear();
baseCalendar->setEnabled(false);
CalendarListViewItem *cal = dynamic_cast<CalendarListViewItem *>(listItem);
if (cal) {
setCalendar(cal->calendar);
baseCalendar->insertItem(i18n("None"));
baseCalendarList.append(0);
int me = 0, i = 0;
TQListViewItemIterator it(calendarList);
for (; it.current(); ++it) {
CalendarListViewItem *item = dynamic_cast<CalendarListViewItem*>(it.current());
if (item && cal != item && !item->hasBaseCalendar(cal)) {
baseCalendar->insertItem(item->text(0));
baseCalendarList.append(item);
i++;
if (item == cal->base) {
me = i;
//kdDebug()<<k_funcinfo<<"item="<<item<<": cal="<<cal->calendar->name()<<" has parent "<<cal->base->calendar->name()<<endl;
}
}
}
baseCalendar->setCurrentItem(me);
baseCalendar->setEnabled(true);
return;
}
calendar->clear();
}
void CalendarListDialogImpl::setCalendar(Calendar *cal) {
calendar->setCalendar(cal);
calendar->setEnabled(true);
}
void CalendarListDialogImpl::slotCalendarModified() {
CalendarListViewItem *item = dynamic_cast<CalendarListViewItem*>(calendarList->currentItem());
if (item) {
item->setState(CalendarListViewItem::Modified);
//kdDebug()<<k_funcinfo<<"("<<item->calendar<<")"<<endl;
}
emit calendarModified();
}
void CalendarListDialogImpl::slotDeleteClicked() {
CalendarListViewItem *item = static_cast<CalendarListViewItem*>(calendarList->selectedItem());
if (item) {
calendarList->takeItem(item);
item->setState(CalendarListViewItem::Deleted);
m_deletedItems.append(item);
emit enableButtonOk(true);
}
}
void CalendarListDialogImpl::slotAddClicked() {
Calendar *cal = new Calendar();
cal->setProject(&project);
CalendarListViewItem *item = new CalendarListViewItem(*this, calendarList, cal);
item->setState(CalendarListViewItem::New);
slotListDoubleClicked(item, TQPoint(), 0);
}
TQPtrList<CalendarListViewItem> &CalendarListDialogImpl::deletedItems() {
return m_deletedItems;
}
CalendarListViewItem *CalendarListDialogImpl::findItem(Calendar *cal) {
if (!cal)
return 0;
TQListViewItemIterator it(calendarList);
for (;it.current(); ++it) {
CalendarListViewItem *item = dynamic_cast<CalendarListViewItem *>(it.current());
if (item && (cal == item->original || cal == item->calendar)) {
//kdDebug()<<k_funcinfo<<"Found: "<<cal->name()<<endl;
return item;
}
}
return 0;
}
void CalendarListDialogImpl::slotItemRenamed(TQListViewItem *itm, int col) {
//kdDebug()<<k_funcinfo<<itm->text(0)<<endl;
itm->setRenameEnabled(col, false);
m_renameItem = 0;
CalendarListViewItem *item = static_cast<CalendarListViewItem*>(itm);
if (item->text(0).isEmpty()) {
item->setText(0, item->oldText); // keep the old name
}
if (item->text(0).isEmpty()) {
// Not allowed
//kdDebug()<<k_funcinfo<<"name empty"<<endl;
emit startRename(item, 0);
return;
}
if (item->text(0) != item->oldText) {
item->setState(CalendarListViewItem::Modified);
item->calendar->setName(item->text(0));
}
renameStopped(item);
slotEnableButtonOk(true);
}
// We don't get notified when rename is cancelled, this is called from the item
void CalendarListDialogImpl::renameStopped(TQListViewItem */*item*/) {
//kdDebug()<<k_funcinfo<<(item?item->text(0):"")<<endl;
m_renameItem = 0;
emit selectionChanged();
}
void CalendarListDialogImpl::slotListDoubleClicked(TQListViewItem *item, const TQPoint&, int col) {
//kdDebug()<<k_funcinfo<<(item?item->text(0):"")<<endl;
if (m_renameItem)
return;
slotStartRename(item, col);
}
void CalendarListDialogImpl::slotRenameStarted(TQListViewItem */*item*/, int /*col*/) {
//kdDebug()<<k_funcinfo<<(item?item->text(0):"")<<endl;
if (calendarList->isRenaming()) {
bDelete->setEnabled(false);
bAdd->setEnabled(false);
}
}
void CalendarListDialogImpl::slotStartRename(TQListViewItem *item, int col) {
//kdDebug()<<k_funcinfo<<(item?item->text(0):"")<<endl;
static_cast<CalendarListViewItem*>(item)->oldText = item->text(col);
item->setRenameEnabled(col, true);
item->startRename(col);
m_renameItem = item;
emit renameStarted(item, col);
}
} //KPlato namespace
#include "kptcalendarlistdialog.moc"
|