PhotostitcherPage.qml 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. import Felgo
  2. import QtQuick
  3. import QtQuick.Controls
  4. import QtQuick.Layouts
  5. import QtMultimedia
  6. import "../controls"
  7. import "../common"
  8. import Frames 0.1
  9. AppPage {
  10. id: photoArchivePage
  11. navigationBarHidden: true
  12. backgroundColor: app.theme_backgroundColor
  13. property bool isLoading: false
  14. property bool rootExpanded: false
  15. property var selectedImages: []
  16. property bool isSelecting: false
  17. property bool isFusing: false
  18. property bool showSharp: true
  19. // 融合按钮
  20. AppButton {
  21. id: fuseButton
  22. anchors.bottom: parent.bottom
  23. anchors.right: parent.right
  24. anchors.margins: 20
  25. text: "图像融合"
  26. visible: selectedImages.length >= 2 && !isFusing
  27. backgroundColor: "#28a779"
  28. textColor: "white"
  29. z: 100
  30. onClicked: {
  31. if (selectedImages.length >= 2) {
  32. isFusing = true
  33. console.log("开始融合", selectedImages.length, "张图片")
  34. app.isGlobalFusing = true
  35. app.globalFusionMessage = "开始融合..."
  36. app.globalFusionPercent = 0
  37. opencvFunc.fuseImagesAsync(selectedImages)
  38. }
  39. }
  40. }
  41. // 取消选择按钮
  42. AppButton {
  43. id: cancelButton
  44. anchors.bottom: parent.bottom
  45. anchors.left: parent.left
  46. anchors.margins: 20
  47. text: "取消选择"
  48. visible: isSelecting && !isFusing
  49. backgroundColor: "#e74c3c"
  50. textColor: "white"
  51. z: 100
  52. onClicked: {
  53. clearSelection()
  54. }
  55. }
  56. // 原图/锐化切换按钮
  57. AppButton {
  58. id: sharpToggleButton
  59. anchors.bottom: parent.bottom
  60. anchors.horizontalCenter: parent.horizontalCenter
  61. anchors.margins: 20
  62. text: showSharp ? "切换到原图" : "切换到锐化图"
  63. visible: imageListModel.count > 0 && !isSelecting && !isFusing
  64. backgroundColor: "#3b82f6"
  65. textColor: "white"
  66. z: 100
  67. onClicked: {
  68. setShowSharp(!showSharp)
  69. }
  70. }
  71. // 连接融合完成信号
  72. Connections {
  73. target: opencvFunc
  74. function onFuseFinished(resultPath) {
  75. isFusing = false
  76. app.isGlobalFusing = false
  77. if (resultPath) {
  78. console.log("融合完成:", resultPath)
  79. // 显示融合结果
  80. imageListModel.append({filePath: resultPath, isFused: true, selected: false})
  81. // 清除选择
  82. clearSelection()
  83. } else {
  84. console.error("融合失败")
  85. }
  86. }
  87. // 融合进度信号
  88. function onFusionProgress(message, percent) {
  89. console.log("融合进度:", message, percent + "%")
  90. // 全局弹窗由 Main.qml 监听
  91. }
  92. }
  93. function clearSelection() {
  94. selectedImages = []
  95. isSelecting = false
  96. for (var i = 0; i < imageListModel.count; i++) {
  97. imageListModel.setProperty(i, "selected", false)
  98. }
  99. }
  100. function toggleImageSelection(filePath) {
  101. // 创建新数组以触发QML属性绑定更新
  102. var temp = selectedImages.slice()
  103. var idx = temp.indexOf(filePath)
  104. if (idx >= 0) {
  105. temp.splice(idx, 1)
  106. } else {
  107. temp.push(filePath)
  108. }
  109. selectedImages = temp
  110. isSelecting = selectedImages.length > 0
  111. // 更新model中的selected状态
  112. for (var i = 0; i < imageListModel.count; i++) {
  113. if (imageListModel.get(i).filePath === filePath) {
  114. imageListModel.setProperty(i, "selected", idx < 0)
  115. break
  116. }
  117. }
  118. }
  119. // 全屏查看组件
  120. Popup {
  121. id: fullscreenPopup
  122. width: parent.width
  123. height: parent.height
  124. modal: true
  125. closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
  126. background: Rectangle {
  127. color: "#CC000000"
  128. }
  129. Item {
  130. id: viewport
  131. anchors.fill: parent
  132. clip: true
  133. Image {
  134. id: image
  135. x: 0
  136. y: 0
  137. width: Math.min(implicitWidth, viewport.width)
  138. height: Math.min(implicitHeight, viewport.height)
  139. asynchronous: true
  140. fillMode: Image.PreserveAspectFit
  141. transformOrigin: Item.TopLeft
  142. scale: 1.0
  143. property real minZoom: 1.0
  144. property real maxZoom: 5.0
  145. onStatusChanged: {
  146. if (status === Image.Ready) {
  147. clampPos()
  148. }
  149. }
  150. }
  151. // 平移
  152. DragHandler {
  153. id: dragHandler
  154. target: null
  155. acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchScreen
  156. property real startX: 0
  157. property real startY: 0
  158. onActiveChanged: {
  159. if (active) {
  160. startX = image.x
  161. startY = image.y
  162. }
  163. }
  164. onTranslationChanged: {
  165. image.x = startX + translation.x
  166. image.y = startY + translation.y
  167. clampPos()
  168. }
  169. }
  170. // 捏合缩放 (触摸):双指缩放在视口中心缩放,保持图像居中
  171. PinchArea {
  172. id: pinchArea
  173. anchors.fill: parent
  174. pinch.target: null
  175. pinch.minimumRotation: 0
  176. pinch.maximumRotation: 0
  177. pinch.minimumScale: 1.0
  178. pinch.maximumScale: 5.0
  179. property real pinchStartZoom: 1.0
  180. onPinchStarted: {
  181. pinchStartZoom = image.scale
  182. }
  183. onPinchUpdated: {
  184. var newZoom = Math.max(image.minZoom, Math.min(image.maxZoom, pinchStartZoom * pinch.scale))
  185. // 以视口中心为锚点缩放,图像保持居中
  186. zoomToPoint(viewport.width / 2, viewport.height / 2, newZoom)
  187. }
  188. }
  189. // 鼠标滚轮缩放
  190. WheelHandler {
  191. id: wheelHandler
  192. target: null
  193. acceptedDevices: PointerDevice.Mouse
  194. onWheel: (event) => {
  195. var factor = event.angleDelta.y > 0 ? 1.15 : (1 / 1.15)
  196. var newZoom = Math.max(image.minZoom, Math.min(image.maxZoom, image.scale * factor))
  197. zoomToPoint(event.x, event.y, newZoom)
  198. }
  199. }
  200. }
  201. // 关闭按钮
  202. RoundButton {
  203. id: closeBtn
  204. text: "关闭" // 设置按钮文本
  205. radius: height/2 // 确保圆形
  206. // 基础样式
  207. width: 80
  208. height: 40
  209. font.pixelSize: 14
  210. font.bold: true
  211. // 颜色配置
  212. palette {
  213. button: "#e74c3c" // 背景色
  214. buttonText: "white" // 文字颜色
  215. }
  216. // 点击效果
  217. background: Rectangle {
  218. radius: closeBtn.radius
  219. color: closeBtn.palette.button
  220. border.color: Qt.darker(color, 1.2)
  221. border.width: 1
  222. Rectangle {
  223. anchors.fill: parent
  224. radius: parent.radius
  225. color: "transparent"
  226. border.color: Qt.lighter(parent.color, 1.2)
  227. border.width: closeBtn.down ? 2 : 0
  228. opacity: closeBtn.down ? 0.5 : 0
  229. Behavior on opacity { NumberAnimation { duration: 100 } }
  230. }
  231. }
  232. // 点击事件
  233. onClicked: {
  234. console.log("关闭操作触发")
  235. fullscreenPopup.close()
  236. // 添加具体关闭逻辑
  237. }
  238. }
  239. }
  240. function showFullscreen(path) {
  241. image.source = "file://" + path
  242. // 等待图片加载出尺寸后再居中
  243. resetTransform()
  244. fullscreenPopup.open()
  245. }
  246. // ===== 原始视频流(.rawv)播放 =====
  247. RawVideoPlayer {
  248. id: rawPlayer
  249. videoSink: videoOutput.videoSink
  250. }
  251. // 分析/融合结果(与视频同目录,命名与拍照图片一致)
  252. Connections {
  253. target: rawPlayer
  254. function onAnalyzeFinished(path) {
  255. if (path) {
  256. console.log("分析完成:", path)
  257. nativeUtils.displayMessageBox("提示", "分析完成,已保存:\n" + path.split("/").pop(), 1)
  258. } else {
  259. console.error("分析失败")
  260. nativeUtils.displayMessageBox("提示", "分析失败,请重试!", 1)
  261. }
  262. }
  263. function onFuseFinished(path) {
  264. if (path) {
  265. console.log("融合完成:", path)
  266. nativeUtils.displayMessageBox("提示", "融合完成,已保存:\n" + path.split("/").pop(), 1)
  267. } else {
  268. console.error("融合失败")
  269. nativeUtils.displayMessageBox("提示", "融合失败,请重试!", 1)
  270. }
  271. }
  272. }
  273. // 打开原始视频流文件并开始播放
  274. function openVideo(filePath) {
  275. console.log("播放原始视频流:", filePath)
  276. if (rawPlayer.open(filePath)) {
  277. videoPopup.videoTitle = filePath.split("/").pop()
  278. videoPopup.open()
  279. rawPlayer.play()
  280. } else {
  281. nativeUtils.displayMessageBox("提示", "无法播放该视频流文件!", 1)
  282. }
  283. }
  284. function closeVideo() {
  285. rawPlayer.stop()
  286. rawPlayer.close()
  287. videoPopup.close()
  288. }
  289. Popup {
  290. id: videoPopup
  291. width: parent.width
  292. height: parent.height
  293. modal: true
  294. closePolicy: Popup.NoAutoClose
  295. property string videoTitle: ""
  296. background: Rectangle {
  297. color: "#000000"
  298. }
  299. VideoOutput {
  300. id: videoOutput
  301. anchors.fill: parent
  302. anchors.bottomMargin: dp(80)
  303. fillMode: VideoOutput.PreserveAspectFit
  304. }
  305. // 标题与播放进度
  306. Column {
  307. anchors.top: parent.top
  308. anchors.topMargin: dp(12)
  309. anchors.horizontalCenter: parent.horizontalCenter
  310. spacing: dp(4)
  311. Text {
  312. anchors.horizontalCenter: parent.horizontalCenter
  313. text: videoPopup.videoTitle
  314. color: "white"
  315. font.bold: true
  316. font.pixelSize: 14
  317. }
  318. Text {
  319. anchors.horizontalCenter: parent.horizontalCenter
  320. text: rawPlayer.position + " / " + rawPlayer.frameCount + " 帧"
  321. color: "#28a779"
  322. font.pixelSize: 12
  323. }
  324. }
  325. // 播放/暂停 + 分析(最清晰帧) + 融合(景深融合) + 关闭
  326. Row {
  327. anchors.bottom: parent.bottom
  328. anchors.bottomMargin: dp(16)
  329. anchors.horizontalCenter: parent.horizontalCenter
  330. spacing: dp(16)
  331. AppButton {
  332. text: rawPlayer.playing ? "暂停" : "播放"
  333. textColor: "white"
  334. backgroundColor: "#28a779"
  335. flat: false
  336. enabled: !rawPlayer.busy
  337. minimumHeight: dp(40)
  338. minimumWidth: dp(80)
  339. onClicked: {
  340. if (rawPlayer.playing) {
  341. rawPlayer.pause()
  342. } else {
  343. rawPlayer.play()
  344. }
  345. }
  346. }
  347. // 分析:识别视频帧中最清晰的一帧,锐化+对比度增强后保存为图片
  348. AppButton {
  349. text: "分析"
  350. textColor: "white"
  351. backgroundColor: "#3b82f6"
  352. flat: false
  353. enabled: !rawPlayer.busy
  354. minimumHeight: dp(40)
  355. minimumWidth: dp(80)
  356. onClicked: {
  357. rawPlayer.pause()
  358. rawPlayer.analyzeSharpestFrame()
  359. }
  360. }
  361. // 融合:对视频帧做景深融合,锐化+对比度增强后保存为图片
  362. AppButton {
  363. text: "融合"
  364. textColor: "white"
  365. backgroundColor: "#8b5cf6"
  366. flat: false
  367. enabled: !rawPlayer.busy
  368. minimumHeight: dp(40)
  369. minimumWidth: dp(80)
  370. onClicked: {
  371. rawPlayer.pause()
  372. rawPlayer.fuseVideoFrames()
  373. }
  374. }
  375. AppButton {
  376. text: "关闭"
  377. textColor: "white"
  378. backgroundColor: "#e74c3c"
  379. flat: false
  380. minimumHeight: dp(40)
  381. minimumWidth: dp(80)
  382. onClicked: closeVideo()
  383. }
  384. }
  385. // 分析/融合处理中提示
  386. Rectangle {
  387. anchors.fill: parent
  388. visible: rawPlayer.busy
  389. color: "#99000000"
  390. z: 10
  391. Column {
  392. anchors.centerIn: parent
  393. spacing: dp(10)
  394. BusyIndicator {
  395. anchors.horizontalCenter: parent.horizontalCenter
  396. running: rawPlayer.busy
  397. width: dp(48)
  398. height: dp(48)
  399. }
  400. Text {
  401. anchors.horizontalCenter: parent.horizontalCenter
  402. text: "正在处理,请稍候..."
  403. color: "white"
  404. font.pixelSize: 15
  405. }
  406. }
  407. }
  408. }
  409. // 重置缩放为初始大小并居中
  410. function resetTransform() {
  411. image.scale = 1.0
  412. clampPos()
  413. }
  414. // 约束图片位置:小于视口时居中,大于视口时限制不露出空白边缘
  415. function clampPos() {
  416. var sw = image.width * image.scale
  417. var sh = image.height * image.scale
  418. var minX, maxX, minY, maxY
  419. if (sw <= viewport.width) {
  420. minX = (viewport.width - sw) / 2
  421. maxX = minX
  422. } else {
  423. minX = viewport.width - sw
  424. maxX = 0
  425. }
  426. if (sh <= viewport.height) {
  427. minY = (viewport.height - sh) / 2
  428. maxY = minY
  429. } else {
  430. minY = viewport.height - sh
  431. maxY = 0
  432. }
  433. image.x = Math.max(minX, Math.min(maxX, image.x))
  434. image.y = Math.max(minY, Math.min(maxY, image.y))
  435. }
  436. // 以视口坐标点(px,py)为锚点缩放图片到 newZoom,保证锚点内容不漂移
  437. function zoomToPoint(px, py, newZoom) {
  438. var oldZoom = image.scale
  439. // 锚点相对图片左上角的偏移(未缩放坐标)
  440. var relX = (px - image.x) / oldZoom
  441. var relY = (py - image.y) / oldZoom
  442. image.scale = newZoom
  443. image.x = px - relX * newZoom
  444. image.y = py - relY * newZoom
  445. clampPos()
  446. }
  447. SplitView {
  448. anchors.fill: parent
  449. orientation: Qt.Horizontal
  450. // 图片显示区域 (70%)
  451. GridView {
  452. id: imageGridView
  453. SplitView.preferredWidth: parent.width * 0.7
  454. clip: true
  455. cellWidth: Math.max(45, Math.floor((imageGridView.width - 10) / 8))
  456. cellHeight: cellWidth
  457. model: ListModel { id: imageListModel }
  458. delegate: Item {
  459. width: imageGridView.cellWidth - 5
  460. height: imageGridView.cellHeight - 5
  461. // 是否为原始视频流文件(.rawv)
  462. readonly property bool isVideo: model.filePath ? rawPlayer.isRawVideo(model.filePath) : false
  463. Rectangle {
  464. anchors.fill: parent
  465. anchors.margins: 2
  466. color: model.selected ? "#28a779" : "transparent"
  467. border.color: model.selected ? "#28a779" : (model.filePath ? "#eee" : "#bbb")
  468. border.width: 1
  469. radius: 3
  470. // 有图片时加载缩略图,缺图时显示灰色占位
  471. Image {
  472. anchors.fill: parent
  473. anchors.margins: 2
  474. source: model.filePath ? "file://" + model.filePath : ""
  475. asynchronous: true
  476. sourceSize.width: 200
  477. sourceSize.height: 200
  478. fillMode: Image.PreserveAspectFit
  479. cache: true
  480. visible: model.filePath !== "" && !isVideo
  481. BusyIndicator {
  482. anchors.centerIn: parent
  483. running: parent.status === Image.Loading
  484. width: 24
  485. height: 24
  486. }
  487. }
  488. // 视频流文件占位:显示播放标识与文件名
  489. Rectangle {
  490. anchors.fill: parent
  491. anchors.margins: 2
  492. visible: isVideo
  493. color: "#1b1b1b"
  494. radius: 3
  495. Column {
  496. anchors.centerIn: parent
  497. spacing: 2
  498. Text {
  499. anchors.horizontalCenter: parent.horizontalCenter
  500. text: "▶"
  501. color: "#28a779"
  502. font.pixelSize: 20
  503. }
  504. Text {
  505. width: imageGridView.cellWidth - 15
  506. anchors.horizontalCenter: parent.horizontalCenter
  507. text: model.filePath ? model.filePath.split("/").pop() : ""
  508. color: "white"
  509. font.pixelSize: 9
  510. elide: Text.ElideMiddle
  511. horizontalAlignment: Text.AlignHCenter
  512. }
  513. }
  514. }
  515. // 缺图占位提示
  516. Text {
  517. anchors.centerIn: parent
  518. visible: model.filePath === ""
  519. text: "缺图"
  520. color: "#999"
  521. font.pixelSize: 11
  522. }
  523. // 序号角标(左上角,右上角为1)
  524. Rectangle {
  525. x: 0
  526. y: 0
  527. width: 18
  528. height: 18
  529. color: Qt.rgba(0, 0, 0, 0.55)
  530. radius: 9
  531. visible: model.serial !== undefined && model.serial >= 1
  532. Text {
  533. anchors.centerIn: parent
  534. text: model.serial
  535. color: "white"
  536. font.bold: true
  537. font.pixelSize: 11
  538. }
  539. }
  540. }
  541. // 选择标记(右侧勾选)
  542. Rectangle {
  543. x: parent.width - width
  544. y: 0
  545. width: 18
  546. height: 18
  547. color: "#28a779"
  548. radius: 9
  549. visible: model.selected // 只对已选中的图片显示勾选标记
  550. Text {
  551. anchors.centerIn: parent
  552. text: "✓"
  553. color: "white"
  554. font.bold: true
  555. }
  556. }
  557. // MouseArea(在外层Rectangle外面,使用delegate Item作为parent)
  558. MouseArea {
  559. anchors.fill: parent
  560. enabled: model.filePath !== ""
  561. onClicked: {
  562. if (!model.filePath) return
  563. // 视频流文件直接播放
  564. if (isVideo) {
  565. openVideo(model.filePath)
  566. return
  567. }
  568. if (isSelecting) {
  569. toggleImageSelection(model.filePath)
  570. } else {
  571. console.log("Selected image:", model.filePath)
  572. showFullscreen(model.filePath)
  573. }
  574. }
  575. onPressAndHold: {
  576. if (!model.filePath) return
  577. if (isVideo) return
  578. toggleImageSelection(model.filePath)
  579. }
  580. }
  581. }
  582. }
  583. // 归档树形列表 (30%)
  584. ScrollView {//滚动容器
  585. SplitView.preferredWidth: parent.width * 0.3
  586. SplitView.minimumWidth: 200
  587. clip: true
  588. // AppText {
  589. // text: "图片按年月日归档"
  590. // }
  591. TreeView {
  592. id: archiveTreeView
  593. model: treeModel
  594. anchors.fill: parent
  595. selectionModel: ItemSelectionModel {} // 处理选择状态
  596. selectionBehavior: TreeView.SelectRows // 整行选择
  597. delegate: Item {
  598. id: root
  599. implicitWidth: archiveTreeView.width
  600. implicitHeight: 40
  601. // 必须声明的 required 属性(由 TreeView 自动填充)
  602. required property TreeView treeView
  603. required property var model
  604. //其他列中的delegate Item将将此属性设置为,并将显示模型中其余列的数据(通常不缩进)。
  605. required property bool isTreeNode
  606. //绘制的model Item是否在视图中展开
  607. required property bool expanded
  608. //绘制的model Item是否在模型中有子项
  609. required property int hasChildren
  610. //包含delegate绘制的model Item的深度。model Item的深度与其在模型中的祖先数量相同
  611. required property int depth
  612. required property int type
  613. Rectangle {
  614. // width: parent.width
  615. // height: parent.height
  616. Row {
  617. spacing: 8
  618. anchors.leftMargin: 10 + depth * 20
  619. // 展开/折叠指示器
  620. Text {
  621. id: indicator
  622. visible: root.type===0 || root.type === 1 || root.type===2 || root.type===3 || root.type===4 || root.type===5
  623. text: root.expanded ? "▼" : "▶"
  624. color: "#666"
  625. anchors.verticalCenter: parent.verticalCenter
  626. //anchors.verticalCenter: parent.verticalCenter
  627. // --- 添加旋转动画 ---
  628. RotationAnimation on rotation {
  629. id: rotationAnim
  630. duration: 200
  631. easing.type: Easing.InOutQuad
  632. }
  633. // 监听 expanded 属性变化以触发动画
  634. onVisibleChanged: {
  635. if(visible) {
  636. rotation = root.expanded ? 90 : 0; // 初始角度
  637. }
  638. }
  639. Connections {
  640. target: root
  641. function onExpandedChanged() {
  642. if(indicator.visible) {
  643. rotationAnim.to = root.expanded ? 90 : 0;
  644. rotationAnim.restart();
  645. }
  646. }
  647. }
  648. }
  649. TapHandler{
  650. onSingleTapped: {
  651. if(root.type===6){
  652. // 原始视频流文件(.rawv):直接打开播放
  653. if (rawPlayer.isRawVideo(model.path)) {
  654. openVideo(model.path)
  655. return
  656. }
  657. imageListModel.clear()
  658. imageListModel.append({filePath: model.path})
  659. }else{
  660. let index = treeView.model.index(row, column)
  661. console.log("row,column",row,column)
  662. treeModel.loadChildren(index); // 同步加载子节点(阻塞直到完成)
  663. treeView.toggleExpanded(row); // 显式展开(确保视图刷新)
  664. }
  665. console.log("root.type:", root.type)
  666. }
  667. onLongPressed: {
  668. console.log("长按:", root.type)
  669. if (model.type===4 || model.type===5) {
  670. // 加载文件夹内容到图片区(支持相册和位置文件夹)
  671. loadFolderContents(model.path)
  672. }
  673. }
  674. }
  675. // 名称
  676. Text {
  677. text: {
  678. if(root.type===5||root.type===4||root.type===3||root.type===2||root.type===1||root.type===0){
  679. return model.name + " (" + model.childCount + ")"
  680. }
  681. return model.name
  682. }
  683. color: (root.type===0 || root.type === 1 || root.type===2 || root.type===3 || root.type===4 || root.type===5) ? "#333" : "#666"
  684. font.bold: root.type===0 || root.type === 1 || root.type===2 || root.type===3 || root.type===4 || root.type===5
  685. anchors.verticalCenter: parent.verticalCenter
  686. elide: Text.ElideRight
  687. width: archiveTreeView.width// - 50 - depth * 20
  688. }
  689. }
  690. }
  691. }
  692. }
  693. }
  694. }
  695. Connections {
  696. target: treeModel
  697. function onLoadingChanged() {
  698. // C++ 里的 loadRootFolders() 跑完之后会把 loading 设回 false
  699. if (!treeModel.loading && !rootExpanded) {
  700. console.log("TreeModel 加载完成,展开根节点")
  701. isLoading = false
  702. // 第一个可见行是第一个年份节点(root 是不可见的内部节点)
  703. var yearIndex = treeModel.index(0, 0)
  704. if (yearIndex.valid) {
  705. // 让 C++ 把该年份下面的 月/日/相册/图片 全部加载好
  706. treeModel.loadChildren(yearIndex)
  707. // 展开第 0 行(Qt.labs.treeview 的 TreeView 用 row 作为参数)
  708. archiveTreeView.expand(0)
  709. rootExpanded = true
  710. }
  711. }
  712. }
  713. }
  714. // 长按文件夹:按蛇形网格(8列x4行=32格,右上角为1)展示图片,默认显示锐化图
  715. function loadFolderContents(folderPath) {
  716. var bySerial = {}
  717. var byBase = {}
  718. var stack = [folderPath]
  719. while (stack.length > 0) {
  720. var currentPath = stack.pop()
  721. var files = treeModel.getFolderContents(currentPath)
  722. for (var i = 0; i < files.length; i++) {
  723. if (files[i].isFolder) {
  724. stack.push(files[i].path)
  725. } else {
  726. collectImageFile(files[i].path, bySerial, byBase)
  727. }
  728. }
  729. }
  730. rebuildImageGrid(bySerial, byBase)
  731. }
  732. // 解析图片文件名:有序号(1-32)归入 bySerial 蛇形网格,无序号归入 byBase 按基础名(时间戳)排序
  733. function collectImageFile(path, bySerial, byBase) {
  734. var base = path.slice(path.lastIndexOf('/') + 1)
  735. var dot = base.lastIndexOf('.')
  736. var stem = dot > 0 ? base.slice(0, dot) : base
  737. var isSharp = stem.indexOf('_sharp_') >= 0
  738. // 去掉 sharp 后缀得到基础名(用于原图/锐化配对)
  739. var key = isSharp ? stem.replace('_sharp', '') : stem
  740. var us = key.lastIndexOf('_')
  741. var serial = us >= 0 ? parseInt(key.slice(us + 1)) : 0
  742. if (serial >= 1 && serial <= 32) {
  743. if (!bySerial[serial]) bySerial[serial] = {orig: "", sharp: ""}
  744. if (isSharp) bySerial[serial].sharp = path
  745. else bySerial[serial].orig = path
  746. } else {
  747. // 无序号:按基础名(通常含时间戳)配对,用于按时间先后排序
  748. if (!byBase[key]) byBase[key] = {orig: "", sharp: "", name: key}
  749. if (isSharp) byBase[key].sharp = path
  750. else byBase[key].orig = path
  751. }
  752. }
  753. // 按蛇形显示顺序重建网格(模型顺序=画面从左到右、从上到下),无序号图片按时间先后追加在末尾
  754. function rebuildImageGrid(bySerial, byBase) {
  755. imageListModel.clear()
  756. var order = [
  757. 32,25,24,17,16,9,8,1,
  758. 31,26,23,18,15,10,7,2,
  759. 30,27,22,19,14,11,6,3,
  760. 29,28,21,20,13,12,5,4
  761. ]
  762. for (var i = 0; i < order.length; i++) {
  763. var s = order[i]
  764. var e = bySerial[s] || {orig: "", sharp: ""}
  765. var fp = showSharp ? (e.sharp || e.orig) : (e.orig || e.sharp)
  766. imageListModel.append({filePath: fp, serial: s, origPath: e.orig, sharpPath: e.sharp, selected: false})
  767. }
  768. // 无序号图片:按基础名(时间戳)先后排序追加
  769. var names = []
  770. for (var k in byBase) names.push(k)
  771. names.sort()
  772. for (var j = 0; j < names.length; j++) {
  773. var be = byBase[names[j]]
  774. var bfp = showSharp ? (be.sharp || be.orig) : (be.orig || be.sharp)
  775. imageListModel.append({filePath: bfp, serial: -1, origPath: be.orig, sharpPath: be.sharp, selected: false})
  776. }
  777. }
  778. // 切换显示 原图/锐化图
  779. function setShowSharp(v) {
  780. showSharp = v
  781. for (var i = 0; i < imageListModel.count; i++) {
  782. var it = imageListModel.get(i)
  783. var fp = showSharp ? (it.sharpPath || it.origPath) : (it.origPath || it.sharpPath)
  784. imageListModel.setProperty(i, "filePath", fp)
  785. }
  786. }
  787. function loadRootDirectory() {
  788. if(isLoading) return
  789. rootExpanded = false
  790. console.log("开始加载根目录...")
  791. // 触发 C++ 里的 resetModel(),异步加载年份目录
  792. treeModel.resetModel()
  793. }
  794. Component.onCompleted: {
  795. loadRootDirectory()
  796. }
  797. }