File indexing completed on 2025-04-27 04:37:42
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 // These aren't the strings you're looking for, move along. 0047 return QStyledItemDelegate::displayText(value, locale); 0048 } 0049 0050 void ConnectionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0051 { 0052 if (index.column() == RemoteDesktopsModel::Favorite) { 0053 QVariant value = index.data(Qt::CheckStateRole); 0054 if (value.isValid()) { 0055 Qt::CheckState checkState = static_cast<Qt::CheckState>(value.toInt()); 0056 QIcon favIcon = QIcon::fromTheme(QStringLiteral("bookmarks")); 0057 QIcon::Mode mode = (checkState == Qt::Checked) ? QIcon::Active : QIcon::Disabled; 0058 favIcon.paint(painter, option.rect, option.decorationAlignment, mode); 0059 } 0060 } else { 0061 QStyledItemDelegate::paint(painter, option, index); 0062 } 0063 } 0064 0065 QSize ConnectionDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 0066 { 0067 if (index.column() == RemoteDesktopsModel::Favorite) 0068 return QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall); 0069 return QStyledItemDelegate::sizeHint(option, index); 0070 }