chiark / gitweb /
Merge tag '14.12' into upstream
[cura.git] / Cura / gui / printWindow.py
index 19b9f365943b089c8334f2e689f820a61432f599..6356df6491486bdc8cb380da8fbf6dd4f347dd80 100644 (file)
@@ -17,20 +17,39 @@ 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):
        def __init__(self, parent, printerConnection, filename):
-               super(printWindowPlugin, 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()))
+               super(printWindowPlugin, self).__init__(parent, -1, style=wx.CLOSE_BOX|wx.CLIP_CHILDREN|wx.CAPTION|wx.SYSTEM_MENU|wx.FRAME_FLOAT_ON_PARENT|wx.MINIMIZE_BOX, title=_("Printing on %s") % (printerConnection.getName()))
                self._printerConnection = printerConnection
                self._basePath = os.path.dirname(filename)
                self._backgroundImage = None
@@ -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,
@@ -52,6 +72,8 @@ class printWindowPlugin(wx.Frame):
                        'addTemperatureGraph': self.script_addTemperatureGraph,
                        'addProgressbar': self.script_addProgressbar,
                        'addButton': self.script_addButton,
+                       'addSpinner': self.script_addSpinner,
+                       'addTextButton': self.script_addTextButton,
 
                        'sendGCode': self.script_sendGCode,
                        'connect': self.script_connect,
@@ -73,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)))
@@ -131,6 +152,50 @@ class printWindowPlugin(wx.Frame):
                self._buttonList.append(button)
                self.Bind(wx.EVT_BUTTON, lambda e: command(data), button)
 
+       def script_addSpinner(self, r, g, b, command, data):
+               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: 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()):
                        for y in xrange(0, self._mapImage.GetHeight()):
@@ -288,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):
        """
@@ -298,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)
@@ -317,7 +386,7 @@ class printWindowBasic(wx.Frame):
                self.OnPowerWarningChange(None)
                self.powerWarningTimer.Start(10000)
 
-               self.statsText = wx.StaticText(self.panel, -1, _("InfoLine from printer connection\nInfoLine from dialog"))
+               self.statsText = wx.StaticText(self.panel, -1, _("InfoLine from printer connection\nInfoLine from dialog\nExtra line\nMore lines for layout\nMore lines for layout\nMore lines for layout"))
 
                self.connectButton = wx.Button(self.panel, -1, _("Connect"))
                #self.loadButton = wx.Button(self.panel, -1, 'Load')
@@ -350,14 +419,13 @@ class printWindowBasic(wx.Frame):
                self.Centre()
 
                self.progress.SetMinSize(self.progress.GetSize())
-               self.statsText.SetLabel('')
+               self.statsText.SetLabel('\n\n\n\n\n\n')
                self._updateButtonStates()
 
                self._printerConnection.addCallback(self._doPrinterConnectionUpdate)
 
                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()
@@ -427,7 +495,18 @@ class printWindowBasic(wx.Frame):
                        info += 'Temperature: %d' % (self._printerConnection.getTemperature(0))
                if self._printerConnection.getBedTemperature() > 0:
                        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())
@@ -497,10 +576,10 @@ class TemperatureGraph(wx.Panel):
                                for n in xrange(0, min(len(t0), len(temp))):
                                        t = float(x - x0) / float(x1 - x0 + 1) * (temp[n] - t0[n]) + t0[n]
                                        dc.SetPen(tempPenBG)
-                                       dc.DrawLine(x, h, x, h - (t * h / 300))
+                                       dc.DrawLine(x, h, x, h - (t * h / 350))
                                bt = float(x - x0) / float(x1 - x0 + 1) * (bedTemp - bt0) + bt0
                                dc.SetPen(bedTempPenBG)
-                               dc.DrawLine(x, h, x, h - (bt * h / 300))
+                               dc.DrawLine(x, h, x, h - (bt * h / 350))
                        t0 = temp
                        bt0 = bedTemp
                        tSP0 = tempSP
@@ -512,7 +591,7 @@ class TemperatureGraph(wx.Panel):
                        dc.SetPen(bgLinePen)
                        dc.DrawLine(x, 0, x, h)
                tmpNr = 0
-               for y in xrange(h - 1, 0, -h * 50 / 300):
+               for y in xrange(h - 1, 0, -h * 50 / 350):
                        dc.SetPen(bgLinePen)
                        dc.DrawLine(0, y, w, y)
                        dc.DrawText(str(tmpNr), 0, y - dc.GetFont().GetPixelSize().GetHeight())
@@ -533,15 +612,15 @@ class TemperatureGraph(wx.Panel):
                                        t = float(x - x0) / float(x1 - x0 + 1) * (temp[n] - t0[n]) + t0[n]
                                        tSP = float(x - x0) / float(x1 - x0 + 1) * (tempSP[n] - tSP0[n]) + tSP0[n]
                                        dc.SetPen(tempSPPen)
-                                       dc.DrawPoint(x, h - (tSP * h / 300))
+                                       dc.DrawPoint(x, h - (tSP * h / 350))
                                        dc.SetPen(tempPen)
-                                       dc.DrawPoint(x, h - (t * h / 300))
+                                       dc.DrawPoint(x, h - (t * h / 350))
                                bt = float(x - x0) / float(x1 - x0 + 1) * (bedTemp - bt0) + bt0
                                btSP = float(x - x0) / float(x1 - x0 + 1) * (bedTempSP - btSP0) + btSP0
                                dc.SetPen(bedTempSPPen)
-                               dc.DrawPoint(x, h - (btSP * h / 300))
+                               dc.DrawPoint(x, h - (btSP * h / 350))
                                dc.SetPen(bedTempPen)
-                               dc.DrawPoint(x, h - (bt * h / 300))
+                               dc.DrawPoint(x, h - (bt * h / 350))
                        t0 = temp
                        bt0 = bedTemp
                        tSP0 = tempSP
@@ -574,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)