/* 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 #include #include #include #include #include #include "filesystemfileproxy.h" #include "helper.h" FilesystemFileProxy::FilesystemFileProxy(QObject *parent) : QSortFilterProxyModel(parent) {} QVariant FilesystemFileProxy::data(const QModelIndex &index, int role) const{ if(!index.isValid()){ return QVariant(); } if(role == Qt::DisplayRole){ if(index.column() == 2){ QString filePath = index.data(QFileSystemModel::FilePathRole).toString(); QString mimeType = Helper::mimeType(filePath); if(mimeType.contains('/')){ mimeType = mimeType.split('/').at(1); mimeType = mimeType.remove(QRegExp("^x-")); return mimeType; } } if(index.column() == 3){ QString filePath = index.data(QFileSystemModel::FilePathRole).toString(); QFileInfo fi(filePath); return fi.lastModified().toString("MM-dd-yyyy hh:mm"); } } return QSortFilterProxyModel::data(index, role); } bool FilesystemFileProxy::filterAcceptsRow(int sourcerow, const QModelIndex &sourceparent) const{ QFileSystemModel *m = static_cast(sourceModel()); QModelIndex idx = m->index(sourcerow, 0, sourceparent); if(!idx.isValid()){ return false; } QString fName = idx.data().toString(); if(fName == "." ){ return false; } return QSortFilterProxyModel::filterAcceptsRow(sourcerow, sourceparent); } bool FilesystemFileProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const { if(left.model()->headerData(left.column(), Qt::Horizontal).toString() == tr("Name")){ if(left.data().toString() == ".."){ if(sortOrder() == Qt::AscendingOrder){ return true; }else{ return false; } } QFileSystemModel *source = static_cast(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(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); }