monthitem.cpp 23.7 KB
Newer Older
1
/*
2
  SPDX-FileCopyrightText: 2008 Bruno Virlet <bruno.virlet@gmail.com>
3

4
  SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
*/

#include "monthitem.h"
#include "helper.h"
#include "monthgraphicsitems.h"
#include "monthscene.h"
#include "monthview.h"
#include "prefs.h"
#include "prefs_base.h" // Ugly, but needed for the Enums

#include <Akonadi/Calendar/IncidenceChanger>
#include <CalendarSupport/KCalPrefs>
#include <CalendarSupport/Utils>

#include <KCalUtils/IncidenceFormatter>
#include <KCalUtils/RecurrenceActions>

#include <KMessageBox>
#include "calendarview_debug.h"

using namespace EventViews;
26
using namespace KCalendarCore;
27 28

MonthItem::MonthItem(MonthScene *monthScene)
Laurent Montel's avatar
Laurent Montel committed
29
    : mMonthScene(monthScene)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
{
}

MonthItem::~MonthItem()
{
    deleteAll();
}

void MonthItem::deleteAll()
{
    qDeleteAll(mMonthGraphicsItemList);
    mMonthGraphicsItemList.clear();
}

QWidget *MonthItem::parentWidget() const
{
Laurent Montel's avatar
Laurent Montel committed
46
    return mMonthScene ? mMonthScene->monthView() : nullptr;
47 48 49 50 51 52 53 54 55 56 57 58 59 60
}

void MonthItem::updateMonthGraphicsItems()
{
    // Remove all items
    qDeleteAll(mMonthGraphicsItemList);
    mMonthGraphicsItemList.clear();

    const QDate monthStartDate = startDate();
    const QDate monthEndDate = endDate();

    // For each row of the month view, create an item to build the whole
    // MonthItem's MonthGraphicsItems.
    for (QDate d = mMonthScene->mMonthView->actualStartDateTime().date();
Laurent Montel's avatar
Laurent Montel committed
61
         d < mMonthScene->mMonthView->actualEndDateTime().date(); d = d.addDays(7)) {
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        QDate end = d.addDays(6);

        int span;
        QDate start;
        if (monthStartDate <= d && monthEndDate >= end) {   // MonthItem takes the whole line
            span = 6;
            start = d;
        } else if (monthStartDate >= d && monthEndDate <= end) {   // starts and ends on this line
            start = monthStartDate;
            span = daySpan();
        } else if (d <= monthEndDate && monthEndDate <= end) {   // MonthItem ends on this line
            span = mMonthScene->getLeftSpan(monthEndDate);
            start = d;
        } else if (d <= monthStartDate && monthStartDate <= end) {   // MonthItem begins on this line
            span = mMonthScene->getRightSpan(monthStartDate);
            start = monthStartDate;
        } else { // MonthItem is not on the line
            continue;
        }

        // A new item needs to be created
Laurent Montel's avatar
Laurent Montel committed
83
        auto *newItem = new MonthGraphicsItem(this);
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
        mMonthGraphicsItemList << newItem;
        newItem->setStartDate(start);
        newItem->setDaySpan(span);
    }

    if (isMoving() || isResizing()) {
        setZValue(100);
    } else {
        setZValue(0);
    }
}

void MonthItem::beginResize()
{
    mOverrideDaySpan = daySpan();
    mOverrideStartDate = startDate();
    mResizing = true;
    setZValue(100);
}

void MonthItem::endResize()
{
    setZValue(0);
    mResizing = false; // startDate() and daySpan() return real values again

    if (mOverrideStartDate != startDate() || mOverrideDaySpan != daySpan()) {
        finalizeResize(mOverrideStartDate, mOverrideStartDate.addDays(mOverrideDaySpan));
    }
}

void MonthItem::beginMove()
{
    mOverrideDaySpan = daySpan();
    mOverrideStartDate = startDate();
    mMoving = true;
    setZValue(100);
}

void MonthItem::endMove()
{
    setZValue(0);
    mMoving = false; // startDate() and daySpan() return real values again

    if (mOverrideStartDate != startDate()) {
        finalizeMove(mOverrideStartDate);
    }
}

