PhotostitcherPage.qml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. // 融合按钮
  17. AppButton {
  18. id: fuseButton
  19. anchors.bottom: parent.bottom
  20. anchors.right: parent.right
  21. anchors.margins: 20
  22. text: "图像融合"
  23. visible: selectedImages.length >= 2 && !isFusing
  24. backgroundColor: "#28a779"
  25. textColor: "white"
  26. z: 100
  27. onClicked: {
  28. if (selectedImages.length >= 2) {
  29. isFusing = true
  30. console.log("开始融合", selectedImages.length, "张图片")
  31. app.isGlobalFusing = true
  32. app.globalFusionMessage = "开始融合..."
  33. app.globalFusionPercent = 0
  34. opencvFunc.fuseImagesAsync(selectedImages)
  35. }
  36. }
  37. }
  38. // 取消选择按钮
  39. AppButton {
  40. id: cancelButton
  41. anchors.bottom: parent.bottom
  42. anchors.left: parent.left
  43. anchors.margins: 20
  44. text: "取消选择"
  45. visible: isSelecting && !isFusing
  46. backgroundColor: "#e74c3c"
  47. textColor: "white"
  48. z: 100
  49. onClicked: {
  50. clearSelection()
  51. }
  52. }
  53. // 连接融合完成信号
  54. Connections {
  55. target: opencvFunc
  56. function onFuseFinished(resultPath) {
  57. isFusing = false
  58. app.isGlobalFusing = false
  59. if (resultPath) {
  60. console.log("融合完成:", resultPath)
  61. // 显示融合结果
  62. imageListModel.append({filePath: resultPath, isFused: true, selected: false})
  63. // 清除选择
  64. clearSelection()
  65. } else {
  66. console.error("融合失败")
  67. }
  68. }
  69. // 融合进度信号
  70. function onFusionProgress(message, percent) {
  71. console.log("融合进度:", message, percent + "%")
  72. // 全局弹窗由 Main.qml 监听
  73. }
  74. }
  75. function clearSelection() {
  76. selectedImages = []
  77. isSelecting = false
  78. for (var i = 0; i < imageListModel.count; i++) {
  79. imageListModel.setProperty(i, "selected", false)
  80. }
  81. }
  82. function toggleImageSelection(filePath) {
  83. // 创建新数组以触发QML属性绑定更新
  84. var temp = selectedImages.slice()
  85. var idx = temp.indexOf(filePath)
  86. if (idx >= 0) {
  87. temp.splice(idx, 1)
  88. } else {
  89. temp.push(filePath)
  90. }
  91. selectedImages = temp
  92. isSelecting = selectedImages.length > 0
  93. // 更新model中的selected状态
  94. for (var i = 0; i < imageListModel.count; i++) {
  95. if (imageListModel.get(i).filePath === filePath) {
  96. imageListModel.setProperty(i, "selected", idx < 0)
  97. break
  98. }
  99. }
  100. }
  101. // 全屏查看组件
  102. Popup {
  103. id: fullscreenPopup
  104. width: parent.width
  105. height: parent.height
  106. modal: true
  107. closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
  108. background: Rectangle {
  109. color: "#CC000000"
  110. }
  111. Flickable {
  112. id: flick
  113. anchors.fill: parent
  114. clip: true
  115. boundsBehavior: Flickable.DragAndOvershootBounds
  116. Image {
  117. id: image
  118. anchors.fill: parent
  119. asynchronous: true
  120. fillMode: Image.PreserveAspectFit
  121. width: Math.min(implicitWidth, parent.width)
  122. height: Math.min(implicitHeight, parent.height)
  123. property real scale: 1.0
  124. }
  125. PinchArea {
  126. anchors.fill: parent
  127. pinch.target: image
  128. pinch.minimumRotation: -360
  129. pinch.maximumRotation: 360
  130. pinch.minimumScale: 0.1
  131. pinch.maximumScale: 10
  132. pinch.dragAxis: Pinch.XAndYAxis
  133. // MouseArea handles drag gestures
  134. MouseArea {
  135. anchors.fill: parent
  136. drag.target: image
  137. drag.minimumX: -image.width
  138. drag.maximumX: fullscreenPopup.width
  139. drag.minimumY: -image.height
  140. drag.maximumY: fullscreenPopup.height
  141. }
  142. onPinchStarted: {
  143. // 保存当前缩放中心和比例
  144. pinchStartScale = image.scale
  145. pinchStartCenter = Qt.point(pinch.center.x, pinch.center.y)
  146. }
  147. onPinchUpdated: {
  148. // 计算新的缩放比例
  149. var newScale = pinchStartScale * pinch.scale
  150. // 限制缩放范围
  151. newScale = Math.max(image.minScale, Math.min(image.maxScale, newScale))
  152. // 应用缩放
  153. image.scale = newScale
  154. // 计算缩放中心点相对于图片的坐标
  155. var imageCenterX = (pinch.center.x - flick.contentX) / image.scale
  156. var imageCenterY = (pinch.center.y - flick.contentY) / image.scale
  157. // 调整内容位置以保持缩放中心
  158. flick.contentX = pinch.center.x - imageCenterX * image.scale
  159. flick.contentY = pinch.center.y - imageCenterY * image.scale
  160. }
  161. onPinchFinished: {
  162. flick.returnToBounds()
  163. }
  164. property real pinchStartScale: 1.0
  165. property point pinchStartCenter: Qt.point(0, 0)
  166. }
  167. }
  168. // 关闭按钮
  169. RoundButton {
  170. id: closeBtn
  171. text: "关闭" // 设置按钮文本
  172. radius: height/2 // 确保圆形
  173. // 基础样式
  174. width: 80
  175. height: 40
  176. font.pixelSize: 14
  177. font.bold: true
  178. // 颜色配置
  179. palette {
  180. button: "#e74c3c" // 背景色
  181. buttonText: "white" // 文字颜色
  182. }
  183. // 点击效果
  184. background: Rectangle {
  185. radius: closeBtn.radius
  186. color: closeBtn.palette.button
  187. border.color: Qt.darker(color, 1.2)
  188. border.width: 1
  189. Rectangle {
  190. anchors.fill: parent
  191. radius: parent.radius
  192. color: "transparent"
  193. border.color: Qt.lighter(parent.color, 1.2)
  194. border.width: closeBtn.down ? 2 : 0
  195. opacity: closeBtn.down ? 0.5 : 0
  196. Behavior on opacity { NumberAnimation { duration: 100 } }
  197. }
  198. }
  199. // 点击事件
  200. onClicked: {
  201. console.log("关闭操作触发")
  202. fullscreenPopup.close()
  203. // 添加具体关闭逻辑
  204. }
  205. }
  206. }
  207. function showFullscreen(path) {
  208. image.source = "file://" + path
  209. fullscreenPopup.open()
  210. resetTransform()
  211. }
  212. SplitView {
  213. anchors.fill: parent
  214. orientation: Qt.Horizontal
  215. // 图片显示区域 (70%)
  216. GridView {
  217. id: imageGridView
  218. SplitView.preferredWidth: parent.width * 0.7
  219. clip: true
  220. cellWidth: 200
  221. cellHeight: 200
  222. model: ListModel { id: imageListModel }
  223. delegate: Item {
  224. width: imageGridView.cellWidth - 5
  225. height: imageGridView.cellHeight - 5
  226. Rectangle {
  227. anchors.fill: parent
  228. anchors.margins: 5
  229. color: model.selected ? "#28a779" : "transparent"
  230. border.color: model.selected ? "#28a779" : "#eee"
  231. border.width: model.selected ? 3 : 1
  232. radius: 4
  233. Image {
  234. anchors.fill: parent
  235. anchors.margins: 2
  236. source: "file://" + filePath
  237. asynchronous: true
  238. sourceSize.width: 200
  239. sourceSize.height: 200
  240. fillMode: Image.PreserveAspectFit
  241. cache: true
  242. BusyIndicator {
  243. anchors.centerIn: parent
  244. running: parent.status === Image.Loading
  245. width: 32
  246. height: 32
  247. }
  248. }
  249. }
  250. // 选择标记(使用绝对定位代替anchors.topRight)
  251. Rectangle {
  252. x: parent.width - width - 7
  253. y: 7
  254. width: 24
  255. height: 24
  256. color: model.selected ? "#28a779" : "rgba(0,0,0,0.5)"
  257. radius: 12
  258. visible: model.selected // 只对已选中的图片显示勾选标记
  259. Text {
  260. anchors.centerIn: parent
  261. text: "✓"
  262. color: "white"
  263. font.bold: true
  264. }
  265. }
  266. // MouseArea(在外层Rectangle外面,使用delegate Item作为parent)
  267. MouseArea {
  268. anchors.fill: parent
  269. onClicked: {
  270. if (isSelecting) {
  271. toggleImageSelection(model.filePath)
  272. } else {
  273. console.log("Selected image:", filePath)
  274. showFullscreen(filePath)
  275. }
  276. }
  277. onPressAndHold: {
  278. toggleImageSelection(model.filePath)
  279. }
  280. }
  281. }
  282. }
  283. // 归档树形列表 (30%)
  284. ScrollView {//滚动容器
  285. SplitView.preferredWidth: parent.width * 0.3
  286. SplitView.minimumWidth: 200
  287. clip: true
  288. // AppText {
  289. // text: "图片按年月日归档"
  290. // }
  291. TreeView {
  292. id: archiveTreeView
  293. model: treeModel
  294. anchors.fill: parent
  295. selectionModel: ItemSelectionModel {} // 处理选择状态
  296. selectionBehavior: TreeView.SelectRows // 整行选择
  297. delegate: Item {
  298. id: root
  299. implicitWidth: archiveTreeView.width
  300. implicitHeight: 40
  301. // 必须声明的 required 属性(由 TreeView 自动填充)
  302. required property TreeView treeView
  303. required property var model
  304. //其他列中的delegate Item将将此属性设置为,并将显示模型中其余列的数据(通常不缩进)。
  305. required property bool isTreeNode
  306. //绘制的model Item是否在视图中展开
  307. required property bool expanded
  308. //绘制的model Item是否在模型中有子项
  309. required property int hasChildren
  310. //包含delegate绘制的model Item的深度。model Item的深度与其在模型中的祖先数量相同
  311. required property int depth
  312. required property int type
  313. Rectangle {
  314. // width: parent.width
  315. // height: parent.height
  316. Row {
  317. spacing: 8
  318. anchors.leftMargin: 10 + depth * 20
  319. // 展开/折叠指示器
  320. Text {
  321. id: indicator
  322. visible: root.type===0 || root.type === 1 || root.type===2 || root.type===3 || root.type===4 || root.type===5
  323. text: root.expanded ? "▼" : "▶"
  324. color: "#666"
  325. anchors.verticalCenter: parent.verticalCenter
  326. //anchors.verticalCenter: parent.verticalCenter
  327. // --- 添加旋转动画 ---
  328. RotationAnimation on rotation {
  329. id: rotationAnim
  330. duration: 200
  331. easing.type: Easing.InOutQuad
  332. }
  333. // 监听 expanded 属性变化以触发动画
  334. onVisibleChanged: {
  335. if(visible) {
  336. rotation = root.expanded ? 90 : 0; // 初始角度
  337. }
  338. }
  339. Connections {
  340. target: root
  341. function onExpandedChanged() {
  342. if(indicator.visible) {
  343. rotationAnim.to = root.expanded ? 90 : 0;
  344. rotationAnim.restart();
  345. }
  346. }
  347. }
  348. }
  349. TapHandler{
  350. onSingleTapped: {
  351. if(root.type===6){
  352. imageListModel.clear()
  353. imageListModel.append({filePath: model.path})
  354. }else{
  355. let index = treeView.model.index(row, column)
  356. console.log("row,column",row,column)
  357. treeModel.loadChildren(index); // 同步加载子节点(阻塞直到完成)
  358. treeView.toggleExpanded(row); // 显式展开(确保视图刷新)
  359. }
  360. console.log("root.type:", root.type)
  361. }
  362. onLongPressed: {
  363. console.log("长按:", root.type)
  364. if (model.type===4 || model.type===5) {
  365. // 加载文件夹内容到图片区(支持相册和位置文件夹)
  366. loadFolderContents(model.path)
  367. }
  368. }
  369. }
  370. // 名称
  371. Text {
  372. text: {
  373. if(root.type===5||root.type===4||root.type===3||root.type===2||root.type===1||root.type===0){
  374. return model.name + " (" + model.childCount + ")"
  375. }
  376. return model.name
  377. }
  378. color: (root.type===0 || root.type === 1 || root.type===2 || root.type===3 || root.type===4 || root.type===5) ? "#333" : "#666"
  379. font.bold: root.type===0 || root.type === 1 || root.type===2 || root.type===3 || root.type===4 || root.type===5
  380. anchors.verticalCenter: parent.verticalCenter
  381. elide: Text.ElideRight
  382. width: archiveTreeView.width// - 50 - depth * 20
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. Connections {
  391. target: treeModel
  392. function onLoadingChanged() {
  393. // C++ 里的 loadRootFolders() 跑完之后会把 loading 设回 false
  394. if (!treeModel.loading && !rootExpanded) {
  395. console.log("TreeModel 加载完成,展开根节点")
  396. isLoading = false
  397. // 第一个可见行是第一个年份节点(root 是不可见的内部节点)
  398. var yearIndex = treeModel.index(0, 0)
  399. if (yearIndex.valid) {
  400. // 让 C++ 把该年份下面的 月/日/相册/图片 全部加载好
  401. treeModel.loadChildren(yearIndex)
  402. // 展开第 0 行(Qt.labs.treeview 的 TreeView 用 row 作为参数)
  403. archiveTreeView.expand(0)
  404. rootExpanded = true
  405. }
  406. }
  407. }
  408. }
  409. // PhotoArchivePage.qml
  410. function loadFolderContents(folderPath) {
  411. imageListModel.clear()
  412. var stack = [folderPath]
  413. while (stack.length > 0) {
  414. var currentPath = stack.pop()
  415. var dir = Qt.createQmlObject('import QtQuick 2.0; QtObject {}', parent)
  416. var files = treeModel.getFolderContents(currentPath)
  417. for (var i = 0; i < files.length; i++) {
  418. if (files[i].isFolder) {
  419. stack.push(files[i].path)
  420. } else {
  421. imageListModel.append({filePath: files[i].path, selected: false})
  422. }
  423. }
  424. }
  425. }
  426. function loadRootDirectory() {
  427. if(isLoading) return
  428. rootExpanded = false
  429. console.log("开始加载根目录...")
  430. // 触发 C++ 里的 resetModel(),异步加载年份目录
  431. treeModel.resetModel()
  432. }
  433. Component.onCompleted: {
  434. loadRootDirectory()
  435. }
  436. }