Warning, file /pim/kalarm/src/alarmlistdelegate.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  alarmlistdelegate.cpp  -  handles editing and display of alarm list
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2007-2023 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "alarmlistdelegate.h"
0010 
0011 #include "functions.h"
0012 #include "resources/eventmodel.h"
0013 #include "resources/resourcedatamodelbase.h"
0014 
0015 #include <KColorScheme>
0016 
0017 #include <QPainter>
0018 
0019 
0020 void AlarmListDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
0021 {
0022     QStyleOptionViewItem opt = option;
0023     if (index.isValid())
0024     {
0025         if (opt.state & QStyle::State_Selected
0026         &&  !index.data(ResourceDataModelBase::EnabledRole).toBool())
0027         {
0028             // Make the text colour for selected disabled alarms
0029             // distinguishable from enabled alarms.
0030             KColorScheme::adjustForeground(opt.palette, KColorScheme::InactiveText, QPalette::HighlightedText, KColorScheme::Selection);
0031         }
0032         switch (index.column())
0033         {
0034             case AlarmListModel::TimeColumn:
0035             {
0036                 const QString str = index.data(ResourceDataModelBase::TimeDisplayRole).toString();
0037                 // Need to pad out spacing to align times without leading zeroes
0038                 const int i = str.indexOf(QLatin1Char('~'));    // look for indicator of a leading zero to be omitted
0039                 if (i >= 0)
0040                 {
0041                     painter->save();
0042                     opt.displayAlignment = Qt::AlignLeft;
0043                     const QVariant value = index.data(Qt::ForegroundRole);
0044                     if (value.isValid())
0045                         opt.palette.setColor(QPalette::Text, value.value<QColor>());
0046                     drawBackground(painter, opt, index);
0047                     const QString time = str.mid(i + 1);
0048                     QRect timeRect;
0049                     if (i > 0)
0050                     {
0051                         QString str0 = str;
0052                         str0[i] = QLatin1Char('0');
0053                         QRect displayRect = textRect(str0, painter, opt);
0054                         timeRect = textRect(time, painter, opt);
0055                         timeRect.moveRight(displayRect.right());
0056                         drawDisplay(painter, opt, displayRect, str.left(i));   // date
0057                     }
0058                     else
0059                         timeRect = textRect(time, painter, opt);
0060                     drawDisplay(painter, opt, timeRect, time);
0061                     painter->restore();
0062                     return;
0063                 }
0064                 break;
0065             }
0066             case AlarmListModel::ColourColumn:
0067             {
0068                 const KAEvent event = static_cast<const EventListModel*>(index.model())->event(index);
0069                 if (event.isValid())
0070                 {
0071                     if (event.commandError() == KAEvent::CmdErr::None)
0072                     {
0073                         if (event.actionTypes() & KAEvent::Action::Display)
0074                             opt.palette.setColor(QPalette::Highlight, event.bgColour());
0075                     }
0076                     else
0077                     {
0078                         opt.font.setBold(true);
0079                         opt.font.setStyleHint(QFont::Serif);
0080                         opt.font.setPointSize(opt.rect.height() - 2);
0081                     }
0082                 }
0083                 break;
0084             }
0085             default:
0086                 break;
0087         }
0088     }
0089     QItemDelegate::paint(painter, opt, index);
0090 }
0091 
0092 QSize AlarmListDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
0093 {
0094     if (index.isValid())
0095     {
0096         switch (index.column())
0097         {
0098             case AlarmListModel::ColourColumn:
0099             {
0100                 int h = option.fontMetrics.lineSpacing();
0101                 return {h * 3 / 4, h};
0102             }
0103         }
0104     }
0105     return QItemDelegate::sizeHint(option, index);
0106 }
0107 
0108 void AlarmListDelegate::edit(KAEvent& event, EventListView* view)
0109 {
0110     KAlarm::editAlarm(event, static_cast<AlarmListView*>(view));   // edit alarm (view-only mode if archived or read-only)
0111 }
0112 
0113 QRect AlarmListDelegate::textRect(const QString& text, QPainter* painter, const QStyleOptionViewItem& opt) const
0114 {
0115     QRect displayRect;
0116     QRect r = opt.rect;
0117     r.setWidth(INT_MAX/256);
0118     displayRect = textRectangle(painter, r, opt.font, text);
0119     displayRect = QStyle::alignedRect(opt.direction, opt.displayAlignment,
0120                                       displayRect.size().boundedTo(opt.rect.size()),
0121                                       opt.rect);
0122     return displayRect;
0123 }
0124 
0125 #include "moc_alarmlistdelegate.cpp"
0126 
0127 // vim: et sw=4: