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

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 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 "kptcalendaredit.h"
00021 #include "kptproject.h"
00022 #include "kptcalendar.h"
00023 #include "kptcalendarpanel.h"
00024 #include "kptmap.h"
00025 #include "intervalitem.h"
00026 
00027 #include <q3buttongroup.h>
00028 #include <QHeaderView>
00029 #include <QPushButton>
00030 #include <qradiobutton.h>
00031 #include <QComboBox>
00032 #include <QLabel>
00033 #include <q3textedit.h>
00034 #include <QLineEdit>
00035 #include <q3datetimeedit.h>
00036 #include <qdatetime.h>
00037 #include <qtabwidget.h>
00038 #include <q3textbrowser.h>
00039 
00040 #include <klocale.h>
00041 
00042 #include <kabc/addressee.h>
00043 #include <kabc/addresseedialog.h>
00044 
00045 #include <QMap>
00046 
00047 #include <kdebug.h>
00048 
00049 namespace KPlato
00050 {
00051 
00052 CalendarEdit::CalendarEdit (QWidget *parent, const char */*name*/)
00053     : CalendarEditBase(parent),
00054       m_calendar(0)
00055  {
00056 
00057     clear();
00058     intervalList->header()->setStretchLastSection(true);
00059 //     intervalList->setShowSortIndicator(true);
00060 //     intervalList->setSorting(0);
00061 
00062     connect (calendarPanel, SIGNAL(dateChanged(QDate)), SLOT(slotDateSelected(QDate)));
00063     connect (calendarPanel, SIGNAL(weekdaySelected(int)), SLOT(slotWeekdaySelected(int)));
00064     connect(calendarPanel, SIGNAL(selectionCleared()), SLOT(slotSelectionCleared()));
00065 
00066     connect (state, SIGNAL(activated(int)), SLOT(slotStateActivated(int)));
00067     connect (bClear, SIGNAL(clicked()), SLOT(slotClearClicked()));
00068     connect (bAddInterval, SIGNAL(clicked()), SLOT(slotAddIntervalClicked()));
00069 
00070     connect (bApply, SIGNAL(clicked()), SLOT(slotApplyClicked()));
00071 }
00072 
00073 void CalendarEdit::slotStateActivated(int id) {
00074     //kDebug()<<k_funcinfo<<"id="<<id<<endl;
00075     if (id == 0) { // undefined
00076         startTime->setEnabled(false);
00077         endTime->setEnabled(false);
00078         bClear->setEnabled(false);
00079         bAddInterval->setEnabled(false);
00080         intervalList->setEnabled(false);
00081         bApply->setEnabled(true);
00082     } else if (id == 1) { // non working
00083         startTime->setEnabled(false);
00084         endTime->setEnabled(false);
00085         bClear->setEnabled(false);
00086         bAddInterval->setEnabled(false);
00087         intervalList->setEnabled(false);
00088         bApply->setEnabled(true);
00089     } else if (id == 2) { //working
00090         startTime->setEnabled(true);
00091         endTime->setEnabled(true);
00092         bClear->setEnabled(true);
00093         bAddInterval->setEnabled(true);
00094         intervalList->setEnabled(true);
00095         bApply->setEnabled(intervalList->topLevelItemCount() > 0);
00096     }
00097 }
00098 
00099 void CalendarEdit::slotClearClicked() {
00100     //kDebug()<<k_funcinfo<<endl;
00101     intervalList->clear();
00102     bApply->setEnabled(false);
00103 }
00104 void CalendarEdit::slotAddIntervalClicked() {
00105     //kDebug()<<k_funcinfo<<endl;
00106     intervalList->addTopLevelItem(new IntervalItem(intervalList, startTime->time(), endTime->time()));
00107     bApply->setEnabled(true);
00108 }
00109 
00110 //NOTE: enum Map::State must match combobox state!
00111 void CalendarEdit::slotApplyClicked() {
00112     //kDebug()<<k_funcinfo<<"("<<m_calendar<<")"<<endl;
00113     DateMap dates = calendarPanel->selectedDates();
00114     for(DateMap::iterator it = dates.begin(); it != dates.end(); ++it) {
00115         QDate date = QDate::fromString(it.key(), Qt::ISODate);
00116         //kDebug()<<k_funcinfo<<"Date: "<<date<<endl;
00117         CalendarDay *calDay = m_calendar->findDay(date);
00118         if (!calDay) {
00119             calDay = new CalendarDay(date);
00120             m_calendar->addDay(calDay);
00121         }
00122         calDay->setState(state->currentIndex()); //NOTE!!
00123         calDay->clearIntervals();
00124         if (calDay->state() == Map::Working) {
00125             int cnt = intervalList->topLevelItemCount();
00126             for (int i = 0; i < cnt; ++i) {
00127                 //kDebug()<<k_funcinfo<<"Adding interval: "<<static_cast<IntervalItem *>(item)->interval().first.toString()<<"-"<<static_cast<IntervalItem *>(item)->interval().second.toString()<<endl;
00128                 calDay->addInterval(static_cast<IntervalItem *>(intervalList->topLevelItem(i))->interval());
00129             }
00130         }
00131     }
00132 
00133     IntMap weekdays = calendarPanel->selectedWeekdays();
00134     for(IntMap::iterator it = weekdays.begin(); it != weekdays.end(); ++it) {
00135         //kDebug()<<k_funcinfo<<"weekday="<<it.key()<<endl;
00136         CalendarDay *weekday = m_calendar->weekday(it.key()-1);
00137         weekday->setState(state->currentIndex());//NOTE!!
00138         weekday->clearIntervals();
00139         if (weekday->state() == Map::Working) {
00140             int cnt = intervalList->topLevelItemCount();
00141             for (int i = 0; i < cnt; ++i) {
00142                 //kDebug()<<k_funcinfo<<"Adding interval: "<<static_cast<IntervalItem *>(item)->interval().first.toString()<<"-"<<static_cast<IntervalItem *>(item)->interval().second.toString()<<endl;
00143                 weekday->addInterval(static_cast<IntervalItem *>(intervalList->topLevelItem(i))->interval());
00144             }
00145         }
00146     }
00147 
00148     calendarPanel->markSelected(state->currentIndex()); //NOTE!!
00149     emit applyClicked();
00150     slotCheckAllFieldsFilled();
00151 }
00152 
00153 void CalendarEdit::slotCheckAllFieldsFilled() {
00154     //kDebug()<<k_funcinfo<<endl;
00155     if (state->currentItem() == 0 /*undefined*/ ||
00156         state->currentIndex() == 1 /*Non-working*/||
00157         (state->currentItem() == 2 /*Working*/ && intervalList->topLevelItemCount() > 0))
00158     {
00159         emit obligatedFieldsFilled(true);
00160     }
00161     else if (state->currentIndex() == 2 && !intervalList->topLevelItemCount() > 0)
00162     {
00163         emit obligatedFieldsFilled(false);
00164     }
00165 }
00166 
00167 void CalendarEdit::setCalendar(Calendar *cal) {
00168     m_calendar = cal;
00169     clear();
00170     calendarPanel->setCalendar(cal);
00171 }
00172 
00173 void CalendarEdit::clear() {
00174     clearPanel();
00175     clearEditPart();
00176 }
00177 
00178 void CalendarEdit::clearPanel() {
00179     calendarPanel->clear();
00180 }
00181 
00182 void CalendarEdit::clearEditPart() {
00183     day->setEnabled(true);
00184     intervalList->clear();
00185     intervalList->setEnabled(false);
00186     startTime->setEnabled(false);
00187     startTime->setTime(QTime(8, 0, 0)); //FIXME
00188     endTime->setEnabled(false);
00189     endTime->setTime(QTime(16, 0, 0)); //FIXME
00190 
00191     bAddInterval->setEnabled(false);
00192     bClear->setEnabled(false);
00193     bApply->setEnabled(false);
00194     state->setEnabled(false);
00195 }
00196 
00197 void CalendarEdit::slotDateSelected(QDate date) {
00198     if (m_calendar == 0)
00199         return;
00200     //kDebug()<<k_funcinfo<<"("<<date.toString()<<")"<<endl;
00201     clearEditPart();
00202     state->clear();
00203     state->addItem(i18n("Undefined"));
00204     state->addItem(i18n("Non-working"));
00205     state->addItem(i18n("Working"));
00206 
00207     CalendarDay *calDay = m_calendar->findDay(date);
00208     state->setEnabled(true);
00209     if (calDay) {
00210         foreach (TimeInterval *i, calDay->workingIntervals()) {
00211             IntervalItem *item = new IntervalItem(intervalList, i->first, i->second);
00212             intervalList->addTopLevelItem(item);
00213         }
00214         if (calDay->state() == Map::Working) {
00215             //kDebug()<<k_funcinfo<<"("<<date.toString()<<") is workday"<<endl;
00216             state->setCurrentIndex(2);
00217             slotStateActivated(2);
00218             bApply->setEnabled(calDay->workingIntervals().count() > 0);
00219         } else if (calDay->state() == Map::NonWorking){
00220             //kDebug()<<k_funcinfo<<"("<<date.toString()<<") is holiday"<<endl;
00221             state->setCurrentIndex(1);
00222             slotStateActivated(1);
00223             bApply->setEnabled(true);
00224         } else  {
00225             //kDebug()<<k_funcinfo<<"("<<date.toString()<<")=none"<<endl;
00226             state->setCurrentIndex(0);
00227             slotStateActivated(0);
00228             bApply->setEnabled(true);
00229         }
00230     } else {
00231         // default
00232         state->setCurrentIndex(0);
00233         slotStateActivated(0);
00234         bApply->setEnabled(true);
00235     }
00236 }
00237 
00238 void CalendarEdit::slotWeekdaySelected(int day_/* 1..7 */) {
00239     if (m_calendar == 0 || day_ < 1 || day_ > 7) {
00240         kError()<<k_funcinfo<<"No calendar or weekday ("<<day_<<") not defined!"<<endl;
00241         return;
00242     }
00243     //kDebug()<<k_funcinfo<<"("<<day_<<")"<<endl;
00244     clearEditPart();
00245     CalendarDay *calDay = m_calendar->weekday(day_-1); // 0..6
00246     if (!calDay) {
00247         kError()<<k_funcinfo<<"Weekday ("<<day_<<") not defined!"<<endl;
00248         return;
00249     }
00250     state->clear();
00251     state->addItem(i18n("Undefined"));
00252     state->addItem(i18n("Non-working"));
00253     state->addItem(i18n("Working"));
00254     foreach (TimeInterval *i, calDay->workingIntervals()) {
00255         IntervalItem *item = new IntervalItem(intervalList, i->first, i->second);
00256         intervalList->addTopLevelItem(item);
00257     }
00258     state->setEnabled(true);
00259     if (calDay->state() == Map::Working) {
00260         //kDebug()<<k_funcinfo<<"("<<day_<<")=workday"<<endl;
00261         state->setCurrentIndex(2);
00262         slotStateActivated(2);
00263         bApply->setEnabled(calDay->workingIntervals().count() > 0);
00264     } else if (calDay->state() == Map::NonWorking) {
00265         //kDebug()<<k_funcinfo<<"("<<day_<<")=Holiday"<<endl;
00266         state->setCurrentIndex(1);
00267         slotStateActivated(1);
00268         bApply->setEnabled(true);
00269     } else {
00270         //kDebug()<<k_funcinfo<<"("<<day_<<")=none"<<endl;
00271         state->setCurrentIndex(0);
00272         slotStateActivated(0);
00273         bApply->setEnabled(true);
00274     }
00275 }
00276 
00277 void CalendarEdit::slotSelectionCleared() {
00278     clearEditPart();
00279 }
00280 
00281 }  //KPlato namespace
00282 
00283 #include "kptcalendaredit.moc"

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