54
53
QHash<QByteArray, int> roleMapping;
57
static bool fileCompareAscending(const QFileInfo &a, const QFileInfo &b)
59
if (a.isDir() && !b.isDir())
62
if (b.isDir() && !a.isDir())
65
return QString::localeAwareCompare(a.fileName(), b.fileName()) < 0;
69
static bool fileCompareDescending(const QFileInfo &a, const QFileInfo &b)
71
if (a.isDir() && !b.isDir())
74
if (b.isDir() && !a.isDir())
77
return QString::localeAwareCompare(a.fileName(), b.fileName()) > 0;
80
static bool dateCompareDescending(const QFileInfo &a, const QFileInfo &b)
82
if (a.isDir() && !b.isDir())
85
if (b.isDir() && !a.isDir())
88
return a.lastModified() > b.lastModified();
91
static bool dateCompareAscending(const QFileInfo &a, const QFileInfo &b)
93
if (a.isDir() && !b.isDir())
96
if (b.isDir() && !a.isDir())
99
return a.lastModified() < b.lastModified();
103
* Sort was originaly done in \ref onItemsAdded() and that code is now in \ref addItem(),
104
* the reason to keep doing sort and do not let QDir does it is that when adding new items
105
* by \ref mkdir() or \paste() it is not necessary to call refresh() to load the entire directory
106
* to organize it items again. New items order/position are organized by \ref addItem()
109
static CompareFunction availableCompareFunctions[2][2] =
111
{fileCompareAscending, fileCompareDescending}
112
,{dateCompareAscending, dateCompareDescending}
57
118
class DirListWorker : public IORequest
105
165
, mShowDirectories(true)
106
166
, mAwaitingResults(false)
107
167
, mShowHiddenFiles(false)
168
, mSortBy(SortByName)
169
, mSortOrder(SortAscending)
170
, mCompareFunction(0)
108
171
, m_fsAction(new FileSystemAction(this) )
110
173
mNameFilters = QStringList() << "*";
203
267
return QVariant();
205
269
if (role == Qt::DecorationRole)
207
QModelIndex idxIcon = createIndex(index.row(), IconSourceRole - FileNameRole);
208
QString iconPath = data(idxIcon, Qt::DisplayRole).toString();
209
QPixmap pic(iconPath);
271
if (index.column() == 0)
273
return QFileIconProvider().icon(mDirectoryContents.at(index.row()));
212
277
role = FileNameRole + index.column();
314
379
emit pathChanged(pathName);
317
static bool fileCompare(const QFileInfo &a, const QFileInfo &b)
319
if (a.isDir() && !b.isDir())
322
if (b.isDir() && !a.isDir())
325
return QString::localeAwareCompare(a.fileName(), b.fileName()) < 0;
328
383
void DirModel::onResultsFetched() {
329
384
if (mAwaitingResults) {
633
688
* \sa insertedRow()
635
690
int DirModel::addItem(const QFileInfo &fi)
637
692
QVector<QFileInfo>::Iterator it = qLowerBound(mDirectoryContents.begin(),
638
693
mDirectoryContents.end(),
641
696
int idx = mDirectoryContents.count();
642
697
if (it == mDirectoryContents.end()) {
643
698
beginInsertRows(QModelIndex(), mDirectoryContents.count(), mDirectoryContents.count());
703
759
void DirModel::setShowHiddenFiles(bool show)
705
mShowHiddenFiles = show;
707
emit showHiddenFilesChanged();
761
if (show != mShowHiddenFiles)
763
mShowHiddenFiles = show;
765
emit showHiddenFilesChanged();
770
void DirModel::toggleShowDirectories()
772
setShowDirectories(!mShowDirectories);
776
void DirModel::toggleShowHiddenFiles()
778
setShowHiddenFiles(!mShowHiddenFiles);
783
DirModel::getSortBy() const
789
void DirModel::setSortBy(SortBy field)
791
if (field != mSortBy)
794
setCompareAndReorder();
795
emit sortByChanged();
801
DirModel::getSortOrder() const
806
void DirModel::setSortOrder(SortOrder order)
808
if ( order != mSortOrder )
811
setCompareAndReorder();
812
emit sortOrderChanged();
817
void DirModel::toggleSortOrder()
819
SortOrder order = static_cast<SortOrder> (mSortOrder ^ 1);
824
void DirModel::toggleSortBy()
826
SortBy by = static_cast<SortBy> (mSortBy ^ 1);
831
* \brief DirModel::setCompareAndReorder() called when SortOrder or SortBy change
833
* It does not reload items from disk, just reorganize items from \a mDirectoryContents array
835
void DirModel::setCompareAndReorder()
837
mCompareFunction = availableCompareFunctions[mSortBy][mSortOrder];
838
if (mDirectoryContents.count() > 0)
840
QVector<QFileInfo> tmpDirectoryContents = mDirectoryContents;
842
mDirectoryContents.clear();
844
for(int counter=0; counter < tmpDirectoryContents.count(); counter++)
846
addItem(tmpDirectoryContents.at(counter));
710
851
// for dirlistworker