PhotostitcherPage.qml 18 KB

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