Переглянути джерело

fix preview for exec page

tuanchris 3 місяців тому
батько
коміт
ec2f1bf2bf

+ 17 - 6
dune-weaver-touch/backend.py

@@ -280,16 +280,27 @@ class Backend(QObject):
             data = json.loads(message)
             if data.get("type") == "status_update":
                 status = data.get("data", {})
-                self._current_file = status.get("current_file", "")
+                new_file = status.get("current_file", "")
+
+                # Detect pattern change and emit executionStarted signal
+                if new_file and new_file != self._current_file:
+                    print(f"🎯 Pattern changed from '{self._current_file}' to '{new_file}'")
+                    # Find preview for the new pattern
+                    preview_path = self._find_pattern_preview(new_file)
+                    print(f"🖼️ Preview path for new pattern: {preview_path}")
+                    # Emit signal so UI can update
+                    self.executionStarted.emit(new_file, preview_path)
+
+                self._current_file = new_file
                 self._is_running = status.get("is_running", False)
-                
+
                 # Handle serial connection status from WebSocket
                 ws_connection_status = status.get("connection_status", False)
                 if ws_connection_status != self._serial_connected:
                     print(f"🔌 WebSocket serial connection status changed: {ws_connection_status}")
                     self._serial_connected = ws_connection_status
                     self.serialConnectionChanged.emit(ws_connection_status)
-                    
+
                     # If we're connected, we need to get the current port
                     if ws_connection_status:
                         # We'll need to fetch the current port via HTTP since WS doesn't include port info
@@ -297,17 +308,17 @@ class Backend(QObject):
                     else:
                         self._current_port = ""
                         self.currentPortChanged.emit("")
-                
+
                 # Handle speed updates from WebSocket
                 ws_speed = status.get("speed", None)
                 if ws_speed and ws_speed != self._current_speed:
                     print(f"⚡ WebSocket speed changed: {ws_speed}")
                     self._current_speed = ws_speed
                     self.speedChanged.emit(ws_speed)
-                
+
                 if status.get("progress"):
                     self._progress = status["progress"].get("percentage", 0)
-                
+
                 self.statusChanged.emit()
                 self.progressChanged.emit()
         except json.JSONDecodeError:

+ 8 - 1
dune-weaver-touch/qml/main.qml

