PhotostitcherPage.qml 25 KB

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