TreeModel.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #ifndef TREEMODEL_H
  2. #define TREEMODEL_H
  3. #include <QAbstractItemModel>
  4. #include <QDir>
  5. #include <QFileInfo>
  6. #include <QIcon>
  7. #include <QStandardPaths>
  8. #include <QMimeDatabase>
  9. #include <QDebug>
  10. #include <QMimeDatabase> // 新增头文件
  11. #include <QRegularExpression>
  12. #include <m_loger.h>
  13. class TreeItem {
  14. public:
  15. enum Type {
  16. Root,
  17. YearFolder,
  18. MonthFolder,
  19. DayFolder,
  20. AlbumFolder,
  21. PositionFolder,
  22. Image
  23. };
  24. explicit TreeItem(Type type, const QString &name, const QString &path,
  25. const QDateTime &lastModified = QDateTime(), TreeItem *parent = nullptr);
  26. ~TreeItem();
  27. void appendChild(TreeItem *child);
  28. TreeItem *child(int row) const;
  29. int childCount() const;
  30. int row() const;
  31. Type type() const { return m_type; }
  32. QString name() const { return m_name; }
  33. QString path() const { return m_path; }
  34. QDateTime lastModified() const { return m_lastModified; }
  35. TreeItem *parent() const { return m_parent; }
  36. void removeAllChildren();
  37. private:
  38. Type m_type;
  39. QString m_name;
  40. QString m_path;
  41. QDateTime m_lastModified;
  42. TreeItem *m_parent;
  43. QList<TreeItem*> m_children;
  44. };
  45. class TreeModel : public QAbstractItemModel
  46. {
  47. Q_OBJECT
  48. Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
  49. public:
  50. explicit TreeModel(QObject *parent = nullptr);
  51. ~TreeModel() override;
  52. // 必须重写 QAbstractItemModel 的接口
  53. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
  54. QModelIndex parent(const QModelIndex &index) const override;
  55. int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  56. int columnCount(const QModelIndex &parent = QModelIndex()) const override;
  57. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
  58. QHash<int, QByteArray> roleNames() const override;
  59. bool loading() const { return m_loading; }
  60. public slots:
  61. Q_INVOKABLE void loadChildren(const QModelIndex &index);
  62. Q_INVOKABLE QVariantList getFolderContents(const QString &folderPath) {
  63. QVariantList contents;
  64. QDir dir(folderPath);
  65. dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
  66. QFileInfoList entries = dir.entryInfoList();
  67. std::sort(entries.begin(), entries.end(), [](const QFileInfo &a, const QFileInfo &b) {
  68. // 目录优先且按时间排序
  69. if(a.isDir() && b.isDir())
  70. return a.lastModified() > b.lastModified();
  71. if(a.isDir()) return true;
  72. if(b.isDir()) return false;
  73. // 文件按名称排序
  74. return a.fileName() < b.fileName();
  75. });
  76. //QMimeDatabase mimeDb;
  77. for (const QFileInfo &entry : entries) {
  78. QVariantMap item;
  79. item["name"] = entry.fileName();
  80. item["path"] = entry.absoluteFilePath();
  81. item["isFolder"] = entry.isDir();
  82. // if (!entry.isDir()) {
  83. // QMimeType mime = mimeDb.mimeTypeForFile(entry);
  84. // item["isImage"] = mime.name().startsWith("image/");
  85. // }
  86. contents.append(item);
  87. }
  88. return contents;
  89. }
  90. void resetModel(); // 可暴露给 QML
  91. signals:
  92. void loadingChanged();
  93. private:
  94. enum RoleNames {
  95. DisplayRole = Qt::DisplayRole,
  96. PathRole = Qt::UserRole + 1,
  97. TypeRole,
  98. LastModifiedRole,
  99. ChildCountRole
  100. };
  101. Q_INVOKABLE void loadRootFolders();
  102. void loadMonths(TreeItem *yearItem, const QModelIndex &parentIndex);
  103. void loadDays(TreeItem *monthItem, const QModelIndex &parentIndex);
  104. void loadAlbums(TreeItem *dayItem, const QModelIndex &parentIndex);
  105. void loadPositionFolders(TreeItem *albumItem, const QModelIndex &parentIndex);
  106. void loadImages(TreeItem *parentItem, const QModelIndex &parentIndex);
  107. TreeItem *m_rootItem;
  108. QString m_picturesPath;
  109. QMimeDatabase m_mimeDb;
  110. QRegularExpression m_yearRegex{R"(^\d{4}$)"};
  111. QRegularExpression m_monthRegex{R"(^(0[1-9]|1[0-2])$)"};
  112. QRegularExpression m_dayRegex{R"(^(0[1-9]|[12][0-9]|3[01])$)"};
  113. QRegularExpression m_albumRegex{R"(^\d{14}$)"};
  114. QRegularExpression m_positionRegex{R"(^X\d+_Z\d+$)"};
  115. bool m_loading = false;
  116. void setLoading(bool loading);
  117. };
  118. #endif // TREEMODEL_H