|
|
@@ -15,8 +15,8 @@ class TreeItem {
|
|
|
public:
|
|
|
enum NodeType { Root = 0, Folder, Image };
|
|
|
|
|
|
- explicit TreeItem(NodeType type, const QString &name, const QString &path = "", TreeItem *parent = nullptr)
|
|
|
- : m_type(type), m_name(name), m_path(path), m_parent(parent) {}
|
|
|
+ explicit TreeItem(NodeType type, const QString &name, const QString &path = "", TreeItem *parent = nullptr,const QDateTime &lastModified = QDateTime())
|
|
|
+ : m_type(type), m_name(name), m_path(path), m_parent(parent),m_lastModified(lastModified.isValid() ? lastModified : QDateTime::currentDateTime()) {} // 修正初始化 // 修正初始化
|
|
|
|
|
|
~TreeItem() { qDeleteAll(m_children); }
|
|
|
|
|
|
@@ -44,16 +44,34 @@ public:
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
void removeAllChildren() {
|
|
|
qDeleteAll(m_children);
|
|
|
m_children.clear();
|
|
|
}
|
|
|
+
|
|
|
+ QDateTime lastModified() const { return m_lastModified; }
|
|
|
+ // 新增方法
|
|
|
+ int cachedChildCount() const {
|
|
|
+ if (m_cachedChildCount == -1 && m_type == Folder) {
|
|
|
+ QDir dir(m_path);
|
|
|
+ dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
|
|
+ m_cachedChildCount = dir.entryList().count();
|
|
|
+ }
|
|
|
+ return m_cachedChildCount;
|
|
|
+ }
|
|
|
+
|
|
|
+ int visibleChildCount() const {
|
|
|
+ return m_cachedChildCount;
|
|
|
+ }
|
|
|
private:
|
|
|
QList<TreeItem*> m_children;
|
|
|
NodeType m_type;
|
|
|
QString m_name;
|
|
|
QString m_path;
|
|
|
TreeItem *m_parent;
|
|
|
+ QDateTime m_lastModified; // 已存在
|
|
|
+ mutable int m_cachedChildCount = -1; // 缓存数量,-1 表示未初始化
|
|
|
};
|
|
|
|
|
|
class TreeModel : public QAbstractItemModel {
|
|
|
@@ -108,6 +126,10 @@ public:
|
|
|
return item->path();
|
|
|
case IsFolderRole:
|
|
|
return item->type() == TreeItem::Folder;
|
|
|
+ case CountRole:
|
|
|
+ return item->cachedChildCount(); // 直接返回子项数量
|
|
|
+ case TimeRole:
|
|
|
+ return item->lastModified();
|
|
|
default:
|
|
|
return QVariant();
|
|
|
}
|
|
|
@@ -118,6 +140,8 @@ public:
|
|
|
roles[Qt::DisplayRole] = "name";
|
|
|
roles[PathRole] = "path";
|
|
|
roles[IsFolderRole] = "isFolder";
|
|
|
+ roles[CountRole] = "count";
|
|
|
+ roles[TimeRole] = "time";
|
|
|
return roles;
|
|
|
}
|
|
|
Q_INVOKABLE void resetModel(); // 暴露给QML调用
|
|
|
@@ -133,69 +157,99 @@ public:
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ //判断是否为根目录的直接子节点
|
|
|
+ bool isRootChild = (parentItem == m_rootItem);
|
|
|
+
|
|
|
+
|
|
|
// 清空现有子项(修复范围错误)
|
|
|
- const int childCount = parentItem->childCount();
|
|
|
- if (childCount > 0) {
|
|
|
- beginRemoveRows(index, 0, childCount - 1);
|
|
|
+ if (parentItem->childCount() > 0) {
|
|
|
+ beginRemoveRows(index, 0, parentItem->childCount() - 1);
|
|
|
parentItem->removeAllChildren(); // 需要实现该函数
|
|
|
endRemoveRows();
|
|
|
}
|
|
|
-
|
|
|
- // 获取文件系统条目
|
|
|
+ // 获取文件系统条目(禁用QDir默认排序)
|
|
|
QDir dir(parentItem->path());
|
|
|
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
|
|
+ dir.setSorting(QDir::NoSort); // 🚨 禁用所有默认排序
|
|
|
+
|
|
|
+ // 获取条目并按新规则排序
|
|
|
auto entries = dir.entryInfoList();
|
|
|
|
|
|
+ // 仅根目录的子项按时间排序
|
|
|
+ if (isRootChild) {
|
|
|
+ std::sort(entries.begin(), entries.end(), [](const QFileInfo &a, const QFileInfo &b) {
|
|
|
+ if (a.isDir() && b.isDir()) {
|
|
|
+ return a.lastModified() > b.lastModified(); // 目录按时间排序
|
|
|
+ }
|
|
|
+ return a.fileName() < b.fileName(); // 文件按名称排序
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ // 非根目录保持默认名称排序
|
|
|
+ std::sort(entries.begin(), entries.end(), [](const QFileInfo &a, const QFileInfo &b) {
|
|
|
+ if (a.isDir() == b.isDir()) {
|
|
|
+ return a.fileName() < b.fileName();
|
|
|
+ }
|
|
|
+ return a.isDir(); // 目录在前
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
qDebug() << "Loading" << entries.count() << "items from" << dir.absolutePath();
|
|
|
|
|
|
- // 处理空文件夹的情况
|
|
|
- if (entries.isEmpty()) {
|
|
|
- beginInsertRows(index, 0, 0);
|
|
|
- parentItem->appendChild(new TreeItem(TreeItem::Folder, "(空)", "", parentItem));
|
|
|
- endInsertRows();
|
|
|
- return;
|
|
|
- }
|
|
|
|
|
|
// 插入新数据(修复行数范围)
|
|
|
beginInsertRows(index, 0, entries.count() - 1);
|
|
|
for (const QFileInfo &entry : entries) {
|
|
|
- TreeItem::NodeType type = entry.isDir() ? TreeItem::Folder : TreeItem::Image;
|
|
|
|
|
|
- // 过滤非图片文件
|
|
|
- if (type == TreeItem::Image) {
|
|
|
+ if (!entry.isDir()) {
|
|
|
QMimeType mime = mimeDb.mimeTypeForFile(entry);
|
|
|
if (!mime.name().startsWith("image/")) continue;
|
|
|
}
|
|
|
|
|
|
parentItem->appendChild(new TreeItem(
|
|
|
- type,
|
|
|
+ entry.isDir() ? TreeItem::Folder : TreeItem::Image,
|
|
|
entry.fileName(),
|
|
|
entry.absoluteFilePath(),
|
|
|
- parentItem
|
|
|
+ parentItem,
|
|
|
+ entry.lastModified() // 传递最后修改时间
|
|
|
));
|
|
|
}
|
|
|
endInsertRows();
|
|
|
-
|
|
|
+ QVector<int> roles = {CountRole, TimeRole, IsFolderRole};
|
|
|
// 强制刷新父节点数据
|
|
|
- emit dataChanged(index, index, {IsFolderRole});
|
|
|
+ emit dataChanged(index, index, roles);
|
|
|
}
|
|
|
// TreeModel.h
|
|
|
+ // 新增方法获取目录时间
|
|
|
+ Q_INVOKABLE qint64 getDirectoryTime(const QString &path) const {
|
|
|
+ QFileInfo info(path);
|
|
|
+ return info.lastModified().toMSecsSinceEpoch();
|
|
|
+ }
|
|
|
Q_INVOKABLE QVariantList getFolderContents(const QString &folderPath) {
|
|
|
QVariantList contents;
|
|
|
QDir dir(folderPath);
|
|
|
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
|
|
|
|
|
- QMimeDatabase mimeDb;
|
|
|
- for (const QFileInfo &entry : dir.entryInfoList()) {
|
|
|
+ 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/");
|
|
|
- }
|
|
|
+ // if (!entry.isDir()) {
|
|
|
+ // QMimeType mime = mimeDb.mimeTypeForFile(entry);
|
|
|
+ // item["isImage"] = mime.name().startsWith("image/");
|
|
|
+ // }
|
|
|
|
|
|
contents.append(item);
|
|
|
}
|
|
|
@@ -204,23 +258,42 @@ public:
|
|
|
private:
|
|
|
enum Roles {
|
|
|
PathRole = Qt::UserRole + 1,
|
|
|
- IsFolderRole = Qt::UserRole + 2
|
|
|
+ IsFolderRole = Qt::UserRole + 2,
|
|
|
+ CountRole = Qt::UserRole + 3, //子项数量
|
|
|
+ TimeRole= Qt::UserRole + 4 //最后修改时间
|
|
|
};
|
|
|
|
|
|
void loadRootFolders() {
|
|
|
+ // 清空原有数据
|
|
|
+ beginResetModel();
|
|
|
+ m_rootItem->removeAllChildren();
|
|
|
+ endResetModel();
|
|
|
+
|
|
|
QDir picturesDir(m_picturesPath);
|
|
|
picturesDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
-
|
|
|
- beginInsertRows(QModelIndex(), 0, picturesDir.count()-1);
|
|
|
- for (const QFileInfo &dirInfo : picturesDir.entryInfoList()) {
|
|
|
+ picturesDir.setSorting(QDir::NoSort); // 禁用默认排序
|
|
|
+ // 获取并排序目录条目
|
|
|
+ QFileInfoList dirEntries = picturesDir.entryInfoList();
|
|
|
+ std::sort(dirEntries.begin(), dirEntries.end(), [](const QFileInfo& a, const QFileInfo& b) {
|
|
|
+ return a.lastModified() > b.lastModified(); // 降序排列(最新的在前)
|
|
|
+ });
|
|
|
+
|
|
|
+ beginInsertRows(QModelIndex(), 0, dirEntries.size()-1);
|
|
|
+ for (const QFileInfo &dirInfo : dirEntries) {
|
|
|
m_rootItem->appendChild(new TreeItem(
|
|
|
TreeItem::Folder,
|
|
|
dirInfo.fileName(),
|
|
|
dirInfo.absoluteFilePath(),
|
|
|
- m_rootItem
|
|
|
+ m_rootItem,
|
|
|
+ dirInfo.lastModified()
|
|
|
));
|
|
|
}
|
|
|
endInsertRows();
|
|
|
+ // 调试输出
|
|
|
+ // qDebug() << "Sorted root folders:";
|
|
|
+ // for (const auto& entry : dirEntries) {
|
|
|
+ // qDebug() << entry.fileName() << entry.lastModified();
|
|
|
+ // }
|
|
|
}
|
|
|
|
|
|
void populateFolderItems(TreeItem *parentFolder) {
|
|
|
@@ -243,7 +316,8 @@ private:
|
|
|
type,
|
|
|
entry.fileName(),
|
|
|
entry.absoluteFilePath(),
|
|
|
- parentFolder
|
|
|
+ parentFolder,
|
|
|
+ entry.lastModified()
|
|
|
));
|
|
|
}
|
|
|
}
|