bool MonthItem::resizeBy(int offsetToPreviousDate)
{
    bool ret = false;
    if (mMonthScene->resizeType() == MonthScene::ResizeLeft) {
        if (mOverrideDaySpan - offsetToPreviousDate >= 0) {
            mOverrideStartDate = mOverrideStartDate.addDays(offsetToPreviousDate);
            mOverrideDaySpan = mOverrideDaySpan - offsetToPreviousDate;
            ret = true;
        }
    } else if (mMonthScene->resizeType() == MonthScene::ResizeRight) {
        if (mOverrideDaySpan + offsetToPreviousDate >= 0) {
            mOverrideDaySpan = mOverrideDaySpan + offsetToPreviousDate;
            ret = true;
        }
    }

    if (ret) {
        updateMonthGraphicsItems();
    }
    return ret;
}

void MonthItem::moveBy(int offsetToPreviousDate)
{
    mOverrideStartDate = mOverrideStartDate.addDays(offsetToPreviousDate);
    updateMonthGraphicsItems();
}

void MonthItem::updateGeometry()
{
162
    for (MonthGraphicsItem *item : qAsConst(mMonthGraphicsItemList)) {
163 164 165 166 167 168
        item->updateGeometry();
    }
}

void MonthItem::setZValue(qreal z)
{
169
    for (MonthGraphicsItem *item : qAsConst(mMonthGraphicsItemList)) {
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
        item->setZValue(z);
    }
}

QDate MonthItem::startDate() const
{
    if (isMoving() || isResizing()) {
        return mOverrideStartDate;
    }

    return realStartDate();
}

QDate MonthItem::endDate() const
{
    if (isMoving() || isResizing()) {
        return mOverrideStartDate.addDays(mOverrideDaySpan);
    }

    return realEndDate();
}

int MonthItem::daySpan() const
{
    if (isMoving() || isResizing()) {
        return mOverrideDaySpan;
    }
197
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
198 199
    QDateTime start(startDate());
    QDateTime end(endDate());
200 201 202 203
#else
    QDateTime start(startDate().startOfDay());
    QDateTime end(endDate().startOfDay());
#endif
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

    if (start.isValid() && end.isValid()) {
        return start.daysTo(end);
    }

    return 0;
}

bool MonthItem::greaterThan(const MonthItem *e1, const MonthItem *e2)
{
    const QDate leftStartDate = e1->startDate();
    const QDate rightStartDate = e2->startDate();

    if (!leftStartDate.isValid() || !rightStartDate.isValid()) {
        return false;
    }

    if (leftStartDate == rightStartDate) {
        const int leftDaySpan = e1->daySpan();
        const int rightDaySpan = e2->daySpan();
        if (leftDaySpan == rightDaySpan) {
            if (e1->allDay() && !e2->allDay()) {
                return true;
            }
            if (!e1->allDay() && e2->allDay()) {
                return false;
            }
            return e1->greaterThanFallback(e2);
        } else {
Laurent Montel's avatar
Laurent Montel committed
233
            return leftDaySpan > rightDaySpan;
234 235 236
        }
    }

237
    return leftStartDate > rightStartDate;
238 239 240 241
}

bool MonthItem::greaterThanFallback(const MonthItem *other) const
{
Laurent Montel's avatar
Laurent Montel committed
242
    const auto *h = qobject_cast<const HolidayMonthItem *>(other);
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

    // If "other" is a holiday, display it first.
    return !h;
}

void MonthItem::updatePosition()
{
    if (!startDate().isValid() || !endDate().isValid()) {
        return;
    }

    int firstFreeSpace = 0;
    for (QDate d = startDate(); d <= endDate(); d = d.addDays(1)) {
        MonthCell *cell = mMonthScene->mMonthCellMap.value(d);
        if (!cell) {
            continue; // cell can be null if the item begins outside the month
        }
        int firstFreeSpaceTmp = cell->firstFreeSpace();
        if (firstFreeSpaceTmp > firstFreeSpace) {
            firstFreeSpace = firstFreeSpaceTmp;
        }
    }

    for (QDate d = startDate(); d <= endDate(); d = d.addDays(1)) {
        MonthCell *cell = mMonthScene->mMonthCellMap.value(d);
        if (!cell) {
            continue;
        }
        cell->addMonthItem(this, firstFreeSpace);
    }

    mPosition = firstFreeSpace;
}

