#ifndef TREEMODEL_H #define TREEMODEL_H #include #include #include #include #include #include #include #include // 新增头文件 #include #include class TreeItem { public: enum Type { Root, YearFolder, MonthFolder, DayFolder, AlbumFolder, PositionFolder, Image }; explicit TreeItem(Type type, const QString &name, const QString &path, const QDateTime &lastModified = QDateTime(), TreeItem *parent = nullptr); ~TreeItem(); void appendChild(TreeItem *child); TreeItem *child(int row) const; int childCount() const; int row() const; Type type() const { return m_type; } QString name() const { return m_name; } QString path() const { return m_path; } QDateTime lastModified() const { return m_lastModified; } TreeItem *parent() const { return m_parent; } void removeAllChildren(); private: Type m_type; QString m_name; QString m_path; QDateTime m_lastModified; TreeItem *m_parent; QList m_children; }; class TreeModel : public QAbstractItemModel { Q_OBJECT Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged) public: explicit TreeModel(QObject *parent = nullptr); ~TreeModel() override; // 必须重写 QAbstractItemModel 的接口 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QHash roleNames() const override; bool loading() const { return m_loading; } public slots: Q_INVOKABLE void loadChildren(const QModelIndex &index); Q_INVOKABLE QVariantList getFolderContents(const QString &folderPath) { QVariantList contents; QDir dir(folderPath); dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); QFileInfoList entries = dir.entryInfoList(); std::sort(entries.begin(), entries.end(), [](const QFileInfo &a, const QFileInfo &b) { // 目录优先且按时间排序 if(a.isDir() && b.isDir()) return a.lastModified() > b.lastModified(); if(a.isDir()) return true; if(b.isDir()) return false; // 文件按名称排序 return a.fileName() < b.fileName(); }); //QMimeDatabase mimeDb; for (const QFileInfo &entry : entries) { QVariantMap item; item["name"] = entry.fileName(); item["path"] = entry.absoluteFilePath(); item["isFolder"] = entry.isDir(); // if (!entry.isDir()) { // QMimeType mime = mimeDb.mimeTypeForFile(entry); // item["isImage"] = mime.name().startsWith("image/"); // } contents.append(item); } return contents; } void resetModel(); // 可暴露给 QML signals: void loadingChanged(); private: enum RoleNames { DisplayRole = Qt::DisplayRole, PathRole = Qt::UserRole + 1, TypeRole, LastModifiedRole, ChildCountRole }; Q_INVOKABLE void loadRootFolders(); void loadMonths(TreeItem *yearItem, const QModelIndex &parentIndex); void loadDays(TreeItem *monthItem, const QModelIndex &parentIndex); void loadAlbums(TreeItem *dayItem, const QModelIndex &parentIndex); void loadPositionFolders(TreeItem *albumItem, const QModelIndex &parentIndex); void loadImages(TreeItem *parentItem, const QModelIndex &parentIndex); TreeItem *m_rootItem; QString m_picturesPath; QMimeDatabase m_mimeDb; QRegularExpression m_yearRegex{R"(^\d{4}$)"}; QRegularExpression m_monthRegex{R"(^(0[1-9]|1[0-2])$)"}; QRegularExpression m_dayRegex{R"(^(0[1-9]|[12][0-9]|3[01])$)"}; QRegularExpression m_albumRegex{R"(^\d{14}$)"}; QRegularExpression m_positionRegex{R"(^X\d+_Z\d+$)"}; bool m_loading = false; void setLoading(bool loading); }; #endif // TREEMODEL_H