#include // TreeModel.cpp #include "TreeModel.h" #include #include #include 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(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(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(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(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(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 TreeModel::roleNames() const { QHash 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 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(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 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 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 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 positions; QList 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 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(); } }