File indexing completed on 2024-04-28 05:11:32

0001 /*
0002   SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
0003   SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "freebusyganttproxymodel.h"
0009 #include <CalendarSupport/FreeBusyItemModel>
0010 
0011 #include <KCalendarCore/FreeBusyPeriod>
0012 #include <KGanttGraphicsView>
0013 
0014 #include <KLocalizedString>
0015 
0016 #include <QLocale>
0017 
0018 using namespace IncidenceEditorNG;
0019 
0020 FreeBusyGanttProxyModel::FreeBusyGanttProxyModel(QObject *parent)
0021     : QSortFilterProxyModel(parent)
0022 {
0023 }
0024 
0025 QVariant FreeBusyGanttProxyModel::data(const QModelIndex &index, int role) const
0026 {
0027     if (!index.isValid()) {
0028         return {};
0029     }
0030     QModelIndex source_index = mapToSource(index);
0031 
0032     // if the index is not valid, then its a toplevel item, which is an attendee
0033     if (!source_index.parent().isValid()) {
0034         switch (role) {
0035         case KGantt::ItemTypeRole:
0036             return KGantt::TypeMulti;
0037         case Qt::DisplayRole:
0038             return source_index.data(Qt::DisplayRole);
0039         default:
0040             return {};
0041         }
0042     }
0043 
0044     // if the index is valid, then it corresponds to a free busy period
0045     auto period = sourceModel()->data(source_index, CalendarSupport::FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>();
0046 
0047     switch (role) {
0048     case KGantt::ItemTypeRole:
0049         return KGantt::TypeTask;
0050     case KGantt::StartTimeRole:
0051         return period.start().toLocalTime();
0052     case KGantt::EndTimeRole:
0053         return period.end().toLocalTime();
0054     case Qt::BackgroundRole:
0055         return QColor(Qt::red);
0056     case Qt::ToolTipRole:
0057         return tooltipify(period);
0058     case Qt::DisplayRole:
0059         return sourceModel()->data(source_index.parent(), Qt::DisplayRole);
0060     default:
0061         return {};
0062     }
0063 }
0064 
0065 QString FreeBusyGanttProxyModel::tooltipify(const KCalendarCore::FreeBusyPeriod &period) const
0066 {
0067     QString toolTip = QStringLiteral("<qt>");
0068     toolTip += QLatin1StringView("<b>") + i18nc("@info:tooltip", "Free/Busy Period") + QLatin1StringView("</b>");
0069     toolTip += QStringLiteral("<hr>");
0070     if (!period.summary().isEmpty()) {
0071         toolTip += QLatin1StringView("<i>") + i18nc("@info:tooltip", "Summary:") + QLatin1StringView("</i>") + QStringLiteral("&nbsp;");
0072         toolTip += period.summary();
0073         toolTip += QStringLiteral("<br>");
0074     }
0075     if (!period.location().isEmpty()) {
0076         toolTip += QLatin1StringView("<i>") + i18nc("@info:tooltip", "Location:") + QLatin1StringView("</i>") + QStringLiteral("&nbsp;");
0077         toolTip += period.location();
0078         toolTip += QStringLiteral("<br>");
0079     }
0080     toolTip += QStringLiteral("<i>") + i18nc("@info:tooltip period start time", "Start:") + QStringLiteral("</i>") + QStringLiteral("&nbsp;");
0081     toolTip += QLocale().toString(period.start().toLocalTime(), QLocale::ShortFormat);
0082     toolTip += QStringLiteral("<br>");
0083     toolTip += QStringLiteral("<i>") + i18nc("@info:tooltip period end time", "End:") + QStringLiteral("</i>") + QStringLiteral("&nbsp;");
0084     toolTip += QLocale().toString(period.end().toLocalTime(), QLocale::ShortFormat);
0085     toolTip += QStringLiteral("<br>");
0086     toolTip += QStringLiteral("</qt>");
0087     return toolTip;
0088 }
0089 
0090 #include "moc_freebusyganttproxymodel.cpp"