File indexing completed on 2024-05-12 05:52:07

0001 /*
0002     SPDX-FileCopyrightText: 2018 Tomaz Canabrava <tcanabrava@kde.org>
0003     SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include <QAbstractTableModel>
0011 #include <QUrl>
0012 
0013 #include <vector>
0014 
0015 class KateMainWindow;
0016 namespace KTextEditor
0017 {
0018 class Document;
0019 }
0020 
0021 struct ModelEntry {
0022     QString fileName; // display string for left column
0023     QString filePath; // display string for right column
0024     KTextEditor::Document *document = nullptr; // document for entry, if already open
0025     int score = -1;
0026 };
0027 
0028 // needs to be defined outside of class to support forward declaration elsewhere
0029 enum KateQuickOpenModelList : int { CurrentProject, AllProjects };
0030 
0031 class KateQuickOpenModel : public QAbstractTableModel
0032 {
0033     Q_OBJECT
0034 public:
0035     enum Role { FileName = Qt::UserRole + 1, FilePath, Score, Document };
0036     explicit KateQuickOpenModel(QObject *parent = nullptr);
0037     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0038     int columnCount(const QModelIndex &parent) const override;
0039     QVariant data(const QModelIndex &idx, int role) const override;
0040     void refresh(KateMainWindow *mainWindow);
0041     // add a convenient in-class alias
0042     using List = KateQuickOpenModelList;
0043     List listMode() const
0044     {
0045         return m_listMode;
0046     }
0047     void setListMode(List mode)
0048     {
0049         m_listMode = mode;
0050     }
0051 
0052     bool isValid(int row) const
0053     {
0054         return row >= 0 && (size_t)row < m_modelEntries.size();
0055     }
0056 
0057     void setScoreForIndex(int row, int score)
0058     {
0059         m_modelEntries[row].score = score;
0060     }
0061 
0062     const QString &idxToFileName(int row) const
0063     {
0064         return m_modelEntries.at(row).fileName;
0065     }
0066 
0067     QStringView idxToFilePath(int row) const
0068     {
0069         const QString &path = m_modelEntries.at(row).filePath;
0070         QStringView pth(path.data(), path.size());
0071         // handle exceptional non-saved file case
0072         return pth.startsWith(QStringView(m_projectBase.data(), m_projectBase.size())) ? pth.mid(m_projectBase.size()) : pth;
0073     }
0074 
0075     int idxScore(const QModelIndex &idx) const
0076     {
0077         if (!idx.isValid()) {
0078             return {};
0079         }
0080         return m_modelEntries.at(idx.row()).score;
0081     }
0082 
0083     bool isOpened(const QModelIndex &idx) const
0084     {
0085         if (!idx.isValid()) {
0086             return {};
0087         }
0088         return m_modelEntries.at(idx.row()).document;
0089     }
0090 
0091     bool isOpened(int row) const
0092     {
0093         return m_modelEntries.at(row).document;
0094     }
0095 
0096 private:
0097     std::vector<ModelEntry> m_modelEntries;
0098     QString m_projectBase;
0099     List m_listMode{};
0100 };