TreeModel.h 4.1 KB

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