@@ -12,11 +12,13 @@ ApplicationWindow {
     width: 800
     height: 480
     title: "Dune Weaver Touch"
-    
+
     property int currentPageIndex: 0
     property alias stackView: stackView
     property alias backend: backend
     property bool shouldNavigateToExecution: false
+    property string currentPatternName: ""
+    property string currentPatternPreview: ""
     
     onCurrentPageIndexChanged: {
         console.log("📱 currentPageIndex changed to:", currentPageIndex)
@@ -46,6 +48,9 @@ ApplicationWindow {
         onExecutionStarted: function(patternName, patternPreview) {
             console.log("🎯 QML: ExecutionStarted signal received! patternName='" + patternName + "', preview='" + patternPreview + "'")
             console.log("🎯 Setting shouldNavigateToExecution = true")
+            // Store pattern info for ExecutionPage
+            window.currentPatternName = patternName
+            window.currentPatternPreview = patternPreview
             // Navigate to Execution tab (index 3) instead of pushing page
             shouldNavigateToExecution = true
             console.log("🎯 shouldNavigateToExecution set to:", shouldNavigateToExecution)
@@ -169,6 +174,8 @@ ApplicationWindow {
                         onLoaded: {
                             item.backend = backend
                             item.stackView = stackView
+                            item.patternName = Qt.binding(function() { return window.currentPatternName })
+                            item.patternPreview = Qt.binding(function() { return window.currentPatternPreview })
                         }
                     }
                 }

+ 20 - 168
dune-weaver-touch/qml/pages/ExecutionPage.qml

@@ -9,16 +9,7 @@ Page {
     property var backend: null
     property var stackView: null
     property string patternName: ""
-    property string patternPreview: ""
-    
-    // Get current pattern info from backend
-    property string currentPattern: backend ? backend.currentFile : ""
-    property string currentPreviewPath: ""
-    property var allPossiblePaths: []
-    property int currentPathIndex: 0
-    property string activeImageSource: ""  // Separate property to avoid binding loop
-    property string repoRoot: ""  // Will hold the absolute path to repository root
-    property bool imageRetryInProgress: false  // Prevent multiple retry attempts
+    property string patternPreview: ""  // Backend provides this via executionStarted signal
     
     // Debug backend connection
     onBackendChanged: {
@@ -34,163 +25,33 @@ Page {
         if (backend) {
             console.log("ExecutionPage: initial serialConnected =", backend.serialConnected)
         }
-        
-        // Find repository root directory
-        findRepoRoot()
     }
     
     // Direct connection to backend signals
     Connections {
         target: backend
-        
+
         function onSerialConnectionChanged(connected) {
             console.log("ExecutionPage: received serialConnectionChanged signal:", connected)
         }
-        
+
         function onConnectionChanged() {
             console.log("ExecutionPage: received connectionChanged signal")
             if (backend) {
                 console.log("ExecutionPage: after connectionChanged, serialConnected =", backend.serialConnected)
             }
         }
-    }
-    
-    onCurrentPatternChanged: {
-        if (currentPattern) {
-            // Generate preview path from current pattern
-            updatePreviewPath()
-        }
-    }
-    
-    function updatePreviewPath() {
-        if (!currentPattern) {
-            console.log("🔍 No current pattern, clearing preview path")
-            currentPreviewPath = ""
-            return
-        }
-        
-        console.log("🔍 Updating preview for pattern:", currentPattern)
-        
-        // Extract just the filename from the path
-        var fileName = currentPattern.split('/').pop()  // Get last part of path
-        var baseName = fileName.replace(".thr", "")
-        console.log("🔍 File name:", fileName, "Base name:", baseName)
-        
-        // Use absolute paths based on discovered repository root
-        var possibleBasePaths = []
-        
-        if (repoRoot) {
-            // Use the discovered repository root
-            possibleBasePaths = [
-                "file://" + repoRoot + "/patterns/cached_images/"
-            ]
-            console.log("🎯 Using repository root for paths:", repoRoot)
-        } else {
-            console.log("⚠️ Repository root not found, using fallback relative paths")
-            // Fallback to relative paths if repo root discovery failed
-            possibleBasePaths = [
-                "../../../patterns/cached_images/",  // Three levels up from QML file location
-                "../../patterns/cached_images/",     // Two levels up (backup)
-                "../../../../patterns/cached_images/"  // Four levels up (backup)
-            ]
-        }
-        
-        var possiblePaths = []
-        
-        // Build paths using all possible base paths
-        // Prioritize PNG format since WebP is not supported on this system
-        for (var i = 0; i < possibleBasePaths.length; i++) {
-            var basePath = possibleBasePaths[i]
-            // First try with .thr suffix (e.g., pattern.thr.png) - PNG first since WebP failed
-            possiblePaths.push(basePath + fileName + ".png")
-            possiblePaths.push(basePath + fileName + ".jpg")
-            possiblePaths.push(basePath + fileName + ".jpeg")
-            // Then try without .thr suffix (e.g., pattern.png) 
-            possiblePaths.push(basePath + baseName + ".png")
-            possiblePaths.push(basePath + baseName + ".jpg")
-            possiblePaths.push(basePath + baseName + ".jpeg")
-        }
-        
-        console.log("🔍 Possible preview paths:", JSON.stringify(possiblePaths))
-        
-        // Store all possible paths for fallback mechanism
-        allPossiblePaths = possiblePaths
-        currentPathIndex = 0
-        
-        // Set the active image source to avoid binding loops
-        if (possiblePaths.length > 0) {
-            currentPreviewPath = possiblePaths[0]
-            activeImageSource = possiblePaths[0]
-            console.log("🎯 Setting preview path to:", currentPreviewPath)
-            console.log("🎯 Setting active image source to:", activeImageSource)
-        } else {
-            console.log("❌ No possible paths found")
-            currentPreviewPath = ""
-            activeImageSource = ""
-        }
-    }
-    
-    function tryNextPreviewPath() {
-        if (allPossiblePaths.length === 0) {
-            console.log("❌ No more paths to try")
-            return false
-        }
-        
-        currentPathIndex++
-        if (currentPathIndex >= allPossiblePaths.length) {
-            console.log("❌ All paths exhausted")
-            return false
-        }
-        
-        currentPreviewPath = allPossiblePaths[currentPathIndex]
-        activeImageSource = allPossiblePaths[currentPathIndex]
-        console.log("🔄 Trying next preview path:", currentPreviewPath)
-        console.log("🔄 Setting active image source to:", activeImageSource)
-        return true
-    }
-    
-    function findRepoRoot() {
-        // Start from the current QML file location and work our way up
-        var currentPath = Qt.resolvedUrl(".").toString()
-        console.log("🔍 Starting search from QML file location:", currentPath)
-        
-        // Remove file:// prefix and get directory parts
-        if (currentPath.startsWith("file://")) {
-            currentPath = currentPath.substring(7)
-        }
-        
-        var pathParts = currentPath.split("/")
-        console.log("🔍 Path parts:", JSON.stringify(pathParts))
-        
-        // Look for the dune-weaver directory by going up the path
-        for (var i = pathParts.length - 1; i >= 0; i--) {
-            if (pathParts[i] === "dune-weaver" || pathParts[i] === "dune-weaver-touch") {
-                // Found it! Build the repo root path
-                var rootPath = "/" + pathParts.slice(1, i + (pathParts[i] === "dune-weaver" ? 1 : 0)).join("/")
-                if (pathParts[i] === "dune-weaver-touch") {
-                    // We need to go up one more level to get to dune-weaver
-                    rootPath = "/" + pathParts.slice(1, i).join("/")
-                }
-                repoRoot = rootPath
-                console.log("🎯 Found repository root:", repoRoot)
-                return
-            }
+
+        function onExecutionStarted(fileName, preview) {
+            console.log("🎯 ExecutionPage: executionStarted signal received!")
+            console.log("🎯 Pattern:", fileName)
+            console.log("🎯 Preview path:", preview)
+            // Update preview directly from backend signal
+            patternName = fileName
+            patternPreview = preview
         }
-        
-        console.log("❌ Could not find repository root")
     }
     
-    // Timer to handle image retry without causing binding loops
-    Timer {
-        id: imageRetryTimer
-        interval: 100  // Small delay to break the binding cycle
-        onTriggered: {
-            if (tryNextPreviewPath()) {
-                console.log("🔄 Retrying with new path after timer...")
-            }
-            imageRetryInProgress = false
-        }
-    }
     
     Rectangle {
         anchors.fill: parent
@@ -258,40 +119,31 @@ Page {
                         anchors.margins: 10
                         source: {
                             var finalSource = ""
-                            
-                            // Try different sources in priority order
+
+                            // Trust the backend's preview path - it already has recursive search
                             if (patternPreview) {
-                                finalSource = "file:///" + patternPreview
-                                console.log("🖼️ Using patternPreview:", finalSource)
-                            } else if (activeImageSource) {
-                                // Use the activeImageSource to avoid binding loops
-                                finalSource = activeImageSource
-                                console.log("🖼️ Using activeImageSource:", finalSource)
+                                // Backend returns absolute path, just add file:// prefix
+                                finalSource = "file://" + patternPreview
+                                console.log("🖼️ Using backend patternPreview:", finalSource)
                             } else {
-                                console.log("🖼️ No preview source available")
+                                console.log("🖼️ No preview from backend")
                             }
-                            
+
                             return finalSource
                         }
                         fillMode: Image.PreserveAspectFit
-                        
+
                         onStatusChanged: {
                             console.log("📷 Image status:", status, "for source:", source)
                             if (status === Image.Error) {
                                 console.log("❌ Image failed to load:", source)
-                                // Use timer to avoid binding loop
-                                if (!imageRetryInProgress) {
-                                    imageRetryInProgress = true
-                                    imageRetryTimer.start()
-                                }
                             } else if (status === Image.Ready) {
                                 console.log("✅ Image loaded successfully:", source)
-                                imageRetryInProgress = false  // Reset on successful load
                             } else if (status === Image.Loading) {
                                 console.log("🔄 Image loading:", source)
                             }
                         }
-                        
+
                         onSourceChanged: {
                             console.log("🔄 Image source changed to:", source)
                         }