| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- #include <TreeModel.h>
- // TreeModel.cpp
- #include "TreeModel.h"
- #include <QStandardPaths>
- #include <QDir>
- #include <QTimer>
- TreeItem::TreeItem(Type type, const QString &name, const QString &path,
- const QDateTime &lastModified, TreeItem *parent)
- : m_type(type), m_name(name), m_path(path),
- m_lastModified(lastModified.isValid() ? lastModified : QDateTime::currentDateTime()),
- m_parent(parent)
- {
- }
- TreeItem::~TreeItem()
- {
- qDeleteAll(m_children);
- }
- void TreeItem::appendChild(TreeItem *child)
- {
- m_children.append(child);
- }
- TreeItem *TreeItem::child(int row) const
- {
- return m_children.value(row);
- }
- int TreeItem::childCount() const
- {
- return m_children.count();
- }
- int TreeItem::row() const
- {
- if (m_parent)
- return m_parent->m_children.indexOf(const_cast<TreeItem*>(this));
- return 0;
- }
- void TreeItem::removeAllChildren()
- {
- qDeleteAll(m_children);
- m_children.clear();
- }
- TreeModel::TreeModel(QObject *parent)
- : QAbstractItemModel(parent)
- , m_rootItem(new TreeItem(TreeItem::Root, "Pictures", ""))
- , m_picturesPath(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation))
- {
- qDebug() << "Pictures Location:" << m_picturesPath;
- QTimer::singleShot(0, this, [this]() {
- resetModel();
- });
- }
- TreeModel::~TreeModel()
- {
- delete m_rootItem;
- }
- QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
- {
- if (!hasIndex(row, column, parent))
- return QModelIndex();
- TreeItem *parentItem = parent.isValid() ? static_cast<TreeItem*>(parent.internalPointer()) : m_rootItem;
- TreeItem *childItem = parentItem->child(row);
- return childItem ? createIndex(row, column, childItem) : QModelIndex();
- }
- QModelIndex TreeModel::parent(const QModelIndex &index) const
- {
- if (!index.isValid())
- return QModelIndex();
- TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
- TreeItem *parentItem = childItem->parent();
- if (parentItem == m_rootItem || !parentItem)
- return QModelIndex();
- return createIndex(parentItem->row(), 0, parentItem);
- }
- int TreeModel::rowCount(const QModelIndex &parent) const
- {
- TreeItem *parentItem = parent.isValid() ? static_cast<TreeItem*>(parent.internalPointer()) : m_rootItem;
- return parentItem->childCount();
- }
- int TreeModel::columnCount(const QModelIndex &) const
- {
- return 1;
- }
- QVariant TreeModel::data(const QModelIndex &index, int role) const
- {
- if (!index.isValid())
- return QVariant();
- TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
- switch (role) {
- case Qt::DisplayRole:
- return item->name();
- case PathRole:
- return item->path();
- case TypeRole:
- return item->type();
- case LastModifiedRole:
- return item->lastModified();
- case ChildCountRole:
- return item->childCount();
- default:
- return QVariant();
- }
- }
- QHash<int, QByteArray> TreeModel::roleNames() const
- {
- QHash<int, QByteArray> roles;
- roles[Qt::DisplayRole] = "name";
- roles[PathRole] = "path";
- roles[TypeRole] = "type";
- roles[LastModifiedRole] = "lastModified";
- roles[ChildCountRole] = "childCount";
- return roles;
- }
- void TreeModel::setLoading(bool loading)
- {
- if (m_loading != loading) {
- m_loading = loading;
- emit loadingChanged();
- }
- }
- void TreeModel::resetModel()
- {
- beginResetModel();
- m_rootItem->removeAllChildren();
- endResetModel();
- setLoading(true);
- QTimer::singleShot(10, this, [this]() {
- loadRootFolders();
- setLoading(false);
- });
- }
- void TreeModel::loadRootFolders()
- {
- QDir picturesDir(m_picturesPath);
- if (!picturesDir.exists()) return;
- picturesDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
- QFileInfoList entries = picturesDir.entryInfoList();
- QList<QFileInfo> yearFolders;
- for (const QFileInfo &info : entries) {
- if (m_yearRegex.match(info.fileName()).hasMatch()) {
- yearFolders.append(info);
- }
- }
- std::sort(yearFolders.begin(), yearFolders.end(), [](const QFileInfo &a, const QFileInfo &b) {
- return a.fileName() > b.fileName(); // 最新年份在前
- });
- if (!yearFolders.isEmpty()) {
- beginInsertRows(QModelIndex(), 0, yearFolders.size() - 1);
- for (const QFileInfo &info : yearFolders) {
- m_rootItem->appendChild(new TreeItem(
- TreeItem::YearFolder,
- info.fileName(),
- info.absoluteFilePath(),
- info.lastModified()));
- }
- endInsertRows();
- }
- }
- void TreeModel::loadChildren(const QModelIndex &index)
- {
- if (!index.isValid()) return;
- TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
- if (item->childCount() > 0) return; // 已加载
- switch (item->type()) {
- case TreeItem::Root:
- break; // 根节点不自动加载子项
- case TreeItem::YearFolder:
- loadMonths(item, index);
- break;
- case TreeItem::MonthFolder:
- loadDays(item, index);
- break;
- case TreeItem::DayFolder:
- loadAlbums(item, index);
- break;
- case TreeItem::AlbumFolder:
- loadPositionFolders(item, index);
- break;
- case TreeItem::PositionFolder:
- loadImages(item, index);
- break;
- default:
- break;
- }
- }
- void TreeModel::loadMonths(TreeItem *yearItem, const QModelIndex &parentIndex)
- {
- QDir dir(yearItem->path());
- dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
- QFileInfoList entries = dir.entryInfoList();
- QList<QFileInfo> months;
- for (const QFileInfo &info : entries) {
- if (m_monthRegex.match(info.fileName()).hasMatch()) {
- months.append(info);
- }
- }
- qDebug() << "Loading months for:" << yearItem->path();
- qDebug() << "Found entries:" << entries.size();
- qDebug() << "Matching months:" << months.size();
- std::sort(months.begin(), months.end(), [](const QFileInfo &a, const QFileInfo &b) {
- return a.fileName() < b.fileName();
- });
- if (!months.isEmpty()) {
- int row = rowCount(parentIndex);
- beginInsertRows(parentIndex, row, row + months.size() - 1);
- for (const QFileInfo &info : months) {
- TreeItem *monthItem = new TreeItem(
- TreeItem::MonthFolder,
- info.fileName(),
- info.absoluteFilePath(),
- info.lastModified(),
- yearItem);
- yearItem->appendChild(monthItem);
- //在这里加载该月下的“日”文件夹
- loadDays(monthItem,index(row + months.indexOf(info), 0, parentIndex));
- }
- endInsertRows();
- }
- }
- void TreeModel::loadDays(TreeItem *monthItem, const QModelIndex &parentIndex)
- {
- QDir dir(monthItem->path());
- dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
- QFileInfoList entries = dir.entryInfoList();
- QList<QFileInfo> days;
- for (const QFileInfo &info : entries) {
- if (m_dayRegex.match(info.fileName()).hasMatch()) {
- days.append(info);
- }
- }
- std::sort(days.begin(), days.end(), [](const QFileInfo &a, const QFileInfo &b) {
- return a.fileName() < b.fileName();
- });
- if (!days.isEmpty()) {
- int row = rowCount(parentIndex);
- beginInsertRows(parentIndex, row, row + days.size() - 1);
- for (const QFileInfo &info : days) {
- TreeItem *dayItem = new TreeItem(
- TreeItem::DayFolder,
- info.fileName(),
- info.absoluteFilePath(),
- info.lastModified(),
- monthItem);
- monthItem->appendChild(dayItem);
- // ✅ 加载该日下的“相册”(时间戳文件夹)
- loadAlbums(dayItem, index(row + days.indexOf(info), 0, parentIndex));
- }
- endInsertRows();
- }
- }
- void TreeModel::loadAlbums(TreeItem *dayItem, const QModelIndex &parentIndex)
- {
- QDir dir(dayItem->path());
- dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
- QFileInfoList entries = dir.entryInfoList();
- QList<QFileInfo> albums;
- for (const QFileInfo &info : entries) {
- if (m_albumRegex.match(info.fileName()).hasMatch()) {
- albums.append(info);
- }
- }
- std::sort(albums.begin(), albums.end(), [](const QFileInfo &a, const QFileInfo &b) {
- return a.fileName() < b.fileName();
- });
- if (!albums.isEmpty()) {
- int row = rowCount(parentIndex);
- beginInsertRows(parentIndex, row, row + albums.size() - 1);
- for (const QFileInfo &info : albums) {
- TreeItem *albumItem = new TreeItem(
- TreeItem::AlbumFolder,
- info.fileName(),
- info.absoluteFilePath(),
- info.lastModified(),
- dayItem);
- dayItem->appendChild(albumItem);
- // ✅ 加载该相册下的位置文件夹 (X{x}_Z{z})
- loadPositionFolders(albumItem, index(row + albums.indexOf(info), 0, parentIndex));
- }
- endInsertRows();
- }
- }
- void TreeModel::loadPositionFolders(TreeItem *albumItem, const QModelIndex &parentIndex)
- {
- QDir dir(albumItem->path());
- dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
- QFileInfoList entries = dir.entryInfoList();
- QList<QFileInfo> positions;
- QList<QFileInfo> directImages;
- for (const QFileInfo &info : entries) {
- if (info.isDir()) {
- if (m_positionRegex.match(info.fileName()).hasMatch()) {
- positions.append(info);
- }
- } else {
- QMimeType mime = m_mimeDb.mimeTypeForFile(info);
- if (mime.name().startsWith("image/")) {
- directImages.append(info);
- }
- }
- }
- std::sort(positions.begin(), positions.end(), [](const QFileInfo &a, const QFileInfo &b) {
- return a.fileName() < b.fileName();
- });
- std::sort(directImages.begin(), directImages.end(), [](const QFileInfo &a, const QFileInfo &b) {
- return a.fileName() < b.fileName();
- });
- int totalRows = positions.size() + directImages.size();
- int currentRow = rowCount(parentIndex);
- if (totalRows == 0) return;
- beginInsertRows(parentIndex, currentRow, currentRow + totalRows - 1);
- // 先添加位置文件夹
- for (int i = 0; i < positions.size(); ++i) {
- const QFileInfo &info = positions[i];
- TreeItem *posItem = new TreeItem(
- TreeItem::PositionFolder,
- info.fileName(),
- info.absoluteFilePath(),
- info.lastModified(),
- albumItem);
- albumItem->appendChild(posItem);
- // 加载该位置下的图片
- loadImages(posItem, index(currentRow + i, 0, parentIndex));
- }
- // 再添加相册下的直接图片
- for (int i = 0; i < directImages.size(); ++i) {
- const QFileInfo &info = directImages[i];
- albumItem->appendChild(new TreeItem(
- TreeItem::Image,
- info.fileName(),
- info.absoluteFilePath(),
- info.lastModified(),
- albumItem));
- }
- endInsertRows();
- }
- void TreeModel::loadImages(TreeItem *albumItem, const QModelIndex &parentIndex)
- {
- QDir dir(albumItem->path());
- dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
- QFileInfoList entries = dir.entryInfoList();
- QList<QFileInfo> images;
- for (const QFileInfo &info : entries) {
- QMimeType mime = m_mimeDb.mimeTypeForFile(info);
- if (mime.name().startsWith("image/")) {
- images.append(info);
- }
- }
- std::sort(images.begin(), images.end(), [](const QFileInfo &a, const QFileInfo &b) {
- return a.fileName() < b.fileName();
- });
- if (!images.isEmpty()) {
- int row = rowCount(parentIndex);
- beginInsertRows(parentIndex, row, row + images.size() - 1);
- for (const QFileInfo &info : images) {
- albumItem->appendChild(new TreeItem(
- TreeItem::Image,
- info.fileName(),
- info.absoluteFilePath(),
- info.lastModified(),
- albumItem));
- }
- endInsertRows();
- }
- }
|