File indexing completed on 2024-04-21 04:58:16

0001 /* This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2009 Pino Toscano <pino@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "konqhistoryproxymodel.h"
0008 
0009 #include "konqhistory.h"
0010 #include "konqhistorysettings.h"
0011 #include <QDateTime>
0012 
0013 KonqHistoryProxyModel::KonqHistoryProxyModel(KonqHistorySettings *settings, QObject *parent)
0014     : KSortFilterProxyModel(parent)
0015     , m_settings(settings)
0016 {
0017     setDynamicSortFilter(true);
0018     setFilterCaseSensitivity(Qt::CaseInsensitive);
0019 
0020     connect(m_settings, &KonqHistorySettings::settingsChanged, this, &KonqHistoryProxyModel::slotSettingsChanged);
0021 }
0022 
0023 KonqHistoryProxyModel::~KonqHistoryProxyModel()
0024 {
0025 }
0026 
0027 QVariant KonqHistoryProxyModel::data(const QModelIndex &index, int role) const
0028 {
0029     if (!sourceModel()) {
0030         return QVariant();
0031     }
0032 
0033     const QModelIndex source_index = mapToSource(index);
0034     QVariant res;
0035     switch (source_index.data(KonqHistory::TypeRole).toInt()) {
0036     case KonqHistory::HistoryType:
0037         switch (role) {
0038         case Qt::ToolTipRole:
0039             if (m_settings->m_detailedTips) {
0040                 res = sourceModel()->data(source_index, KonqHistory::DetailedToolTipRole);
0041             } else {
0042                 res = sourceModel()->data(source_index, Qt::ToolTipRole);
0043             }
0044             break;
0045         case Qt::FontRole: {
0046             const QDateTime current = QDateTime::currentDateTime();
0047             const QDateTime entryDate = source_index.data(KonqHistory::LastVisitedRole).toDateTime();
0048 
0049             QDateTime dt;
0050             if (m_settings->m_metricYoungerThan == KonqHistorySettings::DAYS) {
0051                 dt = current.addDays(-int(m_settings->m_valueYoungerThan));
0052             } else {
0053                 dt = current.addSecs(-(int(m_settings->m_valueYoungerThan) * 60));
0054             }
0055 
0056             if (entryDate > dt) {
0057                 res = QVariant::fromValue(m_settings->m_fontYoungerThan);
0058             } else {
0059                 if (m_settings->m_metricOlderThan == KonqHistorySettings::DAYS) {
0060                     dt = current.addDays(-int(m_settings->m_valueOlderThan));
0061                 } else {
0062                     dt = current.addSecs(-(int(m_settings->m_valueOlderThan) * 60));
0063                 }
0064                 if (entryDate < dt) {
0065                     res = QVariant::fromValue(m_settings->m_fontOlderThan);
0066                 }
0067             }
0068             break;
0069         }
0070         }
0071         break;
0072     case KonqHistory::GroupType:
0073         break;
0074     }
0075     if (res.isNull()) {
0076         res = QSortFilterProxyModel::data(index, role);
0077     }
0078     return res;
0079 }
0080 
0081 bool KonqHistoryProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0082 {
0083     switch (left.data(KonqHistory::TypeRole).toInt()) {
0084     case KonqHistory::HistoryType:
0085         Q_ASSERT(right.data(KonqHistory::TypeRole).toInt() == KonqHistory::HistoryType);
0086         if (m_settings->m_sortsByName) {
0087             return left.data().toString() < right.data().toString();
0088         } else {
0089             return left.data(KonqHistory::LastVisitedRole).toDateTime() > right.data(KonqHistory::LastVisitedRole).toDateTime();
0090         }
0091     case KonqHistory::GroupType:
0092         Q_ASSERT(right.data(KonqHistory::TypeRole).toInt() == KonqHistory::GroupType);
0093         if (m_settings->m_sortsByName) {
0094             return left.data().toString() < right.data().toString();
0095         } else {
0096             return left.data(KonqHistory::LastVisitedRole).toDateTime() > right.data(KonqHistory::LastVisitedRole).toDateTime();
0097         }
0098     }
0099     return QSortFilterProxyModel::lessThan(left, right);
0100 }
0101 
0102 void KonqHistoryProxyModel::slotSettingsChanged()
0103 {
0104     beginResetModel();
0105     endResetModel();
0106 }
0107