kplato

kptcalendaredit.cc

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 <qbuttongroup.h>
00028 #include <qheader.h>
00029 #include <qpushbutton.h>
00030 #include <qradiobutton.h>
00031 #include <qcombobox.h>
00032 #include <qlabel.h>
00033 #include <qtextedit.h>
00034 #include <qlineedit.h>
00035 #include <qdatetimeedit.h>
00036 #include <qdatetime.h>
00037 #include <qtabwidget.h>
00038 #include <qtextbrowser.h>
00039 
00040 #include <klocale.h>
00041 
00042 #include <kabc/addressee.h>
00043 #include <kabc/addresseedialog.h>
00044 
00045 #include <qmap.h>
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()->setStretchEnabled(true, 0);
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     //kdDebug()<<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->firstChild());
00096     }
00097 }
00098 
00099 void CalendarEdit::slotClearClicked() {
00100     //kdDebug()<<k_funcinfo<<endl;
00101     intervalList->clear();
00102     bApply->setEnabled(false);
00103 }
00104 void CalendarEdit::slotAddIntervalClicked() {
00105     //kdDebug()<<k_funcinfo<<endl;
00106     intervalList->insertItem(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     //kdDebug()<<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         //kdDebug()<<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->currentItem()); //NOTE!!
00123         calDay->clearIntervals();
00124         if (calDay->state() == Map::Working) {
00125             for (QListViewItem *item = intervalList->firstChild(); item; item = item->nextSibling()) {
00126                 //kdDebug()<<k_funcinfo<<"Adding interval: "<<static_cast<IntervalItem *>(item)->interval().first.toString()<<"-"<<static_cast<IntervalItem *>(item)->interval().second.toString()<<endl;
00127                 calDay->addInterval(static_cast<IntervalItem *>(item)->interval());
00128             }
00129         }
00130     }
00131 
00132     IntMap weekdays = calendarPanel->selectedWeekdays();
00133     for(IntMap::iterator it = weekdays.begin(); it != weekdays.end(); ++it) {
00134         //kdDebug()<<k_funcinfo<<"weekday="<<it.key()<<endl;
00135         CalendarDay *weekday = m_calendar->weekday(it.key()-1);
00136         weekday->setState(state->currentItem());//NOTE!!
00137         weekday->clearIntervals();
00138         if (weekday->state() == Map::Working) {
00139             for (QListViewItem *item = intervalList->firstChild(); item; item = item->nextSibling()) {
00140                 //kdDebug()<<k_funcinfo<<"Adding interval: "<<static_cast<IntervalItem *>(item)->interval().first.toString()<<"-"<<static_cast<IntervalItem *>(item)->interval().second.toString()<<endl;
00141                 weekday->addInterval(static_cast<IntervalItem *>(item)->interval());
00142             }
00143         }
00144     }
00145 
00146     calendarPanel->markSelected(state->currentItem()); //NOTE!!
00147     emit applyClicked();
00148     slotCheckAllFieldsFilled();
00149 }
00150 
00151 void CalendarEdit::slotCheckAllFieldsFilled() {
00152     //kdDebug()<<k_funcinfo<<endl;
00153     if (state->currentItem() == 0 /*undefined*/ ||
00154         state->currentItem() == 1 /*Non-working*/||
00155         (state->currentItem() == 2 /*Working*/ && intervalList->firstChild()))
00156     {
00157         emit obligatedFieldsFilled(true);
00158     }
00159     else if (state->currentItem() == 2 && !intervalList->firstChild())
00160     {
00161         emit obligatedFieldsFilled(false);
00162     }
00163 }
00164 
00165 void CalendarEdit::setCalendar(Calendar *cal) {
00166     m_calendar = cal;
00167     clear();
00168     calendarPanel->setCalendar(cal);
00169 }
00170 
00171 void CalendarEdit::clear() {
00172     clearPanel();
00173     clearEditPart();
00174 }
00175 
00176 void CalendarEdit::clearPanel() {
00177     calendarPanel->clear();
00178 }
00179 
00180 void CalendarEdit::clearEditPart() {
00181     day->setEnabled(true);
00182     intervalList->clear();
00183     intervalList->setEnabled(false);
00184     startTime->setEnabled(false);
00185     startTime->setTime(QTime(8, 0, 0)); //FIXME
00186     endTime->setEnabled(false);
00187     endTime->setTime(QTime(16, 0, 0)); //FIXME
00188 
00189     bAddInterval->setEnabled(false);
00190     bClear->setEnabled(false);
00191     bApply->setEnabled(false);
00192     state->setEnabled(false);
00193 }
00194 
00195 void CalendarEdit::slotDateSelected(QDate date) {
00196     if (m_calendar == 0)
00197         return;
00198     //kdDebug()<<k_funcinfo<<"("<<date.toString()<<")"<<endl;
00199     clearEditPart();
00200     state->clear();
00201     state->insertItem(i18n("Undefined"));
00202     state->insertItem(i18n("Non-working"));
00203     state->insertItem(i18n("Working"));
00204 
00205     CalendarDay *calDay = m_calendar->findDay(date);
00206     state->setEnabled(true);
00207     if (calDay) {
00208         QPtrListIterator<QPair<QTime, QTime> > it = calDay->workingIntervals();
00209         for (; it.current(); ++it) {
00210             IntervalItem *item = new IntervalItem(intervalList, it.current()->first, it.current()->second);
00211             intervalList->insertItem(item);
00212         }
00213         if (calDay->state() == Map::Working) {
00214             //kdDebug()<<k_funcinfo<<"("<<date.toString()<<") is workday"<<endl;
00215             state->setCurrentItem(2);
00216             slotStateActivated(2);
00217             bApply->setEnabled(calDay->workingIntervals().count() > 0);
00218         } else if (calDay->state() == Map::NonWorking){
00219             //kdDebug()<<k_funcinfo<<"("<<date.toString()<<") is holiday"<<endl;
00220             state->setCurrentItem(1);
00221             slotStateActivated(1);
00222             bApply->setEnabled(true);
00223         } else  {
00224             //kdDebug()<<k_funcinfo<<"("<<date.toString()<<")=none"<<endl;
00225             state->setCurrentItem(0);
00226             slotStateActivated(0);
00227             bApply->setEnabled(true);
00228         }
00229     } else {
00230         // default
00231         state->setCurrentItem(0);
00232         slotStateActivated(0);
00233         bApply->setEnabled(true);
00234     }
00235 }
00236 
00237 void CalendarEdit::slotWeekdaySelected(int day_/* 1..7 */) {
00238     if (m_calendar == 0 || day_ < 1 || day_ > 7) {
00239         kdError()<<k_funcinfo<<"No calendar or weekday ("<<day_<<") not defined!"<<endl;
00240         return;
00241     }
00242     //kdDebug()<<k_funcinfo<<"("<<day_<<")"<<endl;
00243     clearEditPart();
00244     CalendarDay *calDay = m_calendar->weekday(day_-1); // 0..6
00245     if (!calDay) {
00246         kdError()<<k_funcinfo<<"Weekday ("<<day_<<") not defined!"<<endl;
00247         return;
00248     }
00249     state->clear();
00250     state->insertItem(i18n("Undefined"));
00251     state->insertItem(i18n("Non-working"));
00252     state->insertItem(i18n("Working"));
00253     QPtrListIterator<QPair<QTime, QTime> > it = calDay->workingIntervals();
00254     for (; it.current(); ++it) {
00255         IntervalItem *item = new IntervalItem(intervalList, it.current()->first, it.current()->second);
00256         intervalList->insertItem(item);
00257     }
00258     state->setEnabled(true);
00259     if (calDay->state() == Map::Working) {
00260         //kdDebug()<<k_funcinfo<<"("<<day_<<")=workday"<<endl;
00261         state->setCurrentItem(2);
00262         slotStateActivated(2);
00263         bApply->setEnabled(calDay->workingIntervals().count() > 0);
00264     } else if (calDay->state() == Map::NonWorking) {
00265         //kdDebug()<<k_funcinfo<<"("<<day_<<")=Holiday"<<endl;
00266         state->setCurrentItem(1);
00267         slotStateActivated(1);
00268         bApply->setEnabled(true);
00269     } else {
00270         //kdDebug()<<k_funcinfo<<"("<<day_<<")=none"<<endl;
00271         state->setCurrentItem(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"
KDE Home | KDE Accessibility Home | Description of Access Keys