picturemodel.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "picturemodel.h"
  2. #include <ftw.h>
  3. #include <stdio.h>
  4. #include <QDir>
  5. #include <QDebug>
  6. PictureModel* PictureModel::model = 0;
  7. int handleDirNode(const char *fpath, const struct stat *sb, int type, struct FTW *ftwbuf) {
  8. if (type == FTW_F) {
  9. PictureModel::instance()->addPath(fpath);
  10. }
  11. return 0;
  12. }
  13. PictureModel::PictureModel(QObject *parent)
  14. { /**/ }
  15. PictureModel *PictureModel::instance()
  16. {
  17. if (!model) {
  18. model = new PictureModel();
  19. }
  20. return model;
  21. }
  22. bool PictureModel::setModelRoot(const QString &root)
  23. {
  24. qDebug() << "Flattening" << root;
  25. QDir currentDir(root);
  26. if (!currentDir.exists()) {
  27. qDebug() << "Being told to watch a non existent directory";
  28. return false;
  29. }
  30. // QString dirName = currentDir.dirName();
  31. // qDebug() << dirName;
  32. // currentDir.cdUp();
  33. // QDir::setCurrent(currentDir.path());
  34. nftw(root.toLatin1().data(), handleDirNode, 1000, FTW_PHYS);
  35. return true;
  36. }
  37. int PictureModel::rowCount(const QModelIndex &parent) const
  38. {
  39. return paths.length();
  40. }
  41. QVariant PictureModel::data(const QModelIndex &index, int role) const
  42. {
  43. if (index.row() < 0 || index.row() >= paths.length())
  44. return QVariant();
  45. return paths.at(index.row());
  46. }
  47. bool PictureModel::addPath(const QString &path)
  48. {
  49. if (!extensions.isEmpty()) {
  50. QString extension = path.mid(path.length() - 3);
  51. if (!extensions.contains(extension))
  52. return false;
  53. }
  54. paths << path;
  55. return true;
  56. }
  57. void PictureModel::addSupportedExtension(const QString &extension)
  58. {
  59. extensions << extension;
  60. }
  61. QHash<int, QByteArray> PictureModel::roleNames() const
  62. {
  63. QHash<int, QByteArray> roles;
  64. roles[PathRole] = "path";
  65. return roles;
  66. }