-
Notifications
You must be signed in to change notification settings - Fork 0
/
filemodel.cpp
197 lines (163 loc) · 4.46 KB
/
filemodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Copyright (C) 2012 by Kunal Parmar <[email protected]>
*
**/
#include "filemodel.h"
#include <QDebug>
#include <QDir>
static QStringList getDrives() {
QFileInfoList fileList = QDir::drives();
QStringList drives;
foreach( QFileInfo file, fileList ){
if( /*file.isWritable() &&*/
file.filePath() != "Z:/" && file.filePath() != "D:/" ){
drives << file.filePath();
}
}
return drives;
}
FileModel::FileModel ( QObject *parent)
: QAbstractItemModel (parent)
, mCurrentDirectory ("")
, mFilesRetrieved (false)
{
QHash<int, QByteArray> roles;
roles[Qt::DisplayRole] = "caption";
roles[IconRole] = "icon";
setRoleNames(roles);
openCurrentDirectory();
}
FileModel::~FileModel ()
{}
QVariant FileModel::data (const QModelIndex &index, int role) const
{
// FileModel* model = const_cast<FileModel*>(this);
// model->updateFiles ();
if ( index.column () > 1 || !index.isValid () || index.row () >= mFiles.length () )
return QVariant ();
switch(role) {
case Qt::DisplayRole:
return mFiles[index.row ()];
case IconRole:
return isDir(index.row());
}
return QVariant ();
}
Qt::ItemFlags FileModel::flags (const QModelIndex &index) const
{
if (index.isValid ())
return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
return 0;
}
QVariant FileModel::headerData (int, Qt::Orientation, int) const
{
return QVariant ();
}
QModelIndex FileModel::index (int row, int col, const QModelIndex& parent) const
{
if (col > 1 || parent.isValid ())
return QModelIndex();
// FileModel* model = const_cast<FileModel*>(this);
// model->updateFiles ();
if (row >= mFiles.length ())
return QModelIndex();
return createIndex (row, col, 0);
}
QModelIndex FileModel::parent (const QModelIndex&) const
{
return QModelIndex();
}
bool FileModel::hasChildren (const QModelIndex& parent) const
{
return rowCount (parent);
}
int FileModel::rowCount (const QModelIndex &parent) const
{
if (parent.column() > 0 || parent.isValid ())
return 0;
// FileModel* model = const_cast<FileModel*>(this);
// model->updateFiles ();
return mFiles.length ();
}
int FileModel::columnCount (const QModelIndex&) const
{
return 2;
}
void FileModel::updateFiles ()
{
if (!mFilesRetrieved) {
// qDebug() << "CurDir:" << currentDirectory();
if( currentDirectory().isEmpty() ) {
mFiles = getDrives();
} else {
mFiles = QDir ( currentDirectory ()).entryList ( QDir::AllEntries | QDir::Writable | QDir::Readable | QDir::NoDot | QDir::NoDotDot,QDir::DirsFirst| QDir::Name);
}
// qDebug() << "retrieving " << mFiles.length () << " files" << endl;
mFilesRetrieved = true;
if (mFiles.length ()) {
beginInsertRows (QModelIndex(), 0, mFiles.length ()-1);
endInsertRows();
}
emit showEmptyDir(mFiles.length()<=0);
}
}
QString FileModel::directory() const {
QString name = QDir( mCurrentDirectory).dirName();
if( name.isEmpty()) {
return mCurrentDirectory;
}
return name;
}
bool FileModel::isDir( int index ) const
{
QString path;
if( currentDirectory().isEmpty()) {
path = mFiles[index];
} else {
path = currentDirectory()+"/"+mFiles[index];
}
QFileInfo fileInfo( path);
return !fileInfo.isFile();
}
void FileModel::setCurrentDirectory (const QString& dir) {
mCurrentDirectory = dir;
openCurrentDirectory();
}
void FileModel::openDirectory( int index)
{
if (index >= 0 && index < mFiles.length() ) {
if( currentDirectory().isEmpty()) {
mCurrentDirectory = mFiles[index];
} else {
mCurrentDirectory = currentDirectory()+"/"+mFiles[index];
}
openCurrentDirectory();
}
}
void FileModel::openCurrentDirectory()
{
mFilesRetrieved = false;
if (mFiles.length ()) {
beginRemoveRows (QModelIndex (), 0, mFiles.length ()-1);
mFiles.clear ();
endRemoveRows();
}
updateFiles();
emit directoryChanged();
}
void FileModel::goUp ()
{
if (mCurrentDirectory.isEmpty())
return;
QDir dir (mCurrentDirectory);
if ( dir.cdUp () ) {
mCurrentDirectory = dir.absolutePath ();
} else {
mCurrentDirectory = "";
}
openCurrentDirectory();
}
bool FileModel::canGoUp ()
{
return !mCurrentDirectory.isEmpty();
}