summaryrefslogtreecommitdiffstats
path: root/filesystemfileproxy.cpp
blob: 31059ec8511c24f746b8fe3f295eea992f7dc974 (plain)
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
/*
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version
  2 of the License, or (at your option) any later version.
*/

#include <QModelIndex>
#include <QVariant>
#include <QFileSystemModel>
#include <QFileInfo>

#include "filesystemfileproxy.h"

FilesystemFileProxy::FilesystemFileProxy(QObject *parent) : QSortFilterProxyModel(parent) {}

bool FilesystemFileProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const {
	if(left.model()->headerData(left.column(), Qt::Horizontal).toString() == tr("Name")){
		QFileSystemModel *source = static_cast<QFileSystemModel*>(sourceModel());
		if(source->isDir(left) && source->isDir(right)){
			return left.data().toString().toLower() < right.data().toString().toLower();
		}
		if(source->isDir(left)){
			return true;
		}
		if(source->isDir(right)){
			return false;
		}
		return left.data().toString().toLower() < right.data().toString().toLower();
	}
	if(left.model()->headerData(left.column(), Qt::Horizontal).toString() == tr("Size")){
		QFileSystemModel *source = static_cast<QFileSystemModel*>(sourceModel());
		QFileInfo lInfo = source->fileInfo(left);
		QFileInfo rInfo = source->fileInfo(right);
		if(lInfo.isDir() && rInfo.isDir()){
			return lInfo.fileName().toLower() < rInfo.fileName().toLower();
		}
		if(lInfo.isDir() && !rInfo.isDir()){
			return true;
		}
		return lInfo.size() < rInfo.size();
	}
	return QSortFilterProxyModel::lessThan(left, right);
}