1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
|
/*
* Copyright 2008,2010 Davide Bettio <davide.bettio@kdemail.net>
* Copyright 2009 John Layt <john@layt.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "calendartable.h"
#include "config-calendartable.h"
//Qt
#include <QtCore/QDate>
#include <QtCore/QListIterator>
#include <QtCore/QTimer>
#include <QtGui/QPainter>
#include <QtGui/QWidget>
#include <QtGui/QGraphicsSceneWheelEvent>
#include <QtGui/QStyleOptionGraphicsItem>
//KDECore
#include <KGlobal>
#include <KDateTime>
#include <KDebug>
#include <KConfigDialog>
#include <KConfigGroup>
//Plasma
#include <Plasma/Svg>
#include <Plasma/Theme>
#include <Plasma/DataEngine>
#include <Plasma/DataEngineManager>
#ifdef HAVE_KDEPIMLIBS
#include "ui_calendarHolidaysConfig.h"
#else
#include "ui_calendarConfig.h"
#endif
#include <cmath>
namespace Plasma
{
static const int DISPLAYED_WEEKS = 6;
class CalendarCellBorder
{
public:
CalendarCellBorder(int c, int w, int d, CalendarTable::CellTypes t, QDate dt)
: cell(c),
week(w),
weekDay(d),
type(t),
date(dt)
{
}
int cell;
int week;
int weekDay;
CalendarTable::CellTypes type;
QDate date;
};
class CalendarTablePrivate
{
public:
enum Populations { NoPendingPopulation = 0, PopulateHolidays = 1, PopulateEvents = 2 };
CalendarTablePrivate(CalendarTable *calTable, const QDate &initialDate = QDate::currentDate())
: q(calTable),
calendarType(-1),
calendar(0),
displayEvents(false),
displayHolidays(false),
calendarDataEngine(0),
automaticUpdates(true),
opacity(0.5),
hoverWeekRow(-1),
hoverWeekdayColumn(-1),
pendingPopulations(NoPendingPopulation),
delayedPopulationTimer(new QTimer())
{
KGlobal::locale()->insertCatalog("libkholidays");
svg = new Svg();
svg->setImagePath("widgets/calendar");
svg->setContainsMultipleImages(true);
delayedPopulationTimer->setInterval(0);
delayedPopulationTimer->setSingleShot(true);
QObject::connect(delayedPopulationTimer, SIGNAL(timeout()), q, SLOT(populateCalendar()));
setDate(initialDate);
}
~CalendarTablePrivate()
{
// Delete the old calendar first if it's not the global calendar
if (calendar != KGlobal::locale()->calendar()) {
delete calendar;
}
if (calendarDataEngine) {
Plasma::DataEngineManager::self()->unloadEngine("calendar");
}
delete svg;
delete delayedPopulationTimer;
}
void setCalendar(const KCalendarSystem *newCalendar)
{
// If not the global calendar, delete the old calendar first
if (calendar != KGlobal::locale()->calendar()) {
delete calendar;
}
calendar = newCalendar;
if (calendar == KGlobal::locale()->calendar()) {
calendarType = -1;
QObject::connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)), q, SLOT(settingsChanged(int)));
} else {
calendarType = q->calendar()->calendarSystem();
QObject::disconnect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)), q, SLOT(settingsChanged(int)));
}
// Force date update to refresh cached date componants then update display
setDate(selectedDate);
updateHoveredPainting(QPointF());
populateHolidays();
populateEvents();
q->update();
}
void setDate(const QDate &setDate)
{
//q->calendar() cannot be used because d is still not assigned
const KCalendarSystem *cal = calendar ? calendar : KGlobal::locale()->calendar();
selectedDate = setDate;
selectedMonth = cal->month(setDate);
selectedYear = cal->year(setDate);
weekDayFirstOfSelectedMonth = weekDayFirstOfMonth(setDate);
daysInWeek = cal->daysInWeek(setDate);
daysInSelectedMonth = cal->daysInMonth(setDate);
daysShownInPrevMonth = (weekDayFirstOfSelectedMonth - cal->weekStartDay() + daysInWeek) % daysInWeek;
// make sure at least one day of the previous month is visible.
// 1 = minimum number of days to show, increase if more days should be forced visible:
if (daysShownInPrevMonth < 1) {
daysShownInPrevMonth += daysInWeek;
}
viewStartDate = dateFromRowColumn(0, 0);
viewEndDate = dateFromRowColumn(DISPLAYED_WEEKS - 1, daysInWeek - 1);
}
//Returns the x co-ordinate of a given column to LTR order, column is 0 to (daysInWeek-1)
//This version does not adjust for RTL, so should not be used directly for drawing
int columnToX(int column)
{
return q->boundingRect().x() +
centeringSpace +
weekBarSpace +
cellW +
((cellW + cellSpace) * column);
}
//Returns the y co-ordinate for given row, row is 0 to (DISPLAYED_WEEKS - 1)
int rowToY(int row)
{
return (int) q->boundingRect().y() +
headerHeight +
headerSpace +
((cellH + cellSpace) * row);
}
//Returns the absolute LTR column for a given x co-ordinate, -1 if outside table
int xToColumn(qreal x)
{
if (x >= columnToX(0) && x < columnToX(daysInWeek)) {
return ((x - centeringSpace) / (cellW + cellSpace)) - 1;
}
return -1;
}
//Returns the absolute row for a given y co-ordinate, -1 if outside table
int yToRow(qreal y)
{
if (y >= rowToY(0) && y < rowToY(DISPLAYED_WEEKS)) {
return (y - headerHeight - headerSpace) / (cellH + cellSpace);
}
return -1;
}
//Convert between column and weekdayColumn depending on LTR or RTL mode
//Note the same calculation used in both directions
int adjustColumn(int column)
{
if (column >= 0 && column < daysInWeek) {
if (q->layoutDirection() == Qt::RightToLeft) {
return daysInWeek - column - 1;
} else {
return column;
}
}
return -1;
}
//Given an x y point in the table return the cell date.
//Note can be an invalid date in the calendar system
QDate dateFromPoint(QPointF point)
{
if (point.isNull()) {
return QDate();
}
int column = xToColumn(point.x());
int row = yToRow(point.y());
if (column < 0 || column >= daysInWeek || row < 0 || row >= DISPLAYED_WEEKS) {
return QDate();
}
return dateFromRowColumn(row, adjustColumn(column));
}
//Given a date in the currently selected month, return the position in the table as a
//row and column. Note no direction is assumed
void rowColumnFromDate(const QDate &cellDate, int &weekRow, int &weekdayColumn)
{
int offset = q->calendar()->day(cellDate) + daysShownInPrevMonth - 1;
weekRow = offset / daysInWeek;
weekdayColumn = offset % daysInWeek;
}
//Given a position in the table as a 0-indexed row and column, return the cell date. Makes
//no assumption about direction. Date returned can be an invalid date in the calendar
//system, or simply invalid.
QDate dateFromRowColumn(int weekRow, int weekdayColumn)
{
QDate cellDate;
//q->calendar() cannot be used because d is still not assigned
const KCalendarSystem *cal = calendar ? calendar : KGlobal::locale()->calendar();
//starting from the first of the month, which is known to always be valid, add/subtract
//number of days to get to the required cell
if (cal->setDate(cellDate, selectedYear, selectedMonth, 1)) {
cellDate = cal->addDays(cellDate, (weekRow * daysInWeek) + weekdayColumn - daysShownInPrevMonth);
}
return cellDate;
}
void updateHoveredPainting(const QPointF &hoverPoint)
{
QRectF oldHoverRect = hoverRect;
hoverRect = QRectF();
hoverWeekdayColumn = -1;
hoverWeekRow = -1;
if (!hoverPoint.isNull()) {
int column = xToColumn(hoverPoint.x());
int row = yToRow(hoverPoint.y());
if (column >= 0 && column < daysInWeek && row >= 0 && row < DISPLAYED_WEEKS) {
hoverRect = QRectF(columnToX(column) - glowRadius,
rowToY(row) - glowRadius,
cellW + glowRadius * 2,
cellH + glowRadius * 2).adjusted(-2,-2,2,2);
hoverWeekdayColumn = adjustColumn(column);
hoverWeekRow = row;
}
}
// now update what is needed, and only what is needed!
if (hoverRect != oldHoverRect) {
if (oldHoverRect.isValid()) {
q->update(oldHoverRect);
}
if (hoverRect.isValid()) {
q->update(hoverRect);
}
if (hoverWeekRow >= 0 && hoverWeekdayColumn >= 0) {
const QDate date = dateFromRowColumn(hoverWeekRow, hoverWeekdayColumn);
emit q->dateHovered(date);
} else {
emit q->dateHovered(QDate());
}
}
}
// calculate weekday number of first day of this month, this is the anchor for all calculations
int weekDayFirstOfMonth(const QDate &cellDate)
{
//q->calendar() cannot be used because d is still not assigned
const KCalendarSystem *cal = calendar ? calendar : KGlobal::locale()->calendar();
Q_UNUSED(cellDate);
QDate firstDayOfMonth;
int weekday = -1;
if ( cal->setDate(firstDayOfMonth, selectedYear, selectedMonth, 1)) {
weekday = cal->dayOfWeek(firstDayOfMonth);
}
return weekday;
}
QString defaultHolidaysRegion()
{
//FIXME: get rid of the query; turn it into a proper async request
return calendarEngine()->query("holidaysDefaultRegion").value("holidaysDefaultRegion").toString();
}
bool isValidHolidaysRegion(const QString &holidayRegion)
{
//FIXME: get rid of the query; turn it into a proper async request
QString queryString = "holidaysIsValidRegion" + QString(':') + holidayRegion;
return calendarEngine()->query(queryString).value(queryString).toBool();
}
bool addHolidaysRegion(const QString &holidayRegion, bool daysOff)
{
//kDebug() << holidayRegion << holidaysRegions.contains(holidayRegion) << isValidHolidaysRegion(holidayRegion);
if (!holidaysRegions.contains(holidayRegion) && isValidHolidaysRegion(holidayRegion)) {
QString queryString = "holidaysRegion" + QString(':') + holidayRegion;
Plasma::DataEngine::Data regions = calendarEngine()->query(queryString);
Plasma::DataEngine::DataIterator it(regions);
while (it.hasNext()) {
it.next();
Plasma::DataEngine::Data region = it.value().toHash();
region.insert("UseForDaysOff", daysOff);
holidaysRegions.insert(it.key(), region);
}
return true;
}
return false;
}
bool holidayIsDayOff(Plasma::DataEngine::Data holidayData)
{
return (holidayData.value("ObservanceType").toString() == "PublicHoliday" &&
holidaysRegions.value(holidayData.value("RegionCode").toString()).value("UseForDaysOff").toBool());
}
void insertPimOccurence(const QString &type, const QDate &date, Plasma::DataEngine::Data occurrence)
{
if (date >= viewStartDate && date <= viewEndDate) {
int julian = date.toJulianDay();
if (type == "Event" && !events.contains(julian, occurrence)) {
events.insert(julian, occurrence);
} else if (type == "Todo" && !todos.contains(julian, occurrence)) {
todos.insert(julian, occurrence);
} else if (type == "Journal" && !journals.contains(julian, occurrence)) {
journals.insert(julian, occurrence);
}
}
}
Plasma::DataEngine *calendarEngine();
void checkIfCalendarEngineNeeded();
void populateHolidays();
void populateEvents();
void populateCalendar();
void settingsChanged(int category);
CalendarTable *q;
int calendarType;
const KCalendarSystem *calendar;
QDate selectedDate;
QDate currentDate;
int selectedMonth;
int selectedYear;
int weekDayFirstOfSelectedMonth;
int daysInWeek;
int daysInSelectedMonth;
int daysShownInPrevMonth;
QDate viewStartDate;
QDate viewEndDate;
bool displayEvents;
bool displayHolidays;
Plasma::DataEngine *calendarDataEngine;
// List of Holiday Regions to display
// Hash key: QString = Holiday Region Code, Data = details of Holiday Region
QHash<QString, Plasma::DataEngine::Data> holidaysRegions;
// Index to Holidays that occur on a given date.
// Hash key: int = Julian Day number of holiday observance, int = key of holiday event
QMultiHash<int, int> holidays;
// Holiday details. A holiday may be observed on multiple dates.
// Hash key: int = key of holiday event, Data = details of holiday
QHash<int, Plasma::DataEngine::Data> holidayEvents;
// Index to Events/Todos/Journals that occur on a given date.
// Hash key: int = Julian Day number of event/todo occurrence, Data = occurence details including start date, end date and Akonadi incidence UID
QMultiHash<int, Plasma::DataEngine::Data> events;
QMultiHash<int, Plasma::DataEngine::Data> todos;
QMultiHash<int, Plasma::DataEngine::Data> journals;
// Event/Todo/Journal details. An event may recur on multiple dates.
// Hash key: QString = Akonadi UID of event/todo/journal, Data = details of event/todo/journal
QHash<QString, Plasma::DataEngine::Data> pimEvents;
QString eventsQuery;
bool automaticUpdates;
QPointF lastSeenMousePos;
#ifdef HAVE_KDEPIMLIBS
Ui::calendarHolidaysConfig calendarConfigUi;
#else
Ui::calendarConfig calendarConfigUi;
#endif
Plasma::Svg *svg;
float opacity; //transparency for the inactive text
QRectF hoverRect;
int hoverWeekRow;
int hoverWeekdayColumn;
int centeringSpace;
int cellW;
int cellH;
int cellSpace;
int headerHeight;
int headerSpace;
int weekBarSpace;
int glowRadius;
QFont weekDayFont;
QFont dateFontBold;
QFont dateFont;
int pendingPopulations;
QTimer *delayedPopulationTimer;
};
CalendarTable::CalendarTable(const QDate &date, QGraphicsWidget *parent)
: QGraphicsWidget(parent), d(new CalendarTablePrivate(this, date))
{
setAcceptHoverEvents(true);
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
}
CalendarTable::CalendarTable(QGraphicsWidget *parent)
: QGraphicsWidget(parent), d(new CalendarTablePrivate(this))
{
setAcceptHoverEvents(true);
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
}
CalendarTable::~CalendarTable()
{
delete d;
}
void CalendarTable::setCalendar(int newCalendarType)
{
if (newCalendarType == d->calendarType) {
return;
}
if (newCalendarType == -1) {
d->setCalendar(KGlobal::locale()->calendar());
} else {
d->setCalendar(KCalendarSystem::create(static_cast<KLocale::CalendarSystem>(newCalendarType)));
}
// Signal out date change so any dependents will update as well
emit dateChanged(date(), date());
emit dateChanged(date());
}
void CalendarTable::setCalendar(const KCalendarSystem *newCalendar)
{
if (newCalendar == d->calendar) {
return;
}
d->setCalendar(newCalendar);
// Signal out date change so any dependents will update as well
emit dateChanged(date(), date());
emit dateChanged(date());
}
const KCalendarSystem *CalendarTable::calendar() const
{
if (d->calendar) {
return d->calendar;
} else {
return KGlobal::locale()->calendar();
}
}
void CalendarTable::setDate(const QDate &newDate)
{
// New date must be valid in the current calendar system
if (!calendar()->isValid(newDate)) {
return;
}
// If new date is the same as old date don't actually need to do anything
if (newDate == date()) {
return;
}
int oldYear = d->selectedYear;
int oldMonth = d->selectedMonth;
QDate oldDate = date();
// now change the date
d->setDate(newDate);
d->updateHoveredPainting(d->lastSeenMousePos);
update(); //FIXME: we shouldn't need this update here, but something in Qt is broken (but with plasmoidviewer everything work)
if (oldYear != d->selectedYear || oldMonth != d->selectedMonth) {
d->populateHolidays();
d->populateEvents();
} else {
// only update the old and the new areas
int row, column;
d->rowColumnFromDate(oldDate, row, column);
update(cellX(column) - d->glowRadius, cellY(row) - d->glowRadius,
d->cellW + d->glowRadius * 2, d->cellH + d->glowRadius * 2);
d->rowColumnFromDate(newDate, row, column);
update(cellX(column) - d->glowRadius, cellY(row) - d->glowRadius,
d->cellW + d->glowRadius * 2, d->cellH + d->glowRadius * 2);
}
emit dateChanged(newDate, oldDate);
emit dateChanged(newDate);
}
const QDate& CalendarTable::date() const
{
return d->selectedDate;
}
void CalendarTable::setDisplayHolidays(bool showHolidays)
{
if (showHolidays) {
if (d->holidaysRegions.isEmpty()) {
d->addHolidaysRegion(d->defaultHolidaysRegion(), true);
}
QMutableHashIterator<QString, Plasma::DataEngine::Data> it(d->holidaysRegions);
while (it.hasNext()) {
it.next();
if (!d->isValidHolidaysRegion(it.key())) {
it.remove();
}
}
} else {
clearHolidays();
d->checkIfCalendarEngineNeeded();
}
if (d->displayHolidays != showHolidays) {
d->displayHolidays = showHolidays;
d->populateHolidays();
}
}
bool CalendarTable::displayHolidays()
{
return d->displayHolidays && !d->holidaysRegions.isEmpty();
}
bool CalendarTable::displayEvents()
{
return d->displayEvents;
}
void CalendarTable::setDisplayEvents(bool display)
{
if (d->displayEvents == display) {
return;
}
d->displayEvents = display;
if (display) {
d->populateEvents();
} else {
if (d->calendarDataEngine) {
d->calendarDataEngine->disconnectSource(d->eventsQuery, this);
}
d->events.clear();
d->todos.clear();
d->journals.clear();
d->pimEvents.clear();
d->checkIfCalendarEngineNeeded();
}
}
void CalendarTable::clearHolidaysRegions()
{
d->holidaysRegions.clear();
clearHolidays();
}
void CalendarTable::addHolidaysRegion(const QString ®ion, bool daysOff)
{
if (d->displayHolidays && d->addHolidaysRegion(region, daysOff)) {
d->populateHolidays();
}
}
QStringList CalendarTable::holidaysRegions() const
{
return d->holidaysRegions.keys();
}
QStringList CalendarTable::holidaysRegionsDaysOff() const
{
QStringList regions;
QHashIterator<QString, Plasma::DataEngine::Data> it(d->holidaysRegions);
while (it.hasNext()) {
it.next();
if (it.value().value("UseForDaysOff").toBool()) {
regions.append(it.key());
}
}
return regions;
}
void CalendarTable::clearHolidays()
{
d->holidayEvents.clear();
d->holidays.clear();
update();
}
void CalendarTable::addHoliday(Plasma::DataEngine::Data holidayData)
{
QDate holidayStartDate = QDate::fromString(holidayData.value("ObservanceStartDate").toString(), Qt::ISODate);
int holidayDuration = holidayData.value("ObservanceDuration").toInt();
int uid = d->holidayEvents.count();
d->holidayEvents.insert(uid, holidayData);
for (int i = 0; i < holidayDuration; ++i) {
d->holidays.insertMulti(holidayStartDate.addDays(i).toJulianDay(), uid);
}
}
bool CalendarTable::dateHasDetails(const QDate &date) const
{
int julian = date.toJulianDay();
return d->holidays.contains(julian) ||
d->events.contains(julian) ||
d->todos.contains(julian) ||
d->journals.contains(julian);
}
QStringList CalendarTable::dateDetails(const QDate &date) const
{
QStringList details;
const int julian = date.toJulianDay();
if (d->holidays.contains(julian)) {
foreach (int holidayUid, d->holidays.values(julian)) {
Plasma::DataEngine::Data holidayData = d->holidayEvents.value(holidayUid);
QString region = holidayData.value("RegionCode").toString();
if (d->holidayIsDayOff(holidayData)) {
details << i18nc("Day off: Holiday name (holiday region)",
"<i>Holiday</i>: %1 (%2)",
holidayData.value("Name").toString(),
d->holidaysRegions.value(region).value("Name").toString());
} else {
QString region = holidayData.value("RegionCode").toString();
details << i18nc("Not day off: Holiday name (holiday region)",
"%1 (%2)",
holidayData.value("Name").toString(),
d->holidaysRegions.value(region).value("Name").toString());
}
}
}
if (d->events.contains(julian)) {
foreach (const Plasma::DataEngine::Data &occurrence, d->events.values(julian)) {
details << i18n("<i>Event</i>: %1", buildOccurrenceDescription(occurrence));
}
}
if (d->todos.contains(julian)) {
foreach (const Plasma::DataEngine::Data &occurrence, d->todos.values(julian)) {
//TODO add Status and Percentage Complete when strings not frozen
details << i18n("<i>Todo</i>: %1", buildOccurrenceDescription(occurrence));
}
}
return details;
}
QString CalendarTable::buildOccurrenceDescription(const Plasma::DataEngine::Data &occurrence) const
{
const QString uid = occurrence.value("OccurrenceUid").toString();
const KDateTime occStartDate = occurrence.value("OccurrenceStartDate").value<KDateTime>();
const KDateTime occEndDate = occurrence.value("OccurrenceEndDate").value<KDateTime>();
const Plasma::DataEngine::Data details = d->pimEvents.value(uid);
if (details.value("AllDay").toBool()) {
return i18nc("All-day calendar event summary", "<br>%1", details.value("Summary").toString());
} else if (!occEndDate.isValid() || occStartDate == occEndDate) {
return i18nc("Time and summary for a calendarevent", "%1<br>%2",
KGlobal::locale()->formatTime(occStartDate.time()),
details.value("Summary").toString());
}
return i18nc("Start and end time and summary for a calendar event", "%1 - %2<br>%3",
KGlobal::locale()->formatTime(occStartDate.time()),
KGlobal::locale()->formatTime(occEndDate.time()),
details.value("Summary").toString());
}
void CalendarTable::setAutomaticUpdateEnabled(bool enabled)
{
d->automaticUpdates = enabled;
}
bool CalendarTable::isAutomaticUpdateEnabled() const
{
return d->automaticUpdates;
}
void CalendarTable::setCurrentDate(const QDate &date)
{
d->currentDate = date;
}
const QDate& CalendarTable::currentDate() const
{
return d->currentDate;
}
QDate CalendarTable::startDate() const
{
return d->viewStartDate;
}
QDate CalendarTable::endDate() const
{
return d->viewEndDate;
}
Plasma::DataEngine *CalendarTablePrivate::calendarEngine()
{
if (!calendarDataEngine) {
calendarDataEngine = Plasma::DataEngineManager::self()->loadEngine("calendar");
}
return calendarDataEngine;
}
void CalendarTablePrivate::checkIfCalendarEngineNeeded()
{
if (calendarDataEngine && !displayHolidays && !displayEvents) {
Plasma::DataEngineManager::self()->unloadEngine("calendar");
calendarDataEngine = 0;
}
}
void CalendarTablePrivate::populateHolidays()
{
pendingPopulations |= PopulateHolidays;
delayedPopulationTimer->start();
}
void CalendarTablePrivate::populateCalendar()
{
if (pendingPopulations & PopulateHolidays) {
holidayEvents.clear();
holidays.clear();
if (q->displayHolidays()) {
// Just fetch the days displayed in the grid
//FIXME: queries are bad, use an async method
QString queryString = "holidays" + QString(':') +
q->holidaysRegions().join(QString(',')) +
QString(':') + viewStartDate.toString(Qt::ISODate) +
QString(':') + viewEndDate.toString(Qt::ISODate);
QList<QVariant> holidays = calendarEngine()->query(queryString).value(queryString).toList();
QMutableListIterator<QVariant> i(holidays);
while (i.hasNext()) {
q->addHoliday(i.next().toHash());
}
}
q->update();
kDebug() << "repainting due to holiday population";
}
if (pendingPopulations & PopulateEvents) {
events.clear();
todos.clear();
journals.clear();
pimEvents.clear();
if (calendarDataEngine && !eventsQuery.isEmpty()) {
calendarDataEngine->disconnectSource(eventsQuery, q);
}
if (displayEvents) {
// Just fetch the days displayed in the grid
eventsQuery = "events" + QString(':') + viewStartDate.toString(Qt::ISODate) +
QString(':') + viewEndDate.toString(Qt::ISODate);
//kDebug() << "connecting to .. " << eventsQuery;
calendarEngine()->connectSource(eventsQuery, q);
} else {
eventsQuery.clear();
}
q->update();
}
pendingPopulations = NoPendingPopulation;
emit q->eventsChanged();
}
void CalendarTablePrivate::populateEvents()
{
pendingPopulations |= PopulateEvents;
delayedPopulationTimer->start();
}
void CalendarTablePrivate::settingsChanged(int category)
{
if (category != KGlobalSettings::SETTINGS_LOCALE) {
return;
}
calendar = KGlobal::locale()->calendar();
q->update();
}
void CalendarTable::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
{
Q_UNUSED(source)
d->events.clear();
d->todos.clear();
d->journals.clear();
d->pimEvents.clear();
foreach (const QVariant &v, data) {
Plasma::DataEngine::Data pimData = v.toHash();
QString type = pimData.value("Type").toString();
QString uid = pimData.value("UID").toString();
d->pimEvents.insert(uid, pimData);
QList<QVariant> occurrenceList = pimData.value("Occurrences").toList();
foreach (const QVariant &occurrence, occurrenceList) {
QDate occStartDate = occurrence.toHash().value("OccurrenceStartDate").value<KDateTime>().date();
if (pimData.value("EventMultiDay").toBool() == true) {
QDate occEndDate = occurrence.toHash().value("OccurrenceEndDate").value<KDateTime>().date();
QDate multiDate = occStartDate;
while (multiDate <= occEndDate) {
d->insertPimOccurence(type, multiDate, occurrence.toHash());
multiDate = multiDate.addDays(1);
}
} else {
d->insertPimOccurence(type, occStartDate, occurrence.toHash());
}
}
}
emit eventsChanged();
update();
}
void CalendarTable::applyConfiguration(KConfigGroup cg)
{
// convert pre 4.11 value if needed
const QVariant oldCalendarTypeValue = cg.readEntry("calendarType", QVariant());
if (oldCalendarTypeValue.type() == QVariant::String) {
const QString oldCalendarType = oldCalendarTypeValue.toString();
int newCalendarType = -1;
if (oldCalendarType == "gregorian") {
newCalendarType = KLocale::GregorianCalendar;
} else if (oldCalendarType == "coptic") {
newCalendarType = KLocale::CopticCalendar;
} else if (oldCalendarType == "ethiopian") {
newCalendarType = KLocale::EthiopianCalendar;
} else if (oldCalendarType == "hebrew") {
newCalendarType = KLocale::HebrewCalendar;
} else if (oldCalendarType == "hijri") {
newCalendarType = KLocale::IslamicCivilCalendar;
} else if (oldCalendarType == "indian-national") {
newCalendarType = KLocale::IndianNationalCalendar;
} else if (oldCalendarType == "jalali") {
newCalendarType = KLocale::JalaliCalendar;
} else if (oldCalendarType == "japanese") {
newCalendarType = KLocale::JapaneseCalendar;
} else if (oldCalendarType == "julian") {
newCalendarType = KLocale::JulianCalendar;
} else if (oldCalendarType == "minguo") {
newCalendarType = KLocale::MinguoCalendar;
} else if (oldCalendarType == "thai") {
newCalendarType = KLocale::ThaiCalendar;
}
cg.writeEntry("calendarType", newCalendarType);
}
setCalendar(cg.readEntry("calendarType", -1));
const bool holidays = cg.readEntry("displayHolidays", true);
clearHolidaysRegions();
if (holidays) {
QStringList regions = cg.readEntry("holidaysRegions", QStringList(d->defaultHolidaysRegion()));
QStringList daysOff = cg.readEntry("holidaysRegionsDaysOff", QStringList(d->defaultHolidaysRegion()));
clearHolidaysRegions();
foreach (const QString ®ion, regions) {
d->addHolidaysRegion(region, daysOff.contains(region));
}
d->populateHolidays();
}
setDisplayHolidays(holidays);
setDisplayEvents(cg.readEntry("displayEvents", true));
}
void CalendarTable::writeConfiguration(KConfigGroup cg)
{
cg.writeEntry("calendarType", d->calendarType);
cg.writeEntry("holidaysRegions", holidaysRegions());
cg.writeEntry("holidaysRegionsDaysOff", holidaysRegionsDaysOff());
cg.writeEntry("displayHolidays", d->displayHolidays);
cg.writeEntry("displayEvents", d->displayEvents);
}
void CalendarTable::createConfigurationInterface(KConfigDialog *parent)
{
QWidget *calendarConfigWidget = new QWidget();
d->calendarConfigUi.setupUi(calendarConfigWidget);
parent->addPage(calendarConfigWidget, i18n("Calendar"), "view-pim-calendar");
const QList<KLocale::CalendarSystem> calendars = KCalendarSystem::calendarSystemsList();
d->calendarConfigUi.calendarComboBox->addItem( i18n("Local"), QVariant( -1 ) );
for (int i = 0; i < calendars.count(); ++i) {
d->calendarConfigUi.calendarComboBox->addItem( KCalendarSystem::calendarLabel( calendars.at(i) ), QVariant( calendars.at(i) ) );
}
d->calendarConfigUi.calendarComboBox->setCurrentIndex( d->calendarConfigUi.calendarComboBox->findData( QVariant( d->calendarType ) ) );
d->calendarConfigUi.displayEvents->setChecked(d->displayEvents);
#ifdef HAVE_KDEPIMLIBS
QHashIterator<QString, Plasma::DataEngine::Data> it(d->holidaysRegions);
while (it.hasNext()) {
it.next();
if (it.value().value("UseForDaysOff").toBool()) {
d->calendarConfigUi.holidayRegionWidget->setRegionUseFlags(it.key(), KHolidays::HolidayRegionSelector::UseDaysOff);
} else {
d->calendarConfigUi.holidayRegionWidget->setRegionUseFlags(it.key(), KHolidays::HolidayRegionSelector::UseInformationOnly);
}
}
d->calendarConfigUi.holidayRegionWidget->setDescriptionHidden(true);
connect(d->calendarConfigUi.holidayRegionWidget, SIGNAL(selectionChanged()), parent, SLOT(settingsModified()));
#endif
connect(d->calendarConfigUi.calendarComboBox, SIGNAL(activated(int)), parent, SLOT(settingsModified()));
connect(d->calendarConfigUi.displayEvents, SIGNAL(stateChanged(int)), parent, SLOT(settingsModified()));
}
void CalendarTable::configAccepted(KConfigGroup cg)
{
setCalendar(d->calendarConfigUi.calendarComboBox->itemData(d->calendarConfigUi.calendarComboBox->currentIndex()).toInt());
setDisplayEvents(d->calendarConfigUi.displayEvents->isChecked());
#ifdef HAVE_KDEPIMLIBS
clearHolidaysRegions();
QHash<QString, KHolidays::HolidayRegionSelector::RegionUseFlags> regions = d->calendarConfigUi.holidayRegionWidget->regionUseFlags();
QHashIterator<QString, KHolidays::HolidayRegionSelector::RegionUseFlags> it(regions);
bool displayHolidays = false;
while (it.hasNext()) {
it.next();
if (it.value() == KHolidays::HolidayRegionSelector::UseDaysOff) {
addHolidaysRegion(it.key(), true);
displayHolidays = true;
} else if (it.value() == KHolidays::HolidayRegionSelector::UseInformationOnly) {
addHolidaysRegion(it.key(), false);
displayHolidays = true;
}
}
setDisplayHolidays(displayHolidays);
#endif
writeConfiguration(cg);
}
//Returns the x co-ordinate for drawing the day cell on the widget given the weekday column
//Note weekdayColumn is 0 to (daysInWeek -1) and is not a real weekDay number (i.e. NOT Monday=1).
//Adjusts automatically for RTL mode, so don't use to calculate absolute positions
int CalendarTable::cellX(int weekdayColumn)
{
return d->columnToX(d->adjustColumn(weekdayColumn));
}
//Returns the y co-ordinate for drawing the day cell on the widget given the weekRow
//weekRow is 0 to (DISPLAYED_WEEKS - 1)
int CalendarTable::cellY(int weekRow)
{
return d->rowToY(weekRow);
}
void CalendarTable::wheelEvent(QGraphicsSceneWheelEvent * event)
{
if (event->delta() < 0) {
setDate(calendar()->addMonths(date(), 1));
} else if (event->delta() > 0) {
setDate(calendar()->addMonths(date(), -1));
}
}
void CalendarTable::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
d->lastSeenMousePos = event->pos();
event->accept();
QDate date = d->dateFromPoint(event->pos());
setDate(date);
emit dateSelected(date);
}
void CalendarTable::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
mousePressEvent(event);
}
void CalendarTable::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
d->lastSeenMousePos = event->pos();
emit tableClicked();
}
void CalendarTable::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
d->lastSeenMousePos = event->pos();
d->updateHoveredPainting(event->pos());
}
void CalendarTable::resizeEvent(QGraphicsSceneResizeEvent * event)
{
Q_UNUSED(event);
QRectF r = contentsRect();
int numCols = d->daysInWeek + 1;
int rectSizeH = int(r.height() / (DISPLAYED_WEEKS + 1));
int rectSizeW = int(r.width() / numCols);
//Using integers to help to keep things aligned to the grid
//kDebug() << r.width() << rectSize;
d->cellSpace = qMax(1, qMin(4, qMin(rectSizeH, rectSizeW) / 20));
d->headerSpace = d->cellSpace * 2;
d->weekBarSpace = d->cellSpace * 2 + 1;
d->cellH = rectSizeH - d->cellSpace;
d->cellW = rectSizeW - d->cellSpace;
d->glowRadius = d->cellW * .1;
d->headerHeight = (int) (d->cellH / 1.5);
d->centeringSpace = qMax(0, int((r.width() - (rectSizeW * numCols) - (d->cellSpace * (numCols -1))) / 2));
// Relative to the cell size
const qreal weekSize = .75;
const qreal dateSize = .8;
d->weekDayFont = Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
d->weekDayFont.setPixelSize(d->cellH * weekSize);
d->dateFontBold = Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
d->dateFontBold.setPixelSize(d->cellH * dateSize);
d->dateFontBold.setBold(true);
d->dateFont = Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
#if QT_VERSION >= 0x040800
d->weekDayFont.setHintingPreference(QFont::PreferNoHinting);
d->dateFontBold.setHintingPreference(QFont::PreferNoHinting);
d->dateFont.setHintingPreference(QFont::PreferNoHinting);
#endif
QFontMetrics fm(d->weekDayFont);
int width = 0;
for (int i = 0; i < d->daysInWeek; i++) {
const QString name = calendar()->weekDayName(i, KCalendarSystem::ShortDayName);
width = qMax(width, fm.width(name));
}
if (width > d->cellW * weekSize) {
d->weekDayFont.setPixelSize(d->weekDayFont.pixelSize() * ((d->cellW * weekSize) / width));
}
fm = QFontMetrics(d->dateFontBold);
width = 0;
for (int i = 10; i <= 52; i++) {
width = qMax(width, fm.width(QString::number(i)));
}
if (width > d->cellW * dateSize) {
d->dateFontBold.setPixelSize(d->dateFontBold.pixelSize() * ((d->cellW * dateSize) / width));
}
d->dateFont.setPixelSize(d->dateFontBold.pixelSize());
}
void CalendarTable::paintCell(QPainter *p, int cell, int weekRow, int weekdayColumn, CellTypes type, const QDate &cellDate)
{
Q_UNUSED(cell);
QString cellSuffix = type & NotInCurrentMonth ? "inactive" : "active";
QRectF cellArea = QRectF(cellX(weekdayColumn), cellY(weekRow), d->cellW, d->cellH);
d->svg->paint(p, cellArea, cellSuffix); // draw background
QColor numberColor = Theme::defaultTheme()->color(Plasma::Theme::TextColor);
if (type & NotInCurrentMonth || type & InvalidDate) {
p->setOpacity(d->opacity);
}
p->setPen(numberColor);
p->setFont(type & Event ? d->dateFontBold : d->dateFont);
if (!(type & InvalidDate)) {
p->drawText(cellArea, Qt::AlignCenter, calendar()->formatDate(cellDate, KLocale::Day, KLocale::ShortNumber), &cellArea); //draw number
}
p->setOpacity(1.0);
}
void CalendarTable::paintBorder(QPainter *p, int cell, int weekRow, int weekdayColumn, CellTypes type, const QDate &cellDate)
{
Q_UNUSED(cell);
Q_UNUSED(cellDate);
if (type & Hovered) {
d->svg->paint(p, QRect(cellX(weekdayColumn), cellY(weekRow), d->cellW, d->cellH), "hoverHighlight");
}
QString elementId;
if (type & Today) {
elementId = "today";
} else if (type & Selected) {
elementId = "selected";
} else if (type & PublicHoliday) {
elementId = "red";
} else if (type & Holiday) {
elementId = "green";
} else {
return;
}
d->svg->paint(p, QRectF(cellX(weekdayColumn) - 1, cellY(weekRow) - 1, d->cellW + 1, d->cellH + 2), elementId);
}
void CalendarTable::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
// Draw weeks numbers column and day header
QRectF r = boundingRect();
d->svg->paint(p, QRectF(r.x() + d->centeringSpace, cellY(0), d->cellW,
cellY(DISPLAYED_WEEKS) - cellY(0) - d->cellSpace), "weeksColumn");
d->svg->paint(p, QRectF(r.x() + d->centeringSpace, r.y(),
cellX(d->daysInWeek) - r.x() - d->cellSpace - d->centeringSpace, d->headerHeight), "weekDayHeader");
QList<CalendarCellBorder> borders;
QList<CalendarCellBorder> hovers;
if (d->automaticUpdates) {
d->currentDate = QDate::currentDate();
}
//weekRow and weekDaycolumn of table are 0 indexed and are not equivalent to weekday or week
//numbers. In LTR mode we count/paint row and column from top-left corner, in RTL mode we
//count/paint from top-right corner, but we don't need to know as cellX() calculates the actual
//painting position for us depending on the mode.
for (int weekRow = 0; weekRow < DISPLAYED_WEEKS; weekRow++) {
for (int weekdayColumn = 0; weekdayColumn < d->daysInWeek; weekdayColumn++) {
int x = cellX(weekdayColumn);
int y = cellY(weekRow);
QRectF cellRect(x, y, d->cellW, d->cellH);
if (!cellRect.intersects(option->exposedRect)) {
continue;
}
QDate cellDate = d->dateFromRowColumn(weekRow, weekdayColumn);
CalendarTable::CellTypes type(CalendarTable::NoType);
// get cell info
const int cellDay = calendar()->day(cellDate);
const int julian = cellDate.toJulianDay();
// check what kind of cell we are
if (calendar()->month(cellDate) != d->selectedMonth) {
type |= CalendarTable::NotInCurrentMonth;
}
if (!calendar()->isValid(cellDate)) {
type |= CalendarTable::InvalidDate;
}
if (cellDate == d->currentDate) {
type |= CalendarTable::Today;
}
if (cellDate == date()) {
type |= CalendarTable::Selected;
}
foreach (int holidayUid, d->holidays.values(julian)) {
if (d->holidayIsDayOff(d->holidayEvents.value(holidayUid))) {
type |= CalendarTable::PublicHoliday;
} else {
type |= CalendarTable::Holiday;
}
}
if (d->events.contains(julian) || d->todos.contains(julian)) {
type |= CalendarTable::Event;
}
if (type != CalendarTable::NoType && type != CalendarTable::NotInCurrentMonth) {
borders.append(CalendarCellBorder(cellDay, weekRow, weekdayColumn, type, cellDate));
}
if (weekRow == d->hoverWeekRow && weekdayColumn == d->hoverWeekdayColumn) {
type |= CalendarTable::Hovered;
hovers.append(CalendarCellBorder(cellDay, weekRow, weekdayColumn, type, cellDate));
}
paintCell(p, cellDay, weekRow, weekdayColumn, type, cellDate);
// FIXME: modify svg to allow for a wider week number cell
// a temporary workaround is to paint only one week number (weekString) when the cell is small
// and both week numbers (accurateWeekString) when there is enough room
if (weekdayColumn == 0) {
QRectF cellRect(r.x() + d->centeringSpace, y, d->cellW, d->cellH);
p->setPen(Theme::defaultTheme()->color(Plasma::Theme::TextColor));
p->setFont(d->dateFont);
p->setOpacity(d->opacity);
QString weekString;
QString accurateWeekString;
if (calendar()->isValid(cellDate)) {
weekString = calendar()->formatDate(cellDate, KLocale::Week, KLocale::LongNumber);
accurateWeekString = weekString;
if (calendar()->dayOfWeek(cellDate) != Qt::Monday) {
QDate nextWeekDate = calendar()->addDays(cellDate, d->daysInWeek);
if (calendar()->isValid(nextWeekDate)) {
if (layoutDirection() == Qt::RightToLeft) {
accurateWeekString.prepend("/").prepend(calendar()->formatDate(nextWeekDate, KLocale::Week, KLocale::LongNumber));
} else {
accurateWeekString.append("/").append(calendar()->formatDate(nextWeekDate, KLocale::Week, KLocale::LongNumber));
}
}
// ensure that weekString is the week number that has the most amout of days in the row
QDate middleWeekDate = calendar()->addDays(cellDate, floor(static_cast<float>(d->daysInWeek / 2)));
if (calendar()->isValid(middleWeekDate)) {
QString middleWeekString = calendar()->formatDate(middleWeekDate, KLocale::Week, KLocale::LongNumber);
if (weekString != middleWeekString) {
weekString = middleWeekString;
}
}
}
}
QFontMetrics fontMetrics(d->dateFont);
if (fontMetrics.width(accurateWeekString) > d->cellW) {
p->drawText(cellRect, Qt::AlignCenter, weekString); //draw number
} else {
p->drawText(cellRect, Qt::AlignCenter, accurateWeekString); //draw number
}
p->setOpacity(1.0);
}
}
}
// Draw days
if (option->exposedRect.intersects(QRect(r.x(), r.y(), r.width(), d->headerHeight))) {
p->setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
int weekStartDay = calendar()->weekStartDay();
for (int i = 0; i < d->daysInWeek; i++){
int weekDay = ((i + weekStartDay - 1) % d->daysInWeek) + 1;
QString dayName = calendar()->weekDayName(weekDay, KCalendarSystem::ShortDayName);
p->setFont(d->weekDayFont);
p->drawText(QRectF(cellX(i), r.y(), d->cellW, d->headerHeight),
Qt::AlignCenter | Qt::AlignVCenter, dayName);
}
}
// Draw hovers
foreach (const CalendarCellBorder &border, hovers) {
p->save();
paintBorder(p, border.cell, border.week, border.weekDay, border.type, border.date);
p->restore();
}
// Draw borders
foreach (const CalendarCellBorder &border, borders) {
p->save();
paintBorder(p, border.cell, border.week, border.weekDay, border.type, border.date);
p->restore();
}
}
} //namespace Plasma
#include "calendartable.moc"
|