OSDN Git Service

ファイル読込を別スレッド化
[gefu/Gefu.git] / foldermodel.h
1 #ifndef FOLDERMODEL_H\r
2 #define FOLDERMODEL_H\r
3 \r
4 #include <QAbstractTableModel>\r
5 #include <QDir>\r
6 #include <QFileIconProvider>\r
7 #include <QFileSystemWatcher>\r
8 #include <QBrush>\r
9 #include <QFont>\r
10 #include <QMutex>\r
11 #include <QPalette>\r
12 class Preferences;\r
13 class ThumbnailWorker;\r
14 \r
15 class FolderModel : public QAbstractTableModel\r
16 {\r
17     Q_OBJECT\r
18 public:\r
19     enum Column {\r
20         Name = 0,\r
21         Extension,\r
22         Size,\r
23         LastModified,\r
24         ColumnCount,\r
25     };\r
26     typedef QVector<QString> History;\r
27 \r
28     explicit FolderModel(QObject *parent = 0);\r
29     ~FolderModel();\r
30 \r
31     void            clearPixmapCache();\r
32     QString         error() const;\r
33     QIcon           fileIcon(const QModelIndex &index) const;\r
34     QFileInfo       fileInfo(const QModelIndex &index) const;\r
35     QString         fileName(const QModelIndex &index) const;\r
36     QString         filePath(const QModelIndex &index) const;\r
37     QDir::Filters   filter() const;\r
38     const History&  history() const;\r
39     bool            isActive() const;\r
40     bool            isDir(const QModelIndex &index) const;\r
41     bool            isHistoryBegin() const;\r
42     bool            isHistoryEnd() const;\r
43     bool            isMarked(const QModelIndex &index) const;\r
44     QFileInfoList   markedItems() const;\r
45     QModelIndex     mkdir(const QString &name);\r
46     QStringList     nameFilters() const;\r
47     QPixmap         pixmap(const QModelIndex &index, const QSize &size) const;\r
48     QString         rootPath() const;\r
49     QModelIndex     search(const QString &value, const QModelIndex &start = QModelIndex(), int step = 1);\r
50     void            setActive();\r
51     void            setFilter(QDir::Filters filters);\r
52     void            setHistoryAt(const QString &path);\r
53     void            setNameFilters(const QStringList &nameFiltes);\r
54     void            setRootPath(const QString &path, bool addHistory = true);\r
55     void            setSorting(QDir::SortFlags sort);\r
56     QDir::SortFlags sorting() const;\r
57     QModelIndex     touch(const QString &name);\r
58     void            updateAppearance(const Preferences &prefs);\r
59 \r
60     static FolderModel* activeModel();\r
61 \r
62 signals:\r
63 \r
64 public slots:\r
65     void    fsWatcher_directoryChanged(const QString &path);\r
66     void    onCdHome();\r
67     void    onCdRoot();\r
68     void    onCdUp();\r
69     void    onHistoryBack();\r
70     void    onHistoryForward();\r
71     void    onMarkAll();\r
72     void    onMarkAllFiles();\r
73     void    onMarkAllOff();\r
74     void    onMarkInvert();\r
75     void    refresh();\r
76     void    thumbnail_Ready(const QString &path, const QPixmap &pixmap);\r
77 \r
78 private:\r
79     static FolderModel* m_activeModel;\r
80 \r
81     typedef QMap<QString, int> CheckContainer;\r
82     typedef QMap<QString, QPixmap> PixmapContainer;\r
83     typedef QMap<QString, QDateTime> DateTimeContainer;\r
84 \r
85     QString             m_error;\r
86     QDir                m_dir;\r
87     QFileInfoList       m_fileInfoList;\r
88     CheckContainer      m_checkStates;\r
89     QFileIconProvider   m_IconProvider;\r
90     QFileSystemWatcher  m_fsWatcher;\r
91     History             m_history;\r
92     int                 m_historyPos;\r
93     ThumbnailWorker*    m_worker;\r
94     PixmapContainer     m_pixmapCache;\r
95     DateTimeContainer   m_lastModifiedCache;\r
96     QMutex              m_pixmapCacheMutex;\r
97     QPalette            m_Palette;\r
98     QFont               m_font;\r
99 \r
100     bool    isDotFile(const QModelIndex &index) const;\r
101     void    setError(const QString &error = QString());\r
102 \r
103     const QBrush&   base() const;\r
104     const QBrush&   text() const;\r
105     const QBrush&   marked() const;\r
106     const QBrush&   markedText() const;\r
107     const QBrush&   hidden() const;\r
108     const QBrush&   readOnly() const;\r
109     const QBrush&   system() const;\r
110 \r
111     // QAbstractItemModel interface\r
112 public:\r
113     int rowCount(const QModelIndex &parent = QModelIndex()) const;\r
114     int columnCount(const QModelIndex &parent = QModelIndex()) const;\r
115     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;\r
116     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;\r
117     Qt::ItemFlags flags(const QModelIndex &index) const;\r
118     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);\r
119     Qt::DropActions supportedDropActions() const;\r
120     QStringList mimeTypes() const;\r
121 };\r
122 \r
123 inline QString FolderModel::error() const\r
124 {\r
125     return m_error;\r
126 }\r
127 \r
128 inline QFileInfo FolderModel::fileInfo(const QModelIndex &index) const\r
129 {\r
130     Q_ASSERT(index.isValid());\r
131     return m_fileInfoList[index.row()];\r
132 }\r
133 \r
134 inline QString FolderModel::fileName(const QModelIndex &index) const\r
135 {\r
136     Q_ASSERT(index.isValid());\r
137     return m_fileInfoList[index.row()].fileName();\r
138 }\r
139 \r
140 inline QString FolderModel::filePath(const QModelIndex &index) const\r
141 {\r
142     Q_ASSERT(index.isValid());\r
143     return m_fileInfoList[index.row()].absoluteFilePath();\r
144 }\r
145 \r
146 inline QDir::Filters FolderModel::filter() const\r
147 {\r
148     return m_dir.filter();\r
149 }\r
150 \r
151 inline const FolderModel::History &FolderModel::history() const\r
152 {\r
153     return m_history;\r
154 }\r
155 \r
156 inline bool FolderModel::isActive() const\r
157 {\r
158     return m_activeModel == this;\r
159 }\r
160 \r
161 inline bool FolderModel::isDir(const QModelIndex &index) const\r
162 {\r
163     return m_fileInfoList[index.row()].isDir();\r
164 }\r
165 \r
166 inline bool FolderModel::isHistoryBegin() const\r
167 {\r
168     return m_historyPos == 0;\r
169 }\r
170 \r
171 inline bool FolderModel::isHistoryEnd() const\r
172 {\r
173     return m_historyPos == m_history.size() - 1;\r
174 }\r
175 \r
176 inline bool FolderModel::isDotFile(const QModelIndex &index) const\r
177 {\r
178     return fileName(index).lastIndexOf(".") == 0;\r
179 }\r
180 \r
181 inline QStringList FolderModel::nameFilters() const\r
182 {\r
183     return m_dir.nameFilters();\r
184 }\r
185 \r
186 inline void FolderModel::refresh()\r
187 {\r
188     setRootPath(rootPath());\r
189 }\r
190 \r
191 inline QString FolderModel::rootPath() const\r
192 {\r
193     return m_dir.absolutePath();\r
194 }\r
195 \r
196 inline void FolderModel::setActive()\r
197 {\r
198     m_activeModel = this;\r
199 }\r
200 \r
201 inline void FolderModel::setError(const QString &error)\r
202 {\r
203     m_error = error;\r
204 }\r
205 \r
206 inline void FolderModel::setFilter(QDir::Filters filters)\r
207 {\r
208     m_dir.setFilter(filters);\r
209 }\r
210 \r
211 inline void FolderModel::setSorting(QDir::SortFlags sort)\r
212 {\r
213     m_dir.setSorting(sort);\r
214 }\r
215 \r
216 inline void FolderModel::setNameFilters(const QStringList &nameFiltes)\r
217 {\r
218     m_dir.setNameFilters(nameFiltes);\r
219 }\r
220 \r
221 inline QDir::SortFlags FolderModel::sorting() const\r
222 {\r
223     return m_dir.sorting();\r
224 }\r
225 \r
226 inline void FolderModel::fsWatcher_directoryChanged(const QString &path)\r
227 {\r
228     Q_UNUSED(path);\r
229     refresh();\r
230 }\r
231 \r
232 inline const QBrush& FolderModel::base() const\r
233 {\r
234     return m_Palette.base();\r
235 }\r
236 inline const QBrush& FolderModel::text() const\r
237 {\r
238     return m_Palette.text();\r
239 }\r
240 inline const QBrush& FolderModel::marked() const\r
241 {\r
242     return m_Palette.highlight();\r
243 }\r
244 inline const QBrush& FolderModel::markedText() const\r
245 {\r
246     return m_Palette.highlightedText();\r
247 }\r
248 inline const QBrush& FolderModel::hidden() const\r
249 {\r
250     return m_Palette.dark();\r
251 }\r
252 inline const QBrush& FolderModel::readOnly() const\r
253 {\r
254     return m_Palette.light();\r
255 }\r
256 inline const QBrush& FolderModel::system() const\r
257 {\r
258     return m_Palette.mid();\r
259 }\r
260 #endif // FOLDERMODEL_H\r