foldermodel.h 11.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/***************************************************************************
 *   Copyright (C) 2008 Fredrik Höglund <fredrik@kde.org>                  *
 *   Copyright (C) 2011 Marco Martin <mart@kde.org>                        *
 *   Copyright (C) 2014 by Eike Hein <hein@kde.org>                        *
 *                                                                         *
 *   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 .        *
 ***************************************************************************/

#ifndef FOLDERMODEL_H
#define FOLDERMODEL_H

25
#include <QImage>
26
27
28
29
30
31
32
33
34
35
36
37
#include <QItemSelection>
#include <QPointer>
#include <QSortFilterProxyModel>
#include <QStringList>
#include <QSet>
#include <QRegExp>

#include <KAbstractViewAdapter>
#include <KActionCollection>
#include <KFilePreviewGenerator>
#include <KDirLister>

38
39
#include <KNewFileMenu>

40
class QDrag;
41
class QItemSelectionModel;
42
class QQuickItem;
43

44
class KFileCopyToMenu;
45
46
class KActionCollection;
class KDirModel;
47
class KDirWatch;
48
class KFileItem;
49
class KFileItemActions;
50
class KJob;
51
52
class KNewFileMenu;

53
54
55
56
namespace KIO {
    class DropJob;
}

57
58
59
60
61
62
63
64
65
66
67
68
class DirLister : public KDirLister
{
    Q_OBJECT

    public:
        DirLister(QObject *parent = 0);
        ~DirLister();

    Q_SIGNALS:
        void error(const QString &string);

    protected:
Laurent Montel's avatar
Laurent Montel committed
69
        void handleError(KIO::Job *job) Q_DECL_OVERRIDE;
70
71
72
73
74
75
76
};

class FolderModel : public QSortFilterProxyModel
{
    Q_OBJECT

    Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)
77
    Q_PROPERTY(QString iconName READ iconName NOTIFY iconNameChanged)
78
    Q_PROPERTY(QUrl resolvedUrl READ resolvedUrl NOTIFY resolvedUrlChanged)
79
    Q_PROPERTY(Status status READ status NOTIFY statusChanged)
80
    Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged)
81
    Q_PROPERTY(bool dragging READ dragging NOTIFY draggingChanged)
82
    Q_PROPERTY(bool usedByContainment READ usedByContainment WRITE setUsedByContainment NOTIFY usedByContainmentChanged)
83
    Q_PROPERTY(bool locked READ locked WRITE setLocked NOTIFY lockedChanged)
84
85
86
87
88
89
90
91
92
93
    Q_PROPERTY(int sortMode READ sortMode WRITE setSortMode NOTIFY sortModeChanged)
    Q_PROPERTY(bool sortDesc READ sortDesc WRITE setSortDesc NOTIFY sortDescChanged)
    Q_PROPERTY(bool sortDirsFirst READ sortDirsFirst WRITE setSortDirsFirst NOTIFY sortDirsFirstChanged)
    Q_PROPERTY(bool parseDesktopFiles READ parseDesktopFiles WRITE setParseDesktopFiles NOTIFY parseDesktopFilesChanged)
    Q_PROPERTY(QObject* viewAdapter READ viewAdapter WRITE setViewAdapter NOTIFY viewAdapterChanged)
    Q_PROPERTY(bool previews READ previews WRITE setPreviews NOTIFY previewsChanged)
    Q_PROPERTY(QStringList previewPlugins READ previewPlugins WRITE setPreviewPlugins NOTIFY previewPluginsChanged)
    Q_PROPERTY(int filterMode READ filterMode WRITE setFilterMode NOTIFY filterModeChanged)
    Q_PROPERTY(QString filterPattern READ filterPattern WRITE setFilterPattern NOTIFY filterPatternChanged)
    Q_PROPERTY(QStringList filterMimeTypes READ filterMimeTypes WRITE setFilterMimeTypes NOTIFY filterMimeTypesChanged)
94
    Q_PROPERTY(QObject* newMenu READ newMenu CONSTANT)
95
96

    public:
97
98
        enum DataRole {
            BlankRole = Qt::UserRole + 1,
99
            OverlaysRole,
100
101
            SelectedRole,
            IsDirRole,
102
            IsLinkRole,
103
            IsHiddenRole,
104
            UrlRole,
105
            LinkDestinationUrl,
106
107
108
109
110
            SizeRole,
            TypeRole,
            FileNameRole
        };

