TreeModel.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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::PicturesLocation))
  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. loadPositionFolders(item, index);
  176. break;
  177. case TreeItem::PositionFolder:
  178. loadImages(item, index);
  179. break;
  180. default:
  181. break;
  182. }
  183. }
  184. void TreeModel::loadMonths(TreeItem *yearItem, const QModelIndex &parentIndex)
  185. {
  186. QDir dir(yearItem->path());
  187. dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
  188. QFileInfoList entries = dir.entryInfoList();
  189. QList<QFileInfo> months;
  190. for (const QFileInfo &info : entries) {
  191. if (m_monthRegex.match(info.fileName()).hasMatch()) {
  192. months.append(info);
  193. }
  194. }
  195. qDebug() << "Loading months for:" << yearItem->path();
  196. qDebug() << "Found entries:" << entries.size();
  197. qDebug() << "Matching months:" << months.size();
  198. std::sort(months.begin(), months.end(), [](const QFileInfo &a, const QFileInfo &b) {
  199. return a.fileName() < b.fileName();
  200. });
  201. if (!months.isEmpty()) {
  202. int row = rowCount(parentIndex);
  203. beginInsertRows(parentIndex, row, row + months.size() - 1);
  204. for (const QFileInfo &info : months) {
  205. TreeItem *monthItem = new TreeItem(
  206. TreeItem::MonthFolder,
  207. info.fileName(),
  208. info.absoluteFilePath(),
  209. info.lastModified(),
  210. yearItem);
  211. yearItem->appendChild(monthItem);
  212. //在这里加载该月下的“日”文件夹
  213. loadDays(monthItem,index(row + months.indexOf(info), 0, parentIndex));
  214. }
  215. endInsertRows();
  216. }
  217. }
  218. void TreeModel::loadDays(TreeItem *monthItem, const QModelIndex &parentIndex)
  219. {
  220. QDir dir(monthItem->path());
  221. dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
  222. QFileInfoList entries = dir.entryInfoList();
  223. QList<QFileInfo> days;
  224. for (const QFileInfo &info : entries) {
  225. if (m_dayRegex.match(info.fileName()).hasMatch()) {
  226. days.append(info);
  227. }
  228. }
  229. std::sort(days.begin(), days.end(), [](const QFileInfo &a, const QFileInfo &b) {
  230. return a.fileName() < b.fileName();
  231. });
  232. if (!days.isEmpty()) {
  233. int row = rowCount(parentIndex);
  234. beginInsertRows(parentIndex, row, row + days.size() - 1);
  235. for (const QFileInfo &info : days) {
  236. TreeItem *dayItem = new TreeItem(
  237. TreeItem::DayFolder,
  238. info.fileName(),
  239. info.absoluteFilePath(),
  240. info.lastModified(),
  241. monthItem);
  242. monthItem->appendChild(dayItem);
  243. // ✅ 加载该日下的“相册”(时间戳文件夹)
  244. loadAlbums(dayItem, index(row + days.indexOf(info), 0, parentIndex));
  245. }
  246. endInsertRows();
  247. }
  248. }
  249. void TreeModel::loadAlbums(TreeItem *dayItem, const QModelIndex &parentIndex)
  250. {
  251. QDir dir(dayItem->path());
  252. dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
  253. QFileInfoList entries = dir.entryInfoList();
  254. QList<QFileInfo> albums;
  255. for (const QFileInfo &info : entries) {
  256. if (m_albumRegex.match(info.fileName()).hasMatch()) {
  257. albums.append(info);
  258. }
  259. }
  260. std::sort(albums.begin(), albums.end(), [](const QFileInfo &a, const QFileInfo &b) {
  261. return a.fileName() < b.fileName();
  262. });
  263. if (!albums.isEmpty()) {
  264. int row = rowCount(parentIndex);
  265. beginInsertRows(parentIndex, row, row + albums.size() - 1);
  266. for (const QFileInfo &info : albums) {
  267. TreeItem *albumItem = new TreeItem(
  268. TreeItem::AlbumFolder,
  269. info.fileName(),
  270. info.absoluteFilePath(),
  271. info.lastModified(),
  272. dayItem);
  273. dayItem->appendChild(albumItem);
  274. // ✅ 加载该相册下的位置文件夹 (X{x}_Z{z})
  275. loadPositionFolders(albumItem, index(row + albums.indexOf(info), 0, parentIndex));
  276. }
  277. endInsertRows();
  278. }
  279. }
  280. void TreeModel::loadPositionFolders(TreeItem *albumItem, const QModelIndex &parentIndex)
  281. {
  282. QDir dir(albumItem->path());
  283. dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
  284. QFileInfoList entries = dir.entryInfoList();
  285. QList<QFileInfo> positions;
  286. QList<QFileInfo> directImages;
  287. for (const QFileInfo &info : entries) {
  288. if (info.isDir()) {
  289. if (m_positionRegex.match(info.fileName()).hasMatch()) {
  290. positions.append(info);
  291. }
  292. } else {
  293. QMimeType mime = m_mimeDb.mimeTypeForFile(info);
  294. if (mime.name().startsWith("image/")) {
  295. directImages.append(info);
  296. }
  297. }
  298. }
  299. std::sort(positions.begin(), positions.end(), [](const QFileInfo &a, const QFileInfo &b) {
  300. return a.fileName() < b.fileName();
  301. });
  302. std::sort(directImages.begin(), directImages.end(), [](const QFileInfo &a, const QFileInfo &b) {
  303. return a.fileName() < b.fileName();
  304. });
  305. int totalRows = positions.size() + directImages.size();
  306. int currentRow = rowCount(parentIndex);
  307. if (totalRows == 0) return;
  308. beginInsertRows(parentIndex, currentRow, currentRow + totalRows - 1);
  309. // 先添加位置文件夹
  310. for (int i = 0; i < positions.size(); ++i) {
  311. const QFileInfo &info = positions[i];
  312. TreeItem *posItem = new TreeItem(
  313. TreeItem::PositionFolder,
  314. info.fileName(),
  315. info.absoluteFilePath(),
  316. info.lastModified(),
  317. albumItem);
  318. albumItem->appendChild(posItem);
  319. // 加载该位置下的图片
  320. loadImages(posItem, index(currentRow + i, 0, parentIndex));
  321. }
  322. // 再添加相册下的直接图片
  323. for (int i = 0; i < directImages.size(); ++i) {
  324. const QFileInfo &info = directImages[i];
  325. albumItem->appendChild(new TreeItem(
  326. TreeItem::Image,
  327. info.fileName(),
  328. info.absoluteFilePath(),
  329. info.lastModified(),
  330. albumItem));
  331. }
  332. endInsertRows();
  333. }
  334. void TreeModel::loadImages(TreeItem *albumItem, const QModelIndex &parentIndex)
  335. {
  336. QDir dir(albumItem->path());
  337. dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
  338. QFileInfoList entries = dir.entryInfoList();
  339. QList<QFileInfo> images;
  340. for (const QFileInfo &info : entries) {
  341. QMimeType mime = m_mimeDb.mimeTypeForFile(info);
  342. if (mime.name().startsWith("image/")) {
  343. images.append(info);
  344. }
  345. }
  346. std::sort(images.begin(), images.end(), [](const QFileInfo &a, const QFileInfo &b) {
  347. return a.fileName() < b.fileName();
  348. });
  349. if (!images.isEmpty()) {
  350. int row = rowCount(parentIndex);
  351. beginInsertRows(parentIndex, row, row + images.size() - 1);
  352. for (const QFileInfo &info : images) {
  353. albumItem->appendChild(new TreeItem(
  354. TreeItem::Image,
  355. info.fileName(),
  356. info.absoluteFilePath(),
  357. info.lastModified(),
  358. albumItem));
  359. }
  360. endInsertRows();
  361. }
  362. }