filemanager/model/foldermodel.h

334 lines
11 KiB
C
Raw Normal View History

2021-04-21 10:51:17 -07:00
/***************************************************************************
* Copyright (C) 2006 David Faure <faure@kde.org> *
* Copyright (C) 2008 Fredrik Höglund <fredrik@kde.org> *
* Copyright (C) 2008 Rafael Fernández López <ereslibre@kde.org> *
* Copyright (C) 2011 Marco Martin <mart@kde.org> *
* Copyright (C) 2014 by Eike Hein <hein@kde.org> *
* Copyright (C) 2021 Reven Martin <revenmartin@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
2021-03-29 01:51:34 -07:00
#ifndef FOLDERMODEL_H
#define FOLDERMODEL_H
#include "../widgets/itemviewadapter.h"
#include "../helper/pathhistory.h"
2021-07-28 12:30:17 -07:00
#include "../mimetype/mimeappmanager.h"
2021-03-29 01:51:34 -07:00
#include <QSortFilterProxyModel>
#include <QItemSelectionModel>
#include <QQmlParserStatus>
#include <QQuickItem>
2021-07-25 10:40:13 -07:00
#include <QPointer>
2021-03-29 01:51:34 -07:00
#include <KDirLister>
#include <KDirModel>
#include <KDirWatch>
#include <KActionCollection>
class QDrag;
2021-09-05 14:39:56 -07:00
class CFileSizeJob;
2021-03-29 01:51:34 -07:00
class FolderModel : public QSortFilterProxyModel, public QQmlParserStatus
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)
Q_PROPERTY(QUrl resolvedUrl READ resolvedUrl NOTIFY resolvedUrlChanged)
2021-11-13 16:01:00 -08:00
Q_PROPERTY(Status status READ status NOTIFY statusChanged)
2021-03-29 01:51:34 -07:00
Q_PROPERTY(int sortMode READ sortMode WRITE setSortMode NOTIFY sortModeChanged)
Q_PROPERTY(bool sortDirsFirst READ sortDirsFirst WRITE setSortDirsFirst NOTIFY sortDirsFirstChanged)
Q_PROPERTY(bool dragging READ dragging NOTIFY draggingChanged)
Q_PROPERTY(QObject *viewAdapter READ viewAdapter WRITE setViewAdapter NOTIFY viewAdapterChanged)
2021-03-29 07:53:21 -07:00
Q_PROPERTY(bool isDesktop READ isDesktop WRITE setIsDesktop NOTIFY isDesktopChanged)
2021-05-30 00:33:45 -07:00
Q_PROPERTY(int selectionCount READ selectionCount NOTIFY selectionCountChanged)
2021-08-31 14:08:00 -07:00
Q_PROPERTY(int filterMode READ filterMode WRITE setFilterMode NOTIFY filterModeChanged)
Q_PROPERTY(QString filterPattern READ filterPattern WRITE setFilterPattern NOTIFY filterPatternChanged)
2021-04-01 01:05:15 -07:00
Q_PROPERTY(int count READ count NOTIFY countChanged)
2021-08-31 14:08:00 -07:00
Q_PROPERTY(QStringList filterMimeTypes READ filterMimeTypes WRITE setFilterMimeTypes NOTIFY filterMimeTypesChanged)
2021-09-05 14:39:56 -07:00
Q_PROPERTY(QString selectedItemSize READ selectedItemSize NOTIFY selectedItemSizeChanged)
2021-09-12 04:30:01 -07:00
Q_PROPERTY(bool showHiddenFiles READ showHiddenFiles WRITE setShowHiddenFiles NOTIFY showHiddenFilesChanged)
Q_PROPERTY(int currentIndex READ currentIndex NOTIFY currentIndexChanged)
2021-03-29 01:51:34 -07:00
public:
enum DataRole {
BlankRole = Qt::UserRole + 1,
SelectedRole,
IsDirRole,
2021-09-12 04:30:01 -07:00
IsHiddenRole,
2021-10-22 02:31:30 -07:00
IsLinkRole,
2021-03-29 01:51:34 -07:00
UrlRole,
2021-08-16 18:53:52 -07:00
DisplayNameRole,
2021-03-29 01:51:34 -07:00
FileNameRole,
2021-04-01 01:05:15 -07:00
FileSizeRole,
2021-03-29 01:51:34 -07:00
IconNameRole,
2021-04-01 01:05:15 -07:00
ThumbnailRole,
2021-08-16 18:53:52 -07:00
ModifiedRole,
IsDesktopFileRole
2021-03-29 01:51:34 -07:00
};
enum FilterMode {
NoFilter = 0,
FilterShowMatches,
FilterHideMatches,
};
enum Status {
None,
Ready,
Listing,
Canceled,
};
Q_ENUM(Status)
struct DragImage {
int row;
QRect rect;
QPoint cursorOffset;
QImage image;
bool blank;
};
explicit FolderModel(QObject *parent = nullptr);
~FolderModel() override;
void classBegin() override;
void componentComplete() override;
QHash<int, QByteArray> roleNames() const override;
static QHash<int, QByteArray> staticRoleNames();
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
2021-09-19 20:04:33 -07:00
int indexForKeyboardSearch(const QString &text, int startFromIndex = 0) const;
2021-03-29 01:51:34 -07:00
KFileItem itemForIndex(const QModelIndex &index) const;
QModelIndex indexForUrl(const QUrl &url) const;
2021-03-29 01:51:34 -07:00
2021-09-19 20:04:33 -07:00
KFileItem fileItem(int index) const;
2021-03-29 01:51:34 -07:00
QList<QUrl> selectedUrls() const;
int currentIndex() const;
2021-03-29 01:51:34 -07:00
QString url() const;
void setUrl(const QString &url);
QUrl resolvedUrl() const;
Q_INVOKABLE QUrl resolve(const QString &url);
Status status() const;
void setStatus(Status status);
int sortMode() const;
void setSortMode(int mode);
bool sortDirsFirst() const;
void setSortDirsFirst(bool enable);
2021-08-31 14:08:00 -07:00
int filterMode() const;
void setFilterMode(int filterMode);
QStringList filterMimeTypes() const;
void setFilterMimeTypes(const QStringList &mimeList);
QString filterPattern() const;
void setFilterPattern(const QString &pattern);
2021-03-29 01:51:34 -07:00
QObject *viewAdapter() const;
void setViewAdapter(QObject *adapter);
bool dragging() const;
bool isDir(const QModelIndex &index, const KDirModel *dirModel) const;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
Qt::DropActions supportedDragActions() const override;
Qt::DropActions supportedDropActions() const override;
KFileItem rootItem() const;
2021-04-01 01:05:15 -07:00
int count() const;
2021-05-30 00:33:45 -07:00
int selectionCount() const;
2021-03-29 01:51:34 -07:00
Q_INVOKABLE QString homePath() const;
Q_INVOKABLE QString desktopPath() const;
Q_INVOKABLE QAction *action(const QString &name) const;
Q_INVOKABLE void up();
Q_INVOKABLE void goBack();
Q_INVOKABLE void goForward();
2021-08-31 13:39:44 -07:00
Q_INVOKABLE void refresh();
2021-12-26 01:14:45 -08:00
Q_INVOKABLE void undo();
2021-03-29 01:51:34 -07:00
Q_INVOKABLE bool supportSetAsWallpaper(const QString &mimeType);
Q_INVOKABLE int fileExtensionBoundary(int row);
Q_INVOKABLE bool hasSelection() const;
Q_INVOKABLE bool isSelected(int row) const;
Q_INVOKABLE bool isBlank(int row) const;
Q_INVOKABLE void setSelected(int row);
Q_INVOKABLE void selectAll();
Q_INVOKABLE void toggleSelected(int row);
Q_INVOKABLE void setRangeSelected(int anchor, int to);
Q_INVOKABLE void updateSelection(const QVariantList &rows, bool toggle);
Q_INVOKABLE void clearSelection();
Q_INVOKABLE void pinSelection();
Q_INVOKABLE void unpinSelection();
Q_INVOKABLE void newFolder();
2021-12-26 09:32:21 -08:00
Q_INVOKABLE void newTextFile();
2021-03-29 01:51:34 -07:00
Q_INVOKABLE void rename(int row, const QString &name);
Q_INVOKABLE void copy();
Q_INVOKABLE void paste();
Q_INVOKABLE void cut();
Q_INVOKABLE void openSelected();
2021-07-29 02:38:47 -07:00
Q_INVOKABLE void showOpenWithDialog();
2021-03-29 01:51:34 -07:00
Q_INVOKABLE void deleteSelected();
Q_INVOKABLE void moveSelectedToTrash();
Q_INVOKABLE void emptyTrash();
2021-03-31 19:25:20 -07:00
Q_INVOKABLE void keyDeletePress();
2021-03-29 01:51:34 -07:00
Q_INVOKABLE void setDragHotSpotScrollOffset(int x, int y);
Q_INVOKABLE void addItemDragImage(int row, int x, int y, int width, int height, const QVariant &image);
Q_INVOKABLE void clearDragImages();
Q_INVOKABLE void dragSelected(int x, int y);
2021-11-03 10:35:09 -07:00
Q_INVOKABLE void drop(QQuickItem *target, QObject *dropEvent, int row);
2021-03-29 01:51:34 -07:00
Q_INVOKABLE void setWallpaperSelected();
Q_INVOKABLE void openContextMenu(QQuickItem *visualParent = nullptr, Qt::KeyboardModifiers modifiers = Qt::NoModifier);
Q_INVOKABLE void openPropertiesDialog();
Q_INVOKABLE void openInTerminal();
Q_INVOKABLE void openChangeWallpaperDialog();
2021-09-03 11:26:01 -07:00
Q_INVOKABLE void openDeleteDialog();
2021-12-02 11:58:55 -08:00
Q_INVOKABLE void openInNewWindow(const QString &url = QString());
2021-03-29 01:51:34 -07:00
2021-09-05 14:39:56 -07:00
Q_INVOKABLE void updateSelectedItemsSize();
2021-09-27 21:00:19 -07:00
Q_INVOKABLE void keyboardSearch(const QString &text);
2021-09-05 14:39:56 -07:00
2021-10-15 03:57:18 -07:00
Q_INVOKABLE void clearPixmapCache();
2021-09-03 10:47:54 -07:00
void restoreFromTrash();
2021-03-29 07:53:21 -07:00
bool isDesktop() const;
void setIsDesktop(bool isDesktop);
2021-09-05 14:39:56 -07:00
QString selectedItemSize() const;
2021-09-12 04:30:01 -07:00
bool showHiddenFiles() const;
void setShowHiddenFiles(bool showHiddenFiles);
2021-03-29 01:51:34 -07:00
signals:
void urlChanged();
2021-11-13 16:01:00 -08:00
void listingCompleted() const;
void listingCanceled() const;
2021-03-29 01:51:34 -07:00
void resolvedUrlChanged();
void statusChanged();
void sortModeChanged();
void sortDescChanged();
void sortDirsFirstChanged();
2021-08-31 14:08:00 -07:00
void filterModeChanged();
2021-03-29 01:51:34 -07:00
void requestRename();
void draggingChanged();
void viewAdapterChanged();
2021-03-29 07:53:21 -07:00
void isDesktopChanged();
2021-05-30 00:33:45 -07:00
void selectionCountChanged();
2021-04-01 01:05:15 -07:00
void countChanged();
2021-08-31 14:08:00 -07:00
void filterPatternChanged();
void filterMimeTypesChanged();
2021-09-05 14:39:56 -07:00
void selectedItemSizeChanged();
2021-09-12 04:30:01 -07:00
void showHiddenFilesChanged();
2021-12-04 23:15:35 -08:00
void scrollToItem(int index);
2021-03-29 01:51:34 -07:00
2021-08-31 14:31:38 -07:00
void notification(const QString &message);
2021-11-13 16:01:00 -08:00
void move(int x, int y, QList<QUrl> urls);
2021-08-31 14:31:38 -07:00
void currentIndexChanged();
2021-03-29 01:51:34 -07:00
private slots:
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
void dragSelectedInternal(int x, int y);
void onRowsInserted(const QModelIndex &parent, int first, int last);
void delayUpdateNeedSelectUrls();
void updateNeedSelectUrls();
2021-03-29 01:51:34 -07:00
private:
void invalidateIfComplete();
void invalidateFilterIfComplete();
void createActions();
void updateActions();
void addDragImage(QDrag *drag, int x, int y);
bool isSupportThumbnails(const QString &mimeType) const;
2021-08-31 14:08:00 -07:00
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
bool matchMimeType(const KFileItem &item) const;
bool matchPattern(const KFileItem &item) const;
2021-03-29 01:51:34 -07:00
private:
KDirModel *m_dirModel;
KDirWatch *m_dirWatch;
2021-09-12 04:30:01 -07:00
KDirLister *m_dirLister;
2021-03-29 01:51:34 -07:00
QItemSelectionModel *m_selectionModel;
QItemSelection m_pinnedSelection;
QString m_url;
QUrl m_newDocumentUrl;
QList<QUrl> m_needSelectUrls;
2021-03-29 01:51:34 -07:00
Status m_status;
int m_sortMode;
bool m_sortDesc;
bool m_sortDirsFirst;
2021-09-12 04:30:01 -07:00
bool m_showHiddenFiles;
2021-08-31 14:08:00 -07:00
FilterMode m_filterMode;
QString m_filterPattern;
bool m_filterPatternMatchAll;
QSet<QString> m_mimeSet;
QList<QRegExp> m_regExps;
2021-03-29 01:51:34 -07:00
bool m_complete;
2021-03-29 07:53:21 -07:00
bool m_isDesktop;
2021-05-30 00:33:45 -07:00
bool m_suffixVisible;
2021-03-29 01:51:34 -07:00
2021-09-05 14:39:56 -07:00
QString m_selectedItemSize;
2021-03-29 01:51:34 -07:00
KActionCollection m_actionCollection;
QHash<int, DragImage *> m_dragImages;
QModelIndexList m_dragIndexes;
QPoint m_dragHotSpotScrollOffset;
bool m_dragInProgress;
2021-11-13 16:01:00 -08:00
QHash<QString, QPoint> m_dropTargetPositions;
QTimer *m_dropTargetPositionsCleanup;
2021-03-29 01:51:34 -07:00
QPointer<ItemViewAdapter> m_viewAdapter;
// Save path history
PathHistory m_pathHistory;
2021-07-28 12:30:17 -07:00
MimeAppManager *m_mimeAppManager;
2021-09-05 14:39:56 -07:00
CFileSizeJob *m_sizeJob;
int m_currentIndex;
2021-12-31 20:20:58 -08:00
QTimer *m_updateNeedSelectTimer;
2021-03-29 01:51:34 -07:00
};
#endif // FOLDERMODEL_H