111
112
113
114
115
116
        enum FilterMode {
            NoFilter = 0,
            FilterShowMatches,
            FilterHideMatches
        };

117
118
119
120
121
122
123
124
        enum Status {
            None,
            Ready,
            Listing,
            Canceled
        };
        Q_ENUM(Status)

125
126
127
        FolderModel(QObject *parent = 0);
        ~FolderModel();

Laurent Montel's avatar
Laurent Montel committed
128
        QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
129
        static QHash<int, QByteArray> staticRoleNames();
130
131
132
133

        QString url() const;
        void setUrl(const QString &url);

134
135
        QString iconName() const;

136
        QUrl resolvedUrl() const;
137
        Q_INVOKABLE QUrl resolve(const QString& url);
138

139
140
        Status status() const;

141
142
        QString errorString() const;

143
144
        bool dragging() const;

145
146
147
        bool usedByContainment() const;
        void setUsedByContainment(bool used);

148
149
150
        bool locked() const;
        void setLocked(bool locked);

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
        int sortMode() const;
        void setSortMode(int mode);

        bool sortDesc() const;
        void setSortDesc(bool desc);

        bool sortDirsFirst() const;
        void setSortDirsFirst(bool enable);

        bool parseDesktopFiles() const;
        void setParseDesktopFiles(bool enable);

        QObject* viewAdapter() const;
        void setViewAdapter(QObject *adapter);

        bool previews() const;
        void setPreviews(bool previews);

        QStringList previewPlugins() const;
        void setPreviewPlugins(const QStringList &previewPlugins);

        int filterMode() const;
        void setFilterMode(int filterMode);

        QString filterPattern() const;
        void setFilterPattern(const QString &pattern);

        QStringList filterMimeTypes() const;
        void setFilterMimeTypes(const QStringList &mimeList);

181
182
        KFileItem rootItem() const;

183
        Q_INVOKABLE void up();
184
        Q_INVOKABLE void cd(int row);
185

186
        Q_INVOKABLE void run(int row);
187
        Q_INVOKABLE void runSelected();
188

189
190
        Q_INVOKABLE void rename(int row, const QString &name);
        Q_INVOKABLE int fileExtensionBoundary(int row);
191

192
        Q_INVOKABLE bool hasSelection();
193
        Q_INVOKABLE bool isSelected(int row);
194
195
        Q_INVOKABLE void setSelected(int row);
        Q_INVOKABLE void toggleSelected(int row);
196
197
        Q_INVOKABLE void setRangeSelected(int anchor, int to);
        Q_INVOKABLE void updateSelection(const QVariantList &rows, bool toggle);
198
        Q_INVOKABLE void clearSelection();
199
200
201
202
203
204
205
206
207
        Q_INVOKABLE void pinSelection();
        Q_INVOKABLE void unpinSelection();

        Q_INVOKABLE void addItemDragImage(int row, int x, int y, int width, int height, const QVariant &image);
        Q_INVOKABLE void clearDragImages();
        Q_INVOKABLE void setDragHotSpotScrollOffset(int x, int y); // FIXME TODO: Propify.
        Q_INVOKABLE QPoint dragCursorOffset(int row);
        Q_INVOKABLE void dragSelected(int x, int y);
        Q_INVOKABLE void drop(QQuickItem *target, QObject *dropEvent, int row);
208
        Q_INVOKABLE void dropCwd(QObject *dropEvent);
209
210

        Q_INVOKABLE bool isBlank(int row) const;
211

212
213
214
        Q_INVOKABLE QAction* action(const QString& name) const;
        QObject* newMenu() const;
        Q_INVOKABLE void updateActions();
215
        Q_INVOKABLE void openContextMenu(QQuickItem *visualParent = nullptr);
216

217
218
        Q_INVOKABLE void linkHere(const QUrl &sourceUrl);

Laurent Montel's avatar
Laurent Montel committed
219
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
220
        int indexForUrl(const QUrl &url) const;
221
222
        KFileItem itemForIndex(const QModelIndex &index) const;
        bool isDir(const QModelIndex &index, const KDirModel *dirModel) const;
Laurent Montel's avatar
Laurent Montel committed
223
        bool lessThan(const QModelIndex &left, const QModelIndex &right) const Q_DECL_OVERRIDE;
