F:/KPlato/koffice/kplato/kptcalendarlistdialog.cc

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 - 2005 Dag Andersen <danders@get2net.dk>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation;
00007    version 2 of the License.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kptcalendarlistdialog.h"
00021 #include "kptproject.h"
00022 #include "kptcalendar.h"
00023 #include "kptcommand.h"
00024 #include "kptpart.h"
00025 
00026 #include <QPushButton>
00027 #include <QComboBox>
00028 #include <QHeaderView>
00029 #include <QLabel>
00030 #include <QLineEdit>
00031 #include <QList>
00032 #include <QTreeWidget>
00033 
00034 #include <klocale.h>
00035 
00036 #include <kabc/addressee.h>
00037 #include <kabc/addresseedialog.h>
00038 
00039 #include <kdebug.h>
00040 
00041 namespace KPlato
00042 {
00043 
00044 class CalendarListViewItem : public QTreeWidgetItem
00045 {
00046 public:
00047     CalendarListViewItem(CalendarListDialogImpl &pan, QTreeWidget *lv, Calendar *cal,  Calendar *orig=0)
00048     : QTreeWidgetItem(lv), panel(pan) {
00049         
00050         setText(0, cal->name());
00051         calendar = cal;
00052         original = orig;
00053         state = None;
00054         base = 0;
00055         
00056         setFlags(flags() | Qt::ItemIsEditable);
00057     }
00058     ~CalendarListViewItem() {
00059         delete calendar;
00060     }
00061 
00062     enum State { None=0, New=1, Modified=2, Deleted=4 };
00063 
00064     void setState(State s) { state |= s; }
00065 
00066     Calendar *baseCalendar() {
00067         if (state & Deleted) return 0;
00068         return original ? original : calendar;
00069     }
00070     bool hasBaseCalendar(CalendarListViewItem *item) {
00071         if (!base) return false;
00072         return base == item || base->hasBaseCalendar(item);
00073     }
00074     KMacroCommand *buildCommand(Part *part, Project &p) {
00075         KMacroCommand *macro=0;
00076         if (state & New) {
00077             if (macro == 0) macro = new KMacroCommand("");
00078             //kDebug()<<k_funcinfo<<"add: "<<calendar->name()<<" p="<<&p<<endl;
00079             base ? calendar->setParent(base->baseCalendar()) : calendar->setParent(0);
00080             macro->addCommand(new CalendarAddCmd(part, &p, calendar));
00081             calendar = 0;
00082         } else if (state & Modified) {
00083             //kDebug()<<k_funcinfo<<"modified: "<<calendar->name()<<endl;
00084             if (original->name() != calendar->name()) {
00085                 if (macro == 0) macro = new KMacroCommand("");
00086                 macro->addCommand(new CalendarModifyNameCmd(part, original, calendar->name()));
00087             }
00088             Calendar *c = base ? base->baseCalendar() : 0;
00089             if (c != original->parent()) {
00090                 if (macro == 0) macro = new KMacroCommand("");
00091                 macro->addCommand(new CalendarModifyParentCmd(part, original, c));
00092                 //kDebug()<<k_funcinfo<<"Base modified: "<<c->name()<<endl;
00093             }
00094 
00095             //kDebug()<<k_funcinfo<<"Check for days deleted: "<<calendar->name()<<endl;
00096             foreach (CalendarDay *day, original->days()) {
00097                 if (calendar->findDay(day->date()) == 0) {
00098                     if (macro == 0) macro = new KMacroCommand("");
00099                     macro->addCommand(new CalendarRemoveDayCmd(part, original, day->date()));
00100                     //kDebug()<<k_funcinfo<<"Removed day"<<endl;
00101                 }
00102             }
00103 
00104             //kDebug()<<k_funcinfo<<"Check for days added or modified: "<<calendar->name()<<endl;
00105             foreach (CalendarDay *org, original->days()) {
00106                 CalendarDay *day = original->findDay(org->date());
00107                 if (day == 0) {
00108                     if (macro == 0) macro = new KMacroCommand("");
00109                     // added
00110                     //kDebug()<<k_funcinfo<<"Added day"<<endl;
00111                     macro->addCommand(new CalendarAddDayCmd(part, original, new CalendarDay(org)));
00112                 } else if (*day != org) {
00113                     if (macro == 0) macro = new KMacroCommand("");
00114                     // modified
00115                     //kDebug()<<k_funcinfo<<"Modified day"<<endl;
00116                     macro->addCommand(new CalendarModifyDayCmd(part, original, new CalendarDay(org)));
00117                 }
00118             }
00119             //kDebug()<<k_funcinfo<<"Check for weekdays modified: "<<calendar->name()<<endl;
00120             CalendarDay *day = 0, *org = 0;
00121             for (int i=0; i < 7; ++i) {
00122                 day = calendar->weekdays()->weekday(i);
00123                 org = original->weekdays()->weekday(i);
00124                 if (day && org) {
00125                     if (*org != *day) {
00126                         if (macro == 0) macro = new KMacroCommand("");
00127                         //kDebug()<<k_funcinfo<<"Weekday["<<i<<"] modified"<<endl;
00128                         macro->addCommand(new CalendarModifyWeekdayCmd(part, original, i, new CalendarDay(day)));
00129                     }
00130                 } else if (day) {
00131                     // shouldn't happen: hmmm, add day to original??
00132                     kError()<<k_funcinfo<<"Should always have 7 weekdays"<<endl;
00133                 } else if (org) {
00134                     // shouldn't happen: set org to default??
00135                     kError()<<k_funcinfo<<"Should always have 7 weekdays"<<endl;
00136                 }
00137             }
00138         }
00139         return macro;
00140     }
00141 
00142     Calendar *calendar;
00143     Calendar *original;
00144     CalendarListViewItem* base;
00145     CalendarListDialogImpl &panel;
00146     QString oldText;
00147 
00148 protected:
00149 /*    virtual void cancelRename(int col) {
00150         //kDebug()<<k_funcinfo<<endl;
00151         if (col == 0 && oldText.isEmpty()) {
00152             return;
00153         }
00154         panel.renameStopped(this);
00155         QTreeWidgetItem::cancelRename(col);
00156         setRenameEnabled(col, false);
00157     }*/
00158 private:
00159     int state;
00160 };
00161 
00162 //----------------------------------------------------
00163 CalendarListDialog::CalendarListDialog(Project &p, QWidget *parent, const char *name)
00164     : KDialog( parent),
00165       project(p)
00166 {
00167     setCaption( i18n("Calendar's Settings") );
00168     setButtons( Ok|Cancel );
00169     setDefaultButton( Ok );
00170     showButtonSeparator( true );
00171     //kDebug()<<k_funcinfo<<&p<<endl;
00172     dia = new CalendarListDialogImpl(p, this);
00173     QList<Calendar*> list = p.calendars();
00174     foreach (Calendar *org, list) {
00175         Calendar *c = new Calendar(org);
00176         //c->setProject(&p);
00177         new CalendarListViewItem(*dia, dia->calendarList, c, org);
00178     }
00179     dia->setBaseCalendars();
00180 
00181     QTreeWidgetItem *f = dia->calendarList->topLevelItem(0);
00182     if (f) {
00183         f->setSelected(true);
00184     }
00185     //kDebug()<<"size="<<size().width()<<"x"<<size().height()<<" hint="<<sizeHint().width()<<"x"<<sizeHint().height()<<endl;
00186     resize(QSize(725, 388).expandedTo(minimumSizeHint()));
00187 
00188     setMainWidget(dia);
00189     enableButtonOk(false);
00190 
00191     connect(this, SIGNAL(okClicked()), SLOT(slotOk()));
00192     connect(dia, SIGNAL(enableButtonOk(bool)), SLOT(enableButtonOk(bool)));
00193     // Don't connect this until the list is filled
00194     connect(dia->calendarList, SIGNAL(itemChanged(QTreeWidgetItem*, int)), dia, SLOT(slotItemChanged(QTreeWidgetItem*, int)));
00195 }
00196 
00197 KCommand *CalendarListDialog::buildCommand(Part *part) {
00198     //kDebug()<<k_funcinfo<<endl;
00199     KMacroCommand *cmd = 0;
00200     int c = dia->calendarList->topLevelItemCount();
00201     for (int i=0; i < c; ++i) {
00202         CalendarListViewItem *item = static_cast<CalendarListViewItem *>(dia->calendarList->topLevelItem(i));
00203         //kDebug()<<k_funcinfo<<item->text(0)<<endl;
00204         KMacroCommand *c = item->buildCommand(part, project);
00205         if (c != 0) {
00206             if (cmd == 0) cmd = new KMacroCommand("");
00207             cmd->addCommand(c);
00208         }
00209     }
00210     foreach (CalendarListViewItem *item, dia->deletedItems()) {
00211         //kDebug()<<k_funcinfo<<"deleted: "<<item->calendar->name()<<endl;
00212         if (item->original) {
00213             if (cmd == 0) cmd = new KMacroCommand("");
00214             cmd->addCommand(new CalendarDeleteCmd(part, item->original));
00215         }
00216     }
00217     if (cmd) {
00218         cmd->setName(i18n("Modify Calendars"));
00219     }
00220     return cmd;
00221 }
00222 
00223 void CalendarListDialog::slotOk() {
00224     accept();
00225 }
00226 
00227 //--------------------------------------------------
00228 CalendarListDialogImpl::CalendarListDialogImpl (Project &p, QWidget *parent)
00229     : CalendarListDialogBase(parent),
00230       project(p),
00231       m_renameItem(0) {
00232 
00233     calendarList->header()->setStretchLastSection(true);
00234     calendarList->setSortingEnabled(true);
00235     calendarList->sortByColumn(0);
00236     calendar->setEnabled(false);
00237     calendarList->setSelectionMode(QAbstractItemView::SingleSelection);
00238     
00239     slotSelectionChanged();
00240 
00241     connect(calendar, SIGNAL(obligatedFieldsFilled(bool)), SLOT(slotEnableButtonOk(bool)));
00242     connect(calendar, SIGNAL(applyClicked()), SLOT(slotCalendarModified()));
00243 
00244     connect(bDelete, SIGNAL(clicked()), SLOT(slotDeleteClicked()));
00245     connect(bAdd, SIGNAL(clicked()), SLOT(slotAddClicked()));
00246     //connect(editName, SIGNAL(returnPressed()), SLOT(slotAddClicked()));
00247 
00248     connect(calendarList, SIGNAL(itemSelectionChanged()), SLOT(slotSelectionChanged()));
00249     connect(calendarList, SIGNAL(doubleClicked(const QModelIndex &)), SLOT(slotListDoubleClicked(const QModelIndex &)));
00250 
00251     connect (baseCalendar, SIGNAL(activated(int)), SLOT(slotBaseCalendarActivated(int)));
00252 
00253     connect(this, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged()));
00254     
00255 }
00256 
00257 CalendarListDialogImpl::~CalendarListDialogImpl() {
00258     if (!m_deletedItems.isEmpty())
00259         delete m_deletedItems.takeFirst();
00260 }
00261 void CalendarListDialogImpl::setBaseCalendars() {
00262     int c = calendarList->topLevelItemCount();
00263     for (int i = 0; i < c; ++i) {
00264         CalendarListViewItem *item = static_cast<CalendarListViewItem *>(calendarList->topLevelItem(i));
00265         item->base = findItem(item->calendar->parent());
00266     }
00267 }
00268 
00269 void CalendarListDialogImpl::slotItemChanged(QTreeWidgetItem *ci, int col) {
00270     if (ci == 0)
00271         return;
00272     //kDebug()<<k_funcinfo<<ci->text(0)<<", "<<col<<endl;
00273     CalendarListViewItem *item = static_cast<CalendarListViewItem*>(ci);
00274     item->calendar->setName(item->text(0));
00275     item->setState(CalendarListViewItem::Modified);
00276     slotEnableButtonOk(true);
00277 }
00278 
00279 void CalendarListDialogImpl::slotEnableButtonOk(bool on) {
00280     emit enableButtonOk(on);
00281 }
00282 
00283 void CalendarListDialogImpl::slotBaseCalendarActivated(int id) {
00284     //kDebug()<<k_funcinfo<<endl;
00285     QList<QTreeWidgetItem*> lst = calendarList->selectedItems();
00286     if (lst.count() == 1) {
00287         CalendarListViewItem *item = static_cast<CalendarListViewItem*>(lst[0]);
00288         item->base = baseCalendarList.at(id);
00289         item->setState(CalendarListViewItem::Modified);
00290         slotEnableButtonOk(true);
00291     } else {
00292         kError()<<k_funcinfo<<"No CalendarListViewItem (or too many)"<<endl;
00293     }
00294 }
00295 
00296 void CalendarListDialogImpl::slotSelectionChanged() {
00297     //kDebug()<<k_funcinfo<<endl;
00298     QList<QTreeWidgetItem *> lst = calendarList->selectedItems();
00299     bDelete->setEnabled(lst.count() > 0);
00300     bAdd->setEnabled(true);
00301     if (lst.count() > 0)
00302         slotSelectionChanged(lst[0]); // we only handle single selection!!
00303     else
00304         slotSelectionChanged(0);
00305 }
00306 
00307 void CalendarListDialogImpl::slotSelectionChanged(QTreeWidgetItem *listItem) {
00308     //kDebug()<<k_funcinfo<<endl;
00309     baseCalendarList.clear();
00310     baseCalendar->clear();
00311     baseCalendar->setEnabled(false);
00312     CalendarListViewItem *cal = dynamic_cast<CalendarListViewItem *>(listItem);
00313     if (cal) {
00314         setCalendar(cal->calendar);
00315         baseCalendar->addItem(i18n("None"));
00316         baseCalendarList.append(0);
00317         int me = 0, i = 0;
00318         int c = calendarList->topLevelItemCount();
00319         for (int it=0; it < c; ++it) {
00320             CalendarListViewItem *item = static_cast<CalendarListViewItem*>(calendarList->topLevelItem(it));
00321             if (cal != item && !item->hasBaseCalendar(cal)) {
00322                 baseCalendar->addItem(item->text(0));
00323                 baseCalendarList.append(item);
00324                 i++;
00325                 if (item == cal->base) {
00326                     me = i;
00327                     //kDebug()<<k_funcinfo<<"item="<<item<<": cal="<<cal->calendar->name()<<" has parent "<<cal->base->calendar->name()<<endl;
00328                 }
00329             }
00330         }
00331         baseCalendar->setCurrentIndex(me);
00332         baseCalendar->setEnabled(true);
00333         return;
00334     }
00335     calendar->clear();
00336 }
00337 void CalendarListDialogImpl::setCalendar(Calendar *cal) {
00338     calendar->setCalendar(cal);
00339     calendar->setEnabled(true);
00340 }
00341 
00342 void CalendarListDialogImpl::slotCalendarModified() {
00343     QList<QTreeWidgetItem*> lst = calendarList->selectedItems();
00344     if (lst.count() == 0) {
00345         return;
00346     }
00347     CalendarListViewItem *item = static_cast<CalendarListViewItem*>(lst[0]);
00348     item->setState(CalendarListViewItem::Modified);
00349     //kDebug()<<k_funcinfo<<"("<<item->calendar<<")"<<endl;
00350     
00351     emit calendarModified();
00352 }
00353 
00354 void CalendarListDialogImpl::slotDeleteClicked() {
00355     QList<QTreeWidgetItem*> lst = calendarList->selectedItems();
00356     foreach (QTreeWidgetItem *i, lst) {
00357         CalendarListViewItem *item = static_cast<CalendarListViewItem*>(i);
00358         calendarList->takeTopLevelItem(calendarList->indexOfTopLevelItem(item));
00359         item->setState(CalendarListViewItem::Deleted);
00360         m_deletedItems.append(item);
00361     }
00362     if (lst.count() > 0)
00363         emit enableButtonOk(true);
00364 }
00365 
00366 void CalendarListDialogImpl::slotAddClicked() {
00367     disconnect(calendarList, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(slotItemChanged(QTreeWidgetItem*, int)));
00368     
00369     Calendar *cal = new Calendar();
00370     cal->setProject(&project);
00371     CalendarListViewItem *item = new CalendarListViewItem(*this, calendarList, cal);
00372     item->setState(CalendarListViewItem::New);
00373     calendarList->clearSelection();
00374     item->setSelected(true);
00375     calendarList->editItem(item, 0);
00376     
00377     connect(calendarList, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(slotItemChanged(QTreeWidgetItem*, int)));
00378 }
00379 
00380 QList<CalendarListViewItem*> &CalendarListDialogImpl::deletedItems() {
00381     return m_deletedItems;
00382 }
00383 
00384 CalendarListViewItem *CalendarListDialogImpl::findItem(Calendar *cal) {
00385     if (!cal)
00386         return 0;
00387     int c = calendarList->topLevelItemCount();
00388     for (int i=0; i < c; ++i) {
00389         CalendarListViewItem *item = static_cast<CalendarListViewItem *>(calendarList->topLevelItem(i));
00390         if (cal == item->original || cal == item->calendar) {
00391             //kDebug()<<k_funcinfo<<"Found: "<<cal->name()<<endl;
00392             return item;
00393         }
00394     }
00395     return 0;
00396 }
00397 
00398 void CalendarListDialogImpl::slotListDoubleClicked(const QModelIndex &index) {
00399     slotListDoubleClicked(calendarList->itemAt(index.row(), index.column()), index.column());
00400 }
00401 
00402 void CalendarListDialogImpl::slotListDoubleClicked(QTreeWidgetItem *item, int col) {
00403     //kDebug()<<k_funcinfo<<(item?item->text(0):"")<<endl;
00404 }
00405 
00406 }  //KPlato namespace
00407 
00408 #include "kptcalendarlistdialog.moc"

Généré le Wed Nov 22 23:40:55 2006 pour KPlato par  doxygen 1.5.1-p1