PCManFM-Qt
foldermodel.h
1 /*
2 
3  Copyright (C) 2012 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 
21 #ifndef FM_FOLDERMODEL_H
22 #define FM_FOLDERMODEL_H
23 
24 #include "libfmqtglobals.h"
25 #include <QAbstractListModel>
26 #include <QIcon>
27 #include <QImage>
28 #include <libfm/fm.h>
29 #include <QList>
30 #include <QVector>
31 #include <QLinkedList>
32 #include <QPair>
33 #include "foldermodelitem.h"
34 
35 namespace Fm {
36 
37 class LIBFM_QT_API FolderModel : public QAbstractListModel {
38 Q_OBJECT
39 public:
40 
41  enum Role {
42  FileInfoRole = Qt::UserRole
43  };
44 
45  enum ColumnId {
46  ColumnFileName,
47  ColumnFileType,
48  ColumnFileSize,
49  ColumnFileMTime,
50  ColumnFileOwner,
51  NumOfColumns
52  };
53 
54 public:
55  FolderModel();
56  virtual ~FolderModel();
57 
58  FmFolder* folder() {
59  return folder_;
60  }
61  void setFolder(FmFolder* new_folder);
62 
63  FmPath* path() {
64  return folder_ ? fm_folder_get_path(folder_) : NULL;
65  }
66 
67  int rowCount(const QModelIndex & parent = QModelIndex()) const;
68  int columnCount (const QModelIndex & parent) const;
69  QVariant data(const QModelIndex & index, int role) const;
70  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
71  QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const;
72  QModelIndex parent( const QModelIndex & index ) const;
73  // void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
74 
75  Qt::ItemFlags flags(const QModelIndex & index) const;
76 
77  virtual QStringList mimeTypes() const;
78  virtual QMimeData* mimeData(const QModelIndexList & indexes) const;
79  virtual Qt::DropActions supportedDropActions() const;
80  virtual bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent);
81 
82  FmFileInfo* fileInfoFromIndex(const QModelIndex& index) const;
83  FolderModelItem* itemFromIndex(const QModelIndex& index) const;
84  QImage thumbnailFromIndex(const QModelIndex& index, int size);
85 
86  void cacheThumbnails(int size);
87  void releaseThumbnails(int size);
88 
89 Q_SIGNALS:
90  void thumbnailLoaded(const QModelIndex& index, int size);
91 
92 public Q_SLOTS:
93  void updateIcons();
94 
95 protected:
96  static void onStartLoading(FmFolder* folder, gpointer user_data);
97  static void onFinishLoading(FmFolder* folder, gpointer user_data);
98  static void onFilesAdded(FmFolder* folder, GSList* files, gpointer user_data);
99  static void onFilesChanged(FmFolder* folder, GSList* files, gpointer user_data);
100  static void onFilesRemoved(FmFolder* folder, GSList* files, gpointer user_data);
101  static void onThumbnailLoaded(FmThumbnailLoader *res, gpointer user_data);
102 
103  void insertFiles(int row, FmFileInfoList* files);
104  void removeAll();
105  QList<FolderModelItem>::iterator findItemByPath(FmPath* path, int* row);
106  QList<FolderModelItem>::iterator findItemByName(const char* name, int* row);
107  QList<FolderModelItem>::iterator findItemByFileInfo(FmFileInfo* info, int* row);
108 
109 private:
110  FmFolder* folder_;
111  // FIXME: should we use a hash table here so item lookup becomes much faster?
112  QList<FolderModelItem> items;
113 
114  // record what size of thumbnails we should cache in an array of <size, refCount> pairs.
115  QVector<QPair<int, int> > thumbnailRefCounts;
116  QLinkedList<FmThumbnailLoader*> thumbnailResults;
117 };
118 
119 }
120 
121 #endif // FM_FOLDERMODEL_H
Definition: appchoosercombobox.cpp:26
Definition: foldermodel.h:37
Definition: foldermodelitem.h:34