filesystemmodel.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "filesystemmodel.h"
  2. #include <QFile>
  3. #include <QFileInfo>
  4. #include <QDateTime>
  5. #include <QDebug>
  6. #include <QDesktopServices>
  7. #include "common.h"
  8. #include <QDir>
  9. FileSystemModel::FileSystemModel(QObject *parent) :
  10. QFileSystemModel(parent)
  11. {
  12. QHash<int, QByteArray> roles;
  13. roles[FileNameRole] = "fileName";
  14. roles[FilePathRole] = "filePath";
  15. roles[SizeRole] = "size";
  16. roles[LastModifiedRole] = "lastModified";
  17. setRoleNames(roles);
  18. // setSorting(QDir::DirsFirst);
  19. }
  20. bool FileSystemModel::isDir(const QModelIndex &index) const
  21. {
  22. return QFileSystemModel::isDir(index);
  23. }
  24. QString FileSystemModel::filePath(const QModelIndex &index) const
  25. {
  26. return QFileSystemModel::filePath(index);
  27. }
  28. QVariant FileSystemModel::data(const QModelIndex &index, int role) const
  29. {
  30. if ( role == SizeRole)
  31. {
  32. QFileInfo info(data(index,FilePathRole).toString());
  33. return Common::sizeToString(info.size());
  34. }
  35. if ( role == LastModifiedRole)
  36. {
  37. QFileInfo info(data(index,FilePathRole).toString());
  38. return info.lastModified();
  39. }
  40. return QFileSystemModel::data(index,role);
  41. }
  42. void FileSystemModel::setDir(const QString &path)
  43. {
  44. setRootPath(path);
  45. mPathIndex = index(path);
  46. emit pathIndexChanged();
  47. }
  48. void FileSystemModel::actionRename(const QModelIndex & index, const QString &name)
  49. {
  50. }
  51. void FileSystemModel::actionRemove(const QModelIndex & index)
  52. {
  53. remove(index);
  54. }
  55. void FileSystemModel::actionMkdir(const QModelIndex & index, const QString &name)
  56. {
  57. qDebug()<<"mkdir "<<name;
  58. if ( index.parent().isValid())
  59. mkdir(index.parent(), name);
  60. }