summaryrefslogtreecommitdiffabout
path: root/korganizer/koeditorgeneralevent.cpp
Unidiff
Diffstat (limited to 'korganizer/koeditorgeneralevent.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--korganizer/koeditorgeneralevent.cpp443
1 files changed, 443 insertions, 0 deletions
diff --git a/korganizer/koeditorgeneralevent.cpp b/korganizer/koeditorgeneralevent.cpp
new file mode 100644
index 0000000..9b93e7e
--- a/dev/null
+++ b/korganizer/koeditorgeneralevent.cpp
@@ -0,0 +1,443 @@
1/*
2 This file is part of KOrganizer.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution.
22*/
23
24#include <qtooltip.h>
25#include <qlayout.h>
26#include <qvbox.h>
27#include <qbuttongroup.h>
28#include <qvgroupbox.h>
29#include <qwidgetstack.h>
30#include <qdatetime.h>
31
32#include <kdebug.h>
33#include <kglobal.h>
34#include <klocale.h>
35#include <kiconloader.h>
36#include <kmessagebox.h>
37#include <kfiledialog.h>
38#include <kstandarddirs.h>
39
40#include <libkcal/event.h>
41
42#include <libkdepim/kdateedit.h>
43
44#include "koprefs.h"
45
46#include "koeditorgeneralevent.h"
47#include "kolocationbox.h"
48#include "koeditorgeneralevent.moc"
49
50KOEditorGeneralEvent::KOEditorGeneralEvent(QObject* parent,
51 const char* name) :
52 KOEditorGeneral( parent, name)
53{
54 connect(this,SIGNAL(dateTimesChanged(QDateTime,QDateTime)),
55 SLOT(setDuration()));
56 connect(this,SIGNAL(dateTimesChanged(QDateTime,QDateTime)),
57 SLOT(emitDateTimeStr()));
58}
59
60KOEditorGeneralEvent::~KOEditorGeneralEvent()
61{
62}
63
64void KOEditorGeneralEvent::finishSetup()
65{
66
67 //disabled
68// QWidget::setTabOrder( mSummaryEdit, mLocationEdit );
69// QWidget::setTabOrder( mLocationEdit, mStartDateEdit );
70// QWidget::setTabOrder( mStartDateEdit, mStartTimeEdit );
71// QWidget::setTabOrder( mStartTimeEdit, mEndDateEdit );
72// QWidget::setTabOrder( mEndDateEdit, mEndTimeEdit );
73// QWidget::setTabOrder( mEndTimeEdit, mNoTimeButton );
74// QWidget::setTabOrder( mNoTimeButton, mAlarmButton );
75// QWidget::setTabOrder( mAlarmButton, mAlarmTimeEdit );
76// QWidget::setTabOrder( mFreeTimeCombo, mCategoriesButton );
77// QWidget::setTabOrder( mCategoriesButton, mSecrecyCombo );
78// QWidget::setTabOrder( mSecrecyCombo, mDescriptionEdit );
79
80
81
82 mSummaryEdit->load(KOLocationBox::SUMMARYEVENT);
83 mSummaryEdit->setFocus();
84}
85
86void KOEditorGeneralEvent::initTime(QWidget *parent,QBoxLayout *topLayout)
87{
88 QBoxLayout *timeLayout = new QVBoxLayout(topLayout);
89
90 QGroupBox *timeGroupBox = new QGroupBox(1,QGroupBox::Horizontal,
91 i18n("Date && Time"),parent);
92 timeLayout->addWidget(timeGroupBox);
93
94 timeGroupBox->layout()->setSpacing( 0 );
95 timeGroupBox->layout()->setMargin( 5 );
96 QFrame *timeBoxFrame = new QFrame(timeGroupBox);
97
98 QGridLayout *layoutTimeBox = new QGridLayout(timeBoxFrame,2,3);
99 layoutTimeBox->setSpacing(topLayout->spacing());
100
101 mStartDateLabel = new QLabel(i18n("Start:"),timeBoxFrame);
102 layoutTimeBox->addWidget(mStartDateLabel,0,0);
103
104 mStartDateEdit = new KDateEdit(timeBoxFrame);
105 layoutTimeBox->addWidget(mStartDateEdit,0,1);
106
107 mStartTimeEdit = new KOTimeEdit(timeBoxFrame);
108 layoutTimeBox->addWidget(mStartTimeEdit,0,2);
109
110
111 mEndDateLabel = new QLabel(i18n("End:"),timeBoxFrame);
112 layoutTimeBox->addWidget(mEndDateLabel,1,0);
113
114 mEndDateEdit = new KDateEdit(timeBoxFrame);
115 layoutTimeBox->addWidget(mEndDateEdit,1,1);
116
117 mEndTimeEdit = new KOTimeEdit(timeBoxFrame);
118 layoutTimeBox->addWidget(mEndTimeEdit,1,2);
119 QWidget* duration = new QWidget( timeBoxFrame );
120 QHBoxLayout *flagsBox = new QHBoxLayout( duration );
121 mNoTimeButton = new QCheckBox(i18n("Allday"),duration);
122 flagsBox->addWidget(mNoTimeButton);
123 connect(mNoTimeButton, SIGNAL(toggled(bool)),SLOT(dontAssociateTime(bool)));
124 mDurationLabel = new QLabel( duration );
125 // if ( KOPrefs::instance()->mCompactDialogs ) {
126 //layoutTimeBox->addMultiCellWidget( mDurationLabel, 3, 3, 0, 3 );
127 //} else {
128 flagsBox->addWidget( mDurationLabel );
129 //}
130 flagsBox->setStretchFactor(mDurationLabel, 10 );
131 mDurationLabel->setAlignment( AlignRight | AlignVCenter);
132 layoutTimeBox->addMultiCellWidget( duration, 2, 2, 0, 3 );
133
134 // time widgets are checked if they contain a valid time
135 connect(mStartTimeEdit, SIGNAL(timeChanged(QTime)),
136 this, SLOT(startTimeChanged(QTime)));
137 connect(mEndTimeEdit, SIGNAL(timeChanged(QTime)),
138 this, SLOT(endTimeChanged(QTime)));
139
140 // date widgets are checked if they contain a valid date
141 connect(mStartDateEdit, SIGNAL(dateChanged(QDate)),
142 this, SLOT(startDateChanged(QDate)));
143 connect(mEndDateEdit, SIGNAL(dateChanged(QDate)),
144 this, SLOT(endDateChanged(QDate)));
145}
146
147void KOEditorGeneralEvent::initClass(QWidget *parent,QBoxLayout *topLayout)
148{
149 QBoxLayout *classLayout = new QHBoxLayout(topLayout);
150
151 QLabel *freeTimeLabel = new QLabel(i18n("Show time as:"),parent);
152 classLayout->addWidget(freeTimeLabel);
153
154 mFreeTimeCombo = new QComboBox(false, parent);
155 mFreeTimeCombo->insertItem(i18n("Busy"));
156 mFreeTimeCombo->insertItem(i18n("Free"));
157 classLayout->addWidget(mFreeTimeCombo);
158}
159
160void KOEditorGeneralEvent::timeStuffDisable(bool disable)
161{
162 mStartTimeEdit->setEnabled( !disable );
163 mEndTimeEdit->setEnabled( !disable );
164
165 setDuration();
166 emitDateTimeStr();
167}
168
169void KOEditorGeneralEvent::dontAssociateTime(bool noTime)
170{
171 timeStuffDisable(noTime);
172 //if(alarmButton->isChecked()) alarmStuffDisable(noTime);
173 allDayChanged(noTime);
174}
175
176void KOEditorGeneralEvent::setDateTimes(QDateTime start, QDateTime end)
177{
178// kdDebug() << "KOEditorGeneralEvent::setDateTimes(): Start DateTime: " << start.toString() << endl;
179
180 mStartDateEdit->setDate(start.date());
181 // KTimeEdit seems to emit some signals when setTime() is called.
182 mStartTimeEdit->blockSignals( true );
183 mStartTimeEdit->setTime(start.time());
184 mStartTimeEdit->blockSignals( false );
185 mEndDateEdit->setDate(end.date());
186 mEndTimeEdit->setTime(end.time());
187
188 mCurrStartDateTime = start;
189 mCurrEndDateTime = end;
190
191 setDuration();
192 emitDateTimeStr();
193}
194
195void KOEditorGeneralEvent::startTimeChanged(QTime newtime)
196{
197 kdDebug() << "KOEditorGeneralEvent::startTimeChanged() " << newtime.toString() << endl;
198
199 int secsep = mCurrStartDateTime.secsTo(mCurrEndDateTime);
200
201 mCurrStartDateTime.setTime(newtime);
202
203 // adjust end time so that the event has the same duration as before.
204 mCurrEndDateTime = mCurrStartDateTime.addSecs(secsep);
205 mEndTimeEdit->setTime(mCurrEndDateTime.time());
206 mEndDateEdit->setDate(mCurrEndDateTime.date());
207
208 emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
209}
210
211void KOEditorGeneralEvent::endTimeChanged(QTime newtime)
212{
213// kdDebug() << "KOEditorGeneralEvent::endTimeChanged " << newtime.toString() << endl;
214
215 QDateTime newdt(mCurrEndDateTime.date(), newtime);
216 mCurrEndDateTime = newdt;
217
218 emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
219}
220
221void KOEditorGeneralEvent::startDateChanged(QDate newdate)
222{
223 int daysep = mCurrStartDateTime.daysTo(mCurrEndDateTime);
224
225 mCurrStartDateTime.setDate(newdate);
226
227 // adjust end date so that the event has the same duration as before
228 mCurrEndDateTime.setDate(mCurrStartDateTime.date().addDays(daysep));
229 mEndDateEdit->setDate(mCurrEndDateTime.date());
230
231 emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
232}
233
234void KOEditorGeneralEvent::endDateChanged(QDate newdate)
235{
236 QDateTime newdt(newdate, mCurrEndDateTime.time());
237
238 if(newdt < mCurrStartDateTime) {
239 // oops, we can't let that happen.
240 newdt = mCurrStartDateTime;
241 mEndDateEdit->setDate(newdt.date());
242 mEndTimeEdit->setTime(newdt.time());
243 }
244 mCurrEndDateTime = newdt;
245
246 emit dateTimesChanged(mCurrStartDateTime,mCurrEndDateTime);
247}
248
249void KOEditorGeneralEvent::setDefaults(QDateTime from,QDateTime to,bool allDay)
250{
251 mSummaryEdit->load(KOLocationBox::SUMMARYEVENT);
252 mLocationEdit->load(KOLocationBox::LOCATION);
253 KOEditorGeneral::setDefaults(allDay);
254
255 mNoTimeButton->setChecked(allDay);
256 timeStuffDisable(allDay);
257 mFreeTimeCombo->setCurrentItem( 0 );
258 setDateTimes(from,to);
259}
260
261void KOEditorGeneralEvent::readEvent( Event *event, bool tmpl )
262{
263 QString tmpStr;
264
265 if ( !tmpl ) {
266 // the rest is for the events only
267 mNoTimeButton->setChecked(event->doesFloat());
268 timeStuffDisable(event->doesFloat());
269
270 setDateTimes(event->dtStart(),event->dtEnd());
271 }
272
273 switch( event->transparency() ) {
274 case Event::Transparent:
275 mFreeTimeCombo->setCurrentItem(1);
276 break;
277 case Event::Opaque:
278 mFreeTimeCombo->setCurrentItem(0);
279 break;
280 }
281
282 mSummaryEdit->load(KOLocationBox::SUMMARYEVENT);
283 mLocationEdit->load(KOLocationBox::LOCATION);
284 readIncidence(event);
285}
286
287void KOEditorGeneralEvent::writeEvent(Event *event)
288{
289// kdDebug() << "KOEditorGeneralEvent::writeEvent()" << endl;
290
291 writeIncidence(event);
292
293 QDate tmpDate;
294 QTime tmpTime;
295 QDateTime tmpDT;
296
297 // temp. until something better happens.
298 QString tmpStr;
299
300 if (mNoTimeButton->isChecked()) {
301 event->setFloats(true);
302 // need to change this.
303 tmpDate = mStartDateEdit->date();
304 tmpTime.setHMS(0,0,0);
305 tmpDT.setDate(tmpDate);
306 tmpDT.setTime(tmpTime);
307 event->setDtStart(tmpDT);
308
309 tmpDate = mEndDateEdit->date();
310 tmpTime.setHMS(0,0,0);
311 tmpDT.setDate(tmpDate);
312 tmpDT.setTime(tmpTime);
313 event->setDtEnd(tmpDT);
314 } else {
315 event->setFloats(false);
316
317 // set date/time end
318 tmpDate = mEndDateEdit->date();
319 tmpTime = mEndTimeEdit->getTime();
320 tmpDT.setDate(tmpDate);
321 tmpDT.setTime(tmpTime);
322 event->setDtEnd(tmpDT);
323
324 // set date/time start
325 tmpDate = mStartDateEdit->date();
326 tmpTime = mStartTimeEdit->getTime();
327 tmpDT.setDate(tmpDate);
328 tmpDT.setTime(tmpTime);
329 event->setDtStart(tmpDT);
330 } // check for float
331 mSummaryEdit->save(KOLocationBox::SUMMARYEVENT);
332
333 event->setTransparency(mFreeTimeCombo->currentItem() > 0
334 ? KCal::Event::Transparent
335 : KCal::Event::Opaque);
336
337// kdDebug() << "KOEditorGeneralEvent::writeEvent() done" << endl;
338}
339
340void KOEditorGeneralEvent::setDuration()
341{
342 QString tmpStr = "", catStr;
343 int hourdiff, minutediff;
344 // end<date is an accepted temporary state while typing, but don't show
345 // any duration if this happens
346 if(mCurrEndDateTime >= mCurrStartDateTime) {
347
348 if (mNoTimeButton->isChecked()) {
349 int daydiff = mCurrStartDateTime.date().daysTo(mCurrEndDateTime.date()) + 1;
350 tmpStr = i18n("Duration: ");
351 tmpStr.append(i18n("1 Day","%n Days",daydiff));
352 } else {
353 int secto = mCurrStartDateTime.secsTo( mCurrEndDateTime );
354 hourdiff = secto / 3600;
355 minutediff = (secto/60 ) % 60;
356 if (hourdiff || minutediff){
357 tmpStr = i18n("Duration: ");
358 if (hourdiff){
359 catStr = i18n("1 h","%n h",hourdiff);
360 tmpStr.append(catStr);
361 }
362 if (hourdiff && minutediff){
363 tmpStr += i18n(", ");
364 }
365 if (minutediff){
366 catStr = i18n("1 min","%n min",minutediff);
367 tmpStr += catStr;
368 }
369 } else tmpStr = "";
370 }
371 }
372 mDurationLabel->setText(tmpStr);
373}
374
375void KOEditorGeneralEvent::emitDateTimeStr()
376{
377 KLocale *l = KGlobal::locale();
378
379 QString from,to;
380 if (mNoTimeButton->isChecked()) {
381 from = l->formatDate(mCurrStartDateTime.date());
382 to = l->formatDate(mCurrEndDateTime.date());
383 } else {
384 from = l->formatDateTime(mCurrStartDateTime);
385 to = l->formatDateTime(mCurrEndDateTime);
386 }
387
388 QString str = i18n("From: %1 To: %2 %3").arg(from).arg(to)
389 .arg(mDurationLabel->text());
390
391 emit dateTimeStrChanged(str);
392}
393
394bool KOEditorGeneralEvent::validateInput()
395{
396// kdDebug() << "KOEditorGeneralEvent::validateInput()" << endl;
397
398 if (!mNoTimeButton->isChecked()) {
399 if (!mStartTimeEdit->inputIsValid()) {
400 KMessageBox::sorry( 0,
401 i18n("Please specify a valid start time, for example '%1'.")
402 .arg( KGlobal::locale()->formatTime( QTime::currentTime() ) ) );
403 return false;
404 }
405
406 if (!mEndTimeEdit->inputIsValid()) {
407 KMessageBox::sorry( 0,
408 i18n("Please specify a valid end time, for example '%1'.")
409 .arg( KGlobal::locale()->formatTime( QTime::currentTime() ) ) );
410 return false;
411 }
412 }
413
414 if (!mStartDateEdit->inputIsValid()) {
415 KMessageBox::sorry( 0,
416 i18n("Please specify a valid start date, for example '%1'.")
417 .arg( KGlobal::locale()->formatDate( QDate::currentDate() ) ) );
418 return false;
419 }
420
421 if (!mEndDateEdit->inputIsValid()) {
422 KMessageBox::sorry( 0,
423 i18n("Please specify a valid end date, for example '%1'.")
424 .arg( KGlobal::locale()->formatDate( QDate::currentDate() ) ) );
425 return false;
426 }
427
428 QDateTime startDt,endDt;
429 startDt.setDate(mStartDateEdit->date());
430 endDt.setDate(mEndDateEdit->date());
431 if (!mNoTimeButton->isChecked()) {
432 startDt.setTime(mStartTimeEdit->getTime());
433 endDt.setTime(mEndTimeEdit->getTime());
434 }
435
436 if (startDt > endDt) {
437 KMessageBox::sorry(0,i18n("The event ends before it starts.\n"
438 "Please correct dates and times."));
439 return false;
440 }
441
442 return KOEditorGeneral::validateInput();
443}