Browse Source

Group printers by section (Connected Printers/Other Printers)

Cleanup redundant code.

CURA-9514
joeydelarago 2 years ago
parent
commit
93e2bef303

+ 26 - 45
cura/Machines/Models/MachineListModel.py

@@ -2,14 +2,12 @@
 # Cura is released under the terms of the LGPLv3 or higher.
 # Cura is released under the terms of the LGPLv3 or higher.
 
 
 from PyQt6.QtCore import Qt, QTimer
 from PyQt6.QtCore import Qt, QTimer
-from typing import Optional, Dict
 
 
 from UM.Qt.ListModel import ListModel
 from UM.Qt.ListModel import ListModel
 from UM.Settings.ContainerStack import ContainerStack
 from UM.Settings.ContainerStack import ContainerStack
 from UM.i18n import i18nCatalog
 from UM.i18n import i18nCatalog
 from UM.Util import parseBool
 from UM.Util import parseBool
 
 
-from cura.PrinterOutput.PrinterOutputDevice import ConnectionType
 from cura.Settings.AbstractMachine import AbstractMachine
 from cura.Settings.AbstractMachine import AbstractMachine
 from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
 from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
 from cura.Settings.GlobalStack import GlobalStack
 from cura.Settings.GlobalStack import GlobalStack
@@ -19,13 +17,10 @@ class MachineListModel(ListModel):
     NameRole = Qt.ItemDataRole.UserRole + 1
     NameRole = Qt.ItemDataRole.UserRole + 1
     IdRole = Qt.ItemDataRole.UserRole + 2
     IdRole = Qt.ItemDataRole.UserRole + 2
     HasRemoteConnectionRole = Qt.ItemDataRole.UserRole + 3
     HasRemoteConnectionRole = Qt.ItemDataRole.UserRole + 3
-    ConnectionTypeRole = Qt.ItemDataRole.UserRole + 4
-    MetaDataRole = Qt.ItemDataRole.UserRole + 5
-    DiscoverySourceRole = Qt.ItemDataRole.UserRole + 6
-    RemovalWarningRole = Qt.ItemDataRole.UserRole + 7
-    IsOnlineRole = Qt.ItemDataRole.UserRole + 8
-    MachineTypeRole = Qt.ItemDataRole.UserRole + 9
-    MachineCountRole = Qt.ItemDataRole.UserRole + 10
+    MetaDataRole = Qt.ItemDataRole.UserRole + 4
+    IsOnlineRole = Qt.ItemDataRole.UserRole + 5
+    MachineTypeRole = Qt.ItemDataRole.UserRole + 6
+    MachineCountRole = Qt.ItemDataRole.UserRole + 7
 
 
     def __init__(self, parent=None) -> None:
     def __init__(self, parent=None) -> None:
         super().__init__(parent)
         super().__init__(parent)
@@ -36,7 +31,6 @@ class MachineListModel(ListModel):
         self.addRoleName(self.IdRole, "id")
         self.addRoleName(self.IdRole, "id")
         self.addRoleName(self.HasRemoteConnectionRole, "hasRemoteConnection")
         self.addRoleName(self.HasRemoteConnectionRole, "hasRemoteConnection")
         self.addRoleName(self.MetaDataRole, "metadata")
         self.addRoleName(self.MetaDataRole, "metadata")
-        self.addRoleName(self.DiscoverySourceRole, "discoverySource")
         self.addRoleName(self.IsOnlineRole, "isOnline")
         self.addRoleName(self.IsOnlineRole, "isOnline")
         self.addRoleName(self.MachineTypeRole, "machineType")
         self.addRoleName(self.MachineTypeRole, "machineType")
         self.addRoleName(self.MachineCountRole, "machineCount")
         self.addRoleName(self.MachineCountRole, "machineCount")
@@ -63,50 +57,37 @@ class MachineListModel(ListModel):
         self._change_timer.start()
         self._change_timer.start()
 
 
     def _update(self) -> None:
     def _update(self) -> None:
-        items = []
+        self.setItems([])  # Clear items
 
 
-        abstract_machine_stacks = CuraContainerRegistry.getInstance().findContainerStacks(type="abstract_machine")
-
-        abstract_machine_stacks.sort(key=lambda machine: machine.getName(), reverse=True)
+        abstract_machine_stacks = CuraContainerRegistry.getInstance().findContainerStacks(type = "abstract_machine")
+        abstract_machine_stacks.sort(key = lambda machine: machine.getName(), reverse = True)
 
 
         for abstract_machine in abstract_machine_stacks:
         for abstract_machine in abstract_machine_stacks:
-            machine_stacks = AbstractMachine.getMachines(abstract_machine)
-
+            online_machine_stacks = AbstractMachine.getMachines(abstract_machine, online_only = True)
 
 
             # Create item for abstract printer
             # Create item for abstract printer
-            items.append(self.createItem(abstract_machine, len(machine_stacks)))
+            self.addItem(abstract_machine, len(online_machine_stacks))
 
 
             # Create list of printers that are children of the abstract printer
             # Create list of printers that are children of the abstract printer
-            for stack in machine_stacks:
-                item = self.createItem(stack)
-                if item:
-                    items.append(item)
+            for stack in online_machine_stacks:
+                self.addItem(stack)
 
 
-        self.setItems(items)
+        offline_machine_stacks = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine", is_online = "False")
+        for stack in offline_machine_stacks:
+            self.addItem(stack)
 
 
-    def createItem(self, container_stack: ContainerStack, machine_count: int = 0) -> Optional[Dict]:
+    def addItem(self, container_stack: ContainerStack, machine_count: int = 0) -> None:
         if parseBool(container_stack.getMetaDataEntry("hidden", False)):
         if parseBool(container_stack.getMetaDataEntry("hidden", False)):
             return
             return
 
 
