chiark / gitweb /
Merge tag '14.12' into upstream
[cura.git] / Cura / gui / printWindow.py
index 947f6643961d6576b6ba365afb724671937fb21c..6356df6491486bdc8cb380da8fbf6dd4f347dd80 100644 (file)
@@ -17,15 +17,34 @@ if sys.platform.startswith('win'):
                """
                ES_CONTINUOUS = 0x80000000
                ES_SYSTEM_REQUIRED = 0x00000001
+               ES_AWAYMODE_REQUIRED = 0x00000040
                #SetThreadExecutionState returns 0 when failed, which is ignored. The function should be supported from windows XP and up.
                if prevent:
-                       ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)
+                       # For Vista and up we use ES_AWAYMODE_REQUIRED to prevent a print from failing if the PC does go to sleep
+                       # As it's not supported on XP, we catch the error and fallback to using ES_SYSTEM_REQUIRED only
+                       if ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED) == 0:
+                               ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)
                else:
                        ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS)
 
+elif sys.platform.startswith('darwin'):
+       import objc
+       bundle = objc.initFrameworkWrapper("IOKit",
+       frameworkIdentifier="com.apple.iokit",
+       frameworkPath=objc.pathForFramework("/System/Library/Frameworks/IOKit.framework"),
+       globals=globals())
+       objc.loadBundleFunctions(bundle, globals(), [("IOPMAssertionCreateWithName", b"i@I@o^I")])
+       def preventComputerFromSleeping(prevent):
+               if prevent:
+                       success, preventComputerFromSleeping.assertionID = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, "Cura is printing", None)
+                       if success != kIOReturnSuccess:
+                               preventComputerFromSleeping.assertionID = None
+               else:
+                       if preventComputerFromSleeping.assertionID is not None:
+                               IOPMAssertionRelease(preventComputerFromSleeping.assertionID)
+                               preventComputerFromSleeping.assertionID = None
 else:
        def preventComputerFromSleeping(prevent):
-               #No preventComputerFromSleeping for MacOS and Linux yet.
                pass
 
 class printWindowPlugin(wx.Frame):
@@ -44,6 +63,7 @@ class printWindowPlugin(wx.Frame):
                self._tempGraph = None
                self._infoText = None
                self._lastUpdateTime = time.time()
+               self._isPrinting = False
 
                variables = {
                        'setImage': self.script_setImage,
@@ -75,7 +95,6 @@ class printWindowPlugin(wx.Frame):
 
                if self._printerConnection.hasActiveConnection() and not self._printerConnection.isActiveConnectionOpen():
                        self._printerConnection.openActiveConnection()
-               preventComputerFromSleeping(True)
 
        def script_setImage(self, guiImage, mapImage):
                self._backgroundImage = wx.BitmapFromImage(wx.Image(os.path.join(self._basePath, guiImage)))
@@ -156,7 +175,7 @@ class printWindowPlugin(wx.Frame):
                self._buttonList.append(spinner)
                self.Bind(wx.EVT_SPINCTRL, lambda e: run_command(spinner), spinner)
 
-       def script_addTextButton(self, r_text, g_text, b_text, r_button, g_button, b_button, text, command, data):
+       def script_addTextButton(self, r_text, g_text, b_text, r_button, g_button, b_button, button_text, command, data):
                x_text, y_text, w_text, h_text = self._getColoredRect(r_text, g_text, b_text)
                if x_text < 0:
                        return
@@ -169,7 +188,7 @@ class printWindowPlugin(wx.Frame):
                text.SetPosition((x_text, y_text))
                text.SetSize((w_text, h_text))
                
-               button = wx.Button(self, -1, _(text))
+               button = wx.Button(self, -1, _(button_text))
                button.SetPosition((x_button, y_button))
                button.SetSize((w_button, h_button))
                button.command = command
@@ -334,6 +353,9 @@ class printWindowPlugin(wx.Frame):
                        self._infoText.SetLabel(info)
                else:
                        self.SetTitle(info.replace('\n', ', '))
+               if connection.isPrinting() != self._isPrinting:
+                       self._isPrinting = connection.isPrinting()
+                       preventComputerFromSleeping(self._isPrinting)
 
 class printWindowBasic(wx.Frame):
        """
@@ -344,6 +366,7 @@ class printWindowBasic(wx.Frame):
                super(printWindowBasic, self).__init__(parent, -1, style=wx.CLOSE_BOX|wx.CLIP_CHILDREN|wx.CAPTION|wx.SYSTEM_MENU|wx.FRAME_TOOL_WINDOW|wx.FRAME_FLOAT_ON_PARENT, title=_("Printing on %s") % (printerConnection.getName()))
                self._printerConnection = printerConnection
                self._lastUpdateTime = 0
+               self._isPrinting = False
 
                self.SetSizer(wx.BoxSizer())
                self.panel = wx.Panel(self)
@@ -403,7 +426,6 @@ class printWindowBasic(wx.Frame):
 
                if self._printerConnection.hasActiveConnection() and not self._printerConnection.isActiveConnectionOpen():
                        self._printerConnection.openActiveConnection()
-               preventComputerFromSleeping(True)
 
        def OnPowerWarningChange(self, e):
                type = self.powerManagement.get_providing_power_source_type()
@@ -475,6 +497,16 @@ class printWindowBasic(wx.Frame):
                        info += ' Bed: %d' % (self._printerConnection.getBedTemperature())
                info += '\n\n'
                self.statsText.SetLabel(info)
+               if connection.isPrinting() != self._isPrinting:
+                       self._isPrinting = connection.isPrinting()
+                       preventComputerFromSleeping(self._isPrinting)
+
+
+       def _addTermLog(self, msg):
+               pass
+
+       def _addTermLog(self, msg):
+               pass
 
        def _updateButtonStates(self):
                self.connectButton.Show(self._printerConnection.hasActiveConnection())
@@ -621,7 +653,7 @@ class TemperatureGraph(wx.Panel):
 
 class LogWindow(wx.Frame):
        def __init__(self, logText):
-               super(LogWindow, self).__init__(None, title="Error log")
+               super(LogWindow, self).__init__(None, title=_("Error log"))
                self.textBox = wx.TextCtrl(self, -1, logText, style=wx.TE_MULTILINE | wx.TE_DONTWRAP | wx.TE_READONLY)
                self.SetSize((500, 400))
                self.Show(True)