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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <qlabel.h>
00032 #include <qcheckbox.h>
00033 #include <qcombobox.h>
00034 #include <qpushbutton.h>
00035 
00036 #include <kdebug.h>
00037 #include <kglobal.h>
00038 #include <klocale.h>
00039 #include <kiconloader.h>
00040 #include <kmessagebox.h>
00041 #include <kfiledialog.h>
00042 #include <kstandarddirs.h>
00043 #include <krestrictedline.h>
00044 #include <ktextedit.h>
00045 
00046 #include <libkcal/event.h>
00047 
00048 #include "ktimeedit.h"
00049 #include <libkdepim/kdateedit.h>
00050 
00051 #include "koprefs.h"
00052 
00053 #include "koeditorgeneralevent.h"
00054 #include "koeditorgeneralevent.moc"
00055 
00056 KOEditorGeneralEvent::KOEditorGeneralEvent(QObject* parent,
00057                                            const char* name) :
00058   KOEditorGeneral( parent, name)
00059 {
00060   connect(this,SIGNAL(dateTimesChanged(QDateTime,QDateTime)),
00061           SLOT(setDuration()));
00062   connect(this,SIGNAL(dateTimesChanged(QDateTime,QDateTime)),
00063           SLOT(emitDateTimeStr()));
00064 }
00065 
00066 KOEditorGeneralEvent::~KOEditorGeneralEvent()
00067 {
00068 }
00069 
00070 void KOEditorGeneralEvent::finishSetup()
00071 {
00072   QWidget::setTabOrder( mSummaryEdit, mLocationEdit );
00073   QWidget::setTabOrder( mLocationEdit, mStartDateEdit );
00074   QWidget::setTabOrder( mStartDateEdit, mStartTimeEdit );
00075   QWidget::setTabOrder( mStartTimeEdit, mEndDateEdit );
00076   QWidget::setTabOrder( mEndDateEdit, mEndTimeEdit );
00077   QWidget::setTabOrder( mEndTimeEdit, mNoTimeButton );
00078   QWidget::setTabOrder( mNoTimeButton, mAlarmButton );
00079   QWidget::setTabOrder( mAlarmButton, mAlarmTimeEdit );
00080   QWidget::setTabOrder( mAlarmTimeEdit, mAlarmIncrCombo );
00081   QWidget::setTabOrder( mAlarmIncrCombo, mAlarmSoundButton );
00082   QWidget::setTabOrder( mAlarmSoundButton, mAlarmProgramButton );
00083   QWidget::setTabOrder( mAlarmProgramButton, mFreeTimeCombo );
00084   QWidget::setTabOrder( mFreeTimeCombo, mDescriptionEdit );
00085   QWidget::setTabOrder( mDescriptionEdit, mCategoriesButton );
00086   QWidget::setTabOrder( mCategoriesButton, mSecrecyCombo );
00087 //  QWidget::setTabOrder( mSecrecyCombo, mDescriptionEdit );
00088 
00089   mSummaryEdit->setFocus();
00090 }
00091 
00092 void KOEditorGeneralEvent::initTime(QWidget *parent,QBoxLayout *topLayout)
00093 {
00094   QBoxLayout *timeLayout = new QVBoxLayout(topLayout);
00095 
00096   QGroupBox *timeGroupBox = new QGroupBox(1,QGroupBox::Horizontal,
00097                                           i18n("Date && Time"),parent);
00098   timeLayout->addWidget(timeGroupBox);
00099 
00100   QFrame *timeBoxFrame = new QFrame(timeGroupBox);
00101 
00102   QGridLayout *layoutTimeBox = new QGridLayout(timeBoxFrame,2,3);
00103   layoutTimeBox->setSpacing(topLayout->spacing());
00104 
00105 
00106   mStartDateLabel = new QLabel(i18n("&Start:"),timeBoxFrame);
00107   layoutTimeBox->addWidget(mStartDateLabel,0,0);
00108 
00109   mStartDateEdit = new KDateEdit(timeBoxFrame);
00110   layoutTimeBox->addWidget(mStartDateEdit,0,1);
00111   mStartDateLabel->setBuddy( mStartDateEdit );
00112 
00113   mStartTimeEdit = new KTimeEdit(timeBoxFrame);
00114   layoutTimeBox->addWidget(mStartTimeEdit,0,2);
00115 
00116 
00117   mEndDateLabel = new QLabel(i18n("&End:"),timeBoxFrame);
00118   layoutTimeBox->addWidget(mEndDateLabel,1,0);
00119 
00120   mEndDateEdit = new KDateEdit(timeBoxFrame);
00121   layoutTimeBox->addWidget(mEndDateEdit,1,1);
00122   mEndDateLabel->setBuddy( mEndDateEdit );
00123 
00124   mEndTimeEdit = new KTimeEdit(timeBoxFrame);
00125   layoutTimeBox->addWidget(mEndTimeEdit,1,2);
00126 
00127   QHBoxLayout *flagsBox = new QHBoxLayout( timeBoxFrame );
00128 
00129   mNoTimeButton = new QCheckBox(i18n("&No time associated"),timeBoxFrame);
00130   flagsBox->addWidget(mNoTimeButton);
00131   connect(mNoTimeButton, SIGNAL(toggled(bool)),SLOT(dontAssociateTime(bool)));
00132 
00133   mDurationLabel = new QLabel( timeBoxFrame );
00134   if ( KOPrefs::instance()->mCompactDialogs ) {
00135     layoutTimeBox->addMultiCellWidget( mDurationLabel, 3, 3, 0, 3 );
00136   } else {
00137     flagsBox->addWidget( mDurationLabel, 0, 2 );
00138   }
00139 
00140   layoutTimeBox->addMultiCellLayout( flagsBox, 2, 2, 0, 3 );
00141 
00142   // time widgets are checked if they contain a valid time
00143   connect(mStartTimeEdit, SIGNAL(timeChanged(QTime)),
00144           this, SLOT(startTimeChanged(QTime)));
00145   connect(mEndTimeEdit, SIGNAL(timeChanged(QTime)),
00146           this, SLOT(endTimeChanged(QTime)));
00147 
00148   // date widgets are checked if they contain a valid date
00149   connect(mStartDateEdit, SIGNAL(dateChanged(QDate)),
00150           this, SLOT(startDateChanged(QDate)));
00151   connect(mEndDateEdit, SIGNAL(dateChanged(QDate)),
00152           this, SLOT(endDateChanged(QDate)));
00153 }
00154 
00155 void KOEditorGeneralEvent::initClass(QWidget *parent,QBoxLayout *topLayout)
00156 {
00157   QBoxLayout *classLayout = new QHBoxLayout(topLayout);
00158 
00159   QLabel *freeTimeLabel = new QLabel(i18n("S&how time as:"),parent);
00160   classLayout->addWidget(freeTimeLabel);
00161 
00162   mFreeTimeCombo = new QComboBox(false, parent);
00163   mFreeTimeCombo->insertItem(i18n("Busy"));
00164   mFreeTimeCombo->insertItem(i18n("Free"));
00165   classLayout->addWidget(mFreeTimeCombo);
00166   freeTimeLabel->setBuddy( mFreeTimeCombo );
00167 }
00168 
00169 void KOEditorGeneralEvent::timeStuffDisable(bool disable)
00170 {
00171   mStartTimeEdit->setEnabled( !disable );
00172   mEndTimeEdit->setEnabled( !disable );
00173 
00174   setDuration();
00175   emitDateTimeStr();
00176 }
00177 
00178 void KOEditorGeneralEvent::dontAssociateTime(bool noTime)
00179 {
00180   timeStuffDisable(noTime);
00181   //if(alarmButton->isChecked()) alarmStuffDisable(noTime);
00182   allDayChanged(noTime);
00183 }
00184 
00185 void KOEditorGeneralEvent::setDateTimes(QDateTime start, QDateTime end)
00186 {
00187 //  kdDebug(5850) << "KOEditorGeneralEvent::setDateTimes(): Start DateTime: " << start.toString() << endl;
00188 
00189   mStartDateEdit->setDate(start.date());
00190   // KTimeEdit seems to emit some signals when setTime() is called.
00191   mStartTimeEdit->blockSignals( true );
00192   mStartTimeEdit->setTime(start.time());
00193   mStartTimeEdit->blockSignals( false );
00194   mEndDateEdit->setDate(end.date());
00195   mEndTimeEdit->setTime(end.time());
00196 
00197   mCurrStartDateTime = start;
00198   mCurrEndDateTime = end;
00199 
00200   setDuration();
00201   emitDateTimeStr();
00202 }
00203 
00204 void KOEditorGeneralEvent::startTimeChanged(QTime newtime)
00205 {
00206   kdDebug(5850) << "KOEditorGeneralEvent::startTimeChanged() " << newtime.toString() << endl;
00207 
00208   int secsep = mCurrStartDateTime.secsTo(mCurrEndDateTime);
00209 
00210   mCurrStartDateTime.setTime(newtime);
00211 
00212   // adjust end time so that the event has the same duration as before.
00213   mCurrEndDateTime = mCurrStartDateTime.addSecs(secsep);
00214   mEndTimeEdit->setTime(mCurrEndDateTime.time());
00215   mEndDateEdit->setDate(mCurrEndDateTime.date());
00216 
00217   emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
00218 }
00219 
00220 void KOEditorGeneralEvent::endTimeChanged(QTime newtime)
00221 {
00222 //  kdDebug(5850) << "KOEditorGeneralEvent::endTimeChanged " << newtime.toString() << endl;
00223 
00224   QDateTime newdt(mCurrEndDateTime.date(), newtime);
00225   mCurrEndDateTime = newdt;
00226 
00227   emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
00228 }
00229 
00230 void KOEditorGeneralEvent::startDateChanged(QDate newdate)
00231 {
00232   int daysep = mCurrStartDateTime.daysTo(mCurrEndDateTime);
00233 
00234   mCurrStartDateTime.setDate(newdate);
00235 
00236   // adjust end date so that the event has the same duration as before
00237   mCurrEndDateTime.setDate(mCurrStartDateTime.date().addDays(daysep));
00238   mEndDateEdit->setDate(mCurrEndDateTime.date());
00239 
00240   emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
00241 }
00242 
00243 void KOEditorGeneralEvent::endDateChanged(QDate newdate)
00244 {
00245   QDateTime newdt(newdate, mCurrEndDateTime.time());
00246 
00247   if(newdt < mCurrStartDateTime) {
00248     // oops, we can't let that happen.
00249     newdt = mCurrStartDateTime;
00250     mEndDateEdit->setDate(newdt.date());
00251     mEndTimeEdit->setTime(newdt.time());
00252   }
00253   mCurrEndDateTime = newdt;
00254 
00255   emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
00256 }
00257 
00258 void KOEditorGeneralEvent::setDefaults(QDateTime from,QDateTime to,bool allDay)
00259 {
00260   KOEditorGeneral::setDefaults(allDay);
00261 
00262   mNoTimeButton->setChecked(allDay);
00263   timeStuffDisable(allDay);
00264 
00265   setDateTimes(from,to);
00266 }
00267 
00268 void KOEditorGeneralEvent::readEvent( Event *event, bool tmpl )
00269 {
00270   QString tmpStr;
00271 
00272   mNoTimeButton->setChecked(event->doesFloat());
00273   timeStuffDisable(event->doesFloat());
00274 
00275   if ( !tmpl ) {
00276     // the rest is for the events only
00277     setDateTimes(event->dtStart(),event->dtEnd());
00278   }
00279 
00280   switch( event->transparency() ) {
00281   case Event::Transparent:
00282     mFreeTimeCombo->setCurrentItem(1);
00283     break;
00284   case Event::Opaque:
00285     mFreeTimeCombo->setCurrentItem(0);
00286     break;
00287   }
00288 
00289   readIncidence(event);
00290 }
00291 
00292 void KOEditorGeneralEvent::writeEvent(Event *event)
00293 {
00294 //  kdDebug(5850) << "KOEditorGeneralEvent::writeEvent()" << endl;
00295 
00296   writeIncidence(event);
00297 
00298   QDate tmpDate;
00299   QTime tmpTime;
00300   QDateTime tmpDT;
00301 
00302   // temp. until something better happens.
00303   QString tmpStr;
00304 
00305   if (mNoTimeButton->isChecked()) {
00306     event->setFloats(true);
00307     // need to change this.
00308     tmpDate = mStartDateEdit->date();
00309     tmpTime.setHMS(0,0,0);
00310     tmpDT.setDate(tmpDate);
00311     tmpDT.setTime(tmpTime);
00312     event->setDtStart(tmpDT);
00313 
00314     tmpDate = mEndDateEdit->date();
00315     tmpTime.setHMS(0,0,0);
00316     tmpDT.setDate(tmpDate);
00317     tmpDT.setTime(tmpTime);
00318     event->setDtEnd(tmpDT);
00319   } else {
00320     event->setFloats(false);
00321 
00322     // set date/time end
00323     tmpDate = mEndDateEdit->date();
00324     tmpTime = mEndTimeEdit->getTime();
00325     tmpDT.setDate(tmpDate);
00326     tmpDT.setTime(tmpTime);
00327     event->setDtEnd(tmpDT);
00328 
00329     // set date/time start
00330     tmpDate = mStartDateEdit->date();
00331     tmpTime = mStartTimeEdit->getTime();
00332     tmpDT.setDate(tmpDate);
00333     tmpDT.setTime(tmpTime);
00334     event->setDtStart(tmpDT);
00335   } // check for float
00336 
00337   event->setTransparency(mFreeTimeCombo->currentItem() > 0
00338                          ? KCal::Event::Transparent
00339                          : KCal::Event::Opaque);
00340 
00341 //  kdDebug(5850) << "KOEditorGeneralEvent::writeEvent() done" << endl;
00342 }
00343 
00344 void KOEditorGeneralEvent::setDuration()
00345 {
00346   QString tmpStr, catStr;
00347   int hourdiff, minutediff;
00348   // end<date is an accepted temporary state while typing, but don't show
00349   // any duration if this happens
00350   if(mCurrEndDateTime >= mCurrStartDateTime) {
00351 
00352     if (mNoTimeButton->isChecked()) {
00353       int daydiff = mCurrStartDateTime.date().daysTo(mCurrEndDateTime.date()) + 1;
00354       tmpStr = i18n("Duration: ");
00355       tmpStr.append(i18n("1 Day","%n Days",daydiff));
00356     } else {
00357       hourdiff = mCurrStartDateTime.date().daysTo(mCurrEndDateTime.date()) * 24;
00358       hourdiff += mCurrEndDateTime.time().hour() -
00359                   mCurrStartDateTime.time().hour();
00360       minutediff = mCurrEndDateTime.time().minute() -
00361                    mCurrStartDateTime.time().minute();
00362       // If minutediff is negative, "borrow" 60 minutes from hourdiff
00363       if (minutediff < 0 && hourdiff > 0) {
00364         hourdiff -= 1;
00365         minutediff += 60;
00366       }
00367       if (hourdiff || minutediff){
00368         tmpStr = i18n("Duration: ");
00369         if (hourdiff){
00370           catStr = i18n("1 hour","%n hours",hourdiff);
00371           tmpStr.append(catStr);
00372         }
00373         if (hourdiff && minutediff){
00374           tmpStr += i18n(", ");
00375         }
00376         if (minutediff){
00377           catStr = i18n("1 minute","%n minutes",minutediff);
00378           tmpStr += catStr;
00379         }
00380       } else tmpStr = "";
00381     }
00382   }
00383   mDurationLabel->setText(tmpStr);
00384 }
00385 
00386 void KOEditorGeneralEvent::emitDateTimeStr()
00387 {
00388   KLocale *l = KGlobal::locale();
00389 
00390   QString from,to;
00391   if (mNoTimeButton->isChecked()) {
00392     from = l->formatDate(mCurrStartDateTime.date());
00393     to = l->formatDate(mCurrEndDateTime.date());
00394   } else {
00395     from = l->formatDateTime(mCurrStartDateTime);
00396     to = l->formatDateTime(mCurrEndDateTime);
00397   }
00398 
00399   QString str = i18n("From: %1   To: %2   %3").arg(from).arg(to)
00400                 .arg(mDurationLabel->text());
00401 
00402   emit dateTimeStrChanged(str);
00403 }
00404 
00405 bool KOEditorGeneralEvent::validateInput()
00406 {
00407 //  kdDebug(5850) << "KOEditorGeneralEvent::validateInput()" << endl;
00408 
00409   if (!mNoTimeButton->isChecked()) {
00410     if (!mStartTimeEdit->inputIsValid()) {
00411       KMessageBox::sorry( 0,
00412           i18n("Please specify a valid start time, for example '%1'.")
00413           .arg( KGlobal::locale()->formatTime( QTime::currentTime() ) ) );
00414       return false;
00415     }
00416 
00417     if (!mEndTimeEdit->inputIsValid()) {
00418       KMessageBox::sorry( 0,
00419           i18n("Please specify a valid end time, for example '%1'.")
00420           .arg( KGlobal::locale()->formatTime( QTime::currentTime() ) ) );
00421       return false;
00422     }
00423   }
00424 
00425   if (!mStartDateEdit->inputIsValid()) {
00426     KMessageBox::sorry( 0,
00427         i18n("Please specify a valid start date, for example '%1'.")
00428         .arg( KGlobal::locale()->formatDate( QDate::currentDate() ) ) );
00429     return false;
00430   }
00431 
00432   if (!mEndDateEdit->inputIsValid()) {
00433     KMessageBox::sorry( 0,
00434         i18n("Please specify a valid end date, for example '%1'.")
00435         .arg( KGlobal::locale()->formatDate( QDate::currentDate() ) ) );
00436     return false;
00437   }
00438 
00439   QDateTime startDt,endDt;
00440   startDt.setDate(mStartDateEdit->date());
00441   endDt.setDate(mEndDateEdit->date());
00442   if (!mNoTimeButton->isChecked()) {
00443     startDt.setTime(mStartTimeEdit->getTime());
00444     endDt.setTime(mEndTimeEdit->getTime());
00445   }
00446 
00447   if (startDt > endDt) {
00448     KMessageBox::sorry(0,i18n("The event ends before it starts.\n"
00449                                  "Please correct dates and times."));
00450     return false;
00451   }
00452 
00453   return KOEditorGeneral::validateInput();
00454 }
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 4 14:43:13 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003