QList<MonthGraphicsItem *> EventViews::MonthItem::monthGraphicsItems() const
{
    return mMonthGraphicsItemList;
}

//-----------------------------------------------------------------
// INCIDENCEMONTHITEM
284
IncidenceMonthItem::IncidenceMonthItem(MonthScene *monthScene, const Akonadi::ETMCalendar::Ptr &calendar, const Akonadi::Item &aitem, const KCalendarCore::Incidence::Ptr &incidence,
285
                                       const QDate &recurStartDate)
Laurent Montel's avatar
Laurent Montel committed
286 287 288 289
    : MonthItem(monthScene)
    , mCalendar(calendar)
    , mIncidence(incidence)
    , mAkonadiItemId(aitem.id())
290 291 292 293 294
{
    mIsEvent = CalendarSupport::hasEvent(aitem);
    mIsJournal = CalendarSupport::hasJournal(aitem);
    mIsTodo = CalendarSupport::hasTodo(aitem);

295
    KCalendarCore::Incidence::Ptr inc = mIncidence;
Laurent Montel's avatar
Laurent Montel committed
296 297
    if (inc->customProperty("KABC", "BIRTHDAY") == QLatin1String("YES")
        || inc->customProperty("KABC", "ANNIVERSARY") == QLatin1String("YES")) {
298 299
        const int years = EventViews::yearDiff(inc->dtStart().date(), recurStartDate);
        if (years > 0) {
300
            inc = KCalendarCore::Incidence::Ptr(inc->clone());
301 302 303 304 305 306 307 308 309 310 311 312
            inc->setReadOnly(false);
            inc->setDescription(i18np("%2 1 year", "%2 %1 years", years, i18n("Age:")));
            inc->setReadOnly(true);
            mIncidence = inc;
        }
    }

    connect(monthScene, &MonthScene::incidenceSelected,
            this, &IncidenceMonthItem::updateSelection);

    // first set to 0, because it's used in startDate()
    mRecurDayOffset = 0;
Laurent Montel's avatar
Laurent Montel committed
313 314
    if ((mIncidence->recurs() || mIncidence->recurrenceId().isValid())
        && startDate().isValid() && recurStartDate.isValid()) {
315 316 317 318 319 320 321 322 323 324
        mRecurDayOffset = startDate().daysTo(recurStartDate);
    }
}

IncidenceMonthItem::~IncidenceMonthItem()
{
}

bool IncidenceMonthItem::greaterThanFallback(const MonthItem *other) const
{
Laurent Montel's avatar
Laurent Montel committed
325
    const auto *o = qobject_cast<const IncidenceMonthItem *>(other);
326 327 328 329 330 331 332
    if (!o) {
        return MonthItem::greaterThanFallback(other);
    }

    if (allDay() != o->allDay()) {
        return allDay();
    }
333
    const KCalendarCore::Incidence::Ptr otherIncidence = o->mIncidence;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

    if (mIncidence->dtStart().time() != otherIncidence->dtStart().time()) {
        return mIncidence->dtStart().time() < otherIncidence->dtStart().time();
    }

    // as a last resort, compare uids
    return mIncidence->uid() < otherIncidence->uid();
}

QDate IncidenceMonthItem::realStartDate() const
{
    if (!mIncidence) {
        return QDate();
    }

Daniel Vrátil's avatar
Daniel Vrátil committed
349 350
    const QDateTime dt = mIncidence->dateTime(Incidence::RoleDisplayStart);
    const QDate start = dt.toLocalTime().date();
351 352 353

    return start.addDays(mRecurDayOffset);
}
Laurent Montel's avatar
Laurent Montel committed
354

355 356 357 358 359 360
QDate IncidenceMonthItem::realEndDate() const
{
    if (!mIncidence) {
        return QDate();
    }

361
    const QDateTime dt = mIncidence->dateTime(KCalendarCore::Incidence::RoleDisplayEnd);
Daniel Vrátil's avatar
Daniel Vrátil committed
362
    const QDate end = dt.toLocalTime().date();
363 364 365

    return end.addDays(mRecurDayOffset);
}
Laurent Montel's avatar
Laurent Montel committed
366

367 368 369 370 371 372 373 374
bool IncidenceMonthItem::allDay() const
{
    return mIncidence->allDay();
}

