korganizer Library API Documentation

koeditorgeneralevent.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qtooltip.h>
00025 #include <qlayout.h>
00026 #include <qvbox.h>
00027 #include <qbuttongroup.h>
00028 #include <qvgroupbox.h>
00029 #include <qwidgetstack.h>
00030 #include <qdatetime.h>
00031 
00032 #include <kdebug.h>
00033 #include <kglobal.h>
00034 #include <klocale.h>
00035 #include <kiconloader.h>
00036 #include <kmessagebox.h>
00037 #include <kfiledialog.h>
00038 #include <kstandarddirs.h>
00039 
00040 #include <libkcal/event.h>
00041 
00042 #include <libkdepim/kdateedit.h>
00043 
00044 #include "koprefs.h"
00045 
00046 #include "koeditorgeneralevent.h"
00047 #include "koeditorgeneralevent.moc"
00048 
00049 KOEditorGeneralEvent::KOEditorGeneralEvent(QObject* parent,
00050                                            const char* name) :
00051   KOEditorGeneral( parent, name)
00052 {
00053   connect(this,SIGNAL(dateTimesChanged(QDateTime,QDateTime)),
00054           SLOT(setDuration()));
00055   connect(this,SIGNAL(dateTimesChanged(QDateTime,QDateTime)),
00056           SLOT(emitDateTimeStr()));
00057 }
00058 
00059 KOEditorGeneralEvent::~KOEditorGeneralEvent()
00060 {
00061 }
00062 
00063 void KOEditorGeneralEvent::finishSetup()
00064 {
00065   QWidget::setTabOrder(mSummaryEdit, mLocationEdit);
00066   QWidget::setTabOrder(mLocationEdit, mStartDateEdit);
00067   QWidget::setTabOrder(mStartDateEdit, mStartTimeEdit);
00068   QWidget::setTabOrder(mStartTimeEdit, mEndDateEdit);
00069   QWidget::setTabOrder(mEndDateEdit, mEndTimeEdit);
00070   QWidget::setTabOrder(mEndTimeEdit, mRecursButton);
00071   QWidget::setTabOrder(mRecursButton, mNoTimeButton);
00072   QWidget::setTabOrder(mNoTimeButton, mAlarmButton);
00073   QWidget::setTabOrder(mAlarmButton, mFreeTimeCombo);
00074   QWidget::setTabOrder(mFreeTimeCombo, mCategoriesButton);
00075   QWidget::setTabOrder(mCategoriesButton, mSecrecyCombo);
00076   QWidget::setTabOrder(mSecrecyCombo, mDescriptionEdit);
00077 
00078   mSummaryEdit->setFocus();
00079 }
00080 
00081 void KOEditorGeneralEvent::initTime(QWidget *parent,QBoxLayout *topLayout)
00082 {
00083   QBoxLayout *timeLayout = new QVBoxLayout(topLayout);
00084 
00085   QGroupBox *timeGroupBox = new QGroupBox(1,QGroupBox::Horizontal,
00086                                           i18n("Date && Time"),parent);
00087   timeLayout->addWidget(timeGroupBox);
00088 
00089   QFrame *timeBoxFrame = new QFrame(timeGroupBox);
00090 
00091   QGridLayout *layoutTimeBox = new QGridLayout(timeBoxFrame,2,3);
00092   layoutTimeBox->setSpacing(topLayout->spacing());
00093 
00094 
00095   mStartDateLabel = new QLabel(i18n("Start:"),timeBoxFrame);
00096   layoutTimeBox->addWidget(mStartDateLabel,0,0);
00097   
00098   mStartDateEdit = new KDateEdit(timeBoxFrame);
00099   layoutTimeBox->addWidget(mStartDateEdit,0,1);
00100 
00101   mStartTimeEdit = new KTimeEdit(timeBoxFrame);
00102   layoutTimeBox->addWidget(mStartTimeEdit,0,2);
00103 
00104 
00105   mEndDateLabel = new QLabel(i18n("End:"),timeBoxFrame);
00106   layoutTimeBox->addWidget(mEndDateLabel,1,0);
00107 
00108   mEndDateEdit = new KDateEdit(timeBoxFrame);
00109   layoutTimeBox->addWidget(mEndDateEdit,1,1);
00110 
00111   mEndTimeEdit = new KTimeEdit(timeBoxFrame);
00112   layoutTimeBox->addWidget(mEndTimeEdit,1,2);
00113 
00114   QHBoxLayout *flagsBox = new QHBoxLayout( timeBoxFrame );
00115 
00116   mRecursButton = new QCheckBox(i18n("Recurring event"),timeBoxFrame);
00117   flagsBox->addWidget(mRecursButton);
00118   connect(mRecursButton,SIGNAL(toggled(bool)),SIGNAL(recursChanged(bool)));
00119 #ifdef KORG_NORECURRENCE
00120   mRecursButton->hide();
00121 #endif
00122 
00123   mNoTimeButton = new QCheckBox(i18n("No time associated"),timeBoxFrame);
00124   flagsBox->addWidget(mNoTimeButton);
00125   connect(mNoTimeButton, SIGNAL(toggled(bool)),SLOT(dontAssociateTime(bool)));
00126 
00127   mDurationLabel = new QLabel(timeBoxFrame);
00128   flagsBox->addWidget(mDurationLabel,0,2);
00129 
00130   layoutTimeBox->addMultiCellLayout(flagsBox,2,2,0,3);
00131 
00132   // time widgets are checked if they contain a valid time
00133   connect(mStartTimeEdit, SIGNAL(timeChanged(QTime)),
00134           this, SLOT(startTimeChanged(QTime)));
00135   connect(mEndTimeEdit, SIGNAL(timeChanged(QTime)),
00136           this, SLOT(endTimeChanged(QTime)));
00137 
00138   // date widgets are checked if they contain a valid date
00139   connect(mStartDateEdit, SIGNAL(dateChanged(QDate)),
00140           this, SLOT(startDateChanged(QDate)));
00141   connect(mEndDateEdit, SIGNAL(dateChanged(QDate)),
00142           this, SLOT(endDateChanged(QDate)));
00143 }
00144 
00145 void KOEditorGeneralEvent::initClass(QWidget *parent,QBoxLayout *topLayout)
00146 {
00147   QBoxLayout *classLayout = new QHBoxLayout(topLayout);
00148 
00149   QLabel *freeTimeLabel = new QLabel(i18n("Show time as:"),parent);
00150   classLayout->addWidget(freeTimeLabel);
00151 
00152   mFreeTimeCombo = new QComboBox(false, parent);
00153   mFreeTimeCombo->insertItem(i18n("Busy"));
00154   mFreeTimeCombo->insertItem(i18n("Free"));
00155   classLayout->addWidget(mFreeTimeCombo);
00156 }
00157 
00158 void KOEditorGeneralEvent::timeStuffDisable(bool disable)
00159 {
00160   mStartTimeEdit->setEnabled( !disable );
00161   mEndTimeEdit->setEnabled( !disable );
00162 
00163   setDuration();
00164   emitDateTimeStr();
00165 }
00166 
00167 void KOEditorGeneralEvent::dontAssociateTime(bool noTime)
00168 {
00169   timeStuffDisable(noTime);
00170   //if(alarmButton->isChecked()) alarmStuffDisable(noTime);
00171   allDayChanged(noTime);
00172 }
00173                 
00174 void KOEditorGeneralEvent::setDateTimes(QDateTime start, QDateTime end)
00175 {
00176 //  kdDebug() << "KOEditorGeneralEvent::setDateTimes(): Start DateTime: " << start.toString() << endl;
00177 
00178   mStartDateEdit->setDate(start.date());
00179   // KTimeEdit seems to emit some signals when setTime() is called.
00180   mStartTimeEdit->blockSignals( true );
00181   mStartTimeEdit->setTime(start.time());
00182   mStartTimeEdit->blockSignals( false );
00183   mEndDateEdit->setDate(end.date());
00184   mEndTimeEdit->setTime(end.time());
00185 
00186   mCurrStartDateTime = start;
00187   mCurrEndDateTime = end;
00188 
00189   setDuration();
00190   emitDateTimeStr();
00191 }
00192 
00193 void KOEditorGeneralEvent::startTimeChanged(QTime newtime)
00194 {
00195   kdDebug() << "KOEditorGeneralEvent::startTimeChanged() " << newtime.toString() << endl;
00196 
00197   int secsep = mCurrStartDateTime.secsTo(mCurrEndDateTime);
00198   
00199   mCurrStartDateTime.setTime(newtime);
00200 
00201   // adjust end time so that the event has the same duration as before.
00202   mCurrEndDateTime = mCurrStartDateTime.addSecs(secsep);
00203   mEndTimeEdit->setTime(mCurrEndDateTime.time());
00204   mEndDateEdit->setDate(mCurrEndDateTime.date());
00205   
00206   emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
00207 }
00208 
00209 void KOEditorGeneralEvent::endTimeChanged(QTime newtime)
00210 {
00211 //  kdDebug() << "KOEditorGeneralEvent::endTimeChanged " << newtime.toString() << endl;
00212 
00213   QDateTime newdt(mCurrEndDateTime.date(), newtime);
00214 
00215   if(newdt < mCurrStartDateTime) {
00216     // oops, can't let that happen.
00217     newdt = mCurrStartDateTime;
00218     mEndTimeEdit->setTime(newdt.time());
00219   }
00220   mCurrEndDateTime = newdt;
00221   
00222   emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
00223 }
00224 
00225 void KOEditorGeneralEvent::startDateChanged(QDate newdate)
00226 {
00227   int daysep = mCurrStartDateTime.daysTo(mCurrEndDateTime);
00228   
00229   mCurrStartDateTime.setDate(newdate);
00230   
00231   // adjust end date so that the event has the same duration as before
00232   mCurrEndDateTime.setDate(mCurrStartDateTime.date().addDays(daysep));
00233   mEndDateEdit->setDate(mCurrEndDateTime.date());
00234 
00235   emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
00236 }
00237 
00238 void KOEditorGeneralEvent::endDateChanged(QDate newdate)
00239 {
00240   QDateTime newdt(newdate, mCurrEndDateTime.time());
00241 
00242   if(newdt < mCurrStartDateTime) {
00243     // oops, we can't let that happen.
00244     newdt = mCurrStartDateTime;
00245     mEndDateEdit->setDate(newdt.date());
00246     mEndTimeEdit->setTime(newdt.time());
00247   }
00248   mCurrEndDateTime = newdt;
00249 
00250   emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
00251 }
00252 
00253 void KOEditorGeneralEvent::setDefaults(QDateTime from,QDateTime to,bool allDay)
00254 {
00255   KOEditorGeneral::setDefaults(allDay);
00256 
00257   mNoTimeButton->setChecked(allDay);
00258   timeStuffDisable(allDay);
00259 
00260   setDateTimes(from,to);
00261 
00262   mRecursButton->setChecked(false);
00263 }
00264 
00265 void KOEditorGeneralEvent::readEvent( Event *event, bool tmpl )
00266 {
00267   QString tmpStr;
00268 
00269   if ( !tmpl ) {
00270     // the rest is for the events only
00271     mNoTimeButton->setChecked(event->doesFloat());
00272     timeStuffDisable(event->doesFloat());
00273 
00274     setDateTimes(event->dtStart(),event->dtEnd());
00275   }
00276 
00277   mRecursButton->setChecked(event->recurrence()->doesRecur());
00278 
00279   if (event->transparency() > 0)
00280     mFreeTimeCombo->setCurrentItem(1);
00281   // else it is implicitly 0 (i.e. busy)
00282 
00283   readIncidence(event);  
00284 }
00285 
00286 void KOEditorGeneralEvent::writeEvent(Event *event)
00287 {
00288 //  kdDebug() << "KOEditorGeneralEvent::writeEvent()" << endl;
00289 
00290   writeIncidence(event);
00291 
00292   QDate tmpDate;
00293   QTime tmpTime;
00294   QDateTime tmpDT;
00295 
00296   // temp. until something better happens.
00297   QString tmpStr;
00298 
00299   if (mNoTimeButton->isChecked()) {
00300     event->setFloats(true);
00301     // need to change this.
00302     tmpDate = mStartDateEdit->date();
00303     tmpTime.setHMS(0,0,0);
00304     tmpDT.setDate(tmpDate);
00305     tmpDT.setTime(tmpTime);
00306     event->setDtStart(tmpDT);
00307 
00308     tmpDate = mEndDateEdit->date();
00309     tmpTime.setHMS(0,0,0);
00310     tmpDT.setDate(tmpDate);
00311     tmpDT.setTime(tmpTime);
00312     event->setDtEnd(tmpDT);
00313   } else {
00314     event->setFloats(false);
00315 
00316     // set date/time end
00317     tmpDate = mEndDateEdit->date();
00318     tmpTime = mEndTimeEdit->getTime();
00319     tmpDT.setDate(tmpDate);
00320     tmpDT.setTime(tmpTime);
00321     event->setDtEnd(tmpDT);
00322 
00323     // set date/time start
00324     tmpDate = mStartDateEdit->date();
00325     tmpTime = mStartTimeEdit->getTime();
00326     tmpDT.setDate(tmpDate);
00327     tmpDT.setTime(tmpTime);
00328     event->setDtStart(tmpDT);
00329   } // check for float
00330   
00331   event->setTransparency(mFreeTimeCombo->currentItem());
00332 
00333 //  kdDebug() << "KOEditorGeneralEvent::writeEvent() done" << endl;
00334 }
00335 
00336 void KOEditorGeneralEvent::setDuration()
00337 {
00338   QString tmpStr, catStr;
00339   int hourdiff, minutediff;
00340 
00341   if (mNoTimeButton->isChecked()) {
00342     int daydiff = mCurrStartDateTime.date().daysTo(mCurrEndDateTime.date()) + 1;
00343     tmpStr = i18n("Duration: ");
00344     tmpStr.append(i18n("1 Day","%n Days",daydiff));
00345   } else {
00346     hourdiff = mCurrStartDateTime.date().daysTo(mCurrEndDateTime.date()) * 24;
00347     hourdiff += mCurrEndDateTime.time().hour() - 
00348       mCurrStartDateTime.time().hour();
00349     minutediff = mCurrEndDateTime.time().minute() -
00350       mCurrStartDateTime.time().minute();
00351     // If minutediff is negative, "borrow" 60 minutes from hourdiff
00352     if (minutediff < 0 && hourdiff > 0) {
00353       hourdiff -= 1;
00354       minutediff += 60;
00355     }
00356     if (hourdiff || minutediff){
00357       tmpStr = i18n("Duration: ");
00358       if (hourdiff){
00359         catStr = i18n("1 hour","%n hours",hourdiff);
00360         tmpStr.append(catStr);
00361       }
00362       if (hourdiff && minutediff){
00363         tmpStr += i18n(", ");
00364       }
00365       if (minutediff){
00366         catStr = i18n("1 minute","%n minutes",minutediff);
00367         tmpStr += catStr;
00368       }
00369     } else tmpStr = "";
00370   }
00371   mDurationLabel->setText(tmpStr);
00372 }
00373 
00374 void KOEditorGeneralEvent::emitDateTimeStr()
00375 {
00376   KLocale *l = KGlobal::locale();
00377   
00378   QString from,to;
00379   if (mNoTimeButton->isChecked()) {
00380     from = l->formatDate(mCurrStartDateTime.date());
00381     to = l->formatDate(mCurrEndDateTime.date());
00382   } else {
00383     from = l->formatDateTime(mCurrStartDateTime);
00384     to = l->formatDateTime(mCurrEndDateTime);
00385   }
00386   
00387   QString str = i18n("From: %1   To: %2   %3").arg(from).arg(to)
00388                 .arg(mDurationLabel->text());
00389                  
00390   emit dateTimeStrChanged(str);
00391 }
00392 
00393 bool KOEditorGeneralEvent::validateInput()
00394 {
00395 //  kdDebug() << "KOEditorGeneralEvent::validateInput()" << endl;
00396 
00397   if (!mNoTimeButton->isChecked()) {
00398     if (!mStartTimeEdit->inputIsValid()) {
00399       KMessageBox::sorry(0,i18n("Please specify a valid start time."));
00400       return false;
00401     }
00402 
00403     if (!mEndTimeEdit->inputIsValid()) {
00404       KMessageBox::sorry(0,i18n("Please specify a valid end time."));
00405       return false;
00406     }
00407   }
00408 
00409   if (!mStartDateEdit->inputIsValid()) {
00410     KMessageBox::sorry(0,i18n("Please specify a valid start date."));
00411     return false;
00412   }
00413 
00414   if (!mEndDateEdit->inputIsValid()) {
00415     KMessageBox::sorry(0,i18n("Please specify a valid end date."));
00416     return false;
00417   }
00418 
00419   QDateTime startDt,endDt;
00420   startDt.setDate(mStartDateEdit->date());
00421   endDt.setDate(mEndDateEdit->date());
00422   if (!mNoTimeButton->isChecked()) {
00423     startDt.setTime(mStartTimeEdit->getTime());
00424     endDt.setTime(mEndTimeEdit->getTime());
00425   }
00426 
00427   if (startDt > endDt) {
00428     KMessageBox::sorry(0,i18n("The event ends before it starts.\n"
00429                                  "Please correct dates and times."));
00430     return false;
00431   }
00432 
00433   return KOEditorGeneral::validateInput();
00434 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:31 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001