224
        Qt::DropActions supportedDragActions() const override;
225

226
227
228
        Q_INVOKABLE void paste();
        Q_INVOKABLE void copy();
        Q_INVOKABLE void cut();
229
        Q_INVOKABLE void deleteSelected();
230
        Q_INVOKABLE void openSelected();
231
        Q_INVOKABLE void undo();
232
        Q_INVOKABLE void refresh();
233

234
235
    Q_SIGNALS:
        void urlChanged() const;
236
        void listingCompleted() const;
237
        void listingCanceled() const;
238
        void iconNameChanged() const;
239
        void resolvedUrlChanged() const;
240
        void statusChanged() const;
241
        void errorStringChanged() const;
242
        void draggingChanged() const;
243
        void usedByContainmentChanged() const;
244
        void lockedChanged() const;
245
246
247
248
249
250
251
252
253
254
255
        void sortModeChanged() const;
        void sortDescChanged() const;
        void sortDirsFirstChanged() const;
        void parseDesktopFilesChanged() const;
        void viewAdapterChanged();
        void previewsChanged() const;
        void previewPluginsChanged() const;
        void filterModeChanged() const;
        void filterPatternChanged() const;
        void filterMimeTypesChanged() const;
        void requestRename() const;
256
        void move(int x, int y, QList<QUrl> urls);
257
        void popupMenuAboutToShow(KIO::DropJob *dropJob, QMimeData *mimeData, int x, int y);
258
259

    protected:
Laurent Montel's avatar
Laurent Montel committed
260
        bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const Q_DECL_OVERRIDE;
261
262
263
264
        bool matchMimeType(const KFileItem &item) const;
        bool matchPattern(const KFileItem &item) const;

    private Q_SLOTS:
265
        void dragSelectedInternal(int x, int y);
266
        void dirListFailed(const QString &error);
267
        void statResult(KJob *job);
Eike Hein's avatar
Eike Hein committed
268
        void evictFromIsDirCache(const KFileItemList &items);
269
270
        void selectionChanged(QItemSelection selected, QItemSelection deselected);
        void pasteTo();
Eike Hein's avatar
Eike Hein committed
271
        void moveSelectedToTrash();
272
        void emptyTrashBin();
273
        void restoreSelectedFromTrash();
Eike Hein's avatar
Eike Hein committed
274
        void undoTextChanged(const QString &text);
275
276

    private:
277
278
279
280
281
282
283
284
        struct DragImage {
            int row;
            QRect rect;
            QPoint cursorOffset;
            QImage image;
            bool blank;
        };

285
286
        void createActions();
        void updatePasteAction();
287
        void addDragImage(QDrag *drag, int x, int y);
288
        void setStatus(Status status);
289
290
        QList<QUrl> selectedUrls(bool forTrash) const;
        KDirModel *m_dirModel;
291
        KDirWatch *m_dirWatch;
292
        QString m_url;
293
        QHash<QUrl, bool> m_isDirCache;
294
        QItemSelectionModel *m_selectionModel;
295
296
297
298
299
        QItemSelection m_pinnedSelection;
        QModelIndexList m_dragIndexes;
        QHash<int, DragImage *> m_dragImages;
        QPoint m_dragHotSpotScrollOffset;
        bool m_dragInProgress;
300
        bool m_urlChangedWhileDragging;
301
302
303
304
        QPointer<KFilePreviewGenerator> m_previewGenerator;
        QPointer<KAbstractViewAdapter> m_viewAdapter;
        KActionCollection m_actionCollection;
        KNewFileMenu *m_newMenu;
305
        KFileItemActions *m_fileItemActions;
306
        KFileCopyToMenu *m_copyToMenu;
307
        Status m_status = Status::None;
308
309
        QString m_errorString;
        bool m_usedByContainment;
310
311
        bool m_locked;
        int m_sortMode; // FIXME TODO: Enumify.
312
313
314
315
316
317
318
319
320
321
322
323
324
        bool m_sortDesc;
        bool m_sortDirsFirst;
        bool m_parseDesktopFiles;
        bool m_previews;
        QStringList m_previewPlugins;
        FilterMode m_filterMode;
        QString m_filterPattern;
        bool m_filterPatternMatchAll;
        QSet<QString> m_mimeSet;
        QList<QRegExp> m_regExps;
};

#endif