bool IncidenceMonthItem::isMoveable() const
{
    return monthScene()->mMonthView->calendar()->hasRight(akonadiItem(),
Laurent Montel's avatar
Laurent Montel committed
375
                                                          Akonadi::Collection::CanChangeItem);
376
}
Laurent Montel's avatar
Laurent Montel committed
377

378 379
bool IncidenceMonthItem::isResizable() const
{
Laurent Montel's avatar
Laurent Montel committed
380 381 382
    return mIsEvent && monthScene()->mMonthView->calendar()->hasRight(
        akonadiItem(),
        Akonadi::Collection::CanChangeItem);
383 384 385 386 387 388 389 390 391 392 393
}

void IncidenceMonthItem::finalizeMove(const QDate &newStartDate)
{
    Q_ASSERT(isMoveable());

    if (startDate().isValid() && newStartDate.isValid()) {
        updateDates(startDate().daysTo(newStartDate),
                    startDate().daysTo(newStartDate));
    }
}
Laurent Montel's avatar
Laurent Montel committed
394 395

void IncidenceMonthItem::finalizeResize(const QDate &newStartDate, const QDate &newEndDate)
396 397 398
{
    Q_ASSERT(isResizable());

Laurent Montel's avatar
Laurent Montel committed
399 400
    if (startDate().isValid() && endDate().isValid()
        && newStartDate.isValid() && newEndDate.isValid()) {
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
        updateDates(startDate().daysTo(newStartDate),
                    endDate().daysTo(newEndDate));
    }
}

void IncidenceMonthItem::updateDates(int startOffset, int endOffset)
{
    Akonadi::IncidenceChanger *changer = monthScene()->incidenceChanger();
    if (!changer || (startOffset == 0 && endOffset == 0)) {
        qCDebug(CALENDARVIEW_LOG) << changer << startOffset << endOffset;
        return;
    }

    Akonadi::Item item = akonadiItem();
    item.setPayload(mIncidence);
    if (mIncidence->recurs()) {
        const int res = monthScene()->mMonthView->showMoveRecurDialog(mIncidence, startDate());
        switch (res) {
Laurent Montel's avatar
Laurent Montel committed
419 420
        case KCalUtils::RecurrenceActions::AllOccurrences:
        {
421
            // All occurrences
422
            KCalendarCore::Incidence::Ptr oldIncidence(mIncidence->clone());
423 424 425 426 427
            setNewDates(mIncidence, startOffset, endOffset);
            changer->modifyIncidence(item, oldIncidence);
            break;
        }
        case KCalUtils::RecurrenceActions::SelectedOccurrence: // Just this occurrence
Laurent Montel's avatar
Laurent Montel committed
428 429
        case KCalUtils::RecurrenceActions::FutureOccurrences:
        {                                                       // All future occurrences
430
            const bool thisAndFuture = (res == KCalUtils::RecurrenceActions::FutureOccurrences);
Daniel Vrátil's avatar
Daniel Vrátil committed
431
            QDateTime occurrenceDate(mIncidence->dtStart());
432
            occurrenceDate.setDate(startDate());
433
            KCalendarCore::Incidence::Ptr newIncidence(KCalendarCore::Calendar::createException(
Laurent Montel's avatar
Laurent Montel committed
434
                                                      mIncidence, occurrenceDate, thisAndFuture));
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
            if (newIncidence) {
                changer->startAtomicOperation(i18n("Move occurrence(s)"));
                setNewDates(newIncidence, startOffset, endOffset);
                changer->createIncidence(newIncidence, item.parentCollection(), parentWidget());
                changer->endAtomicOperation();
            } else {
                KMessageBox::sorry(
                    parentWidget(),
                    i18n("Unable to add the exception item to the calendar. "
                         "No change will be done."),
                    i18n("Error Occurred"));
            }
            break;
        }
        }
    } else { // Doesn't recur
451
        KCalendarCore::Incidence::Ptr oldIncidence(mIncidence->clone());
452 453 454 455 456 457 458 459 460 461 462 463 464 465
        setNewDates(mIncidence, startOffset, endOffset);
        changer->modifyIncidence(item, oldIncidence);
    }
}

void IncidenceMonthItem::updateSelection(const Akonadi::Item &incidence, const QDate &date)
{
    Q_UNUSED(date);
    setSelected(incidence == akonadiItem());
}

