/* This file is part of the KDE project Copyright (C) 2004 - 2005 Dag Andersen 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 #include #include #include #include #include #include #include #include #include #include #include #include #include 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()<name()<<" p="<<&p<setParent(base->baseCalendar()) : calendar->setParent(0); macro->addCommand(new CalendarAddCmd(part, &p, calendar)); calendar = 0; } else if (state & Modified) { //kdDebug()<name()<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()<name()<name()< 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()<name()< 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()<addCommand(new CalendarAddDayCmd(part, original, new CalendarDay(cit.current()))); } else if (*day != cit.current()) { if (macro == 0) macro = new KMacroCommand(""); // modified //kdDebug()<addCommand(new CalendarModifyDayCmd(part, original, new CalendarDay(cit.current()))); } } //kdDebug()<name()<weekdays()->weekday(i); org = original->weekdays()->weekday(i); if (day && org) { if (*org != *day) { if (macro == 0) macro = new KMacroCommand(""); //kdDebug()<addCommand(new CalendarModifyWeekdayCmd(part, original, i, new CalendarDay(day))); } } else if (day) { // shouldn't happen: hmmm, add day to original?? kdError()< list = p.calendars(); TQPtrListIterator 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="<calendarList); for (;cit.current(); ++cit) { CalendarListViewItem *item = dynamic_cast(cit.current()); if (item) { KMacroCommand *c = item->buildCommand(part, project); if (c != 0) { if (cmd == 0) cmd = new KMacroCommand(""); cmd->addCommand(c); } } } TQPtrListIterator it = dia->deletedItems(); for (; it.current(); ++it) { //kdDebug()<calendar->name()<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, TQ_SIGNAL(obligatedFieldsFilled(bool)), TQ_SLOT(slotEnableButtonOk(bool))); connect(calendar, TQ_SIGNAL(applyClicked()), TQ_SLOT(slotCalendarModified())); connect(bDelete, TQ_SIGNAL(clicked()), TQ_SLOT(slotDeleteClicked())); connect(bAdd, TQ_SIGNAL(clicked()), TQ_SLOT(slotAddClicked())); //connect(editName, TQ_SIGNAL(returnPressed()), TQ_SLOT(slotAddClicked())); connect(calendarList, TQ_SIGNAL(selectionChanged()), TQ_SLOT(slotSelectionChanged())); connect(calendarList, TQ_SIGNAL(doubleClicked(TQListViewItem*, const TQPoint&, int)), TQ_SLOT(slotListDoubleClicked(TQListViewItem*, const TQPoint&, int))); connect(calendarList, TQ_SIGNAL(itemRenamed(TQListViewItem*, int)), TQ_SLOT(slotItemRenamed(TQListViewItem*, int))); connect (baseCalendar, TQ_SIGNAL(activated(int)), TQ_SLOT(slotBaseCalendarActivated(int))); // Internal rename stuff connect(this, TQ_SIGNAL(renameStarted(TQListViewItem*, int)), TQ_SLOT(slotRenameStarted(TQListViewItem*, int))); connect(this, TQ_SIGNAL(startRename(TQListViewItem*, int)), TQ_SLOT(slotStartRename(TQListViewItem*, int))); connect(this, TQ_SIGNAL(selectionChanged()), TQ_SLOT(slotSelectionChanged())); } void CalendarListDialogImpl::setBaseCalendars() { TQListViewItemIterator it(calendarList); for (;it.current(); ++it) { CalendarListViewItem *item = dynamic_cast(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(calendarList->selectedItem()); if (item) { item->base = baseCalendarList.at(id); item->setState(CalendarListViewItem::Modified); slotEnableButtonOk(true); } else { kdError()<selectedItem(); bDelete->setEnabled((bool)item); bAdd->setEnabled(true); slotSelectionChanged(item); } void CalendarListDialogImpl::slotSelectionChanged(TQListViewItem *listItem) { //kdDebug()<clear(); baseCalendar->setEnabled(false); CalendarListViewItem *cal = dynamic_cast(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(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()<base->calendar->name()<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(calendarList->currentItem()); if (item) { item->setState(CalendarListViewItem::Modified); //kdDebug()<calendar<<")"<(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 &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(it.current()); if (item && (cal == item->original || cal == item->calendar)) { //kdDebug()<name()<text(0)<setRenameEnabled(col, false); m_renameItem = 0; CalendarListViewItem *item = static_cast(itm); if (item->text(0).isEmpty()) { item->setText(0, item->oldText); // keep the old name } if (item->text(0).isEmpty()) { // Not allowed //kdDebug()<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()<text(0):"")<text(0):"")<text(0):"")<isRenaming()) { bDelete->setEnabled(false); bAdd->setEnabled(false); } } void CalendarListDialogImpl::slotStartRename(TQListViewItem *item, int col) { //kdDebug()<text(0):"")<(item)->oldText = item->text(col); item->setRenameEnabled(col, true); item->startRename(col); m_renameItem = item; emit renameStarted(item, col); } } //KPlato namespace #include "kptcalendarlistdialog.moc"