TreeModel.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include <TreeModel.h>
  2. // TreeModel.cpp
  3. #include "TreeModel.h"
  4. #include <QStandardPaths>
  5. #include <QDir>
  6. #include <QTimer>
  7. TreeItem::TreeItem(Type type, const QString &name, const QString &path,
  8. const QDateTime &lastModified, TreeItem *parent)
  9. : m_type(type), m_name(name), m_path(path),
  10. m_lastModified(lastModified.isValid() ? lastModified : QDateTime::currentDateTime()),
  11. m_parent(parent)
  12. {
  13. }
  14. TreeItem::~TreeItem()
  15. {
  16. qDeleteAll(m_children);
  17. }
  18. void TreeItem::appendChild(TreeItem *child)
  19. {
  20. m_children.append(child);
  21. }
  22. TreeItem *TreeItem::child(int row) const
  23. {
  24. return m_children.value(row);
  25. }
  26. int TreeItem::childCount() const
  27. {
  28. return m_children.count();
  29. }
  30. int TreeItem::row() const
  31. {
  32. if (m_parent)
  33. return m_parent->m_children.indexOf(const_cast<TreeItem*>(this));
  34. return 0;
  35. }
  36. void TreeItem::removeAllChildren()
  37. {
  38. qDeleteAll(m_children);
  39. m_children.clear();
  40. }
  41. TreeModel::TreeModel(QObject *parent)
  42. : QAbstractItemModel(parent)
  43. , m_rootItem(new TreeItem(TreeItem::Root, "Pictures", ""))
  44. , m_picturesPath(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/Pictures")
  45. {
  46. qDebug() << "Pictures Location:" << m_picturesPath;
  47. QTimer::singleShot(0, this, [this]() {
  48. resetModel();
  49. });
  50. }
  51. TreeModel::~TreeModel()
  52. {
  53. delete m_rootItem;
  54. }
  55. QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
  56. {
  57. if (!hasIndex(row, column, parent))
  58. return QModelIndex();
  59. TreeItem *parentItem = parent.isValid() ? static_cast<TreeItem*>(parent.internalPointer()) : m_rootItem;
  60. TreeItem *childItem = parentItem->child(row);
  61. return childItem ? createIndex(row, column, childItem) : QModelIndex();
  62. }
  63. QModelIndex TreeModel::parent(const QModelIndex &index) const
  64. {
  65. if (!index.isValid())
  66. return QModelIndex();
  67. TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
  68. TreeItem *parentItem = childItem->parent();
  69. if (parentItem == m_rootItem || !parentItem)
  70. return QModelIndex();
  71. return createIndex(parentItem->row(), 0, parentItem);
  72. }
  73. int TreeModel::rowCount(const QModelIndex &parent) const
  74. {
  75. TreeItem *parentItem = parent.isValid() ? static_cast<TreeItem*>(parent.internalPointer()) : m_rootItem;
  76. return parentItem->childCount();
  77. }
  78. int TreeModel::columnCount(const QModelIndex &) const
  79. {
  80. return 1;
  81. }
  82. QVariant TreeModel::data(const QModelIndex &index, int role) const
  83. {
  84. if (!index.isValid())
  85. return QVariant();
  86. TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
  87. switch (role) {
  88. case Qt::DisplayRole:
  89. return item->name();
  90. case PathRole:
  91. return item->path();
  92. case TypeRole:
  93. return item->type();
  94. case LastModifiedRole:
  95. return item->lastModified();
  96. case ChildCountRole:
  97. return item->childCount();
  98. default:
  99. return QVariant();
  100. }
  101. }
  102. QHash<int, QByteArray> TreeModel::roleNames() const
  103. {
  104. QHash<int, QByteArray> roles;
  105. roles[Qt::DisplayRole] = "name";
  106. roles[PathRole] = "path";
  107. roles[TypeRole] = "type";
  108. roles[LastModifiedRole] = "lastModified";
  109. roles[ChildCountRole] = "childCount";
  110. return roles;
  111. }
  112. void TreeModel::setLoading(bool loading)
  113. {
  114. if (m_loading != loading) {
  115. m_loading = loading;
  116. emit loadingChanged();
  117. }
  118. }
  119. void TreeModel::resetModel()
  120. {
  121. beginResetModel();
  122. m_rootItem->removeAllChildren();
  123. endResetModel();
  124. setLoading(true);
  125. QTimer::singleShot(10, this, [this]() {
  126. loadRootFolders();
  127. setLoading(false);
  128. });
  129. }
  130. void TreeModel::loadRootFolders()
  131. {
  132. QDir picturesDir(m_picturesPath);
  133. if (!picturesDir.exists()) return;
  134. picturesDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
  135. QFileInfoList entries = picturesDir.entryInfoList();
  136. QList<QFileInfo> yearFolders;
  137. for (const QFileInfo &info : entries) {
  138. if (m_yearRegex.match(info.fileName()).hasMatch()) {
  139. yearFolders.append(info);
  140. }
  141. }
  142. std::sort(yearFolders.begin(), yearFolders.end(), [](const QFileInfo &a, const QFileInfo &b) {
  143. return a.fileName() > b.fileName(); // 最新年份在前
  144. });
  145. if (!yearFolders.isEmpty()) {
  146. beginInsertRows(QModelIndex(), 0, yearFolders.size() - 1);
  147. for (const QFileInfo &info : yearFolders) {
  148. m_rootItem->appendChild(new TreeItem(
  149. TreeItem::YearFolder,
  150. info.fileName(),
  151. info.absoluteFilePath(),
  152. info.lastModified()));
  153. }
  154. endInsertRows();
  155. }
  156. }
  157. void TreeModel::loadChildren(const QModelIndex &index)
  158. {
  159. if (!index.isValid()) return;
  160. TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
  161. if (item->childCount() > 0) return; // 已加载
  162. switch (item->type()) {
  163. case TreeItem::Root:
  164. break; // 根节点不自动加载子项
  165. case TreeItem::YearFolder:
  166. loadMonths(item, index);
  167. break;
  168. case TreeItem::MonthFolder:
  169. loadDays(item, index);
  170. break;
  171. case TreeItem::DayFolder:
  172. loadAlbums(item, index);
  173. break;
  174. case TreeItem::AlbumFolder:
  175. loadImages(item, index);
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. void TreeModel::loadMonths(TreeItem *yearItem, const QModelIndex &parentIndex)
  182. {
  183. QDir dir(yearItem->path());
  184. dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
  185. QFileInfoList entries = dir.entryInfoList();
  186. QList<QFileInfo> months;
  187. for (const QFileInfo &info : entries) {
  188. if (m_monthRegex.match(info.fileName()).hasMatch()) {
  189. months.append(info);
  190. }
  191. }
  192. qDebug() << "Loading months for:" << yearItem->path();
  193. qDebug() << "Found entries:" << entries.size();
  194. qDebug() << "Matching months:" << months.size();
  195. std::sort(months.begin(), months.end(), [](const QFileInfo &a, const QFileInfo &b) {
  196. return a.fileName() < b.fileName();
  197. });
  198. if (!months.isEmpty()) {
  199. int row = rowCount(parentIndex);
  200. beginInsertRows(parentIndex, row, row + months.size() - 1);
  201. for (const QFileInfo &info : months) {
  202. TreeItem *monthItem = new TreeItem(
  203. TreeItem::MonthFolder,
  204. info.fileName(),
  205. info.absoluteFilePath(),
  206. info.lastModified(),
  207. yearItem);
  208. yearItem->appendChild(monthItem);
  209. //在这里加载该月下的“日”文件夹
  210. loadDays(monthItem,index(row + months.indexOf(info), 0, parentIndex));
  211. }
  212. endInsertRows();
  213. }
  214. }
  215. void TreeModel::loadDays(TreeItem *monthItem, const QModelIndex &parentIndex)
  216. {
  217. QDir dir(monthItem->path());
  218. dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
  219. QFileInfoList entries = dir.entryInfoList();
  220. QList<QFileInfo> days;
  221. for (const QFileInfo &info : entries) {
  222. if (m_dayRegex.match(info.fileName()).hasMatch()) {
  223. days.append(info);
  224. }
  225. }
  226. std::sort(days.begin(), days.end(), [](const QFileInfo &a, const QFileInfo &b) {
  227. return a.fileName() < b.fileName();
  228. });
  229. if (!days.isEmpty()) {
  230. int row = rowCount(parentIndex);
  231. beginInsertRows(parentIndex, row, row + days.size() - 1);
  232. for (const QFileInfo &info : days) {
  233. TreeItem *dayItem = new TreeItem(
  234. TreeItem::DayFolder,
  235. info.fileName(),
  236. info.absoluteFilePath(),
  237. info.lastModified(),
  238. monthItem);
  239. monthItem->appendChild(dayItem);
  240. // ✅ 加载该日下的“相册”(时间戳文件夹)
  241. loadAlbums(dayItem, index(row + days.indexOf(info), 0, parentIndex));
  242. }
  243. endInsertRows();
  244. }
  245. }
  246. void TreeModel::loadAlbums(TreeItem *dayItem, const QModelIndex &parentIndex)
  247. {
  248. QDir dir(dayItem->path());
  249. dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
  250. QFileInfoList entries = dir.entryInfoList();
  251. QList<QFileInfo> albums;
  252. for (const QFileInfo &info : entries) {
  253. if (m_albumRegex.match(info.fileName()).hasMatch()) {
  254. albums.append(info);
  255. }
  256. }
  257. std::sort(albums.begin(), albums.end(), [](const QFileInfo &a, const QFileInfo &b) {
  258. return a.fileName() < b.fileName();
  259. });
  260. if (!albums.isEmpty()) {
  261. int row = rowCount(parentIndex);
  262. beginInsertRows(parentIndex, row, row + albums.size() - 1);
  263. for (const QFileInfo &info : albums) {
  264. TreeItem *albumItem = new TreeItem(
  265. TreeItem::AlbumFolder,
  266. info.fileName(),
  267. info.absoluteFilePath(),
  268. info.lastModified(),
  269. dayItem);
  270. dayItem->appendChild(albumItem);
  271. // ✅ 加载该相册下的图片
  272. loadImages(albumItem, index(row + albums.indexOf(info), 0, parentIndex));
  273. }
  274. endInsertRows();
  275. }
  276. }
  277. void TreeModel::loadImages(TreeItem *albumItem, const QModelIndex &parentIndex)
  278. {
  279. QDir dir(albumItem->path());
  280. dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
  281. QFileInfoList entries = dir.entryInfoList();
  282. QList<QFileInfo> images;
  283. for (const QFileInfo &info : entries) {
  284. QMimeType mime = m_mimeDb.mimeTypeForFile(info);
  285. if (mime.name().startsWith("image/")) {
  286. images.append(info);
  287. }
  288. }
  289. std::sort(images.begin(), images.end(), [](const QFileInfo &a, const QFileInfo &b) {
  290. return a.fileName() < b.fileName();
  291. });
  292. if (!images.isEmpty()) {
  293. int row = rowCount(parentIndex);
  294. beginInsertRows(parentIndex, row, row + images.size() - 1);
  295. for (const QFileInfo &info : images) {
  296. albumItem->appendChild(new TreeItem(
  297. TreeItem::Image,
  298. info.fileName(),
  299. info.absoluteFilePath(),
  300. info.lastModified(),
  301. albumItem));
  302. }
  303. endInsertRows();
  304. }
  305. }