Browse Source

Don't show context menu button if it is empty

It will likely be empty for guest accounts on networks where the administrator is the only one with rights to access other people's prints.

Contributes to issue CURA-9220.
Ghostkeeper 2 years ago
parent
commit
e9f9730364

+ 92 - 79
plugins/UM3NetworkPrinting/resources/qml/MonitorContextMenu.qml

@@ -13,10 +13,94 @@ import Cura 1.6 as Cura
  */
 Item
 {
+    id: monitorContextMenu
     property alias target: popUp.target
 
     property var printJob: null
 
+    //Everything in the pop-up only gets evaluated when showing the pop-up.
+    //However we want to show the button for showing the pop-up only if there is anything visible inside it.
+    //So compute here the visibility of the menu items, so that we can use it for the visibility of the button.
+    property bool sendToTopVisible:
+    {
+        if (printJob && (printJob.state == "queued" || printJob.state == "error") && !isAssigned(printJob)) {
+            if (OutputDevice && OutputDevice.queuedPrintJobs[0] && OutputDevice.canWriteOthersPrintJobs) {
+                return OutputDevice.queuedPrintJobs[0].key != printJob.key;
+            }
+        }
+        return false;
+    }
+    property bool deleteVisible:
+    {
+        if(!printJob)
+        {
+            return false;
+        }
+        if(printJob.isMine)
+        {
+            if(!OutputDevice.canWriteOwnPrintJobs)
+            {
+                return false;
+            }
+        }
+        else
+        {
+            if(!OutputDevice.canWriteOthersPrintJobs)
+            {
+                return false;
+            }
+        }
+        var states = ["queued", "error", "sent_to_printer"];
+        return states.indexOf(printJob.state) !== -1;
+    }
+    property bool pauseVisible:
+    {
+        if(!printJob)
+        {
+            return false;
+        }
+        if(printJob.isMine)
+        {
+            if(!OutputDevice.canWriteOwnPrintJobs)
+            {
+                return false;
+            }
+        }
+        else
+        {
+            if(!OutputDevice.canWriteOthersPrintJobs)
+            {
+                return false;
+            }
+        }
+        var states = ["printing", "pausing", "paused", "resuming"];
+        return states.indexOf(printJob.state) !== -1;
+    }
+    property bool abortVisible:
+    {
+        if(!printJob)
+        {
+            return false;
+        }
+        if(printJob.isMine)
+        {
+            if(!OutputDevice.canWriteOwnPrintJobs)
+            {
+                return false;
+            }
+        }
+        else
+        {
+            if(!OutputDevice.canWriteOthersPrintJobs)
+            {
+                return false;
+            }
+        }
+        var states = ["pre_print", "printing", "pausing", "paused", "resuming"];
+        return states.indexOf(printJob.state) !== -1;
+    }
+    property bool hasItems: sendToTopVisible || deleteVisible || pauseVisible || abortVisible
+
     GenericPopUp
     {
         id: popUp
@@ -46,20 +130,15 @@ Item
 
                 spacing: Math.floor(UM.Theme.getSize("default_margin").height / 2)
 
-                PrintJobContextMenuItem {
-                    onClicked: {
+                PrintJobContextMenuItem
+                {
+                    onClicked:
+                    {
                         sendToTopConfirmationDialog.visible = true;
                         popUp.close();
                     }
                     text: catalog.i18nc("@label", "Move to top");
-                    visible: {
-                        if (printJob && (printJob.state == "queued" || printJob.state == "error") && !isAssigned(printJob)) {
-                            if (OutputDevice && OutputDevice.queuedPrintJobs[0] && OutputDevice.canWriteOthersPrintJobs) {
-                                return OutputDevice.queuedPrintJobs[0].key != printJob.key;
-                            }
-                        }
-                        return false;
-                    }
+                    visible: monitorContextMenu.sendToTopVisible
                 }
 
                 PrintJobContextMenuItem
@@ -70,29 +149,7 @@ Item
                         popUp.close();
                     }
                     text: catalog.i18nc("@label", "Delete");
-                    visible:
-                    {
-                        if(!printJob)
-                        {
-                            return false;
-                        }
-                        if(printJob.isMine)
-                        {
-                            if(!OutputDevice.canWriteOwnPrintJobs)
-                            {
-                                return false;
-                            }
-                        }
-                        else
-                        {
-                            if(!OutputDevice.canWriteOthersPrintJobs)
-                            {
-                                return false;
-                            }
-                        }
-                        var states = ["queued", "error", "sent_to_printer"];
-                        return states.indexOf(printJob.state) !== -1;
-                    }
+                    visible: monitorContextMenu.deleteVisible
                 }
 
                 PrintJobContextMenuItem
@@ -131,29 +188,7 @@ Item
                                 catalog.i18nc("@label", "Pause");
                         }
                     }
-                    visible:
-                    {
-                        if(!printJob)
-                        {
-                            return false;
-                        }
-                        if(printJob.isMine)
-                        {
-                            if(!OutputDevice.canWriteOwnPrintJobs)
-                            {
-                                return false;
-                            }
-                        }
-                        else
-                        {
-                            if(!OutputDevice.canWriteOthersPrintJobs)
-                            {
-                                return false;
-                            }
-                        }
-                        var states = ["printing", "pausing", "paused", "resuming"];
-                        return states.indexOf(printJob.state) !== -1;
-                    }
+                    visible: monitorContextMenu.pauseVisible
                 }
 
                 PrintJobContextMenuItem
@@ -165,29 +200,7 @@ Item
                         popUp.close();
                     }
                     text: printJob && printJob.state == "aborting" ? catalog.i18nc("@label", "Aborting...") : catalog.i18nc("@label", "Abort");
-                    visible:
-                    {
-                        if (!printJob)
-                        {
-                            return false;
-                        }
-                        if(printJob.isMine)
-                        {
-                            if(!OutputDevice.canWriteOwnPrintJobs)
-                            {
-                                return false;
-                            }
-                        }
-                        else
-                        {
-                            if(!OutputDevice.canWriteOthersPrintJobs)
-                            {
-                                return false;
-                            }
-                        }
-                        var states = ["pre_print", "printing", "pausing", "paused", "resuming"];
-                        return states.indexOf(printJob.state) !== -1;
-                    }
+                    visible: monitorContextMenu.abortVisible
                 }
             }
         }

+ 5 - 1
plugins/UM3NetworkPrinting/resources/qml/MonitorPrintJobCard.qml

@@ -206,7 +206,11 @@ Item
         onClicked: enabled ? contextMenu.switchPopupState() : {}
         visible:
         {
-            if (!printJob)
+            if(!printJob)
+            {
+                return false;
+            }
+            if(!contextMenu.hasItems)
             {
                 return false;
             }

+ 7 - 2
plugins/UM3NetworkPrinting/resources/qml/MonitorPrinterCard.qml

@@ -209,8 +209,13 @@ Item
             onClicked: enabled ? contextMenu.switchPopupState() : {}
             visible:
             {
-                if (!printer || !printer.activePrintJob) {
-                    return false
+                if(!printer || !printer.activePrintJob)
+                {
+                    return false;
+                }
+                if(!contextMenu.hasItems)
+                {
+                    return false;
                 }
                 var states = ["queued", "error", "sent_to_printer", "pre_print", "printing", "pausing", "paused", "resuming"]
                 return states.indexOf(printer.activePrintJob.state) !== -1