-        has_remote_connection = False
-
-        for connection_type in container_stack.configuredConnectionTypes:
-            has_remote_connection |= connection_type in [ConnectionType.NetworkConnection.value,
-                                                         ConnectionType.CloudConnection.value]
-
-        device_name = container_stack.getMetaDataEntry("group_name", container_stack.getName())
-        default_removal_warning = self._catalog.i18nc(
-            "@label {0} is the name of a printer that's about to be deleted.",
-            "Are you sure you wish to remove {0}? This cannot be undone!", device_name
-        )
-
-        return {"name": device_name,
-                "id": container_stack.getId(),
-                "hasRemoteConnection": has_remote_connection,
-                "metadata": container_stack.getMetaData().copy(),
-                "section": self._catalog.i18nc("@label", "Connected printers"),
-                "removalWarning": container_stack.getMetaDataEntry("removal_warning", default_removal_warning),
-                "isOnline": container_stack.getMetaDataEntry("is_online", False),
-                "machineType": container_stack.getMetaDataEntry("type"),
-                "machineCount": machine_count,
-                }
+        isOnline = parseBool(container_stack.getMetaDataEntry("is_online", False))
+        if container_stack.getMetaDataEntry("type") == "abstract_machine":
+            isOnline = True
+
+        self.appendItem({"name": container_stack.getName(),
+                         "id": container_stack.getId(),
+                         "metadata": container_stack.getMetaData().copy(),
+                         "isOnline": isOnline,
+                         "machineType": container_stack.getMetaDataEntry("type"),
+                         "machineCount": machine_count,
+                         })

+ 8 - 2
cura/Settings/AbstractMachine.py

@@ -1,6 +1,7 @@
 from typing import List
 from typing import List
 
 
 from UM.Settings.ContainerStack import ContainerStack
 from UM.Settings.ContainerStack import ContainerStack
+from UM.Util import parseBool
 from cura.PrinterOutput.PrinterOutputDevice import ConnectionType
 from cura.PrinterOutput.PrinterOutputDevice import ConnectionType
 from cura.Settings.GlobalStack import GlobalStack
 from cura.Settings.GlobalStack import GlobalStack
 from UM.MimeTypeDatabase import MimeType, MimeTypeDatabase
 from UM.MimeTypeDatabase import MimeType, MimeTypeDatabase
@@ -15,7 +16,7 @@ class AbstractMachine(GlobalStack):
         self.setMetaDataEntry("type", "abstract_machine")
         self.setMetaDataEntry("type", "abstract_machine")
 
 
     @classmethod
     @classmethod
-    def getMachines(cls, abstract_machine: ContainerStack) -> List[ContainerStack]:
+    def getMachines(cls, abstract_machine: ContainerStack, online_only = False) -> List[ContainerStack]:
         """ Fetches all container stacks that match definition_id with an abstract machine.
         """ Fetches all container stacks that match definition_id with an abstract machine.
 
 
         :param abstractMachine: The abstract machine stack.
         :param abstractMachine: The abstract machine stack.
@@ -30,7 +31,12 @@ class AbstractMachine(GlobalStack):
 
 
         printer_type = abstract_machine.definition.getId()
         printer_type = abstract_machine.definition.getId()
 
 
-        return [machine for machine in registry.findContainerStacks(type="machine") if machine.definition.id == printer_type and ConnectionType.CloudConnection in machine.configuredConnectionTypes]
+        machines = [machine for machine in registry.findContainerStacks(type="machine") if machine.definition.id == printer_type and ConnectionType.CloudConnection in machine.configuredConnectionTypes]
+
+        if online_only:
+            machines = [machine for machine in machines if parseBool(machine.getMetaDataEntry("is_online", False))]
+
+        return machines
 
 
 
 
 ## private:
 ## private:

+ 0 - 1
resources/qml/PrinterSelector/MachineListButton.qml

@@ -67,7 +67,6 @@ Button
                 right: parent.right
                 right: parent.right
                 top: buttonText.top
                 top: buttonText.top
                 bottom: buttonText.bottom
                 bottom: buttonText.bottom
-                verticalCenter: parent.verticalCenter
             }
             }
             visible: model.machineType == "abstract_machine"
             visible: model.machineType == "abstract_machine"
 
 

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

@@ -11,7 +11,7 @@ ListView
 {
 {
     id: listView
     id: listView
     model: Cura.MachineListModel {}
     model: Cura.MachineListModel {}
-    section.property: "section"
+    section.property: "isOnline"
     property real contentHeight: childrenRect.height
     property real contentHeight: childrenRect.height
 
 
     ScrollBar.vertical: UM.ScrollBar
     ScrollBar.vertical: UM.ScrollBar
@@ -21,7 +21,7 @@ ListView
 
 
     section.delegate: UM.Label
     section.delegate: UM.Label
     {
     {
-        text: section == "true" ? catalog.i18nc("@label", "Connected printers") : catalog.i18nc("@label", "Preset printers")
+        text: section == "true" ? catalog.i18nc("@label", "Connected printers") : catalog.i18nc("@label", "Other printers")
         width: parent.width - scrollBar.width
         width: parent.width - scrollBar.width
         height: UM.Theme.getSize("action_button").height
         height: UM.Theme.getSize("action_button").height
         leftPadding: UM.Theme.getSize("default_margin").width
         leftPadding: UM.Theme.getSize("default_margin").width