chiark / gitweb /
Merge tag '14.12' into upstream
[cura.git] / Cura / gui / printWindow.py
index 2c905b72be19e007b4da88025dc92c9f0f383164..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,
@@ -53,6 +73,7 @@ class printWindowPlugin(wx.Frame):
                        'addProgressbar': self.script_addProgressbar,
                        'addButton': self.script_addButton,
                        'addSpinner': self.script_addSpinner,
+                       'addTextButton': self.script_addTextButton,
 
                        'sendGCode': self.script_sendGCode,
                        'connect': self.script_connect,
@@ -74,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)))
@@ -136,14 +156,45 @@ class printWindowPlugin(wx.Frame):
                x, y, w, h = self._getColoredRect(r, g, b)
                if x < 0:
                        return
+
+               def run_command(spinner):
+                       value = spinner.GetValue()
+                       print "Value (%s) and (%s)" % (spinner.last_value, value)
+                       if spinner.last_value != '' and value != 0:
+                               spinner.command(spinner.data % value)
+                               spinner.last_value = value
+
                spinner = wx.SpinCtrl(self, -1, style=wx.TE_PROCESS_ENTER)
                spinner.SetRange(0, 300)
                spinner.SetPosition((x, y))
                spinner.SetSize((w, h))
+               spinner.SetValue(0)
                spinner.command = command
                spinner.data = data
+               spinner.last_value = ''
                self._buttonList.append(spinner)
-               self.Bind(wx.EVT_SPINCTRL, lambda e: command(data % (spinner.GetValue())), 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, 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
+               x_button, y_button, w_button, h_button = self._getColoredRect(r_button, g_button, b_button)
+               if x_button < 0:
+                       return
+               from wx.lib.intctrl import IntCtrl
+               text = IntCtrl(self, -1)
+               text.SetBounds(0, 300)
+               text.SetPosition((x_text, y_text))
+               text.SetSize((w_text, h_text))
+               
+               button = wx.Button(self, -1, _(button_text))
+               button.SetPosition((x_button, y_button))
+               button.SetSize((w_button, h_button))
+               button.command = command
+               button.data = data
+               self._buttonList.append(button)
+               self.Bind(wx.EVT_BUTTON, lambda e: command(data % text.GetValue()), button)
 
        def _getColoredRect(self, r, g, b):
                for x in xrange(0, self._mapImage.GetWidth()):
@@ -302,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):
        """
@@ -312,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)
@@ -371,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()
@@ -443,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())
@@ -589,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)