Warning, file /network/krdc/connectiondelegate.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2009 Tony Murray <murraytony@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "connectiondelegate.h" 0008 #include "remotedesktopsmodel.h" 0009 0010 #include <KIconLoader> 0011 #include <KLocalizedString> 0012 0013 #include <QDateTime> 0014 #include <QIcon> 0015 0016 ConnectionDelegate::ConnectionDelegate(QObject *parent) : 0017 QStyledItemDelegate(parent) 0018 { 0019 } 0020 0021 QString ConnectionDelegate::displayText(const QVariant &value, const QLocale& locale) const 0022 { 0023 if (value.type() == QVariant::DateTime) { 0024 QDateTime lastConnected = QDateTime(value.toDateTime()); 0025 QDateTime currentTime = QDateTime::currentDateTimeUtc(); 0026 0027 int daysAgo = lastConnected.daysTo(currentTime); 0028 if (daysAgo <= 1 && lastConnected.secsTo(currentTime) < 86400) { 0029 int minutesAgo = lastConnected.secsTo(currentTime) / 60; 0030 int hoursAgo = minutesAgo / 60; 0031 if (hoursAgo < 1) { 0032 if (minutesAgo < 1) 0033 return i18n("Less than a minute ago"); 0034 return i18np("A minute ago", "%1 minutes ago", minutesAgo); 0035 } else { 0036 return i18np("An hour ago", "%1 hours ago", hoursAgo); 0037 } 0038 } else { // 1 day or more 0039 if (daysAgo < 30) 0040 return i18np("Yesterday", "%1 days ago", daysAgo); 0041 if (daysAgo < 365) 0042 return i18np("Over a month ago", "%1 months ago", daysAgo / 30); 0043 return i18np("A year ago", "%1 years ago", daysAgo / 365); 0044 } 0045 0046 } 0047 // These aren't the strings you're looking for, move along. 0048 return QStyledItemDelegate::displayText(value, locale); 0049 } 0050 0051 void ConnectionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0052 { 0053 if (index.column() == RemoteDesktopsModel::Favorite) { 0054 QVariant value = index.data(Qt::CheckStateRole); 0055 if (value.isValid()) { 0056 Qt::CheckState checkState = static_cast<Qt::CheckState>(value.toInt()); 0057 QIcon favIcon = QIcon::fromTheme(QStringLiteral("bookmarks")); 0058 QIcon::Mode mode = (checkState == Qt::Checked) ? QIcon::Active : QIcon::Disabled; 0059 favIcon.paint(painter, option.rect, option.decorationAlignment, mode); 0060 0061 } 0062 } else { 0063 QStyledItemDelegate::paint(painter, option, index); 0064 } 0065 } 0066 0067 QSize ConnectionDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 0068 { 0069 if (index.column() == RemoteDesktopsModel::Favorite) 0070 return QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall); 0071 return QStyledItemDelegate::sizeHint(option, index); 0072 }