QString IncidenceMonthItem::text(bool end) const
{
    QString ret = mIncidence->summary();
Laurent Montel's avatar
Laurent Montel committed
466 467
    if (!allDay() && !mIsJournal
        && monthScene()->monthView()->preferences()->showTimeInMonthView()) {
468 469 470
        // Prepend the time str to the text
        QString timeStr;
        if (mIsTodo) {
471
            KCalendarCore::Todo::Ptr todo = mIncidence.staticCast<Todo>();
Daniel Vrátil's avatar
Daniel Vrátil committed
472
            timeStr = QLocale().toString(todo->dtDue().toLocalTime().time(), QLocale::ShortFormat);
473 474
        } else {
            if (!end) {
475 476 477
                QTime time;
                if (mIncidence->recurs()) {
                    const auto start = mIncidence->dtStart().addDays(mRecurDayOffset).addSecs(-1);
Laurent Montel's avatar
Laurent Montel committed
478
                    time = mIncidence->recurrence()->getNextDateTime(start).toLocalTime().time();
479
                } else {
Daniel Vrátil's avatar
Daniel Vrátil committed
480
                    time = mIncidence->dtStart().toLocalTime().time();
481 482
                }
                timeStr = QLocale().toString(time, QLocale::ShortFormat);
483
            } else {
484
                KCalendarCore::Event::Ptr event = mIncidence.staticCast<Event>();
Laurent Montel's avatar
Laurent Montel committed
485 486
                timeStr = QLocale().toString(
                    event->dtEnd().toLocalTime().time(), QLocale::ShortFormat);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
            }
        }
        if (!timeStr.isEmpty()) {
            if (!end) {
                ret = timeStr + QLatin1Char(' ') + ret;
            } else {
                ret = ret + QLatin1Char(' ') + timeStr;
            }
        }
    }

    return ret;
}

QString IncidenceMonthItem::toolTipText(const QDate &date) const
{
    return KCalUtils::IncidenceFormatter::toolTipStr(
Laurent Montel's avatar
Laurent Montel committed
504 505
        CalendarSupport::displayName(mCalendar.data(), akonadiItem().parentCollection()),
        mIncidence, date, true);
506 507 508 509 510 511 512 513 514 515 516 517 518
}

QVector<QPixmap> IncidenceMonthItem::icons() const
{
    QVector<QPixmap> ret;

    if (!mIncidence) {
        return ret;
    }

    bool specialEvent = false;
    Akonadi::Item item = akonadiItem();

Laurent Montel's avatar
Laurent Montel committed
519 520
    const QSet<EventView::ItemIcon> icons
        = monthScene()->monthView()->preferences()->monthViewIcons();
521 522 523 524

    QString customIconName;
    if (icons.contains(EventViews::EventView::CalendarCustomIcon)) {
        const QString iconName = monthScene()->monthView()->iconForItem(item);
Laurent Montel's avatar
Laurent Montel committed
525 526
        if (!iconName.isEmpty() && iconName != QLatin1String("view-calendar")
            && iconName != QLatin1String("office-calendar")) {
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
            customIconName = iconName;
            ret << QPixmap(cachedSmallIcon(iconName));
        }
    }

    if (mIsEvent) {
        if (mIncidence->customProperty("KABC", "ANNIVERSARY") == QLatin1String("YES")) {
            specialEvent = true;
            ret << monthScene()->anniversaryPixmap();
        } else if (mIncidence->customProperty("KABC", "BIRTHDAY") == QLatin1String("YES")) {
            specialEvent = true;
            // Disabling birthday icon because it's the birthday agent's icon
            // and we allow to display the agent's icon now.
            //ret << monthScene()->birthdayPixmap();
        }

        // smartins: Disabling the event Pixmap because:
        // 1. Save precious space so we can read the event's title better.
        // 2. We don't need a pixmap to tell us an item is an event we
        //    only need one to tell us it's not, as month view was designed for events.
        // 3. If only to-dos and journals have a pixmap they will be distinguished
        //    from event's much easier.

        // ret << monthScene()->eventPixmap();
Laurent Montel's avatar
Laurent Montel committed
551 552 553
    } else if ((mIsTodo || mIsJournal) && icons.contains(mIsTodo
                                                         ? EventView::TaskIcon
                                                         : EventView::JournalIcon)) {
Daniel Vrátil's avatar
Daniel Vrátil committed
554
        QDateTime occurrenceDateTime = mIncidence->dateTime(Incidence::RoleRecurrenceStart);
555 556 557 558 559 560 561 562
        occurrenceDateTime.setDate(realStartDate());

        const QString incidenceIconName = mIncidence->iconName(occurrenceDateTime);
        if (customIconName != incidenceIconName) {
            ret << QPixmap(cachedSmallIcon(incidenceIconName));
        }
    }

Laurent Montel's avatar
Laurent Montel committed
563 564 565
    if (icons.contains(EventView::ReadOnlyIcon)
        && !monthScene()->mMonthView->calendar()->hasRight(item, Akonadi::Collection::CanChangeItem)
        && !specialEvent) {
566 567 568 569 570
        ret << monthScene()->readonlyPixmap();
    }

    /* sorry, this looks too cluttered. disable until we can
       make something prettier; no idea at this time -- allen */
Laurent Montel's avatar
Laurent Montel committed
571 572
    if (icons.contains(EventView::ReminderIcon)
        && mIncidence->hasEnabledAlarms() && !specialEvent) {
573 574
        ret << monthScene()->alarmPixmap();
    }
Laurent Montel's avatar
Laurent Montel committed
575 576
    if (icons.contains(EventView::RecurringIcon)
        && mIncidence->recurs() && !specialEvent) {
577 578 579 580 581 582 583 584 585 586
        ret << monthScene()->recurPixmap();
    }
    //TODO: check what to do with Reply

    return ret;
}

QColor IncidenceMonthItem::catColor() const
{
    Q_ASSERT(mIncidence);
587
    const auto &prefs = monthScene()->monthView()->preferences();
588

589 590 591 592 593 594 595 596 597
    const auto &categories = mIncidence->categories();
    if (categories.isEmpty() || !CalendarSupport::KCalPrefs::instance()->hasCategoryColor(categories.first())) {
        const auto &colorPreference = prefs->monthViewColors();
        if (colorPreference == PrefsBase::CategoryOnly) {
            return CalendarSupport::KCalPrefs::instance()->unsetCategoryColor();
        }
        return EventViews::resourceColor(akonadiItem(), prefs);
    }
    return CalendarSupport::KCalPrefs::instance()->categoryColor(categories.first());
598 599 600 601
}

QColor IncidenceMonthItem::bgColor() const
{
602
    const auto &prefs = monthScene()->monthView()->preferences();
603

604
    if (!prefs->todosUseCategoryColors() && mIsTodo) {
605 606 607
        Todo::Ptr todo = CalendarSupport::todo(akonadiItem());
        Q_ASSERT(todo);
        if (todo) {
608 609 610 611 612 613 614 615 616 617
            // this is dtDue if there's no dtRecurrence
            const auto dtRecurrence = todo->dtRecurrence().toLocalTime().date();
            const auto today = QDate::currentDate();
            if (startDate() >= dtRecurrence) {
                if (todo->isOverdue() && today > startDate()) {
                    return prefs->todoOverdueColor();
                }
                if (today == startDate() && !todo->isCompleted()) {
                    return prefs->todoDueTodayColor();
                }
618 619 620 621
            }
        }
    }

622 623 624 625
    const auto &colorPreference = prefs->monthViewColors();
    const auto bgDisplaysResource = colorPreference == PrefsBase::MonthItemResourceInsideCategoryOutside
                                    || colorPreference == PrefsBase::MonthItemResourceOnly;
    return bgDisplaysResource ? EventViews::resourceColor(akonadiItem(), prefs) : catColor();
626 627 628 629
}

QColor IncidenceMonthItem::frameColor() const
{
630 631 632 633 634
    const auto &prefs = monthScene()->monthView()->preferences();
    const auto frameDisplaysResource =
        (prefs->monthViewColors() == PrefsBase::MonthItemResourceOnly
         || prefs->monthViewColors() == PrefsBase::MonthItemCategoryInsideResourceOutside);
    const auto frameColor = frameDisplaysResource ? EventViews::resourceColor(akonadiItem(), prefs) : catColor();
635 636 637 638 639 640 641 642 643 644 645 646
    return EventView::itemFrameColor(frameColor, selected());
}

