Browse Source

Make `MachineSelector` more generic by detaching logic from view

By detaching the logic from the view custom handlers can be added to selecting printers. This was needed as the MachineSelector in the `WorkspaceDialog` needed to do something different from the `PreparMenu`.

CURA-9424
c.lamboo 2 years ago
parent
commit
dd4e1c66b7

+ 14 - 36
plugins/3MFReader/WorkspaceDialog.qml

@@ -115,42 +115,20 @@ UM.Dialog
                             height: parent.height
                             height: parent.height
                             anchors.centerIn: parent
                             anchors.centerIn: parent
                             machineListModel: manager.updatableMachinesModel
                             machineListModel: manager.updatableMachinesModel
-//                            onCurrentIndexChanged:
-//                            {
-//                                if (model.getItem(currentIndex).id == "new"
-//                                    && model.getItem(currentIndex).type == "default_option")
-//                                {
-//                                    manager.setResolveStrategy("machine", "new")
-//                                }
-//                                else
-//                                {
-//                                    manager.setResolveStrategy("machine", "override")
-//                                    manager.setMachineToOverride(model.getItem(currentIndex).id)
-//                                }
-//                            }
-//
-//                            function machineVisibleChanged()
-//                            {
-//                                if (!visible) {return}
-//
-//                                currentIndex = 0
-//                                // If the project printer exists in Cura, set it as the default dropdown menu option.
-//                                // No need to check object 0, which is the "Create new" option
-//                                for (var i = 1; i < model.count; i++)
-//                                {
-//                                    if (model.getItem(i).name == manager.machineName)
-//                                    {
-//                                        currentIndex = i
-//                                        break
-//                                    }
-//                                }
-//                                // The project printer does not exist in Cura. If there is at least one printer of the same
-//                                // type, select the first one, else set the index to "Create new"
-//                                if (currentIndex == 0 && model.count > 1)
-//                                {
-//                                    currentIndex = 1
-//                                }
-//                            }
+                            machineName: manager.machineName
+
+                            isNetworkPrinter: false
+                            isConnectedCloudPrinter: false
+                            isCloudRegistered: false
+                            isGroup: false
+
+                            onSelectPrinter: function(machine)
+                            {
+                                toggleContent();
+                                manager.setResolveStrategy("machine", "override")
+                                manager.setMachineToOverride(machine.id)
+                                machineSelector.machineName = machine.name
+                            }
                         }
                         }
                     }
                     }
 
 

+ 7 - 0
plugins/PrepareStage/PrepareMenu.qml

@@ -56,6 +56,13 @@ Item
                 Layout.fillWidth: true
                 Layout.fillWidth: true
                 Layout.fillHeight: true
                 Layout.fillHeight: true
 
 
+                machineManager: Cura.MachineManager
+                onSelectPrinter: function(machine)
+                {
+                    toggleContent();
+                    Cura.MachineManager.setActiveMachine(machine.id);
+                }
+
                 machineListModel: Cura.MachineListModel {}
                 machineListModel: Cura.MachineListModel {}
 
 
                 buttons: [
                 buttons: [

+ 22 - 18
resources/qml/PrinterSelector/MachineSelector.qml

@@ -11,12 +11,25 @@ Cura.ExpandablePopup
 {
 {
     id: machineSelector
     id: machineSelector
 
 
-    property bool isNetworkPrinter: Cura.MachineManager.activeMachineHasNetworkConnection
-    property bool isConnectedCloudPrinter: Cura.MachineManager.activeMachineHasCloudConnection
-    property bool isCloudRegistered: Cura.MachineManager.activeMachineHasCloudRegistration
-    property bool isGroup: Cura.MachineManager.activeMachineIsGroup
+    property Cura.MachineManager machineManager
+    property bool isNetworkPrinter: machineManager.activeMachineHasNetworkConnection
+    property bool isConnectedCloudPrinter: machineManager.activeMachineHasCloudConnection
+    property bool isCloudRegistered: machineManager.activeMachineHasCloudRegistration
+    property bool isGroup: machineManager.activeMachineIsGroup
+    property string machineName: {
+        if (isNetworkPrinter && machineManager.activeMachineNetworkGroupName != "")
+        {
+            return machineManager.activeMachineNetworkGroupName
+        }
+        if (machineManager.activeMachine != null)
+        {
+            return machineManager.activeMachine.name
+        }
+        return ""
+    }
 
 
     property alias machineListModel: machineSelectorList.model
     property alias machineListModel: machineSelectorList.model
+    property alias onSelectPrinter: machineSelectorList.onSelectPrinter
 
 
     property list<Item> buttons
     property list<Item> buttons
 
 
@@ -46,7 +59,7 @@ Cura.ExpandablePopup
             {
             {
                 if (Cura.API.account.isLoggedIn)
                 if (Cura.API.account.isLoggedIn)
                 {
                 {
-                    if (Cura.MachineManager.activeMachineIsLinkedToCurrentAccount)
+                    if (machineManager.activeMachineIsLinkedToCurrentAccount)
                     {
                     {
                         return catalog.i18nc("@status", "The cloud printer is offline. Please check if the printer is turned on and connected to the internet.")
                         return catalog.i18nc("@status", "The cloud printer is offline. Please check if the printer is turned on and connected to the internet.")
                     }
                     }
@@ -59,7 +72,8 @@ Cura.ExpandablePopup
                 {
                 {
                     return catalog.i18nc("@status", "The cloud connection is currently unavailable. Please sign in to connect to the cloud printer.")
                     return catalog.i18nc("@status", "The cloud connection is currently unavailable. Please sign in to connect to the cloud printer.")
                 }
                 }
-            } else
+            }
+            else
             {
             {
                 return catalog.i18nc("@status", "The cloud connection is currently unavailable. Please check your internet connection.")
                 return catalog.i18nc("@status", "The cloud connection is currently unavailable. Please check your internet connection.")
             }
             }
@@ -81,18 +95,8 @@ Cura.ExpandablePopup
 
 
     headerItem: Cura.IconWithText
     headerItem: Cura.IconWithText
     {
     {
-        text:
-        {
-            if (isNetworkPrinter && Cura.MachineManager.activeMachineNetworkGroupName != "")
-            {
-                return Cura.MachineManager.activeMachineNetworkGroupName
-            }
-            if(Cura.MachineManager.activeMachine != null)
-            {
-                return Cura.MachineManager.activeMachine.name
-            }
-            return ""
-        }
+        text: machineName
+
         source:
         source:
         {
         {
             if (isGroup)
             if (isGroup)

+ 2 - 2
resources/qml/PrinterSelector/MachineSelectorList.qml

@@ -12,6 +12,7 @@ ListView
     id: listView
     id: listView
     section.property: "isOnline"
     section.property: "isOnline"
     property real contentHeight: childrenRect.height
     property real contentHeight: childrenRect.height
+    property var onSelectPrinter
 
 
     ScrollBar.vertical: UM.ScrollBar
     ScrollBar.vertical: UM.ScrollBar
     {
     {
@@ -42,8 +43,7 @@ ListView
                     listView.model.setShowCloudPrinters(true);
                     listView.model.setShowCloudPrinters(true);
                     break;
                     break;
                 case "MACHINE":
                 case "MACHINE":
-                    toggleContent()
-                    Cura.MachineManager.setActiveMachine(model.id)
+                    if (typeof onSelectPrinter === "function") onSelectPrinter(model);
                     break;
                     break;
                 default:
                 default:
             }
             }