00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <math.h>
00025
00026 #include <qpainter.h>
00027 #include <qbuttongroup.h>
00028 #include <qpushbutton.h>
00029 #include <qradiobutton.h>
00030 #include <qlayout.h>
00031 #include <qrect.h>
00032
00033 #include <kglobal.h>
00034 #include <klocale.h>
00035 #include <kstandarddirs.h>
00036 #include <kmessagebox.h>
00037 #include <ktempfile.h>
00038 #include <kdebug.h>
00039
00040
00041 #include <libkcal/todo.h>
00042
00043 #include "koprefsdialog.h"
00044 #include "koprefs.h"
00045 #ifndef KORG_NOPLUGINS
00046 #include "kocore.h"
00047 #endif
00048 #include <libkdepim/kdateedit.h>
00049
00050 #include "calprinter.h"
00051 #include "calprinter.moc"
00052
00053 CalPrinter::CalPrinter(QWidget *parent, Calendar *calendar)
00054 : QObject(0L, "CalPrinter")
00055 {
00056 mCalendar = calendar;
00057 mParent = parent;
00058 mPrinter = new KPrinter;
00059 mPrintDialog = new CalPrintDialog(mPrinter,parent);
00060 mPrinter->setOrientation(KPrinter::Landscape);
00061
00062 updateConfig();
00063 }
00064
00065 CalPrinter::~CalPrinter()
00066 {
00067 delete mPrinter;
00068 }
00069
00070 void CalPrinter::setupPrinter()
00071 {
00072 KOPrefsDialog *optionsDlg = new KOPrefsDialog(mParent);
00073 optionsDlg->readConfig();
00074 optionsDlg->showPrinterTab();
00075 connect(optionsDlg, SIGNAL(configChanged()),
00076 mParent, SLOT(updateConfig()));
00077
00078
00079 optionsDlg->show();
00080 }
00081
00082 void CalPrinter::preview(PrintType pt, const QDate &fd, const QDate &td)
00083 {
00084 mPrintDialog->setPreview(true);
00085 mPrintDialog->setRange(fd,td);
00086
00087 switch(pt) {
00088 case Day:
00089 mPrintDialog->setPrintDay();
00090 break;
00091 case Week:
00092 mPrintDialog->setPrintWeek();
00093 break;
00094 case Month:
00095 mPrintDialog->setPrintMonth();
00096 break;
00097 case Todolist:
00098 mPrintDialog->setPrintTodo();
00099 break;
00100 case TimeTable:
00101 mPrintDialog->setPrintTimeTable();
00102 break;
00103 }
00104
00105 if (mPrintDialog->exec() == QDialog::Accepted) {
00106 doPreview(mPrintDialog->printType(),mPrintDialog->fromDate(),
00107 mPrintDialog->toDate());
00108 }
00109 }
00110
00111 void CalPrinter::doPreview(int pt, QDate fd, QDate td)
00112 {
00113 mPrinter->setPreviewOnly(true);
00114
00115 switch(pt) {
00116 case Day:
00117 printDay(fd, td);
00118 break;
00119 case Week:
00120 printWeek(fd, td);
00121 break;
00122 case Month:
00123 printMonth(fd, td);
00124 break;
00125 case Todolist:
00126 printTodo(fd, td);
00127 break;
00128 case TimeTable:
00129 printTimeTable(fd, td);
00130 break;
00131 }
00132
00133
00134 mPrinter->setPreviewOnly(false);
00135 }
00136
00137 void CalPrinter::print(PrintType pt, const QDate &fd, const QDate &td)
00138 {
00139 mPrintDialog->setPreview(false);
00140 mPrintDialog->setRange(fd,td);
00141
00142 switch(pt) {
00143 case Day:
00144 mPrintDialog->setPrintDay();
00145 break;
00146 case Week:
00147 mPrintDialog->setPrintWeek();
00148 break;
00149 case Month:
00150 mPrintDialog->setPrintMonth();
00151 break;
00152 case Todolist:
00153 mPrintDialog->setPrintTodo();
00154 break;
00155 case TimeTable:
00156 mPrintDialog->setPrintTimeTable();
00157 break;
00158 }
00159
00160 if (mPrintDialog->exec() == QDialog::Accepted) {
00161 doPrint(mPrintDialog->printType(),mPrintDialog->fromDate(),
00162 mPrintDialog->toDate());
00163 }
00164 }
00165
00166 void CalPrinter::doPrint(int pt, QDate fd, QDate td)
00167 {
00168 if (!mPrinter->setup(mParent)) return;
00169
00170 switch(pt) {
00171 case Day:
00172 printDay(fd, td);
00173 break;
00174 case Week:
00175 printWeek(fd, td);
00176 break;
00177 case Month:
00178 printMonth(fd, td);
00179 break;
00180 case Todolist:
00181 printTodo(fd, td);
00182 break;
00183 case TimeTable:
00184 printTimeTable(fd, td);
00185 break;
00186 }
00187 }
00188
00190
00191 void CalPrinter::updateConfig()
00192 {
00193 mStartHour = KOPrefs::instance()->mDayBegins;
00194 }
00195
00196 void CalPrinter::printDay(const QDate &fd, const QDate &td)
00197 {
00198 QPainter p;
00199 QDate curDay, fromDay, toDay;
00200
00201 mPrinter->setOrientation(KPrinter::Portrait);
00202
00203 fromDay = fd;
00204 curDay = fd;
00205 toDay = td;
00206
00207 p.begin(mPrinter);
00208
00209
00210 int margin = 36;
00211 p.setViewport(margin, margin,
00212 p.viewport().width()-margin,
00213 p.viewport().height()-margin);
00214 int pageWidth = p.viewport().width();
00215 int pageHeight = p.viewport().height();
00216 mHeaderHeight = 72;
00217 mSubHeaderHeight = 20;
00218
00219 do {
00220 drawHeader(p, curDay,toDay,curDay,
00221 pageWidth, mHeaderHeight, Day);
00222 drawDay(p, curDay, pageWidth, pageHeight);
00223 curDay = curDay.addDays(1);
00224 if (curDay <= toDay)
00225 mPrinter->newPage();
00226 } while (curDay <= toDay);
00227
00228 p.end();
00229 }
00230
00231 void CalPrinter::printWeek(const QDate &fd, const QDate &td)
00232 {
00233 QPainter p;
00234 QDate curWeek, fromWeek, toWeek;
00235
00236 mPrinter->setOrientation(KPrinter::Portrait);
00237
00238 if (KGlobal::locale()->weekStartsMonday()) {
00239
00240 fromWeek = fd.addDays(-(fd.dayOfWeek()-1));
00241
00242 toWeek = td.addDays(7-fd.dayOfWeek());
00243 } else {
00244
00245 fromWeek = fd.addDays(-(fd.dayOfWeek()%7));
00246
00247 toWeek = td.addDays(6-td.dayOfWeek());
00248 }
00249
00250 p.begin(mPrinter);
00251
00252
00253 int margin = 36;
00254 p.setViewport(margin, margin,
00255 p.viewport().width()-margin,
00256 p.viewport().height()-margin);
00257 int pageWidth = p.viewport().width();
00258 int pageHeight = p.viewport().height();
00259 mHeaderHeight = 72;
00260 mSubHeaderHeight = 20;
00261
00262 curWeek = fromWeek.addDays(6);
00263 do {
00264 drawHeader(p, curWeek.addDays(-6), curWeek,
00265 curWeek,
00266 pageWidth, mHeaderHeight, Week);
00267 drawWeek(p, curWeek, pageWidth, pageHeight);
00268 curWeek = curWeek.addDays(7);
00269 if (curWeek <= toWeek)
00270 mPrinter->newPage();
00271 } while (curWeek <= toWeek);
00272
00273 p.end();
00274 }
00275
00276 void CalPrinter::printMonth(const QDate &fd, const QDate &td)
00277 {
00278 QPainter p;
00279 QDate curMonth, fromMonth, toMonth;
00280
00281 mPrinter->setOrientation(KPrinter::Landscape);
00282
00283 fromMonth = fd.addDays(-(fd.day()-1));
00284 toMonth = td.addDays(td.daysInMonth()-td.day());
00285
00286 p.begin(mPrinter);
00287
00288
00289 int margin = 36;
00290 p.setViewport(margin, margin,
00291 p.viewport().width()-margin,
00292 p.viewport().height()-margin);
00293 int pageWidth = p.viewport().width();
00294 int pageHeight = p.viewport().height();
00295 mHeaderHeight = 72;
00296 mSubHeaderHeight = 20;
00297
00298 curMonth = fromMonth;
00299 do {
00300 drawHeader(p, fromMonth,
00301 toMonth, curMonth,
00302 pageWidth, mHeaderHeight, Month);
00303 drawDaysOfWeek(p, curMonth, pageWidth, pageHeight);
00304 drawMonth(p, curMonth, pageWidth, pageHeight);
00305 curMonth = curMonth.addDays(fromMonth.daysInMonth()+1);
00306 if (curMonth <= toMonth)
00307 mPrinter->newPage();
00308 } while (curMonth <= toMonth);
00309
00310 p.end();
00311 }
00312
00313 void CalPrinter::printTimeTable(const QDate &fd, const QDate &td)
00314 {
00315 QPainter p;
00316 QDate curWeek, fromWeek, toWeek;
00317
00318 mPrinter->setOrientation(KPrinter::Landscape);
00319
00320 if (KGlobal::locale()->weekStartsMonday()) {
00321
00322 fromWeek = fd.addDays(-(fd.dayOfWeek()-1));
00323
00324 toWeek = td.addDays(7-fd.dayOfWeek());
00325 } else {
00326
00327 fromWeek = fd.addDays(-(fd.dayOfWeek()%7));
00328
00329 toWeek = td.addDays(6-td.dayOfWeek());
00330 }
00331
00332 p.begin(mPrinter);
00333
00334
00335 int margin = 36;
00336 p.setViewport(margin, margin,
00337 p.viewport().width()-margin,
00338 p.viewport().height()-margin);
00339 int pageWidth = p.viewport().width();
00340 int pageHeight = p.viewport().height();
00341 mHeaderHeight = 36;
00342 mSubHeaderHeight = 20;
00343
00344 curWeek = fromWeek.addDays(6);
00345 do {
00346
00347
00348
00349 drawTimeTable(p, curWeek, pageWidth, pageHeight);
00350 curWeek = curWeek.addDays(7);
00351 if (curWeek <= toWeek)
00352 mPrinter->newPage();
00353 } while (curWeek <= toWeek);
00354
00355 p.end();
00356 }
00357
00358
00359 void CalPrinter::printTodo(const QDate &fd, const QDate &td)
00360 {
00361 QPainter p;
00362
00363
00364 mPrinter->setOrientation(KPrinter::Portrait);
00365
00366 p.begin(mPrinter);
00367 int pageWidth = p.viewport().width();
00368 int pageHeight = p.viewport().height();
00369 mHeaderHeight = pageHeight/7 - 20;
00370
00371 int pospriority = 10;
00372 int possummary = 60;
00373 int posdue = pageWidth - 85;
00374 int lineSpacing = 15;
00375 int fontHeight = 10;
00376
00377 drawHeader(p, fd, td, fd, pageWidth, mHeaderHeight, Todolist);
00378
00379 mCurrentLinePos = mHeaderHeight + 5;
00380 kdDebug() << "Header Height: " << mCurrentLinePos << endl;
00381
00382 QPtrList<Todo> todoList = mCalendar->todos();
00383 todoList.first();
00384 int count = 1;
00385 QString outStr;
00386
00387 p.setFont(QFont("helvetica", 10));
00388 lineSpacing = p.fontMetrics().lineSpacing();
00389
00390 p.setFont(QFont("helvetica", 10, QFont::Bold));
00391 outStr += i18n("Priority");
00392
00393 p.drawText(pospriority, mHeaderHeight - 2,
00394 outStr);
00395 outStr.truncate(0);
00396 outStr += i18n("Summary");
00397
00398 p.drawText(possummary, mHeaderHeight - 2,
00399 outStr);
00400 outStr.truncate(0);
00401 outStr += i18n("Due");
00402
00403 p.drawText(posdue, mHeaderHeight - 2,
00404 outStr);
00405 p.setFont(QFont("helvetica", 10));
00406
00407 fontHeight = p.fontMetrics().height();
00408 for(int cprior = 1; cprior <= 6; cprior++) {
00409 Todo *currEvent(todoList.first());
00410 while (currEvent != NULL) {
00411
00412
00413 if (currEvent->relatedTo()){
00414 currEvent = todoList.next();
00415 continue;
00416 }
00417
00418 QDate start = currEvent->dtStart().date();
00419
00420 if ( (!start.isValid()) && (start >= td) ) {
00421 currEvent = todoList.next();
00422 continue;
00423 }
00424
00425 int priority = currEvent->priority();
00426
00427 if ((priority != cprior) || ((cprior==6) && (priority==0))) {
00428 currEvent = todoList.next();
00429 continue;
00430 }
00431
00432 drawTodo(count,currEvent,p);
00433 currEvent = todoList.next();
00434 ++count;
00435 }
00436 }
00437 p.end();
00438 }
00439
00440 void CalPrinter::drawTodo(int count, Todo * item, QPainter &p,int level,QRect *r)
00441 {
00442 QString outStr;
00443 KLocale *local = KGlobal::locale();
00444 int pageWidth = p.viewport().width();
00445 int pospriority = 10;
00446 int possummary = 60;
00447 int posdue = pageWidth - 85;
00448 int fontHeight = 10;
00449 int priority=item->priority();
00450 QRect rect;
00451 QRect startpoint;
00452
00453 int lineEndHeight = mCurrentLinePos+p.fontMetrics().lineSpacing()+p.fontMetrics().height();
00454 if ( lineEndHeight > p.viewport().height()) {
00455 mCurrentLinePos = 0;
00456 mPrinter->newPage();
00457 }
00458
00459
00460
00461 if(r) {
00462 pospriority = r->right() + 1;
00463 }
00464
00465
00466 if (priority > 0) {
00467 outStr.setNum(priority);
00468 rect = p.boundingRect(pospriority,mCurrentLinePos + 10,
00469 5,-1,AlignCenter,outStr);
00470
00471 rect.setWidth(18);
00472 rect.setHeight(18);
00473 p.drawText(rect,AlignCenter, outStr);
00474 p.drawRect(rect);
00475 startpoint = rect;
00476 }
00477
00478
00479 if (level > 0) {
00480 int center,bottom,to,endx;
00481 center = r->left() + (r->width()/2);
00482 bottom = r->bottom() + 1;
00483 to = rect.top() + (rect.height()/2);
00484 endx = rect.left();
00485 p.moveTo(center,bottom);
00486 p.lineTo(center,to);
00487 p.lineTo(endx,to);
00488 }
00489
00490
00491 outStr=item->summary();
00492 int left = possummary+(level*10);
00493 rect = p.boundingRect(left,rect.top(),
00494 (posdue-(left + rect.width() + 5)),-1,WordBreak,outStr);
00495 QRect newrect;
00496 p.drawText(rect,WordBreak,outStr,-1,&newrect);
00497
00498
00499 if (item->hasDueDate()) {
00500 outStr = local->formatDate(item->dtDue().date(),true);
00501 rect = p.boundingRect(posdue,mCurrentLinePos, mCurrentLinePos,-1,AlignTop|AlignLeft,outStr);
00502 p.drawText(rect, mCurrentLinePos, outStr);
00503 }
00504
00505
00506 if (item->isCompleted()) {
00507 p.drawLine( 5, (mCurrentLinePos)-fontHeight/2 + 2,
00508 pageWidth-5, mCurrentLinePos-fontHeight/2 + 2);
00509 }
00510
00511
00512 mCurrentLinePos=newrect.bottom() + 10;
00513
00514
00515 QPtrList<Incidence> l = item->relations();
00516 Incidence *c;
00517 for(c=l.first();c;c=l.next()) {
00518 drawTodo(count, static_cast<Todo *> (c),p,level+1,&startpoint);
00519 }
00520 }
00521
00522
00524
00525 void CalPrinter::drawHeader(QPainter &p, const QDate &fd, const QDate &td,
00526 const QDate &cd,
00527 int width, int height, PrintType pt)
00528 {
00529 KLocale *local = KGlobal::locale();
00530 QFont font("helvetica", 18, QFont::Bold);
00531 p.drawRect(0, 0, width, height);
00532 p.fillRect(1, 1,
00533 width-2,
00534 height-2,
00535 QBrush(Dense7Pattern));
00536
00537 p.setFont(font);
00538 int lineSpacing = p.fontMetrics().lineSpacing();
00539 QString title;
00540 QString myOwner(mCalendar->getOwner());
00541
00542
00543
00544
00545 switch(pt) {
00546 case TimeTable:
00547 break;
00548
00549 case Todolist:
00550 title += i18n("To-do items:");
00551
00552 p.drawText(5, lineSpacing,title);
00553 break;
00554 case Month:
00555 title += local->monthName(cd.month());
00556 title += " ";
00557 title += QString::number(cd.year());
00558 p.drawText(5, lineSpacing, title );
00559 break;
00560 case Week:
00561
00562 title += local->formatDate(fd);
00563
00564 p.drawText(5, lineSpacing, title );
00565 title.truncate(0);
00566
00567 title += local->formatDate(td);
00568 p.drawText(5, 2*lineSpacing, title);
00569 break;
00570 case Day:
00571
00572 title += local->formatDate(fd,false);
00573 p.drawText(5, lineSpacing, title );
00574
00575 }
00576
00577
00578 switch (pt) {
00579 case TimeTable: break;
00580 case Todolist:
00581 case Week:
00582 case Day:
00583 drawSmallMonth(p, QDate(cd.addDays(-cd.day()+1)),
00584 width/2+5, 5, 100, height-10);
00585 break;
00586
00587 drawSmallMonth(p, QDate(cd.addDays(cd.daysInMonth()-cd.day()+1)),
00588 width/2+width/4+5, 5, 100, height-10);
00589 case Month:
00590 drawSmallMonth(p, QDate(cd.addDays(-cd.day())),
00591 width/2+5, 5, 100, height-10);
00592
00593 drawSmallMonth(p, QDate(cd.addDays(cd.daysInMonth()-cd.day()+1)),
00594 width/2+width/4+5, 5, 100, height-10);
00595
00596 }
00597 }
00598
00599
00600
00601
00602
00603 void CalPrinter::drawDaysOfWeekBox(QPainter &p, const QDate &qd,
00604 int x, int y, int width, int height)
00605 {
00606 KLocale *local = KGlobal::locale();
00607
00608 p.setFont(QFont("helvetica", 10, QFont::Bold));
00609 p.drawRect(x, y, width, height);
00610 p.fillRect(x+1, y+1,
00611 width-2, height-2,
00612 QBrush(Dense7Pattern));
00613 p.drawText(x+5, y, width-10, height, AlignCenter | AlignVCenter,
00614 local->weekDayName(qd.dayOfWeek()));
00615 }
00616
00617 void CalPrinter::drawDayBox(QPainter &p, const QDate &qd,
00618 int x, int y, int width, int height,
00619 bool fullDate)
00620 {
00621 KLocale *local = KGlobal::locale();
00622 QString dayNumStr;
00623 QPtrList<Event> eventList;
00624 QString ampm;
00625
00626 #ifndef KORG_NOPLUGINS
00627 QString hstring(KOCore::self()->holiday(qd));
00628 #else
00629 QString hstring;
00630 #endif
00631
00632
00633 if (fullDate) {
00634
00635
00636
00637
00638
00639
00640
00641 dayNumStr = local->weekDayName(qd.dayOfWeek()) + ' ' + local->monthName(qd.month(), true) + ' ' + QString::number(qd.day());
00642 } else {
00643 dayNumStr = QString::number(qd.day());
00644 }
00645
00646 p.drawRect(x, y, width, height);
00647
00648 p.drawRect(x, y, width, mSubHeaderHeight);
00649 p.fillRect(x+1, y+1, width-2, mSubHeaderHeight-2, QBrush(Dense7Pattern));
00650 if (!hstring.isEmpty()) {
00651 p.setFont(QFont("helvetica", 8, QFont::Bold, TRUE));
00652
00653 p.drawText(x+5, y, width-25, mSubHeaderHeight, AlignLeft | AlignVCenter,
00654 hstring);
00655 }
00656 p.setFont(QFont("helvetica", 10, QFont::Bold));
00657 p.drawText(x+5, y, width-10, mSubHeaderHeight, AlignRight | AlignVCenter,
00658 dayNumStr);
00659
00660 eventList = mCalendar->events(qd, TRUE);
00661 eventList.first();
00662 int count = 1;
00663 QString outStr;
00664 Event *currEvent(eventList.first());
00665 p.setFont(QFont("helvetica", 8));
00666 int lineSpacing = p.fontMetrics().lineSpacing();
00667
00668 while (count <= 9 && (currEvent != NULL)) {
00669 if (currEvent->doesFloat() || currEvent->isMultiDay())
00670 outStr = currEvent->summary();
00671
00672 else {
00673 QTime t1 = currEvent->dtStart().time();
00674
00675 outStr = local->formatTime(t1);
00676 outStr += " " + currEvent->summary();
00677
00678 }
00679
00680 p.drawText(x+5, y+(lineSpacing*(count+1)), width-10, lineSpacing,
00681 AlignLeft|AlignVCenter, outStr);
00682 currEvent = eventList.next();
00683 ++count;
00684 }
00685
00686 if ( count <= 9 ) {
00687 QPtrList<Todo> todos = mCalendar->todos( qd );
00688 Todo *todo;
00689 for( todo = todos.first(); todo && count <= 9; todo = todos.next() ) {
00690 QString text;
00691 if (todo->hasDueDate()) {
00692 if (!todo->doesFloat()) {
00693 text += KGlobal::locale()->formatTime(todo->dtDue().time());
00694 text += " ";
00695 }
00696 }
00697 text += i18n("To-Do: %1").arg(todo->summary());
00698
00699 p.drawText(x+5, y+(lineSpacing*(count+1)), width-10, lineSpacing,
00700 AlignLeft|AlignVCenter, text);
00701 ++count;
00702 }
00703 }
00704 }
00705
00706 void CalPrinter::drawTTDayBox(QPainter &p, const QDate &qd,
00707 int x, int y, int width, int height,
00708 bool fullDate)
00709 {
00710 KLocale *local = KGlobal::locale();
00711 QString dayNumStr;
00712 QPtrList<Event> eventList;
00713 QString ampm;
00714
00715 #ifndef KORG_NOPLUGINS
00716 QString hstring(KOCore::self()->holiday(qd));
00717 #else
00718 QString hstring;
00719 #endif
00720
00721
00722 if (fullDate) {
00723
00724
00725
00726
00727
00728
00729 dayNumStr = local->weekDayName(qd.dayOfWeek()) + ' ' + local->monthName(qd.month(), true) + ' ' + QString::number(qd.day());
00730 } else {
00731 dayNumStr = QString::number(qd.day());
00732 }
00733
00734 p.drawRect(x, y, width, mSubHeaderHeight);
00735 p.fillRect(x+1, y+1, width-2, mSubHeaderHeight-2, QBrush(Dense5Pattern));
00736 p.setFont(QFont("helvetica", 10, QFont::Bold));
00737 p.drawText(x+5, y, width, mSubHeaderHeight,
00738 AlignCenter | AlignVCenter | AlignJustify | WordBreak,
00739 dayNumStr);
00740
00741 p.drawRect(x, y+mSubHeaderHeight, width, height);
00742
00743
00744 int cury=y+mSubHeaderHeight+height;
00745 for(int i=1; i<=12;i++){
00746 cury+=height;
00747 p.drawLine(x,cury,x+width,cury);
00748 }
00749
00750 p.drawLine(x+width,y,x+width,y+mSubHeaderHeight+(13*height));
00751
00752 p.setFont(QFont("helvetica", 10));
00753 QBrush oldBrush=p.brush();
00754 p.setBrush(QBrush(Dense5Pattern));
00755
00756 eventList = mCalendar->events(qd, TRUE);
00757 Event *currEvent;
00758
00759
00760 QString MultiDayStr;
00761 for (currEvent = eventList.first(); currEvent; currEvent = eventList.next()) {
00762 if (currEvent->doesFloat() || currEvent->isMultiDay()) {
00763 if(MultiDayStr) MultiDayStr += ", ";
00764 MultiDayStr += currEvent->summary();
00765 }
00766 else {
00767 int startTime = currEvent->dtStart().time().hour();
00768 int endTime = currEvent->dtEnd().time().hour();
00769 float minuteInc = height / 60.0;
00770 if ((startTime >= mStartHour) && (endTime <= (mStartHour + 12))) {
00771 startTime -= mStartHour;
00772 int startMinuteOff = (int) (minuteInc * currEvent->dtStart().time().minute());
00773 int currentyPos =y+mSubHeaderHeight+height+startMinuteOff+startTime*height;
00774 endTime -= mStartHour;
00775 int endMinuteOff = (int) (minuteInc * currEvent->dtEnd().time().minute());
00776 int eventLenght=(endMinuteOff-startMinuteOff) + (endTime - startTime)*height;
00777 kdDebug() << currEvent->summary() << ": " << " x=" << x << " currY=" << currentyPos << " width=" << width << " lenght=" << eventLenght;
00778 p.drawRect(x, currentyPos,
00779 width, eventLenght);
00780 p.drawText(x,
00781 currentyPos,
00782 width,
00783 eventLenght,
00784 AlignCenter | AlignVCenter | AlignJustify | WordBreak, currEvent->summary());
00785 }
00786 }
00787 }
00788
00789 p.setBrush(oldBrush);
00790
00791
00792 if(MultiDayStr.length()!=0)
00793 p.fillRect(x+1,y+1+mSubHeaderHeight, width-2, height-2, QBrush(Dense5Pattern));
00794 p.setFont(QFont("helvetica", 10));
00795 p.drawText(x, y+mSubHeaderHeight, width, height, AlignCenter | AlignVCenter| AlignJustify | WordBreak,
00796 MultiDayStr);
00797 }
00798
00799
00800 void CalPrinter::drawDaysOfWeek(QPainter &p, const QDate &qd,
00801 int width, int )
00802 {
00803 int offset=mHeaderHeight+5;
00804 int cellWidth = width/7;
00805 int cellHeight = mSubHeaderHeight;
00806 QDate monthDate(QDate(qd.year(), qd.month(),1));
00807
00808 if (KGlobal::locale()->weekStartsMonday())
00809
00810 monthDate = monthDate.addDays(-(monthDate.dayOfWeek()-1));
00811 else
00812
00813 monthDate = monthDate.addDays(-(monthDate.dayOfWeek()%7));
00814
00815 for (int col = 0; col < 7; col++) {
00816 drawDaysOfWeekBox(p, monthDate,
00817 col*cellWidth, offset,
00818 cellWidth, cellHeight);
00819 monthDate = monthDate.addDays(1);
00820 }
00821 }
00822
00823 void CalPrinter::drawDay(QPainter &p, const QDate &qd, int width, int height)
00824 {
00825 int startHour = mStartHour;
00826 int endHour = 20;
00827 int offset = mHeaderHeight + 5;
00828 QPtrList<Event> eventList = mCalendar->events(qd, TRUE);
00829 Event *currEvent;
00830
00831 p.setFont(QFont("helvetica", 14));
00832 p.setBrush(QBrush(Dense7Pattern));
00833 currEvent = eventList.first();
00834 int allDays = 0;
00835 while(currEvent) {
00836 if (currEvent->doesFloat()) {
00837 p.drawRect(20, offset, width-25, 35);
00838 p.drawText(30, offset+10, width-40, 30, AlignLeft | AlignTop,
00839 currEvent->summary());
00840 offset += 40;
00841 allDays++;
00842 eventList.remove();
00843 currEvent = eventList.current();
00844 } else {
00845 currEvent = eventList.next();
00846 }
00847 }
00848 startHour += (allDays/2);
00849 p.setBrush(QBrush());
00850 int tmpEnd;
00851 for (currEvent = eventList.first(); currEvent;
00852 currEvent = eventList.next()) {
00853 if (currEvent->dtStart().time().hour() < startHour)
00854 startHour = currEvent->dtStart().time().hour();
00855 tmpEnd = currEvent->dtEnd().time().hour();
00856 if (currEvent->dtEnd().time().minute() > 0)
00857 tmpEnd++;
00858 if (tmpEnd > endHour)
00859 endHour = tmpEnd;
00860 }
00861 int hours = endHour - startHour;
00862 int cellHeight = (height-offset) / hours;
00863 int cellWidth = width-80;
00864
00865 QString numStr;
00866 for (int i = 0; i < hours; i++) {
00867 p.drawRect(0, offset+i*cellHeight, 75, cellHeight);
00868 p.drawLine(37, offset+i*cellHeight+(cellHeight/2),
00869 75, offset+i*cellHeight+(cellHeight/2));
00870
00871 if ( !KGlobal::locale()->use12Clock() ) {
00872 numStr.setNum(i+startHour);
00873 if (cellHeight > 40) {
00874 p.setFont(QFont("helvetica", 20, QFont::Bold));
00875 } else {
00876 p.setFont(QFont("helvetica", 16, QFont::Bold));
00877 }
00878 p.drawText(0, offset+i*cellHeight, 33, cellHeight/2,
00879 AlignTop|AlignRight, numStr);
00880 p.setFont(QFont("helvetica", 14, QFont::Bold));
00881 p.drawText(37, offset+i*cellHeight, 45, cellHeight/2,
00882 AlignTop | AlignLeft, "00");
00883 } else {
00884 QTime time( i + startHour, 0 );
00885 numStr = KGlobal::locale()->formatTime( time );
00886 p.setFont(QFont("helvetica", 14, QFont::Bold));
00887 p.drawText(4, offset+i*cellHeight, 70, cellHeight/2,
00888 AlignTop|AlignLeft, numStr);
00889 }
00890
00891 p.drawRect(80, offset+i*cellHeight,cellWidth, cellHeight);
00892 p.drawLine(80, offset+i*cellHeight+(cellHeight/2),
00893 cellWidth+80, offset+i*cellHeight+(cellHeight/2));
00894
00895 }
00896
00897 p.setFont(QFont("helvetica", 14));
00898 p.setBrush(QBrush(Dense7Pattern));
00899 for (currEvent = eventList.first(); currEvent;
00900 currEvent = eventList.next()) {
00901 int startTime = currEvent->dtStart().time().hour();
00902 int endTime = currEvent->dtEnd().time().hour();
00903 float minuteInc = cellHeight / 60.0;
00904 if ((startTime >= startHour) && (endTime <= (startHour + hours))) {
00905 startTime -= startHour;
00906 int startMinuteOff = (int) (minuteInc *
00907 currEvent->dtStart().time().minute());
00908 int endMinuteOff = (int) (minuteInc * currEvent->dtEnd().time().minute());
00909 int cheight = (int) (minuteInc *
00910 currEvent->dtStart().secsTo(currEvent->dtEnd()) / 60 );
00911 p.drawRect(80, offset+startMinuteOff+startTime*cellHeight,
00912 cellWidth, cheight);
00913 p.drawText(85, offset+startMinuteOff+startTime*cellHeight+5, cellWidth-10,
00914 cheight-10, AlignLeft | AlignTop, currEvent->summary());
00915 }
00916 }
00917 p.setBrush(QBrush(NoBrush));
00918 }
00919
00920 void CalPrinter::drawWeek(QPainter &p, const QDate &qd, int width, int height)
00921 {
00922 QDate weekDate = qd;
00923 int offset = mHeaderHeight+5;
00924 int cellWidth = width/2;
00925 int cellHeight = (height-offset)/3;
00926
00927 if (KGlobal::locale()->weekStartsMonday())
00928
00929 weekDate = qd.addDays(-(qd.dayOfWeek()-1));
00930 else
00931
00932 weekDate = qd.addDays(-(qd.dayOfWeek()%7));
00933
00934 for (int i = 0; i < 7; i++, weekDate = weekDate.addDays(1)) {
00935 if (i < 3)
00936 drawDayBox(p, weekDate, 0, offset+i*cellHeight,
00937 cellWidth, cellHeight, TRUE);
00938 else
00939 if ((weekDate.dayOfWeek() == 6 && KGlobal::locale()->weekStartsMonday()) ||
00940 (weekDate.dayOfWeek() == 5 && !KGlobal::locale()->weekStartsMonday()))
00941 drawDayBox(p, weekDate, cellWidth, offset+2*cellHeight,
00942 cellWidth, cellHeight/2, TRUE);
00943 else if ((weekDate.dayOfWeek() == 7 && KGlobal::locale()->weekStartsMonday()) ||
00944 (weekDate.dayOfWeek() == 6 && !KGlobal::locale()->weekStartsMonday()))
00945 drawDayBox(p, weekDate, cellWidth, offset+2*cellHeight+(cellHeight/2),
00946 cellWidth, cellHeight/2, TRUE);
00947 else
00948 drawDayBox(p, weekDate, cellWidth, offset+(i%3)*cellHeight,
00949 cellWidth, cellHeight, TRUE);
00950 }
00951 }
00952
00953 void CalPrinter::drawTimeTable(QPainter &p, const QDate &qd, int width, int height)
00954 {
00955 QDate weekDate = qd;
00956 int offset = 5;
00957 int cellWidthTimeline = 40;
00958 int hoursToPrint = 12;
00959 int cellWidth = (width-cellWidthTimeline)/6;
00960 int cellHeight = (height-offset) / (hoursToPrint+1);
00961 int ystartTimeLine =offset+mSubHeaderHeight+cellHeight;
00962
00963 if (KGlobal::locale()->weekStartsMonday())
00964
00965 weekDate = qd.addDays(-(qd.dayOfWeek()-1));
00966 else
00967
00968 weekDate = qd.addDays(-(qd.dayOfWeek()%7));
00969
00970
00971 QString numStr;
00972 for (int i = 0; i < hoursToPrint; i++) {
00973 p.drawRect(0, ystartTimeLine+i*cellHeight,
00974 cellWidthTimeline, cellHeight);
00975 p.drawLine(cellWidthTimeline/2,
00976 ystartTimeLine+i*cellHeight+(cellHeight/2),
00977 cellWidthTimeline, ystartTimeLine+i*cellHeight+(cellHeight/2));
00978 numStr.setNum(i+mStartHour);
00979 p.setFont(QFont("helvetica", 10, QFont::Bold));
00980 p.drawText(0, ystartTimeLine+i*cellHeight,
00981 cellWidthTimeline/2, cellHeight/2,
00982 AlignTop|AlignRight, numStr);
00983 p.setFont(QFont("helvetica", 8, QFont::Bold));
00984 p.drawText(cellWidthTimeline/2+2,
00985 ystartTimeLine+i*cellHeight, cellWidthTimeline/2,
00986 cellHeight/2, AlignTop | AlignLeft, "00");
00987 }
00988
00989
00990 for (int i = 0; i < 7; i++, weekDate = weekDate.addDays(1)) {
00991 if (i < 6)
00992 drawTTDayBox(p, weekDate, cellWidthTimeline+i*cellWidth, offset,
00993 cellWidth, cellHeight, TRUE);
00994 }
00995 }
00996
00997 void CalPrinter::drawMonth(QPainter &p, const QDate &qd,
00998 int width, int height)
00999 {
01000 int weekdayCol;
01001 int offset = mHeaderHeight+5+mSubHeaderHeight;
01002 int cellWidth = width/7;
01003 int cellHeight = (height-offset) / 5;
01004 QDate monthDate(QDate(qd.year(), qd.month(), 1));
01005
01006 if (KGlobal::locale()->weekStartsMonday())
01007 weekdayCol = monthDate.dayOfWeek() - 1;
01008 else
01009 weekdayCol = monthDate.dayOfWeek() % 7;
01010 monthDate = monthDate.addDays(-weekdayCol);
01011
01012 for (int row = 0; row < (weekdayCol + qd.daysInMonth() - 1 )/7 + 1; row++) {
01013 for (int col = 0; col < 7; col++) {
01014 drawDayBox(p, monthDate, col*cellWidth, offset+row*cellHeight,
01015 cellWidth, cellHeight);
01016 monthDate = monthDate.addDays(1);
01017 }
01018 }
01019 }
01020
01021
01022 void CalPrinter::drawSmallMonth(QPainter &p, const QDate &qd,
01023 int x, int y, int width, int height)
01024 {
01025 bool firstCol = TRUE;
01026 QDate monthDate(QDate(qd.year(), qd.month(), 1));
01027 QDate monthDate2;
01028 int month = monthDate.month();
01029
01030
01031 p.setFont(QFont("helvetica", 8, QFont::Bold));
01032
01033 p.drawText(x, y, width, height/4, AlignCenter, KGlobal::locale()->monthName(qd.month()));
01034
01035 int cellWidth = width/7;
01036 int cellHeight = height/8;
01037 QString tmpStr;
01038 KLocale *local = KGlobal::locale();
01039
01040 if (KGlobal::locale()->weekStartsMonday())
01041
01042 monthDate2 = monthDate.addDays(-(monthDate.dayOfWeek()-1));
01043 else
01044
01045 monthDate2 = monthDate.addDays(-(monthDate.dayOfWeek()%7));
01046
01047
01048 p.setFont(QFont("helvetica", 8, QFont::Bold));
01049 for (int col = 0; col < 7; col++) {
01050
01051 tmpStr=local->weekDayName(monthDate2.dayOfWeek())[0].upper();
01052 p.drawText(x+col*cellWidth, y+height/4, cellWidth, cellHeight,
01053 AlignCenter, tmpStr);
01054 monthDate2 = monthDate2.addDays(1);
01055 }
01056
01057
01058 p.drawLine(x, y+height/4+cellHeight, x+width, y+height/4+cellHeight);
01059
01060 for (int row = 0; row < 5; row++) {
01061 for (int col = 0; col < 7; col++) {
01062 if (monthDate.month() != month)
01063 break;
01064 if (firstCol) {
01065 firstCol = FALSE;
01066 if (KGlobal::locale()->weekStartsMonday())
01067 col = monthDate.dayOfWeek() - 1;
01068 else
01069 col = monthDate.dayOfWeek() % 7;
01070 }
01071 p.drawText(x+col*cellWidth,
01072 y+height/4+cellHeight+(row*cellHeight),
01073 cellWidth, cellHeight, AlignCenter,
01074 tmpStr.setNum(monthDate.day()));
01075 monthDate = monthDate.addDays(1);
01076 }
01077 }
01078 }
01079
01080
01081
01082 CalPrintDialog::CalPrintDialog(KPrinter *p, QWidget *parent, const char *name)
01083 : QDialog(parent, name, true)
01084 {
01085 mPrinter = p;
01086
01087 setCaption(i18n("Print"));
01088
01089 QVBoxLayout *layout = new QVBoxLayout(this, 10);
01090
01091 QGroupBox *rangeGroup = new QGroupBox(this);
01092 rangeGroup->setTitle(i18n("Date Range"));
01093
01094 QVBoxLayout *layout2 = new QVBoxLayout(rangeGroup, 10);
01095 layout2->addSpacing(10);
01096 QHBoxLayout *subLayout2 = new QHBoxLayout();
01097 layout2->addLayout(subLayout2);
01098
01099 mFromDateEdit = new KDateEdit(rangeGroup);
01100
01101
01102
01103 subLayout2->addWidget(mFromDateEdit);
01104
01105 mToDateEdit = new KDateEdit(rangeGroup);
01106
01107
01108 subLayout2->addWidget(mToDateEdit);
01109
01110 layout->addWidget(rangeGroup);
01111
01112 mTypeGroup = new QButtonGroup(i18n("View Type"), this);
01113 QVBoxLayout *layout3 = new QVBoxLayout(mTypeGroup, 10);
01114 layout3->addSpacing(10);
01115
01116 QRadioButton *rButt;
01117 layout3->addWidget(rButt = new QRadioButton(i18n("Day"), mTypeGroup));
01118 rButt->setMinimumHeight(rButt->sizeHint().height()-5);
01119 connect(rButt, SIGNAL(clicked()), this, SLOT(setPrintDay()));
01120
01121
01122 layout3->addWidget(rButt = new QRadioButton(i18n("Week"), mTypeGroup));
01123 rButt->setMinimumHeight(rButt->sizeHint().height()-5);
01124 connect(rButt, SIGNAL(clicked()), this, SLOT(setPrintWeek()));
01125
01126 layout3->addWidget(rButt = new QRadioButton(i18n("Month"), mTypeGroup));
01127 rButt->setMinimumHeight(rButt->sizeHint().height()-5);
01128 connect(rButt, SIGNAL(clicked()), this, SLOT(setPrintMonth()));
01129
01130 layout3->addWidget(rButt = new QRadioButton(i18n("To-do"), mTypeGroup));
01131 rButt->setMinimumHeight(rButt->sizeHint().height()-5);
01132 connect(rButt, SIGNAL(clicked()), this, SLOT(setPrintTodo()));
01133
01134 layout3->addWidget(rButt = new QRadioButton(i18n("Timetable"), mTypeGroup));
01135 rButt->setMinimumHeight(rButt->sizeHint().height()-5);
01136 connect(rButt, SIGNAL(clicked()), this, SLOT(setPrintTimeTable()));
01137
01138 layout->addWidget(mTypeGroup);
01139
01140 #if 0
01141 KSeparator *hLine = new KSeparator( KSeparator::HLine, this);
01142 layout->addWidget(hLine);
01143 #endif
01144
01145 QHBoxLayout *subLayout = new QHBoxLayout();
01146 layout->addLayout(subLayout);
01147
01148 mOkButton = new QPushButton(this);
01149 connect(mOkButton,SIGNAL(clicked()),SLOT(accept()));
01150 mOkButton->setDefault(true);
01151 mOkButton->setAutoDefault(true);
01152 subLayout->addWidget(mOkButton);
01153
01154 QPushButton *button = new QPushButton(i18n("&Cancel"), this);
01155 connect(button, SIGNAL(clicked()),SLOT(reject()));
01156 subLayout->addWidget(button);
01157 }
01158
01159 CalPrintDialog::~CalPrintDialog()
01160 {
01161 }
01162
01163 void CalPrintDialog::setRange(const QDate &from, const QDate &to)
01164 {
01165 mFromDateEdit->setDate(from);
01166 mToDateEdit->setDate(to);
01167 }
01168
01169 void CalPrintDialog::setPreview(bool preview)
01170 {
01171 mOkButton->setText(preview ? i18n("&Preview") : i18n("&Print..."));
01172 }
01173
01174 QDate CalPrintDialog::fromDate() const
01175 {
01176 return mFromDateEdit->date();
01177 }
01178
01179 QDate CalPrintDialog::toDate() const
01180 {
01181 return mToDateEdit->date();
01182 }
01183
01184 CalPrinter::PrintType CalPrintDialog::printType() const
01185 {
01186 return mPrintType;
01187 }
01188
01189 void CalPrintDialog::setPrintDay()
01190 {
01191 mTypeGroup->setButton(0);
01192 mPrintType = CalPrinter::Day;
01193 }
01194
01195 void CalPrintDialog::setPrintWeek()
01196 {
01197 mTypeGroup->setButton(1);
01198 mPrintType = CalPrinter::Week;
01199 }
01200
01201 void CalPrintDialog::setPrintMonth()
01202 {
01203 mTypeGroup->setButton(2);
01204 mPrintType = CalPrinter::Month;
01205 }
01206
01207 void CalPrintDialog::setPrintTodo()
01208 {
01209 mTypeGroup->setButton(3);
01210 mPrintType = CalPrinter::Todolist;
01211 }
01212
01213 void CalPrintDialog::setPrintTimeTable()
01214 {
01215 mTypeGroup->setButton(4);
01216 mPrintType = CalPrinter::TimeTable;
01217 }