picturemodel.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "picturemodel.h"
  2. #include <QDir>
  3. #include <QDebug>
  4. #include <QCoreApplication>
  5. struct FSNode {
  6. FSNode(const QString& rname, const FSNode *pparent = nullptr)
  7. : name(rname),
  8. parent(pparent) { /**/ }
  9. static QString qualifyNode(const FSNode *node);
  10. const QString name;
  11. const FSNode *parent;
  12. };
  13. QString FSNode::qualifyNode(const FSNode *node) {
  14. QString qualifiedPath;
  15. while(node->parent != nullptr) {
  16. qualifiedPath = "/" + node->name + qualifiedPath;
  17. node = node->parent;
  18. }
  19. qualifiedPath = node->name + qualifiedPath;
  20. return qualifiedPath;
  21. }
  22. PictureModel::PictureModel(QObject *parent)
  23. : QAbstractListModel(parent)
  24. { /**/ }
  25. PictureModel::~PictureModel()
  26. {
  27. // TODO: Destroy model
  28. }
  29. void PictureModel::addModelNode(const FSNode* parentNode)
  30. {
  31. QCoreApplication::processEvents();
  32. // TODO: Check for symlink recursion
  33. QDir parentDir(FSNode::qualifyNode(parentNode));
  34. for(const QString &currentDir : parentDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
  35. const FSNode *dir = new FSNode(currentDir, parentNode);
  36. addModelNode(dir);
  37. }
  38. for(const QString &currentFile : parentDir.entryList(QDir::Files)) {
  39. if (!extensions.isEmpty()) {
  40. QString extension = currentFile.mid(currentFile.length() - 3);
  41. if (!extensions.contains(extension))
  42. continue;
  43. }
  44. const FSNode *file = new FSNode(currentFile, parentNode);
  45. files << file;
  46. emit countChanged();
  47. }
  48. }
  49. void PictureModel::setModelRoot(const QString &root)
  50. {
  51. QDir currentDir(root);
  52. if (!currentDir.exists()) {
  53. qDebug() << "Being told to watch a non existent directory";
  54. }
  55. addModelNode(new FSNode(root));
  56. }
  57. void PictureModel::setSupportedExtensions(QStringList extensions) {
  58. this->extensions = extensions;
  59. }
  60. int PictureModel::rowCount(const QModelIndex &parent) const
  61. {
  62. Q_UNUSED(parent)
  63. return files.length();
  64. }
  65. QUrl PictureModel::randomPicture() const
  66. {
  67. if (files.size() <= 0)
  68. return QString("qrc:///qt_logo_green_rgb.png");
  69. return QUrl::fromLocalFile(FSNode::qualifyNode(files.at(qrand()%files.size())));
  70. }
  71. QVariant PictureModel::data(const QModelIndex &index, int role) const
  72. {
  73. Q_UNUSED(role)
  74. if (index.row() < 0 || index.row() >= files.length())
  75. return QVariant();
  76. const FSNode *node = files.at(index.row());
  77. return FSNode::qualifyNode(node);
  78. }
  79. void PictureModel::addSupportedExtension(const QString &extension)
  80. {
  81. extensions << extension;
  82. }
  83. QHash<int, QByteArray> PictureModel::roleNames() const
  84. {
  85. QHash<int, QByteArray> roles;
  86. roles[PathRole] = "path";
  87. return roles;
  88. }