| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #ifndef TREEMODEL_H
- #define TREEMODEL_H
- #include <QAbstractItemModel>
- #include <QDir>
- #include <QFileInfo>
- #include <QIcon>
- #include <QStandardPaths>
- #include <QMimeDatabase>
- #include <QDebug>
- #include <QMimeDatabase> // 新增头文件
- #include <QRegularExpression>
- #include <m_loger.h>
- class TreeItem {
- public:
- enum Type {
- Root,
- YearFolder,
- MonthFolder,
- DayFolder,
- AlbumFolder,
- 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<TreeItem*> 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<int, QByteArray> 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 loadImages(TreeItem *albumItem, 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}$)"};
- bool m_loading = false;
- void setLoading(bool loading);
- };
- #endif // TREEMODEL_H
|