Akonadi::Item IncidenceMonthItem::akonadiItem() const
{
    if (mIncidence) {
        return monthScene()->mMonthView->calendar()->item(mIncidence);
    } else {
        return Akonadi::Item();
    }
}

647
KCalendarCore::Incidence::Ptr IncidenceMonthItem::incidence() const
648 649 650 651 652 653 654 655 656
{
    return mIncidence;
}

Akonadi::Item::Id IncidenceMonthItem::akonadiItemId() const
{
    return mAkonadiItemId;
}

657
void IncidenceMonthItem::setNewDates(const KCalendarCore::Incidence::Ptr &incidence, int startOffset, int endOffset)
658 659 660 661 662 663 664
{
    if (mIsTodo) {
        // For to-dos endOffset is ignored because it will always be == to startOffset because we only
        // support moving to-dos, not resizing them. There are no multi-day to-dos.
        // Lets just call it offset to reduce confusion.
        const int offset = startOffset;

665
        KCalendarCore::Todo::Ptr todo = incidence.staticCast<Todo>();
Laurent Montel's avatar
Laurent Montel committed
666
        QDateTime due = todo->dtDue();
Daniel Vrátil's avatar
Daniel Vrátil committed
667
        QDateTime start = todo->dtStart();
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
        if (due.isValid()) {   // Due has priority over start.
            // We will only move the due date, unlike events where we move both.
            due = due.addDays(offset);
            todo->setDtDue(due);

            if (start.isValid() && start > due) {
                // Start can't be bigger than due.
                todo->setDtStart(due);
            }
        } else if (start.isValid()) {
            // So we're displaying a to-do that doesn't have due date, only start...
            start = start.addDays(offset);
            todo->setDtStart(start);
        } else {
            // This never happens
Laurent Montel's avatar
Laurent Montel committed
683 684
            qCWarning(CALENDARVIEW_LOG) << "Move what? uid:" << todo->uid() << "; summary="
                                        << todo->summary();
685 686 687 688
        }
    } else {
        incidence->setDtStart(incidence->dtStart().addDays(startOffset));
        if (mIsEvent) {
689
            KCalendarCore::Event::Ptr event = incidence.staticCast<Event>();
690 691 692 693 694 695 696
            event->setDtEnd(event->dtEnd().addDays(endOffset));
        }
    }
}

//-----------------------------------------------------------------
// HOLIDAYMONTHITEM
Laurent Montel's avatar
Laurent Montel committed
697 698 699 700
HolidayMonthItem::HolidayMonthItem(MonthScene *monthScene, const QDate &date, const QString &name)
    : MonthItem(monthScene)
    , mDate(date)
    , mName(name)
701 702 703 704 705 706 707 708 709
{
}

HolidayMonthItem::~HolidayMonthItem()
{
}

bool HolidayMonthItem::greaterThanFallback(const MonthItem *other) const
{
Laurent Montel's avatar
Laurent Montel committed
710
    const auto *o = qobject_cast<const HolidayMonthItem *>(other);
711 712 713 714 715 716 717 718 719 720 721 722 723
    if (o) {
        return MonthItem::greaterThanFallback(other);
    }

    // always put holidays on top
    return false;
}

void HolidayMonthItem::finalizeMove(const QDate &newStartDate)
{
    Q_UNUSED(newStartDate);
    Q_ASSERT(false);
}
Laurent Montel's avatar
Laurent Montel committed
724 725

void HolidayMonthItem::finalizeResize(const QDate &newStartDate, const QDate &newEndDate)
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
{
    Q_UNUSED(newStartDate);
    Q_UNUSED(newEndDate);
    Q_ASSERT(false);
}

QVector<QPixmap> HolidayMonthItem::icons() const
{
    QVector<QPixmap> ret;
    ret << monthScene()->holidayPixmap();

    return ret;
}

QColor HolidayMonthItem::bgColor() const
{
    // FIXME: Currently, only this value is settable in the options.
    // There is a monthHolidaysBackgroundColor() option too. Maybe it would be
    // wise to merge those two.
    return monthScene()->monthView()->preferences()->agendaHolidaysBackgroundColor();
}

QColor HolidayMonthItem::frameColor() const
{
    return Qt::black;
}