1 /**************************************************************************
2 ** This file is part of LiteIDE
3 **
4 ** Copyright (c) 2011-2019 LiteIDE. All rights reserved.
5 **
6 ** This library is free software; you can redistribute it and/or
7 ** modify it under the terms of the GNU Lesser General Public
8 ** License as published by the Free Software Foundation; either
9 ** version 2.1 of the License, or (at your option) any later version.
10 **
11 ** This library is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ** Lesser General Public License for more details.
15 **
16 ** In addition, as a special exception,  that plugins developed for LiteIDE,
17 ** are allowed to remain closed sourced and can be distributed under any license .
18 ** These rights are included in the file LGPL_EXCEPTION.txt in this package.
19 **
20 **************************************************************************/
21 // Module: filesystemmodel.h
22 // Creator: visualfc <visualfc@gmail.com>
23 
24 #ifndef FILESYSTEMMODEL_H
25 #define FILESYSTEMMODEL_H
26 
27 #include <QAbstractItemModel>
28 #include <QStringList>
29 #include <QIcon>
30 #include <QFileInfo>
31 #include <QDir>
32 #include <QMutex>
33 
34 class FileSystemModel;
35 class QFileSystemWatcher;
36 class FileNode
37 {
38 public:
39     FileNode(FileSystemModel *model);
40     FileNode(FileSystemModel *model,const QString &path, FileNode *parent);
41     virtual ~FileNode();
42     FileNode* parent();
43     FileNode* child(int row);
44     int childCount();
45     int row() const;
46     QList<FileNode*>* children();
47     QString path() const;
48     QString text() const;
49     QFileInfo fileInfo() const;
50     bool isExist() const;
51     bool isDir() const;
52     bool isFile() const;
53     void clear();
54     void reload();
55     FileNode *findPath(const QString &path);
56 protected:
57     FileSystemModel *m_model;
58     FileNode *m_parent;
59     QList<FileNode*> *m_children;
60     QString m_path;
61     QString m_text;
62     bool    m_bWatcher;
63 };
64 
65 class QFileIconProvider;
66 class QFileSystemWatcher;
67 class QTreeView;
68 class FileSystemModel : public QAbstractItemModel
69 {
70     Q_OBJECT
71 public:
72     explicit FileSystemModel(QObject *parent = 0);
73     ~FileSystemModel();
74     void clear();
75     void reload();
76     void setFilter(QDir::Filters filters);
77     void setDirSort(QDir::SortFlags flags);
78     QDir::Filters filter() const;
79     bool isShowHideFiles() const;
80     QDir::SortFlags dirSort() const;
81     bool removeRootPath(const QString &path);
82     bool addRootPath(const QString &path);
83     void setRootPathList(const QStringList &pathList);
84     void setRootPath(const QString &path);
85     QStringList rootPathList() const;
86     QList<QModelIndex> findPaths(const QString &path) const;
87     QModelIndex findPath(const QString &path) const;
88     QString filePath(const QModelIndex &index) const;
89     FileNode *nodeFromIndex(const QModelIndex &index) const;
90     void setStartIndex(const QModelIndex &index);
91     void setStartPath(const QString &path);
92     QModelIndex startIndex() const;
93     QString startPath() const;
94     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
95     virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
96     virtual QModelIndex parent(const QModelIndex &child) const;
97     virtual QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const;
98     virtual QVariant data(const QModelIndex &index, int role) const;
99     bool isRootPathNode(FileNode *node) const;
100     bool isRootPathNodeFillPath() const;
101     void addWatcher(const QString &path);
102     void removeWatcher(const QString &path);
103 signals:
104     void direcotryChanged(QString);
105 public slots:
106     void reloadDirectory(const QString &path);
107 protected:
108     QModelIndex findPathHelper(const QString &path, const QModelIndex &parentIndex) const;
109     QStringList m_pathList;
110     FileNode *m_rootNode;
111     QString   m_startPath;
112     QFileIconProvider *m_iconProvider;
113     QFileSystemWatcher *m_fileWatcher;
114     QMap<QString,int> m_fileWatcherMap;
115     QTreeView *m_treeView;
116     QDir::Filters m_filters;
117     QDir::SortFlags m_sorts;
118     QMutex m_mutex;
119 };
120 
121 #endif // FILESYSTEMMODEL_H
122