File indexing completed on 2024-04-28 05:49:18
0001 /* This file is part of the KDE project 0002 0003 SPDX-FileCopyrightText: 2018 Gregor Mi <codestruct@posteo.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #pragma once 0009 0010 #include <QAbstractTableModel> 0011 #include <QIcon> 0012 #include <QString> 0013 0014 #include <doc_or_widget.h> 0015 0016 namespace KTextEditor 0017 { 0018 class Document; 0019 } 0020 0021 namespace detail 0022 { 0023 /** 0024 * Represents one item in the table view of the tab switcher. 0025 */ 0026 class FilenameListItem 0027 { 0028 public: 0029 explicit FilenameListItem(DocOrWidget doc); 0030 0031 DocOrWidget document; 0032 QIcon icon() const; 0033 QString documentName() const; 0034 QString fullPath() const; 0035 0036 /** 0037 * calculated from documentName and fullPath 0038 */ 0039 QString displayPathPrefix; 0040 }; 0041 using FilenameList = std::vector<FilenameListItem>; 0042 0043 class TabswitcherFilesModel : public QAbstractTableModel 0044 { 0045 Q_OBJECT 0046 0047 public: 0048 explicit TabswitcherFilesModel(QObject *parent = nullptr); 0049 ~TabswitcherFilesModel() override = default; 0050 bool insertDocument(int row, DocOrWidget document); 0051 bool removeDocument(DocOrWidget document); 0052 0053 /** 0054 * Clears all data from the model 0055 */ 0056 void clear(); 0057 0058 /** 0059 * NOTE: The returned pointer will become invalid as soon as the underlying vector changes. 0060 */ 0061 DocOrWidget item(int row) const; 0062 0063 /** 0064 * Move the document to row 0. 0065 */ 0066 void raiseDocument(DocOrWidget document); 0067 0068 /* 0069 * Use this method to update all items. 0070 * This is typically needed when a document name changes, since then the prefix paths change, 0071 * so all items need an update. 0072 */ 0073 void updateItems(); 0074 0075 /** 0076 * Reimplemented to return the column count of top-level items. 0077 */ 0078 int columnCount(const QModelIndex &parent = QModelIndex()) const override; 0079 0080 /** 0081 * Reimplemented to return the top-level row count. 0082 */ 0083 int rowCount(const QModelIndex &parent = QModelIndex()) const override; 0084 0085 /** 0086 * Returns the data for the requested model index. 0087 */ 0088 QVariant data(const QModelIndex &index, int role) const override; 0089 0090 /** 0091 * Reimplemented to remove the specified rows. 0092 * The paret is always ignored since this is a table model. 0093 */ 0094 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; 0095 0096 private: 0097 FilenameList data_; 0098 }; 0099 0100 QString longestCommonPrefix(std::vector<QString> const &strs); 0101 0102 }