File indexing completed on 2024-07-14 05:39:17

0001 /*
0002     kfindtreeview.h
0003     SPDX-FileCopyrightText: 2009 Dario Andres Rodriguez <andresbajotierra@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 
0007 */
0008 
0009 #ifndef KFINDTREEVIEW__H
0010 #define KFINDTREEVIEW__H
0011 
0012 #include <KFileItem>
0013 
0014 #include <QAbstractTableModel>
0015 #include <QDir>
0016 #include <QDragMoveEvent>
0017 #include <QIcon>
0018 #include <QSortFilterProxyModel>
0019 #include <QTreeView>
0020 #include <QUrl>
0021 
0022 class QMenu;
0023 class KFindTreeView;
0024 class KActionCollection;
0025 class KfindDlg;
0026 
0027 class KFindItem
0028 {
0029 public:
0030     explicit KFindItem(const KFileItem & = KFileItem(), const QString &subDir = QString(), const QString &matchingLine = QString());
0031 
0032     QVariant data(int column, int role) const;
0033 
0034     KFileItem getFileItem() const;
0035 
0036     bool isValid() const;
0037 
0038 private:
0039     KFileItem m_fileItem;
0040     QString m_matchingLine;
0041     QString m_subDir;
0042     QString m_permission;
0043     QIcon m_icon;
0044 };
0045 
0046 class KFindItemModel : public QAbstractTableModel
0047 {
0048 public:
0049     explicit KFindItemModel(KFindTreeView *parent);
0050 
0051     void insertFileItems(const QList< QPair<KFileItem, QString> > &);
0052 
0053     void removeItem(const QUrl &);
0054     bool isInserted(const QUrl &);
0055 
0056     void clear();
0057 
0058     Qt::DropActions supportedDropActions() const override;
0059 
0060     Qt::ItemFlags flags(const QModelIndex &) const override;
0061     QMimeData *mimeData(const QModelIndexList &) const override;
0062 
0063     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0064 
0065     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0066     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0067     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0068 
0069     KFindItem itemAtIndex(const QModelIndex &index) const;
0070 
0071     QList<KFindItem> getItemList() const;
0072 
0073 private:
0074     QList<KFindItem> m_itemList;
0075     KFindTreeView *m_view;
0076 };
0077 
0078 class KFindSortFilterProxyModel : public QSortFilterProxyModel
0079 {
0080     Q_OBJECT
0081 
0082 public:
0083     explicit KFindSortFilterProxyModel(QObject *parent = nullptr)
0084         : QSortFilterProxyModel(parent)
0085     {
0086     }
0087 
0088 protected:
0089     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
0090 };
0091 
0092 class KFindTreeView : public QTreeView
0093 {
0094     Q_OBJECT
0095 public:
0096     explicit KFindTreeView(QWidget *parent, KfindDlg *findDialog);
0097     ~KFindTreeView() override;
0098 
0099     void beginSearch(const QUrl &baseUrl);
0100     void endSearch();
0101 
0102     void insertItems(const QList< QPair<KFileItem, QString> > &);
0103     void removeItem(const QUrl &url);
0104 
0105     bool isInserted(const QUrl &url) const;
0106 
0107     QString reducedDir(const QString &fullDir);
0108 
0109     int itemCount() const;
0110 
0111     QList<QUrl> selectedUrls() const;
0112 
0113 public Q_SLOTS:
0114     void copySelection();
0115     void copySelectionPath();
0116     void contextMenuRequested(const QPoint &p);
0117     void saveResults();
0118 
0119 private Q_SLOTS:
0120     void deleteSelectedFiles();
0121     void moveToTrashSelectedFiles();
0122 
0123     void slotExecute(const QModelIndex &index);
0124     void slotExecuteSelected();
0125 
0126     void openContainingFolder();
0127 
0128     void reconfigureMouseSettings();
0129     void updateMouseButtons();
0130 
0131 protected:
0132     void dragMoveEvent(QDragMoveEvent *e) override;
0133 
0134 Q_SIGNALS:
0135     void resultSelected(bool);
0136 
0137 private:
0138     void resizeToContents();
0139 
0140     QList<QPersistentModelIndex> selectedVisibleIndexes();
0141 
0142     QDir m_baseDir;
0143 
0144     KFindItemModel *const m_model;
0145     KFindSortFilterProxyModel *const m_proxyModel;
0146     KActionCollection *m_actionCollection = nullptr;
0147     QMenu *m_contextMenu = nullptr;
0148 
0149     Qt::MouseButtons m_mouseButtons;
0150 
0151     KfindDlg *const m_kfindDialog;
0152 };
0153 
0154 #endif