17class QQuickFolderListModelPrivate
19 Q_DECLARE_PUBLIC(QQuickFolderListModel)
22 QQuickFolderListModelPrivate(QQuickFolderListModel *q) : q_ptr(q) { }
24 QQuickFolderListModel *q_ptr;
27 FileInfoThread fileInfoThread;
28 QList<FileProperty> data;
29 QHash<
int, QByteArray> roleNames;
30 QQuickFolderListModel::SortField sortField = QQuickFolderListModel::Name;
31 QStringList nameFilters = { QLatin1String(
"*") };
32 QQuickFolderListModel::Status status = QQuickFolderListModel::Null;
33 bool sortReversed =
false;
34 bool showFiles =
true;
36 bool showDirsFirst =
false;
37 bool showDotAndDotDot =
false;
39 bool showDotDot =
false;
40 bool showOnlyReadable =
false;
41 bool showHidden =
false;
42 bool caseSensitive =
true;
43 bool sortCaseSensitive =
true;
44 bool resettingModel =
false;
46 ~QQuickFolderListModelPrivate() {}
50 void finishModelReset();
53 void _q_directoryChanged(
const QString &directory,
const QList<FileProperty> &list);
54 void _q_directoryUpdated(
const QString &directory,
const QList<FileProperty> &list,
int fromIndex,
int toIndex);
55 void _q_sortFinished(
const QList<FileProperty> &list);
56 void _q_statusChanged(QQuickFolderListModel::Status s);
58 static QString resolvePath(
const QUrl &path);
62void QQuickFolderListModelPrivate::init()
64 Q_Q(QQuickFolderListModel);
65 qRegisterMetaType<QList<FileProperty> >(
"QList<FileProperty>");
66 qRegisterMetaType<QQuickFolderListModel::Status>(
"QQuickFolderListModel::Status");
67 q->connect(&fileInfoThread, SIGNAL(directoryChanged(QString,QList<FileProperty>)),
68 q, SLOT(_q_directoryChanged(QString,QList<FileProperty>)));
69 q->connect(&fileInfoThread, SIGNAL(directoryUpdated(QString,QList<FileProperty>,
int,
int)),
70 q, SLOT(_q_directoryUpdated(QString,QList<FileProperty>,
int,
int)));
71 q->connect(&fileInfoThread, SIGNAL(sortFinished(QList<FileProperty>)),
72 q, SLOT(_q_sortFinished(QList<FileProperty>)));
73 q->connect(&fileInfoThread, SIGNAL(statusChanged(QQuickFolderListModel::Status)),
74 q, SLOT(_q_statusChanged(QQuickFolderListModel::Status)));
75 q->connect(q, SIGNAL(rowCountChanged()), q, SIGNAL(countChanged()));
79void QQuickFolderListModelPrivate::updateSorting()
81 Q_Q(QQuickFolderListModel);
83 QDir::SortFlags flags;
86 case QQuickFolderListModel::Unsorted:
87 flags |= QDir::Unsorted;
89 case QQuickFolderListModel::Name:
92 case QQuickFolderListModel::Time:
95 case QQuickFolderListModel::Size:
98 case QQuickFolderListModel::Type:
103 emit q->layoutAboutToBeChanged();
106 flags |= QDir::Reversed;
107 if (!sortCaseSensitive)
108 flags |= QDir::IgnoreCase;
110 fileInfoThread.setSortFlags(flags);
113void QQuickFolderListModelPrivate::finishModelReset()
115 Q_Q(QQuickFolderListModel);
116 const bool wasDataEmpty = data.isEmpty();
118 qCDebug(lcFolderListModel) <<
"about to emit endResetModel";
121 emit q->rowCountChanged();
122 if (status != QQuickFolderListModel::Null) {
123 status = QQuickFolderListModel::Null;
124 emit q->statusChanged();
126 resettingModel =
false;
129void QQuickFolderListModelPrivate::_q_directoryChanged(
const QString &directory,
const QList<FileProperty> &list)
131 qCDebug(lcFolderListModel) <<
"_q_directoryChanged called with directory" << directory;
132 Q_Q(QQuickFolderListModel);
134 if (!resettingModel) {
135 resettingModel =
true;
136 q->beginResetModel();
141 qCDebug(lcFolderListModel) <<
"- endResetModel called";
142 emit q->rowCountChanged();
143 emit q->folderChanged();
144 resettingModel =
false;
148void QQuickFolderListModelPrivate::_q_directoryUpdated(
const QString &directory,
const QList<FileProperty> &list,
int fromIndex,
int toIndex)
150 Q_Q(QQuickFolderListModel);
154 if (data.size() == list.size()) {
155 QModelIndex modelIndexFrom = q->createIndex(fromIndex, 0);
156 QModelIndex modelIndexTo = q->createIndex(toIndex, 0);
158 emit q->dataChanged(modelIndexFrom, modelIndexTo);
164 if (data.size() > 0) {
165 q->beginRemoveRows(parent, 0, data.size() - 1);
169 if (list.size() > 0) {
170 if (toIndex > list.size() - 1)
171 toIndex = list.size() - 1;
172 q->beginInsertRows(parent, 0, data.size() - 1);
175 emit q->rowCountChanged();
179void QQuickFolderListModelPrivate::_q_sortFinished(
const QList<FileProperty> &list)
181 Q_Q(QQuickFolderListModel);
182 qCDebug(lcFolderListModel) <<
"_q_sortFinished called with" << list.size() <<
"files";
185 if (data.size() > 0) {
186 qCDebug(lcFolderListModel) <<
"- removing all existing rows...";
187 q->beginRemoveRows(parent, 0, data.size()-1);
190 qCDebug(lcFolderListModel) <<
"- ...removed all existing rows";
193 qCDebug(lcFolderListModel) <<
"- inserting sorted rows...";
194 q->beginInsertRows(parent, 0, list.size()-1);
197 qCDebug(lcFolderListModel) <<
"- ... inserted sorted rows";
210QString QQuickFolderListModelPrivate::resolvePath(
const QUrl &path)
212 QString localPath = QQmlFile::urlToLocalFileOrQrc(path);
213 QUrl localUrl = QUrl(localPath);
214 if (!localUrl.authority().isEmpty())
216 QString fullPath = localUrl.path();
217 if (localUrl.scheme().size())
218 fullPath = localUrl.scheme() + QLatin1Char(
':') + fullPath;
219 return QDir::cleanPath(fullPath);
334QQuickFolderListModel::QQuickFolderListModel(QObject *parent)
335 : QAbstractListModel(parent), d_ptr(
new QQuickFolderListModelPrivate(
this))
337 Q_D(QQuickFolderListModel);
338 d->roleNames[FileNameRole] =
"fileName";
339 d->roleNames[FilePathRole] =
"filePath";
340 d->roleNames[FileBaseNameRole] =
"fileBaseName";
341 d->roleNames[FileSuffixRole] =
"fileSuffix";
342 d->roleNames[FileSizeRole] =
"fileSize";
343 d->roleNames[FileLastModifiedRole] =
"fileModified";
344 d->roleNames[FileLastReadRole] =
"fileAccessed";
345 d->roleNames[FileIsDirRole] =
"fileIsDir";
346 d->roleNames[FileUrlRole] =
"fileUrl";
354QVariant QQuickFolderListModel::data(
const QModelIndex &index,
int role)
const
356 Q_D(
const QQuickFolderListModel);
359 const int row = index.row();
360 if (row < 0 || row >= d->data.size())
366 rv = d->data.at(row).fileName();
369 rv = d->data.at(row).filePath();
371 case FileBaseNameRole:
372 rv = d->data.at(row).baseName();
375 rv = d->data.at(row).suffix();
378 rv = d->data.at(row).size();
380 case FileLastModifiedRole:
381 rv = d->data.at(row).lastModified();
383 case FileLastReadRole:
384 rv = d->data.at(row).lastRead();
387 rv = d->data.at(row).isDir();
390 rv = QUrl::fromLocalFile(d->data.at(row).filePath());
440void QQuickFolderListModel::setFolder(
const QUrl &folder)
442 Q_D(QQuickFolderListModel);
444 if (folder == d->currentDir)
452 if (d->resettingModel)
453 d->finishModelReset();
455 d->resettingModel =
true;
457 QString resolvedPath = QQuickFolderListModelPrivate::resolvePath(folder);
459 qCDebug(lcFolderListModel) <<
"about to emit beginResetModel since our folder was set to" << folder;
463 if (!d->currentDir.isEmpty())
464 d->fileInfoThread.removePath(d->currentDir.path());
466 d->currentDir = folder;
468 QFileInfo info(resolvedPath);
469 if (!info.exists() || !info.isDir()) {
470 d->finishModelReset();
474 d->fileInfoThread.setPath(resolvedPath);
491void QQuickFolderListModel::setRootFolder(
const QUrl &path)
493 Q_D(QQuickFolderListModel);
498 QString resolvedPath = QQuickFolderListModelPrivate::resolvePath(path);
500 QFileInfo info(resolvedPath);
501 if (!info.exists() || !info.isDir())
503 if (path != d->rootDir) {
504 d->fileInfoThread.setRootPath(resolvedPath);
506 emit rootFolderChanged();
552void QQuickFolderListModel::setNameFilters(
const QStringList &filters)
554 Q_D(QQuickFolderListModel);
555 if (d->nameFilters == filters)
557 d->fileInfoThread.setNameFilters(filters);
558 d->nameFilters = filters;
559 emit nameFilterChanged();
566void QQuickFolderListModel::componentComplete()
568 Q_D(QQuickFolderListModel);
569 QString localPath = QQmlFile::urlToLocalFileOrQrc(d->currentDir);
570 if (localPath.isEmpty() || !QDir(localPath).exists())
571 setFolder(QUrl::fromLocalFile(QDir::currentPath()));
572 d->fileInfoThread.start(QThread::LowPriority);
727void QQuickFolderListModel::setShowDirsFirst(
bool on)
729 Q_D(QQuickFolderListModel);
731 if (on != d->showDirsFirst) {
732 d->fileInfoThread.setShowDirsFirst(on);
733 d->showDirsFirst = on;
734 emit showDirsFirstChanged();
755void QQuickFolderListModel::setShowDotAndDotDot(
bool on)
757 Q_D(QQuickFolderListModel);
759 if (on != d->showDotAndDotDot) {
760 d->fileInfoThread.setShowDotAndDotDot(on);
761 d->showDotAndDotDot = on;
762 emit showDotAndDotDotChanged();
865void QQuickFolderListModel::setShowOnlyReadable(
bool on)
867 Q_D(QQuickFolderListModel);
869 if (on != d->showOnlyReadable) {
870 d->fileInfoThread.setShowOnlyReadable(on);
871 d->showOnlyReadable = on;
872 emit showOnlyReadableChanged();
891void QQuickFolderListModel::setCaseSensitive(
bool on)
893 Q_D(QQuickFolderListModel);
895 if (on != d->caseSensitive) {
896 d->fileInfoThread.setCaseSensitive(on);
897 d->caseSensitive = on;
898 emit caseSensitiveChanged();
955void QQuickFolderListModel::setSortCaseSensitive(
bool on)
957 Q_D(QQuickFolderListModel);
959 if (on != d->sortCaseSensitive) {
960 d->sortCaseSensitive = on;
962 emit sortCaseSensitiveChanged();