PhotostitcherPage.qml 14 KB

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