ExecutionPage.qml 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. import QtQuick 2.15
  2. import QtQuick.Controls 2.15
  3. import QtQuick.Layouts 1.15
  4. import Qt.labs.folderlistmodel 2.15
  5. import "../components"
  6. Page {
  7. id: page
  8. property var backend: null
  9. property var stackView: null
  10. property string patternName: ""
  11. property string patternPreview: ""
  12. // Get current pattern info from backend
  13. property string currentPattern: backend ? backend.currentFile : ""
  14. property string currentPreviewPath: ""
  15. property var allPossiblePaths: []
  16. property int currentPathIndex: 0
  17. property string activeImageSource: "" // Separate property to avoid binding loop
  18. property string repoRoot: "" // Will hold the absolute path to repository root
  19. property bool imageRetryInProgress: false // Prevent multiple retry attempts
  20. // Debug backend connection
  21. onBackendChanged: {
  22. console.log("ExecutionPage: backend changed to", backend)
  23. if (backend) {
  24. console.log("ExecutionPage: backend.serialConnected =", backend.serialConnected)
  25. console.log("ExecutionPage: backend.isConnected =", backend.isConnected)
  26. }
  27. }
  28. Component.onCompleted: {
  29. console.log("ExecutionPage: Component completed, backend =", backend)
  30. if (backend) {
  31. console.log("ExecutionPage: initial serialConnected =", backend.serialConnected)
  32. }
  33. // Find repository root directory
  34. findRepoRoot()
  35. }
  36. // Direct connection to backend signals
  37. Connections {
  38. target: backend
  39. function onSerialConnectionChanged(connected) {
  40. console.log("ExecutionPage: received serialConnectionChanged signal:", connected)
  41. }
  42. function onConnectionChanged() {
  43. console.log("ExecutionPage: received connectionChanged signal")
  44. if (backend) {
  45. console.log("ExecutionPage: after connectionChanged, serialConnected =", backend.serialConnected)
  46. }
  47. }
  48. function onApiCallStarted(apiName) {
  49. console.log("✨ API call started:", apiName)
  50. }
  51. function onApiCallCompleted(apiName, success) {
  52. console.log("✨ API call completed:", apiName, "success:", success)
  53. if (success) {
  54. // Show success feedback
  55. successAnimation.show(apiName)
  56. }
  57. }
  58. }
  59. onCurrentPatternChanged: {
  60. if (currentPattern) {
  61. // Generate preview path from current pattern
  62. updatePreviewPath()
  63. }
  64. }
  65. function updatePreviewPath() {
  66. if (!currentPattern) {
  67. console.log("🔍 No current pattern, clearing preview path")
  68. currentPreviewPath = ""
  69. return
  70. }
  71. console.log("🔍 Updating preview for pattern:", currentPattern)
  72. // Extract just the filename from the path
  73. var fileName = currentPattern.split('/').pop() // Get last part of path
  74. var baseName = fileName.replace(".thr", "")
  75. console.log("🔍 File name:", fileName, "Base name:", baseName)
  76. // Use absolute paths based on discovered repository root
  77. var possibleBasePaths = []
  78. if (repoRoot) {
  79. // Use the discovered repository root
  80. possibleBasePaths = [
  81. "file://" + repoRoot + "/patterns/cached_images/"
  82. ]
  83. console.log("🎯 Using repository root for paths:", repoRoot)
  84. } else {
  85. console.log("⚠️ Repository root not found, using fallback relative paths")
  86. // Fallback to relative paths if repo root discovery failed
  87. possibleBasePaths = [
  88. "../../../patterns/cached_images/", // Three levels up from QML file location
  89. "../../patterns/cached_images/", // Two levels up (backup)
  90. "../../../../patterns/cached_images/" // Four levels up (backup)
  91. ]
  92. }
  93. var possiblePaths = []
  94. // Build paths using all possible base paths
  95. // Prioritize PNG format since WebP is not supported on this system
  96. for (var i = 0; i < possibleBasePaths.length; i++) {
  97. var basePath = possibleBasePaths[i]
  98. // First try with .thr suffix (e.g., pattern.thr.png) - PNG first since WebP failed
  99. possiblePaths.push(basePath + fileName + ".png")
  100. possiblePaths.push(basePath + fileName + ".jpg")
  101. possiblePaths.push(basePath + fileName + ".jpeg")
  102. // Then try without .thr suffix (e.g., pattern.png)
  103. possiblePaths.push(basePath + baseName + ".png")
  104. possiblePaths.push(basePath + baseName + ".jpg")
  105. possiblePaths.push(basePath + baseName + ".jpeg")
  106. }
  107. console.log("🔍 Possible preview paths:", JSON.stringify(possiblePaths))
  108. // Store all possible paths for fallback mechanism
  109. allPossiblePaths = possiblePaths
  110. currentPathIndex = 0
  111. // Set the active image source to avoid binding loops
  112. if (possiblePaths.length > 0) {
  113. currentPreviewPath = possiblePaths[0]
  114. activeImageSource = possiblePaths[0]
  115. console.log("🎯 Setting preview path to:", currentPreviewPath)
  116. console.log("🎯 Setting active image source to:", activeImageSource)
  117. } else {
  118. console.log("❌ No possible paths found")
  119. currentPreviewPath = ""
  120. activeImageSource = ""
  121. }
  122. }
  123. function tryNextPreviewPath() {
  124. if (allPossiblePaths.length === 0) {
  125. console.log("❌ No more paths to try")
  126. return false
  127. }
  128. currentPathIndex++
  129. if (currentPathIndex >= allPossiblePaths.length) {
  130. console.log("❌ All paths exhausted")
  131. return false
  132. }
  133. currentPreviewPath = allPossiblePaths[currentPathIndex]
  134. activeImageSource = allPossiblePaths[currentPathIndex]
  135. console.log("🔄 Trying next preview path:", currentPreviewPath)
  136. console.log("🔄 Setting active image source to:", activeImageSource)
  137. return true
  138. }
  139. function findRepoRoot() {
  140. // Start from the current QML file location and work our way up
  141. var currentPath = Qt.resolvedUrl(".").toString()
  142. console.log("🔍 Starting search from QML file location:", currentPath)
  143. // Remove file:// prefix and get directory parts
  144. if (currentPath.startsWith("file://")) {
  145. currentPath = currentPath.substring(7)
  146. }
  147. var pathParts = currentPath.split("/")
  148. console.log("🔍 Path parts:", JSON.stringify(pathParts))
  149. // Look for the dune-weaver directory by going up the path
  150. for (var i = pathParts.length - 1; i >= 0; i--) {
  151. if (pathParts[i] === "dune-weaver" || pathParts[i] === "dune-weaver-touch") {
  152. // Found it! Build the repo root path
  153. var rootPath = "/" + pathParts.slice(1, i + (pathParts[i] === "dune-weaver" ? 1 : 0)).join("/")
  154. if (pathParts[i] === "dune-weaver-touch") {
  155. // We need to go up one more level to get to dune-weaver
  156. rootPath = "/" + pathParts.slice(1, i).join("/")
  157. }
  158. repoRoot = rootPath
  159. console.log("🎯 Found repository root:", repoRoot)
  160. return
  161. }
  162. }
  163. console.log("❌ Could not find repository root")
  164. }
  165. // Timer to handle image retry without causing binding loops
  166. Timer {
  167. id: imageRetryTimer
  168. interval: 100 // Small delay to break the binding cycle
  169. onTriggered: {
  170. if (tryNextPreviewPath()) {
  171. console.log("🔄 Retrying with new path after timer...")
  172. }
  173. imageRetryInProgress = false
  174. }
  175. }
  176. // Success animation overlay
  177. Rectangle {
  178. id: successAnimation
  179. anchors.centerIn: parent
  180. width: 120
  181. height: 120
  182. radius: 60
  183. color: "#22c55e"
  184. opacity: 0
  185. visible: opacity > 0
  186. property string currentAction: ""
  187. function show(actionName) {
  188. currentAction = actionName
  189. showAnimation.start()
  190. }
  191. Text {
  192. anchors.centerIn: parent
  193. text: "✓"
  194. font.pixelSize: 48
  195. color: "white"
  196. font.bold: true
  197. }
  198. Text {
  199. anchors.bottom: parent.bottom
  200. anchors.horizontalCenter: parent.horizontalCenter
  201. anchors.bottomMargin: -30
  202. text: successAnimation.currentAction + " successful"
  203. font.pixelSize: 14
  204. color: "#22c55e"
  205. font.bold: true
  206. }
  207. SequentialAnimation {
  208. id: showAnimation
  209. PropertyAnimation {
  210. target: successAnimation
  211. property: "opacity"
  212. from: 0
  213. to: 1
  214. duration: 200
  215. easing.type: Easing.OutQuart
  216. }
  217. PropertyAnimation {
  218. target: successAnimation
  219. property: "scale"
  220. from: 0.8
  221. to: 1.1
  222. duration: 300
  223. easing.type: Easing.OutBack
  224. }
  225. PropertyAnimation {
  226. target: successAnimation
  227. property: "scale"
  228. from: 1.1
  229. to: 1
  230. duration: 200
  231. easing.type: Easing.OutQuart
  232. }
  233. PauseAnimation {
  234. duration: 800
  235. }
  236. PropertyAnimation {
  237. target: successAnimation
  238. property: "opacity"
  239. from: 1
  240. to: 0
  241. duration: 300
  242. easing.type: Easing.InQuart
  243. }
  244. }
  245. }
  246. Rectangle {
  247. anchors.fill: parent
  248. color: "#f5f5f5"
  249. }
  250. ColumnLayout {
  251. anchors.fill: parent
  252. spacing: 0
  253. // Header (consistent with other pages)
  254. Rectangle {
  255. Layout.fillWidth: true
  256. Layout.preferredHeight: 50
  257. color: "white"
  258. // Bottom border
  259. Rectangle {
  260. anchors.bottom: parent.bottom
  261. width: parent.width
  262. height: 1
  263. color: "#e5e7eb"
  264. }
  265. RowLayout {
  266. anchors.fill: parent
  267. anchors.leftMargin: 15
  268. anchors.rightMargin: 10
  269. ConnectionStatus {
  270. backend: page.backend
  271. Layout.rightMargin: 8
  272. }
  273. Label {
  274. text: "Pattern Execution"
  275. font.pixelSize: 18
  276. font.bold: true
  277. color: "#333"
  278. }
  279. Item {
  280. Layout.fillWidth: true
  281. }
  282. }
  283. }
  284. // Content - Side by side layout
  285. Item {
  286. Layout.fillWidth: true
  287. Layout.fillHeight: true
  288. Row {
  289. anchors.fill: parent
  290. spacing: 0
  291. // Left side - Pattern Preview (60% of width)
  292. Rectangle {
  293. width: parent.width * 0.6
  294. height: parent.height
  295. color: "#ffffff"
  296. Image {
  297. anchors.fill: parent
  298. anchors.margins: 10
  299. source: "" // Disabled to prevent WebP decoding errors on touch display
  300. fillMode: Image.PreserveAspectFit
  301. onStatusChanged: {
  302. console.log("📷 Image status:", status, "for source:", source)
  303. if (status === Image.Error) {
  304. console.log("❌ Image failed to load:", source)
  305. // Use timer to avoid binding loop
  306. if (!imageRetryInProgress) {
  307. imageRetryInProgress = true
  308. imageRetryTimer.start()
  309. }
  310. } else if (status === Image.Ready) {
  311. console.log("✅ Image loaded successfully:", source)
  312. imageRetryInProgress = false // Reset on successful load
  313. } else if (status === Image.Loading) {
  314. console.log("🔄 Image loading:", source)
  315. }
  316. }
  317. onSourceChanged: {
  318. console.log("🔄 Image source changed to:", source)
  319. }
  320. Rectangle {
  321. anchors.fill: parent
  322. color: "#f0f0f0"
  323. visible: parent.status === Image.Error || parent.source == ""
  324. Column {
  325. anchors.centerIn: parent
  326. spacing: 10
  327. Text {
  328. text: "⚙️"
  329. font.pixelSize: 48
  330. color: "#ccc"
  331. anchors.horizontalCenter: parent.horizontalCenter
  332. }
  333. Text {
  334. text: "Pattern Preview"
  335. color: "#999"
  336. font.pixelSize: 14
  337. anchors.horizontalCenter: parent.horizontalCenter
  338. }
  339. }
  340. }
  341. }
  342. }
  343. // Divider
  344. Rectangle {
  345. width: 1
  346. height: parent.height
  347. color: "#e5e7eb"
  348. }
  349. // Right side - Controls (40% of width)
  350. Rectangle {
  351. width: parent.width * 0.4 - 1
  352. height: parent.height
  353. color: "white"
  354. Column {
  355. anchors.left: parent.left
  356. anchors.right: parent.right
  357. anchors.top: parent.top
  358. anchors.margins: 10
  359. spacing: 15
  360. // Pattern Name
  361. Rectangle {
  362. width: parent.width
  363. height: 50
  364. radius: 8
  365. color: "#f8f9fa"
  366. border.color: "#e5e7eb"
  367. border.width: 1
  368. Column {
  369. anchors.centerIn: parent
  370. spacing: 4
  371. Label {
  372. text: "Current Pattern"
  373. font.pixelSize: 10
  374. color: "#666"
  375. anchors.horizontalCenter: parent.horizontalCenter
  376. }
  377. Label {
  378. text: {
  379. // Use WebSocket current pattern first, then fallback to passed parameter
  380. var displayName = ""
  381. if (backend && backend.currentFile) displayName = backend.currentFile
  382. else if (patternName) displayName = patternName
  383. else return "No pattern running"
  384. // Clean up the name for display
  385. var parts = displayName.split('/')
  386. displayName = parts[parts.length - 1]
  387. displayName = displayName.replace('.thr', '')
  388. return displayName
  389. }
  390. font.pixelSize: 12
  391. font.bold: true
  392. color: "#333"
  393. anchors.horizontalCenter: parent.horizontalCenter
  394. width: parent.parent.width - 20
  395. elide: Text.ElideMiddle
  396. horizontalAlignment: Text.AlignHCenter
  397. }
  398. }
  399. }
  400. // Progress
  401. Rectangle {
  402. width: parent.width
  403. height: 70
  404. radius: 8
  405. color: "#f8f9fa"
  406. border.color: "#e5e7eb"
  407. border.width: 1
  408. Column {
  409. anchors.fill: parent
  410. anchors.margins: 10
  411. spacing: 8
  412. Label {
  413. text: "Progress"
  414. font.pixelSize: 12
  415. font.bold: true
  416. color: "#333"
  417. }
  418. ProgressBar {
  419. width: parent.width
  420. height: 8
  421. value: backend ? backend.progress / 100 : 0
  422. }
  423. Label {
  424. text: backend ? Math.round(backend.progress) + "%" : "0%"
  425. anchors.horizontalCenter: parent.horizontalCenter
  426. font.pixelSize: 14
  427. font.bold: true
  428. color: "#333"
  429. }
  430. }
  431. }
  432. // Control Buttons
  433. Rectangle {
  434. width: parent.width
  435. height: 180
  436. radius: 8
  437. color: "#f8f9fa"
  438. border.color: "#e5e7eb"
  439. border.width: 1
  440. Column {
  441. anchors.fill: parent
  442. anchors.margins: 10
  443. spacing: 10
  444. Label {
  445. text: "Controls"
  446. font.pixelSize: 12
  447. font.bold: true
  448. color: "#333"
  449. }
  450. // Pause/Resume button
  451. Rectangle {
  452. width: parent.width
  453. height: 35
  454. radius: 6
  455. color: {
  456. if (backend && (backend.isPausing || backend.isResuming)) return "#1e40af"
  457. if (pauseMouseArea.pressed) return "#1e40af"
  458. if (backend && backend.currentFile !== "") return "#2563eb"
  459. return "#9ca3af"
  460. }
  461. Row {
  462. anchors.centerIn: parent
  463. spacing: 5
  464. // Loading spinner
  465. Rectangle {
  466. width: 14
  467. height: 14
  468. radius: 7
  469. color: "transparent"
  470. border.color: "white"
  471. border.width: 2
  472. visible: backend && (backend.isPausing || backend.isResuming)
  473. RotationAnimation {
  474. target: parent
  475. from: 0
  476. to: 360
  477. duration: 1000
  478. running: parent.visible
  479. loops: Animation.Infinite
  480. }
  481. }
  482. Text {
  483. text: {
  484. if (backend && backend.isPausing) return "Pausing..."
  485. if (backend && backend.isResuming) return "Resuming..."
  486. return (backend && backend.isRunning) ? "⏸ Pause" : "▶ Resume"
  487. }
  488. color: "white"
  489. font.pixelSize: 12
  490. font.bold: true
  491. }
  492. }
  493. MouseArea {
  494. id: pauseMouseArea
  495. anchors.fill: parent
  496. enabled: backend && backend.currentFile !== "" && !backend.isPausing && !backend.isResuming
  497. onClicked: {
  498. if (backend) {
  499. if (backend.isRunning) {
  500. backend.pauseExecution()
  501. } else {
  502. backend.resumeExecution()
  503. }
  504. }
  505. }
  506. }
  507. }
  508. // Stop button
  509. Rectangle {
  510. width: parent.width
  511. height: 35
  512. radius: 6
  513. color: {
  514. if (backend && backend.isStopping) return "#b91c1c"
  515. if (stopMouseArea.pressed) return "#b91c1c"
  516. if (backend && backend.currentFile !== "") return "#dc2626"
  517. return "#9ca3af"
  518. }
  519. Row {
  520. anchors.centerIn: parent
  521. spacing: 5
  522. // Loading spinner
  523. Rectangle {
  524. width: 14
  525. height: 14
  526. radius: 7
  527. color: "transparent"
  528. border.color: "white"
  529. border.width: 2
  530. visible: backend && backend.isStopping
  531. RotationAnimation {
  532. target: parent
  533. from: 0
  534. to: 360
  535. duration: 1000
  536. running: parent.visible
  537. loops: Animation.Infinite
  538. }
  539. }
  540. Text {
  541. text: (backend && backend.isStopping) ? "Stopping..." : "⏹ Stop"
  542. color: "white"
  543. font.pixelSize: 12
  544. font.bold: true
  545. }
  546. }
  547. MouseArea {
  548. id: stopMouseArea
  549. anchors.fill: parent
  550. enabled: backend && backend.currentFile !== "" && !backend.isStopping
  551. onClicked: {
  552. if (backend) {
  553. backend.stopExecution()
  554. }
  555. }
  556. }
  557. }
  558. // Skip button
  559. Rectangle {
  560. width: parent.width
  561. height: 35
  562. radius: 6
  563. color: {
  564. if (backend && backend.isSkipping) return "#525252"
  565. if (skipMouseArea.pressed) return "#525252"
  566. if (backend && backend.currentFile !== "") return "#6b7280"
  567. return "#9ca3af"
  568. }
  569. Row {
  570. anchors.centerIn: parent
  571. spacing: 5
  572. // Loading spinner
  573. Rectangle {
  574. width: 14
  575. height: 14
  576. radius: 7
  577. color: "transparent"
  578. border.color: "white"
  579. border.width: 2
  580. visible: backend && backend.isSkipping
  581. RotationAnimation {
  582. target: parent
  583. from: 0
  584. to: 360
  585. duration: 1000
  586. running: parent.visible
  587. loops: Animation.Infinite
  588. }
  589. }
  590. Text {
  591. text: (backend && backend.isSkipping) ? "Skipping..." : "⏭ Skip"
  592. color: "white"
  593. font.pixelSize: 12
  594. font.bold: true
  595. }
  596. }
  597. MouseArea {
  598. id: skipMouseArea
  599. anchors.fill: parent
  600. enabled: backend && backend.currentFile !== "" && !backend.isSkipping
  601. onClicked: {
  602. if (backend) {
  603. backend.skipPattern()
  604. }
  605. }
  606. }
  607. }
  608. }
  609. }
  610. }
  611. }
  612. }
  613. }
  614. }
  615. }