PhotostitcherPage.qml 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. import Felgo
  2. import QtQuick
  3. import QtQuick.Controls
  4. import QtQuick.Layouts
  5. import "../controls"
  6. import "../common"
  7. import Frames 0.1
  8. import QtMultimedia
  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. nativeUtils.displayMessageBox("提示", "分析失败,请重试!", 1)
  260. }
  261. }
  262. function onFuseFinished(path) {
  263. if (path) {
  264. console.log("融合完成:", path)
  265. nativeUtils.displayMessageBox("提示", "融合完成,已保存:\n" + path.split("/").pop(), 1)
  266. } else {
  267. nativeUtils.displayMessageBox("提示", "融合失败,请重试!", 1)
  268. }
  269. }
  270. // 录制视频流文件已被删除:关闭播放并刷新界面上的相关条目
  271. function onFileRemoved(path) {
  272. console.log("录制视频流已删除:", path)
  273. closeVideo()
  274. for (var i = imageListModel.count - 1; i >= 0; i--) {
  275. if (imageListModel.get(i).filePath === path)
  276. imageListModel.remove(i)
  277. }
  278. treeModel.resetModel() //归档树重新加载,去掉已删除的条目
  279. nativeUtils.displayMessageBox("提示", "已删除:\n" + path.split("/").pop(), 1)
  280. }
  281. }
  282. // 打开原始视频流文件并开始播放
  283. function openVideo(filePath) {
  284. console.log("播放原始视频流:", filePath)
  285. if (rawPlayer.open(filePath)) {
  286. videoPopup.videoTitle = filePath.split("/").pop()
  287. videoPopup.open()
  288. rawPlayer.play()
  289. } else {
  290. nativeUtils.displayMessageBox("提示", "无法播放该视频流文件!", 1)
  291. }
  292. }
  293. function closeVideo() {
  294. rawPlayer.stop()
  295. rawPlayer.close()
  296. videoPopup.close()
  297. }
  298. Popup {
  299. id: videoPopup
  300. width: parent.width
  301. height: parent.height
  302. modal: true
  303. closePolicy: Popup.NoAutoClose
  304. property string videoTitle: ""
  305. background: Rectangle {
  306. color: "#000000"
  307. }
  308. VideoOutput {
  309. id: videoOutput
  310. anchors.fill: parent
  311. anchors.bottomMargin: dp(80)
  312. fillMode: VideoOutput.PreserveAspectFit
  313. }
  314. Column {
  315. anchors.top: parent.top
  316. anchors.topMargin: dp(12)
  317. anchors.horizontalCenter: parent.horizontalCenter
  318. spacing: dp(4)
  319. Text {
  320. anchors.horizontalCenter: parent.horizontalCenter
  321. text: videoPopup.videoTitle
  322. color: "white"
  323. font.bold: true
  324. font.pixelSize: 14
  325. }
  326. Text {
  327. anchors.horizontalCenter: parent.horizontalCenter
  328. text: rawPlayer.position + " / " + rawPlayer.frameCount + " 帧"
  329. color: "#28a779"
  330. font.pixelSize: 12
  331. }
  332. }
  333. Row {
  334. anchors.bottom: parent.bottom
  335. anchors.bottomMargin: dp(16)
  336. anchors.horizontalCenter: parent.horizontalCenter
  337. spacing: dp(16)
  338. AppButton {
  339. text: rawPlayer.playing ? "暂停" : "播放"
  340. textColor: "white"
  341. backgroundColor: "#28a779"
  342. flat: false
  343. enabled: !rawPlayer.busy
  344. minimumHeight: dp(40)
  345. minimumWidth: dp(80)
  346. onClicked: {
  347. if (rawPlayer.playing) rawPlayer.pause()
  348. else rawPlayer.play()
  349. }
  350. }
  351. AppButton {
  352. text: "分析"
  353. textColor: "white"
  354. backgroundColor: "#3b82f6"
  355. flat: false
  356. enabled: !rawPlayer.busy
  357. minimumHeight: dp(40)
  358. minimumWidth: dp(80)
  359. onClicked: {
  360. rawPlayer.pause()
  361. rawPlayer.analyzeSharpestFrame()
  362. }
  363. }
  364. AppButton {
  365. text: "融合"
  366. textColor: "white"
  367. backgroundColor: "#8b5cf6"
  368. flat: false
  369. enabled: !rawPlayer.busy
  370. minimumHeight: dp(40)
  371. minimumWidth: dp(80)
  372. onClicked: {
  373. rawPlayer.pause()
  374. rawPlayer.fuseVideoFrames()
  375. }
  376. }
  377. AppButton {
  378. text: "删除"
  379. textColor: "white"
  380. backgroundColor: "#e74c3c"
  381. flat: false
  382. enabled: !rawPlayer.busy
  383. minimumHeight: dp(40)
  384. minimumWidth: dp(80)
  385. onClicked: deleteConfirmDialog.open()
  386. }
  387. AppButton {
  388. text: "关闭"
  389. textColor: "white"
  390. backgroundColor: "#555555"
  391. flat: false
  392. minimumHeight: dp(40)
  393. minimumWidth: dp(80)
  394. onClicked: closeVideo()
  395. }
  396. }
  397. Rectangle {
  398. anchors.fill: parent
  399. visible: rawPlayer.busy
  400. color: "#99000000"
  401. z: 10
  402. Column {
  403. anchors.centerIn: parent
  404. spacing: dp(10)
  405. BusyIndicator {
  406. anchors.horizontalCenter: parent.horizontalCenter
  407. running: rawPlayer.busy
  408. width: dp(48)
  409. height: dp(48)
  410. }
  411. Text {
  412. anchors.horizontalCenter: parent.horizontalCenter
  413. text: "正在处理,请稍候..."
  414. color: "white"
  415. font.pixelSize: 15
  416. }
  417. }
  418. }
  419. }
  420. // 删除录制视频流前的确认框(用 Popup 实现,避免与 Felgo 的 Dialog 同名冲突)
  421. Popup {
  422. id: deleteConfirmDialog
  423. width: parent.width * 0.8
  424. height: dp(170)
  425. x: (parent.width - width) / 2
  426. y: (parent.height - height) / 2
  427. padding: 0
  428. modal: true
  429. closePolicy: Popup.NoAutoClose
  430. background: Rectangle {
  431. color: "white"
  432. radius: dp(6)
  433. }
  434. Text {
  435. id: confirmTitle
  436. anchors.left: parent.left
  437. anchors.right: parent.right
  438. anchors.top: parent.top
  439. anchors.margins: dp(12)
  440. color: "#333333"
  441. font.bold: true
  442. font.pixelSize: 15
  443. text: "删除视频流"
  444. }
  445. Text {
  446. id: confirmText
  447. anchors.left: parent.left
  448. anchors.right: parent.right
  449. anchors.top: confirmTitle.bottom
  450. anchors.leftMargin: dp(12)
  451. anchors.rightMargin: dp(12)
  452. anchors.topMargin: dp(8)
  453. color: "black"
  454. wrapMode: Text.Wrap
  455. text: "确定删除该录制视频流文件?\n" + videoPopup.videoTitle
  456. }
  457. Row {
  458. anchors.horizontalCenter: parent.horizontalCenter
  459. anchors.top: confirmText.bottom
  460. anchors.topMargin: dp(16)
  461. spacing: dp(10)
  462. AppButton {
  463. text: "取消"
  464. flat: false
  465. textColor: "black"
  466. backgroundColor: "#e9e9e9"
  467. minimumHeight: dp(36)
  468. minimumWidth: dp(70)
  469. onClicked: deleteConfirmDialog.close()
  470. }
  471. AppButton {
  472. text: "删除"
  473. flat: false
  474. textColor: "white"
  475. backgroundColor: "#e74c3c"
  476. minimumHeight: dp(36)
  477. minimumWidth: dp(70)
  478. onClicked: {
  479. deleteConfirmDialog.close()
  480. deleteCurrentVideo()
  481. }
  482. }
  483. }
  484. }
  485. // 删除当前正在播放的录制视频流文件(.rawv,保留分析/融合结果图片)
  486. function deleteCurrentVideo() {
  487. if (rawPlayer.source === "")
  488. return
  489. if (!rawPlayer.removeCurrentFile())
  490. nativeUtils.displayMessageBox("提示", "删除失败,请重试!", 1)
  491. }
  492. // 重置缩放为初始大小并居中
  493. function resetTransform() {
  494. image.scale = 1.0
  495. clampPos()
  496. }
  497. // 约束图片位置:小于视口时居中,大于视口时限制不露出空白边缘
  498. function clampPos() {
  499. var sw = image.width * image.scale
  500. var sh = image.height * image.scale
  501. var minX, maxX, minY, maxY
  502. if (sw <= viewport.width) {
  503. minX = (viewport.width - sw) / 2
  504. maxX = minX
  505. } else {
  506. minX = viewport.width - sw
  507. maxX = 0
  508. }
  509. if (sh <= viewport.height) {
  510. minY = (viewport.height - sh) / 2
  511. maxY = minY
  512. } else {
  513. minY = viewport.height - sh
  514. maxY = 0
  515. }
  516. image.x = Math.max(minX, Math.min(maxX, image.x))
  517. image.y = Math.max(minY, Math.min(maxY, image.y))
  518. }
  519. // 以视口坐标点(px,py)为锚点缩放图片到 newZoom,保证锚点内容不漂移
  520. function zoomToPoint(px, py, newZoom) {
  521. var oldZoom = image.scale
  522. // 锚点相对图片左上角的偏移(未缩放坐标)
  523. var relX = (px - image.x) / oldZoom
  524. var relY = (py - image.y) / oldZoom
  525. image.scale = newZoom
  526. image.x = px - relX * newZoom
  527. image.y = py - relY * newZoom
  528. clampPos()
  529. }
  530. SplitView {
  531. anchors.fill: parent
  532. orientation: Qt.Horizontal
  533. // 图片显示区域 (70%)
  534. GridView {
  535. id: imageGridView
  536. SplitView.preferredWidth: parent.width * 0.7
  537. clip: true
  538. cellWidth: Math.max(45, Math.floor((imageGridView.width - 10) / 8))
  539. cellHeight: cellWidth
  540. model: ListModel { id: imageListModel }
  541. delegate: Item {
  542. width: imageGridView.cellWidth - 5
  543. height: imageGridView.cellHeight - 5
  544. // 是否为原始视频流文件(.rawv)
  545. readonly property bool isVideo: model.filePath ? rawPlayer.isRawVideo(model.filePath) : false
  546. Rectangle {
  547. anchors.fill: parent
  548. anchors.margins: 2
  549. color: model.selected ? "#28a779" : "transparent"
  550. border.color: model.selected ? "#28a779" : (model.filePath ? "#eee" : "#bbb")
  551. border.width: 1
  552. radius: 3
  553. // 有图片时加载缩略图,缺图时显示灰色占位
  554. Image {
  555. anchors.fill: parent
  556. anchors.margins: 2
  557. source: model.filePath ? "file://" + model.filePath : ""
  558. asynchronous: true
  559. sourceSize.width: 200
  560. sourceSize.height: 200
  561. fillMode: Image.PreserveAspectFit
  562. cache: true
  563. visible: model.filePath !== "" && !isVideo
  564. BusyIndicator {
  565. anchors.centerIn: parent
  566. running: parent.status === Image.Loading
  567. width: 24
  568. height: 24
  569. }
  570. }
  571. // 视频流文件占位:显示播放标识与文件名
  572. Rectangle {
  573. anchors.fill: parent
  574. anchors.margins: 2
  575. visible: isVideo
  576. color: "#1b1b1b"
  577. radius: 3
  578. Column {
  579. anchors.centerIn: parent
  580. spacing: 2
  581. Text {
  582. anchors.horizontalCenter: parent.horizontalCenter
  583. text: "▶"
  584. color: "#28a779"
  585. font.pixelSize: 20
  586. }
  587. Text {
  588. width: imageGridView.cellWidth - 15
  589. anchors.horizontalCenter: parent.horizontalCenter
  590. text: model.filePath ? model.filePath.split("/").pop() : ""
  591. color: "white"
  592. font.pixelSize: 9
  593. elide: Text.ElideMiddle
  594. horizontalAlignment: Text.AlignHCenter
  595. }
  596. }
  597. }
  598. // 缺图占位提示
  599. Text {
  600. anchors.centerIn: parent
  601. visible: model.filePath === ""
  602. text: "缺图"
  603. color: "#999"
  604. font.pixelSize: 11
  605. }
  606. // 序号角标(左上角,右上角为1)
  607. Rectangle {
  608. x: 0
  609. y: 0
  610. width: 18
  611. height: 18
  612. color: Qt.rgba(0, 0, 0, 0.55)
  613. radius: 9
  614. visible: model.serial !== undefined && model.serial >= 1
  615. Text {
  616. anchors.centerIn: parent
  617. text: model.serial
  618. color: "white"
  619. font.bold: true
  620. font.pixelSize: 11
  621. }
  622. }
  623. }
  624. // 选择标记(右侧勾选)
  625. Rectangle {
  626. x: parent.width - width
  627. y: 0
  628. width: 18
  629. height: 18
  630. color: "#28a779"
  631. radius: 9
  632. visible: model.selected // 只对已选中的图片显示勾选标记
  633. Text {
  634. anchors.centerIn: parent
  635. text: "✓"
  636. color: "white"
  637. font.bold: true
  638. }
  639. }
  640. // MouseArea(在外层Rectangle外面,使用delegate Item作为parent)
  641. MouseArea {
  642. anchors.fill: parent
  643. enabled: model.filePath !== ""
  644. onClicked: {
  645. if (!model.filePath) return
  646. // 视频流文件直接打开播放
  647. if (isVideo) {
  648. openVideo(model.filePath)
  649. return
  650. }
  651. if (isSelecting) {
  652. toggleImageSelection(model.filePath)
  653. } else {
  654. console.log("Selected image:", model.filePath)
  655. showFullscreen(model.filePath)
  656. }
  657. }
  658. onPressAndHold: {
  659. if (!model.filePath) return
  660. if (isVideo) return
  661. toggleImageSelection(model.filePath)
  662. }
  663. }
  664. }
  665. }
  666. // 归档树形列表 (30%)
  667. ScrollView {//滚动容器
  668. SplitView.preferredWidth: parent.width * 0.3
  669. SplitView.minimumWidth: 200
  670. clip: true
  671. // AppText {
  672. // text: "图片按年月日归档"
  673. // }
  674. TreeView {
  675. id: archiveTreeView
  676. model: treeModel
  677. anchors.fill: parent
  678. selectionModel: ItemSelectionModel {} // 处理选择状态
  679. selectionBehavior: TreeView.SelectRows // 整行选择
  680. delegate: Item {
  681. id: root
  682. implicitWidth: archiveTreeView.width
  683. implicitHeight: 40
  684. // 必须声明的 required 属性(由 TreeView 自动填充)
  685. required property TreeView treeView
  686. required property var model
  687. //其他列中的delegate Item将将此属性设置为,并将显示模型中其余列的数据(通常不缩进)。
  688. required property bool isTreeNode
  689. //绘制的model Item是否在视图中展开
  690. required property bool expanded
  691. //绘制的model Item是否在模型中有子项
  692. required property int hasChildren
  693. //包含delegate绘制的model Item的深度。model Item的深度与其在模型中的祖先数量相同
  694. required property int depth
  695. required property int type
  696. Rectangle {
  697. // width: parent.width
  698. // height: parent.height
  699. Row {
  700. spacing: 8
  701. anchors.leftMargin: 10 + depth * 20
  702. // 展开/折叠指示器
  703. Text {
  704. id: indicator
  705. visible: root.type===0 || root.type === 1 || root.type===2 || root.type===3 || root.type===4 || root.type===5
  706. text: root.expanded ? "▼" : "▶"
  707. color: "#666"
  708. anchors.verticalCenter: parent.verticalCenter
  709. //anchors.verticalCenter: parent.verticalCenter
  710. // --- 添加旋转动画 ---
  711. RotationAnimation on rotation {
  712. id: rotationAnim
  713. duration: 200
  714. easing.type: Easing.InOutQuad
  715. }
  716. // 监听 expanded 属性变化以触发动画
  717. onVisibleChanged: {
  718. if(visible) {
  719. rotation = root.expanded ? 90 : 0; // 初始角度
  720. }
  721. }
  722. Connections {
  723. target: root
  724. function onExpandedChanged() {
  725. if(indicator.visible) {
  726. rotationAnim.to = root.expanded ? 90 : 0;
  727. rotationAnim.restart();
  728. }
  729. }
  730. }
  731. }
  732. TapHandler{
  733. onSingleTapped: {
  734. if(root.type===6){
  735. // 原始视频流文件(.rawv):直接打开播放
  736. if (rawPlayer.isRawVideo(model.path)) {
  737. openVideo(model.path)
  738. return
  739. }
  740. imageListModel.clear()
  741. imageListModel.append({filePath: model.path})
  742. }else{
  743. let index = treeView.model.index(row, column)
  744. console.log("row,column",row,column)
  745. treeModel.loadChildren(index); // 同步加载子节点(阻塞直到完成)
  746. treeView.toggleExpanded(row); // 显式展开(确保视图刷新)
  747. }
  748. console.log("root.type:", root.type)
  749. }
  750. onLongPressed: {
  751. console.log("长按:", root.type)
  752. if (model.type===4 || model.type===5) {
  753. // 加载文件夹内容到图片区(支持相册和位置文件夹)
  754. loadFolderContents(model.path)
  755. }
  756. }
  757. }
  758. // 名称
  759. Text {
  760. text: {
  761. if(root.type===5||root.type===4||root.type===3||root.type===2||root.type===1||root.type===0){
  762. return model.name + " (" + model.childCount + ")"
  763. }
  764. return model.name
  765. }
  766. color: (root.type===0 || root.type === 1 || root.type===2 || root.type===3 || root.type===4 || root.type===5) ? "#333" : "#666"
  767. font.bold: root.type===0 || root.type === 1 || root.type===2 || root.type===3 || root.type===4 || root.type===5
  768. anchors.verticalCenter: parent.verticalCenter
  769. elide: Text.ElideRight
  770. width: archiveTreeView.width// - 50 - depth * 20
  771. }
  772. }
  773. }
  774. }
  775. }
  776. }
  777. }
  778. Connections {
  779. target: treeModel
  780. function onLoadingChanged() {
  781. // C++ 里的 loadRootFolders() 跑完之后会把 loading 设回 false
  782. if (!treeModel.loading && !rootExpanded) {
  783. console.log("TreeModel 加载完成,展开根节点")
  784. isLoading = false
  785. // 第一个可见行是第一个年份节点(root 是不可见的内部节点)
  786. var yearIndex = treeModel.index(0, 0)
  787. if (yearIndex.valid) {
  788. // 让 C++ 把该年份下面的 月/日/相册/图片 全部加载好
  789. treeModel.loadChildren(yearIndex)
  790. // 展开第 0 行(Qt.labs.treeview 的 TreeView 用 row 作为参数)
  791. archiveTreeView.expand(0)
  792. rootExpanded = true
  793. }
  794. }
  795. }
  796. }
  797. // 长按文件夹:按蛇形网格(8列x4行=32格,右上角为1)展示图片,默认显示锐化图
  798. function loadFolderContents(folderPath) {
  799. var bySerial = {}
  800. var byBase = {}
  801. var stack = [folderPath]
  802. while (stack.length > 0) {
  803. var currentPath = stack.pop()
  804. var files = treeModel.getFolderContents(currentPath)
  805. for (var i = 0; i < files.length; i++) {
  806. if (files[i].isFolder) {
  807. stack.push(files[i].path)
  808. } else {
  809. collectImageFile(files[i].path, bySerial, byBase)
  810. }
  811. }
  812. }
  813. rebuildImageGrid(bySerial, byBase)
  814. }
  815. // 解析图片文件名:有序号(1-32)归入 bySerial 蛇形网格,无序号归入 byBase 按基础名(时间戳)排序
  816. function collectImageFile(path, bySerial, byBase) {
  817. var base = path.slice(path.lastIndexOf('/') + 1)
  818. var dot = base.lastIndexOf('.')
  819. var stem = dot > 0 ? base.slice(0, dot) : base
  820. var isSharp = stem.indexOf('_sharp_') >= 0
  821. // 去掉 sharp 后缀得到基础名(用于原图/锐化配对)
  822. var key = isSharp ? stem.replace('_sharp', '') : stem
  823. var us = key.lastIndexOf('_')
  824. var serial = us >= 0 ? parseInt(key.slice(us + 1)) : 0
  825. if (serial >= 1 && serial <= 32) {
  826. if (!bySerial[serial]) bySerial[serial] = {orig: "", sharp: ""}
  827. if (isSharp) bySerial[serial].sharp = path
  828. else bySerial[serial].orig = path
  829. } else {
  830. // 无序号:按基础名(通常含时间戳)配对,用于按时间先后排序
  831. if (!byBase[key]) byBase[key] = {orig: "", sharp: "", name: key}
  832. if (isSharp) byBase[key].sharp = path
  833. else byBase[key].orig = path
  834. }
  835. }
  836. // 按蛇形显示顺序重建网格(模型顺序=画面从左到右、从上到下),无序号图片按时间先后追加在末尾
  837. function rebuildImageGrid(bySerial, byBase) {
  838. imageListModel.clear()
  839. var order = [
  840. 32,25,24,17,16,9,8,1,
  841. 31,26,23,18,15,10,7,2,
  842. 30,27,22,19,14,11,6,3,
  843. 29,28,21,20,13,12,5,4
  844. ]
  845. for (var i = 0; i < order.length; i++) {
  846. var s = order[i]
  847. var e = bySerial[s] || {orig: "", sharp: ""}
  848. var fp = showSharp ? (e.sharp || e.orig) : (e.orig || e.sharp)
  849. imageListModel.append({filePath: fp, serial: s, origPath: e.orig, sharpPath: e.sharp, selected: false})
  850. }
  851. // 无序号图片:按基础名(时间戳)先后排序追加
  852. var names = []
  853. for (var k in byBase) names.push(k)
  854. names.sort()
  855. for (var j = 0; j < names.length; j++) {
  856. var be = byBase[names[j]]
  857. var bfp = showSharp ? (be.sharp || be.orig) : (be.orig || be.sharp)
  858. imageListModel.append({filePath: bfp, serial: -1, origPath: be.orig, sharpPath: be.sharp, selected: false})
  859. }
  860. }
  861. // 切换显示 原图/锐化图
  862. function setShowSharp(v) {
  863. showSharp = v
  864. for (var i = 0; i < imageListModel.count; i++) {
  865. var it = imageListModel.get(i)
  866. var fp = showSharp ? (it.sharpPath || it.origPath) : (it.origPath || it.sharpPath)
  867. imageListModel.setProperty(i, "filePath", fp)
  868. }
  869. }
  870. function loadRootDirectory() {
  871. if(isLoading) return
  872. rootExpanded = false
  873. console.log("开始加载根目录...")
  874. // 触发 C++ 里的 resetModel(),异步加载年份目录
  875. treeModel.resetModel()
  876. }
  877. Component.onCompleted: {
  878. loadRootDirectory()
  879. }
  880. }