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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
#include "androidfiledialog.h"
#include "androidutils.h"
#include "ui_androidfiledialog.h"
#include <QAbstractTableModel>
#include <QApplication>
#include <QFileInfo>
typedef QLatin1String _;
static bool androidFileLessThan(const AndroidUtils::FileInfo &file1, const AndroidUtils::FileInfo &file2)
{
if (file1.type != file2.type)
return file1.type == AndroidUtils::FileInfo::Directory;
return file1.name < file2.name;
}
const int FileInfoRole = Qt::UserRole + 1;
static QString humanSize(double size)
{
QStringList list;
list << " KB" << " MB" << " GB";
QStringListIterator i(list);
QString unit("bytes");
while(size >= 1024.0 && i.hasNext())
{
unit = i.next();
size /= 1024.0;
}
return QString::number(size, 'f', 2) + unit;
}
class AndroidFileDialogModel : public QAbstractTableModel
{
public:
AndroidFileDialogModel(QObject * parent = 0)
: QAbstractTableModel(parent)
{}
void setDir(const QString &dir)
{
m_fileList = m_androidUtils.ls(dir + _("/"));
setFilterSuffix(m_suffix);
}
void setFilterSuffix(const QString &suffix)
{
m_suffix = suffix;
beginResetModel();
if (!m_suffix.isEmpty()) {
AndroidUtils::FileInfoList::iterator it = m_fileList.begin();
while(it != m_fileList.end()) {
if (it->type == AndroidUtils::FileInfo::File &&
!it->name.endsWith(m_suffix)) {
it = m_fileList.erase(it);
} else {
++it;
}
}
}
std::sort(m_fileList.begin(), m_fileList.end(), androidFileLessThan);
endResetModel();
}
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
if (!index.isValid() || index.row() >= m_fileList.size())
return QVariant();
QVariant ret;
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case 0:
ret = m_fileList.at(index.row()).name;
break;
case 1:
{
const AndroidUtils::FileInfo &file = m_fileList.at(index.row());
if (file.type == AndroidUtils::FileInfo::File)
ret = humanSize(file.size);
}
break;
default:
break;
}
break;
case Qt::DecorationRole:
if (index.column() == 0) {
if (m_fileList.at(index.row()).type == AndroidUtils::FileInfo::File)
ret = qApp->style()->standardIcon(QStyle::SP_FileIcon);
else
ret = qApp->style()->standardIcon(QStyle::SP_DirIcon);
}
break;
case Qt::TextAlignmentRole:
ret = int(index.column() == 1 ? Qt::AlignRight : Qt::AlignLeft) | Qt::AlignVCenter;
break;
case FileInfoRole:
ret = QVariant::fromValue(m_fileList.at(index.row()));
break;
default:
break;
}
return ret;
}
virtual QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const {
if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
return QVariant();
QVariant ret;
switch (section) {
case 0:
ret = tr("Name");
break;
case 1:
ret = tr("Size");
break;
}
return ret;
}
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const
{
return 2;
}
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const
{
if (parent.isValid())
return 0;
return m_fileList.size();
}
private:
AndroidUtils m_androidUtils;
QString m_suffix;
AndroidUtils::FileInfoList m_fileList;
};
AndroidFileDialog::AndroidFileDialog(Mode mode, QWidget *parent)
: QDialog(parent),
m_ui(new Ui::AndroidFileDialog),
m_model(new AndroidFileDialogModel(this)),
m_mode(mode)
{
m_ui->setupUi(this);
m_ui->pathComboBox->setModel(&m_pathModel);
m_ui->filesView->setModel(m_model);
m_ui->upButton->setIcon(qApp->style()->standardIcon(QStyle::SP_ArrowUp));
m_ui->buttonBox->setStandardButtons(QDialogButtonBox::StandardButton(m_mode) | QDialogButtonBox::Cancel);
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(accept()));
connect(m_ui->upButton, SIGNAL(clicked()), SLOT(upFolder()));
connect(m_ui->pathComboBox, SIGNAL(activated(QString)), SLOT(setDir(QString)));
QHeaderView *hv = m_ui->filesView->horizontalHeader();
hv->setSectionResizeMode(0, QHeaderView::Stretch);
hv->setSectionResizeMode(1, QHeaderView::ResizeToContents);
connect(m_ui->filesView, SIGNAL(activated(QModelIndex)), SLOT(itemActivated(QModelIndex)));
}
AndroidFileDialog::~AndroidFileDialog()
{
delete m_model;
}
void AndroidFileDialog::setInitialPath(const QString &path)
{
AndroidUtils::FileInfoList files = AndroidUtils().ls(path);
if (files.size() == 1 && files.at(0).type == AndroidUtils::FileInfo::File) {
QFileInfo fi(path);
m_ui->fileLineEdit->setText(fi.fileName());
setDir(fi.absolutePath());
} else {
if (files.isEmpty() && m_mode == SaveFile) {
QFileInfo fi(path);
m_ui->fileLineEdit->setText(fi.fileName());
setDir(fi.absolutePath());
} else {
m_ui->fileLineEdit->setText(QString());
setDir(path);
}
}
}
void AndroidFileDialog::setSuffix(const QString &suffix)
{
m_model->setFilterSuffix(suffix);
}
QString AndroidFileDialog::filePath() const
{
return m_dir + _("/") + m_ui->fileLineEdit->text();
}
QString AndroidFileDialog::getOpenFileName(QWidget *parent, const QString &caption, const QString &path, const QString &filter)
{
AndroidFileDialog d(OpenFile, parent);
d.setWindowTitle(caption);
d.setSuffix(filter);
d.setInitialPath(path);
if (d.exec() == QDialog::Accepted)
return d.filePath();
return QString();
}
QString AndroidFileDialog::getSaveFileName(QWidget *parent, const QString &caption, const QString &path, const QString &filter)
{
AndroidFileDialog d(SaveFile, parent);
d.setWindowTitle(caption);
d.setSuffix(filter);
d.setInitialPath(path);
if (d.exec() == QDialog::Accepted)
return d.filePath();
return QString();
}
void AndroidFileDialog::accept()
{
if (m_mode == SaveFile) {
QDialog::accept();
return;
}
AndroidUtils::FileInfoList files = AndroidUtils().ls(filePath());
if (files.size() == 1 && files.at(0).type == AndroidUtils::FileInfo::File)
QDialog::accept();
}
void AndroidFileDialog::upFolder()
{
setDir(QFileInfo(m_dir).absolutePath());
}
void AndroidFileDialog::itemActivated(QModelIndex index)
{
QVariant file = index.data(FileInfoRole);
if (file.isNull())
return;
AndroidUtils::FileInfo f = file.value<AndroidUtils::FileInfo>();
if (f.type == AndroidUtils::FileInfo::Directory) {
setDir(m_dir + _("/") + f.name);
} else {
m_ui->fileLineEdit->setText(f.name);
accept();
}
}
void AndroidFileDialog::setDir(const QString &dir)
{
m_dir = dir;
while (m_dir.endsWith(QLatin1Char('/')))
m_dir.chop(1);
if (m_dir.isEmpty())
m_dir = _("/");
QStringList paths;
QString dirname = m_dir;
paths << dirname;
while (dirname != _("/")) {
dirname = QFileInfo(dirname).absolutePath();
paths << dirname;
}
m_pathModel.setStringList(paths);
m_ui->pathComboBox->setCurrentIndex(0);
m_ui->upButton->setEnabled(paths.size() > 1);
m_model->setDir(dir);
}
Q_DECLARE_METATYPE(AndroidUtils::FileInfo);
|