File indexing completed on 2024-05-05 05:51:27

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2010 Thomas Fjellstrom <thomas@fjellstrom.ca>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "katefiletreeproxymodel.h"
0008 #include "katefiletreedebug.h"
0009 #include "katefiletreemodel.h"
0010 
0011 #include <QCollator>
0012 #include <ktexteditor/document.h>
0013 
0014 KateFileTreeProxyModel::KateFileTreeProxyModel(QObject *parent)
0015     : QSortFilterProxyModel(parent)
0016 {
0017 }
0018 
0019 void KateFileTreeProxyModel::setSourceModel(QAbstractItemModel *model)
0020 {
0021     Q_ASSERT(qobject_cast<KateFileTreeModel *>(model)); // we don't really work with anything else
0022     QSortFilterProxyModel::setSourceModel(model);
0023 }
0024 
0025 KTextEditor::Document *KateFileTreeProxyModel::docFromIndex(const QModelIndex &index)
0026 {
0027     return data(index, KateFileTreeModel::DocumentRole).value<KTextEditor::Document *>();
0028 }
0029 
0030 QList<KTextEditor::Document *> KateFileTreeProxyModel::docTreeFromIndex(const QModelIndex &index)
0031 {
0032     return data(index, KateFileTreeModel::DocumentTreeRole).value<QList<KTextEditor::Document *>>();
0033 }
0034 
0035 bool KateFileTreeProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0036 {
0037     const KateFileTreeModel *model = static_cast<KateFileTreeModel *>(sourceModel());
0038 
0039     const bool left_isdir = model->isDir(left);
0040     const bool right_isdir = model->isDir(right);
0041 
0042     // in tree mode, there will be parent nodes, we want to put those first
0043     if (left_isdir != right_isdir) {
0044         return ((left_isdir - right_isdir)) > 0;
0045     }
0046 
0047     QCollator collate;
0048     collate.setCaseSensitivity(Qt::CaseInsensitive);
0049     collate.setNumericMode(true);
0050 
0051     switch (sortRole()) {
0052     case Qt::DisplayRole: {
0053         const QString left_name = model->data(left).toString();
0054         const QString right_name = model->data(right).toString();
0055         return collate.compare(left_name, right_name) < 0;
0056     }
0057 
0058     case KateFileTreeModel::PathRole: {
0059         const auto l = model->data(left, KateFileTreeModel::DocumentRole).value<KTextEditor::Document *>();
0060         const auto r = model->data(right, KateFileTreeModel::DocumentRole).value<KTextEditor::Document *>();
0061         if (l && r) {
0062             return l->url() < r->url();
0063         }
0064         return false;
0065     }
0066 
0067     case KateFileTreeModel::OpeningOrderRole:
0068     case CustomSorting:
0069         return (left.row() - right.row()) < 0;
0070     }
0071 
0072     return false;
0073 }
0074 
0075 QModelIndex KateFileTreeProxyModel::docIndex(const KTextEditor::Document *doc) const
0076 {
0077     return mapFromSource(static_cast<KateFileTreeModel *>(sourceModel())->docIndex(doc));
0078 }
0079 
0080 bool KateFileTreeProxyModel::isDir(const QModelIndex &index) const
0081 {
0082     return static_cast<KateFileTreeModel *>(sourceModel())->isDir(mapToSource(index));
0083 }
0084 
0085 QModelIndex KateFileTreeProxyModel::widgetIndex(QWidget *w) const
0086 {
0087     return mapFromSource(static_cast<KateFileTreeModel *>(sourceModel())->widgetIndex(w));
0088 }
0089 
0090 bool KateFileTreeProxyModel::isWidgetDir(const QModelIndex &i) const
0091 {
0092     return static_cast<KateFileTreeModel *>(sourceModel())->isWidgetDir(mapToSource(i));
0093 }
0094 
0095 bool KateFileTreeProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0096 {
0097     // Child rows accepted as is
0098     if (source_parent.isValid()) {
0099         return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
0100     }
0101 
0102     const auto index = sourceModel()->index(source_row, 0, source_parent);
0103     if (static_cast<KateFileTreeModel *>(sourceModel())->isWidgetDir(index)) {
0104         return sourceModel()->rowCount(index) > 0;
0105     }
0106     return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
0107 }
0108 
0109 #include "moc_katefiletreeproxymodel.cpp"