1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
11 #TODO: This does not belong here!
12 if sys.platform.startswith('win'):
13 def preventComputerFromSleeping(frame, prevent):
15 Function used to prevent the computer from going into sleep mode.
16 :param prevent: True = Prevent the system from going to sleep from this point on.
17 :param prevent: False = No longer prevent the system from going to sleep.
19 ES_CONTINUOUS = 0x80000000
20 ES_SYSTEM_REQUIRED = 0x00000001
21 ES_AWAYMODE_REQUIRED = 0x00000040
22 #SetThreadExecutionState returns 0 when failed, which is ignored. The function should be supported from windows XP and up.
24 # For Vista and up we use ES_AWAYMODE_REQUIRED to prevent a print from failing if the PC does go to sleep
25 # As it's not supported on XP, we catch the error and fallback to using ES_SYSTEM_REQUIRED only
26 if ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED) == 0:
27 ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)
29 ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS)
31 elif sys.platform.startswith('darwin'):
33 bundle = objc.initFrameworkWrapper("IOKit",
34 frameworkIdentifier="com.apple.iokit",
35 frameworkPath=objc.pathForFramework("/System/Library/Frameworks/IOKit.framework"),
37 objc.loadBundleFunctions(bundle, globals(), [("IOPMAssertionCreateWithName", b"i@I@o^I")])
38 def preventComputerFromSleeping(frame, prevent):
40 success, preventComputerFromSleeping.assertionID = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, "Cura is printing", None)
41 if success != kIOReturnSuccess:
42 preventComputerFromSleeping.assertionID = None
44 if preventComputerFromSleeping.assertionID is not None:
45 IOPMAssertionRelease(preventComputerFromSleeping.assertionID)
46 preventComputerFromSleeping.assertionID = None
48 def preventComputerFromSleeping(frame, prevent):
49 if os.path.isfile("/usr/bin/xdg-screensaver"):
51 cmd = ['xdg-screensaver', 'suspend' if prevent else 'resume', str(frame.GetHandle())]
56 class printWindowPlugin(wx.Frame):
57 def __init__(self, parent, printerConnection, filename):
58 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()))
59 self._printerConnection = printerConnection
60 self._basePath = os.path.dirname(filename)
61 self._backgroundImage = None
62 self._colorCommandMap = {}
65 self._termInput = None
66 self._termHistory = []
67 self._termHistoryIdx = 0
68 self._progressBar = None
69 self._tempGraph = None
71 self._lastUpdateTime = time.time()
72 self._isPrinting = False
75 'setImage': self.script_setImage,
76 'addColorCommand': self.script_addColorCommand,
77 'addTerminal': self.script_addTerminal,
78 'addTemperatureGraph': self.script_addTemperatureGraph,
79 'addProgressbar': self.script_addProgressbar,
80 'addButton': self.script_addButton,
81 'addSpinner': self.script_addSpinner,
82 'addTextButton': self.script_addTextButton,
84 'sendGCode': self.script_sendGCode,
85 'sendMovementGCode': self.script_sendMovementGCode,
86 'connect': self.script_connect,
87 'startPrint': self.script_startPrint,
88 'pausePrint': self.script_pausePrint,
89 'cancelPrint': self.script_cancelPrint,
90 'showErrorLog': self.script_showErrorLog,
92 execfile(filename, variables, variables)
94 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
95 self.Bind(wx.EVT_PAINT, self.OnDraw)
96 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftClick)
97 self.Bind(wx.EVT_CLOSE, self.OnClose)
99 self._updateButtonStates()
101 self._printerConnection.addCallback(self._doPrinterConnectionUpdate)
103 if self._printerConnection.hasActiveConnection() and not self._printerConnection.isActiveConnectionOpen():
104 self._printerConnection.openActiveConnection()
106 def script_setImage(self, guiImage, mapImage):
107 self._backgroundImage = wx.BitmapFromImage(wx.Image(os.path.join(self._basePath, guiImage)))
108 self._mapImage = wx.Image(os.path.join(self._basePath, mapImage))
109 self.SetClientSize(self._mapImage.GetSize())
111 def script_addColorCommand(self, r, g, b, command, data = None):
112 self._colorCommandMap[(r, g, b)] = (command, data)
114 def script_addTerminal(self, r, g, b):
115 x, y, w, h = self._getColoredRect(r, g, b)
116 if x < 0 or self._termLog is not None:
118 f = wx.Font(8, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
119 self._termLog = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_DONTWRAP)
120 self._termLog.SetFont(f)
121 self._termLog.SetEditable(0)
122 self._termInput = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
123 self._termInput.SetFont(f)
125 self._termLog.SetPosition((x, y))
126 self._termLog.SetSize((w, h - self._termInput.GetSize().GetHeight()))
127 self._termInput.SetPosition((x, y + h - self._termInput.GetSize().GetHeight()))
128 self._termInput.SetSize((w, self._termInput.GetSize().GetHeight()))
129 self.Bind(wx.EVT_TEXT_ENTER, self.OnTermEnterLine, self._termInput)
130 self._termInput.Bind(wx.EVT_CHAR, self.OnTermKey)
132 def script_addTemperatureGraph(self, r, g, b):
133 x, y, w, h = self._getColoredRect(r, g, b)
134 if x < 0 or self._tempGraph is not None:
136 self._tempGraph = TemperatureGraph(self)
138 self._tempGraph.SetPosition((x, y))
139 self._tempGraph.SetSize((w, h))
141 def script_addProgressbar(self, r, g, b):
142 x, y, w, h = self._getColoredRect(r, g, b)
145 self._progressBar = wx.Gauge(self, -1, range=1000)
147 self._progressBar.SetPosition((x, y))
148 self._progressBar.SetSize((w, h))
150 def script_addButton(self, r, g, b, text, command, data = None):
151 x, y, w, h = self._getColoredRect(r, g, b)
154 button = wx.Button(self, -1, _(text))
155 button.SetPosition((x, y))
156 button.SetSize((w, h))
157 button.command = command
159 self._buttonList.append(button)
160 self.Bind(wx.EVT_BUTTON, lambda e: command(data), button)
162 def script_addSpinner(self, r, g, b, command, data):
163 x, y, w, h = self._getColoredRect(r, g, b)
167 def run_command(spinner):
168 value = spinner.GetValue()
169 print "Value (%s) and (%s)" % (spinner.last_value, value)
170 if spinner.last_value != '' and value != 0:
171 spinner.command(spinner.data % value)
172 spinner.last_value = value
174 spinner = wx.SpinCtrl(self, -1, style=wx.TE_PROCESS_ENTER)
175 spinner.SetRange(0, 300)
176 spinner.SetPosition((x, y))
177 spinner.SetSize((w, h))
179 spinner.command = command
181 spinner.last_value = ''
182 self._buttonList.append(spinner)
183 self.Bind(wx.EVT_SPINCTRL, lambda e: run_command(spinner), spinner)
185 def script_addTextButton(self, r_text, g_text, b_text, r_button, g_button, b_button, button_text, command, data):
186 x_text, y_text, w_text, h_text = self._getColoredRect(r_text, g_text, b_text)
189 x_button, y_button, w_button, h_button = self._getColoredRect(r_button, g_button, b_button)
192 from wx.lib.intctrl import IntCtrl
193 text = IntCtrl(self, -1)
194 text.SetBounds(0, 300)
195 text.SetPosition((x_text, y_text))
196 text.SetSize((w_text, h_text))
198 button = wx.Button(self, -1, _(button_text))
199 button.SetPosition((x_button, y_button))
200 button.SetSize((w_button, h_button))
201 button.command = command
203 self._buttonList.append(button)
204 self.Bind(wx.EVT_BUTTON, lambda e: command(data % text.GetValue()), button)
206 def _getColoredRect(self, r, g, b):
207 for x in xrange(0, self._mapImage.GetWidth()):
208 for y in xrange(0, self._mapImage.GetHeight()):
209 if self._mapImage.GetRed(x, y) == r and self._mapImage.GetGreen(x, y) == g and self._mapImage.GetBlue(x, y) == b:
211 while x+w < self._mapImage.GetWidth() and self._mapImage.GetRed(x + w, y) == r and self._mapImage.GetGreen(x + w, y) == g and self._mapImage.GetBlue(x + w, y) == b:
214 while y+h < self._mapImage.GetHeight() and self._mapImage.GetRed(x, y + h) == r and self._mapImage.GetGreen(x, y + h) == g and self._mapImage.GetBlue(x, y + h) == b:
217 print "Failed to find color: ", r, g, b
220 def script_sendGCode(self, data = None):
221 for line in data.split(';'):
224 self._printerConnection.sendCommand(line)
226 def script_sendMovementGCode(self, data = None):
227 if not self._printerConnection.isPaused() and not self._printerConnection.isPrinting():
228 self.script_sendGCode(data)
230 def script_connect(self, data = None):
231 self._printerConnection.openActiveConnection()
233 def script_startPrint(self, data = None):
234 if self._printerConnection.isPrinting() or self._printerConnection.isPaused():
235 self._printerConnection.pause(not self._printerConnection.isPaused())
237 self._printerConnection.startPrint()
239 def script_cancelPrint(self, e):
240 self._printerConnection.cancelPrint()
242 def script_pausePrint(self, e):
243 self._printerConnection.pause(not self._printerConnection.isPaused())
245 def script_showErrorLog(self, e):
246 LogWindow(self._printerConnection.getErrorLog())
248 def OnEraseBackground(self, e):
252 dc = wx.BufferedPaintDC(self, self._backgroundImage)
254 def OnLeftClick(self, e):
255 r = self._mapImage.GetRed(e.GetX(), e.GetY())
256 g = self._mapImage.GetGreen(e.GetX(), e.GetY())
257 b = self._mapImage.GetBlue(e.GetX(), e.GetY())
258 if (r, g, b) in self._colorCommandMap:
259 command = self._colorCommandMap[(r, g, b)]
260 command[0](command[1])
262 def OnClose(self, e):
263 if self._printerConnection.hasActiveConnection():
264 if self._printerConnection.isPrinting() or self._printerConnection.isPaused():
265 pass #TODO: Give warning that the close will kill the print.
266 self._printerConnection.closeActiveConnection()
267 self._printerConnection.removeCallback(self._doPrinterConnectionUpdate)
268 #TODO: When multiple printer windows are open, closing one will enable sleeping again.
269 preventComputerFromSleeping(self, False)
270 self._printerConnection.cancelPrint()
273 def OnTermEnterLine(self, e):
274 if not self._printerConnection.isAbleToSendDirectCommand():
276 line = self._termInput.GetValue()
279 self._addTermLog('> %s\n' % (line))
280 self._printerConnection.sendCommand(line)
281 self._termHistory.append(line)
282 self._termHistoryIdx = len(self._termHistory)
283 self._termInput.SetValue('')
285 def OnTermKey(self, e):
286 if len(self._termHistory) > 0:
287 if e.GetKeyCode() == wx.WXK_UP:
288 self._termHistoryIdx -= 1
289 if self._termHistoryIdx < 0:
290 self._termHistoryIdx = len(self._termHistory) - 1
291 self._termInput.SetValue(self._termHistory[self._termHistoryIdx])
292 if e.GetKeyCode() == wx.WXK_DOWN:
293 self._termHistoryIdx -= 1
294 if self._termHistoryIdx >= len(self._termHistory):
295 self._termHistoryIdx = 0
296 self._termInput.SetValue(self._termHistory[self._termHistoryIdx])
299 def _addTermLog(self, line):
300 if self._termLog is not None:
301 if len(self._termLog.GetValue()) > 10000:
302 self._termLog.SetValue(self._termLog.GetValue()[-10000:])
303 self._termLog.SetInsertionPointEnd()
304 if type(line) != unicode:
305 line = unicode(line, 'utf-8', 'replace')
306 self._termLog.AppendText(line.encode('utf-8', 'replace'))
308 def _updateButtonStates(self):
309 hasPauseButton = False
310 for button in self._buttonList:
311 if button.command == self.script_pausePrint:
312 hasPauseButton = True
315 for button in self._buttonList:
316 if button.command == self.script_connect:
317 button.Show(self._printerConnection.hasActiveConnection())
318 button.Enable(not self._printerConnection.isActiveConnectionOpen() and \
319 not self._printerConnection.isActiveConnectionOpening())
320 elif button.command == self.script_pausePrint:
321 button.Show(self._printerConnection.hasPause())
322 if not self._printerConnection.hasActiveConnection() or \
323 self._printerConnection.isActiveConnectionOpen():
324 button.Enable(self._printerConnection.isPrinting() or \
325 self._printerConnection.isPaused())
326 if self._printerConnection.isPaused():
327 button.SetLabel(_("Resume"))
329 button.SetLabel(_("Pause"))
332 elif button.command == self.script_startPrint:
333 if hasPauseButton or not self._printerConnection.hasPause():
334 if not self._printerConnection.hasActiveConnection() or \
335 self._printerConnection.isActiveConnectionOpen():
336 button.Enable(not self._printerConnection.isPrinting() and \
337 not self._printerConnection.isPaused())
341 if not self._printerConnection.hasActiveConnection() or \
342 self._printerConnection.isActiveConnectionOpen():
343 if self._printerConnection.isPrinting():
344 button.SetLabel(_("Pause"))
346 if self._printerConnection.isPaused():
347 button.SetLabel(_("Resume"))
349 button.SetLabel(_("Print"))
353 elif button.command == self.script_cancelPrint:
354 if not self._printerConnection.hasActiveConnection() or \
355 self._printerConnection.isActiveConnectionOpen():
356 button.Enable(self._printerConnection.isPrinting() or \
357 self._printerConnection.isPaused())
360 elif button.command == self.script_showErrorLog:
361 button.Show(self._printerConnection.isInErrorState())
362 if self._termInput is not None:
363 self._termInput.Enable(self._printerConnection.isAbleToSendDirectCommand())
365 def _doPrinterConnectionUpdate(self, connection, extraInfo = None):
366 wx.CallAfter(self.__doPrinterConnectionUpdate, connection, extraInfo)
367 if self._tempGraph is not None:
369 for n in xrange(0, 4):
370 t = connection.getTemperature(0)
375 self._tempGraph.addPoint(temp, [0] * len(temp), connection.getBedTemperature(), 0)
377 def __doPrinterConnectionUpdate(self, connection, extraInfo):
379 if self._lastUpdateTime + 0.5 > t and extraInfo is None:
381 self._lastUpdateTime = t
383 if extraInfo is not None and len(extraInfo) > 0:
384 self._addTermLog('< %s\n' % (extraInfo))
386 self._updateButtonStates()
387 isPrinting = connection.isPrinting() or connection.isPaused()
388 if self._progressBar is not None:
390 self._progressBar.SetValue(connection.getPrintProgress() * 1000)
392 self._progressBar.SetValue(0)
393 info = connection.getStatusString()
395 if self._printerConnection.getTemperature(0) is not None:
396 info += 'Temperature: %d' % (self._printerConnection.getTemperature(0))
397 if self._printerConnection.getBedTemperature() > 0:
398 info += ' Bed: %d' % (self._printerConnection.getBedTemperature())
399 if self._infoText is not None:
400 self._infoText.SetLabel(info)
402 self.SetTitle(info.replace('\n', ', '))
403 if isPrinting != self._isPrinting:
404 self._isPrinting = isPrinting
405 preventComputerFromSleeping(self, self._isPrinting)
407 class printWindowBasic(wx.Frame):
409 Printing window for USB printing, network printing, and any other type of printer connection we can think off.
410 This is only a basic window with minimal information.
412 def __init__(self, parent, printerConnection):
413 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()))
414 self._printerConnection = printerConnection
415 self._lastUpdateTime = 0
416 self._isPrinting = False
418 self.SetSizer(wx.BoxSizer())
419 self.panel = wx.Panel(self)
420 self.GetSizer().Add(self.panel, 1, flag=wx.EXPAND)
421 self.sizer = wx.GridBagSizer(2, 2)
422 self.panel.SetSizer(self.sizer)
424 self.powerWarningText = wx.StaticText(parent=self.panel,
426 label=_("Your computer is running on battery power.\nConnect your computer to AC power or your print might not finish."),
427 style=wx.ALIGN_CENTER)
428 self.powerWarningText.SetBackgroundColour('red')
429 self.powerWarningText.SetForegroundColour('white')
430 self.powerManagement = power.PowerManagement()
431 self.powerWarningTimer = wx.Timer(self)
432 self.Bind(wx.EVT_TIMER, self.OnPowerWarningChange, self.powerWarningTimer)
433 self.OnPowerWarningChange(None)
434 self.powerWarningTimer.Start(10000)
436 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"))
438 self.connectButton = wx.Button(self.panel, -1, _("Connect"))
439 #self.loadButton = wx.Button(self.panel, -1, 'Load')
440 self.printButton = wx.Button(self.panel, -1, _("Print"))
441 self.pauseButton = wx.Button(self.panel, -1, _("Pause"))
442 self.cancelButton = wx.Button(self.panel, -1, _("Cancel print"))
443 self.errorLogButton = wx.Button(self.panel, -1, _("Error log"))
444 self.progress = wx.Gauge(self.panel, -1, range=1000)
446 self.sizer.Add(self.powerWarningText, pos=(0, 0), span=(1, 5), flag=wx.EXPAND|wx.BOTTOM, border=5)
447 self.sizer.Add(self.statsText, pos=(1, 0), span=(1, 5), flag=wx.LEFT, border=5)
448 self.sizer.Add(self.connectButton, pos=(2, 0))
449 #self.sizer.Add(self.loadButton, pos=(2,1))
450 self.sizer.Add(self.printButton, pos=(2, 1))
451 self.sizer.Add(self.pauseButton, pos=(2, 2))
452 self.sizer.Add(self.cancelButton, pos=(2, 3))
453 self.sizer.Add(self.errorLogButton, pos=(2, 4))
454 self.sizer.Add(self.progress, pos=(3, 0), span=(1, 5), flag=wx.EXPAND)
456 self.Bind(wx.EVT_CLOSE, self.OnClose)
457 self.connectButton.Bind(wx.EVT_BUTTON, self.OnConnect)
458 #self.loadButton.Bind(wx.EVT_BUTTON, self.OnLoad)
459 self.printButton.Bind(wx.EVT_BUTTON, self.OnPrint)
460 self.pauseButton.Bind(wx.EVT_BUTTON, self.OnPause)
461 self.cancelButton.Bind(wx.EVT_BUTTON, self.OnCancel)
462 self.errorLogButton.Bind(wx.EVT_BUTTON, self.OnErrorLog)
468 self.progress.SetMinSize(self.progress.GetSize())
469 self.statsText.SetLabel('\n\n\n\n\n\n')
470 self._updateButtonStates()
472 self._printerConnection.addCallback(self._doPrinterConnectionUpdate)
474 if self._printerConnection.hasActiveConnection() and not self._printerConnection.isActiveConnectionOpen():
475 self._printerConnection.openActiveConnection()
477 def OnPowerWarningChange(self, e):
478 type = self.powerManagement.get_providing_power_source_type()
479 if type == power.POWER_TYPE_AC and self.powerWarningText.IsShown():
480 self.powerWarningText.Hide()
485 elif type != power.POWER_TYPE_AC and not self.powerWarningText.IsShown():
486 self.powerWarningText.Show()
492 def OnClose(self, e):
493 if self._printerConnection.hasActiveConnection():
494 if self._printerConnection.isPrinting() or self._printerConnection.isPaused():
495 pass #TODO: Give warning that the close will kill the print.
496 self._printerConnection.closeActiveConnection()
497 self._printerConnection.removeCallback(self._doPrinterConnectionUpdate)
498 #TODO: When multiple printer windows are open, closing one will enable sleeping again.
499 preventComputerFromSleeping(self, False)
502 def OnConnect(self, e):
503 self._printerConnection.openActiveConnection()
508 def OnPrint(self, e):
509 self._printerConnection.startPrint()
511 def OnCancel(self, e):
512 self._printerConnection.cancelPrint()
514 def OnPause(self, e):
515 self._printerConnection.pause(not self._printerConnection.isPaused())
517 def OnErrorLog(self, e):
518 LogWindow(self._printerConnection.getErrorLog())
520 def _doPrinterConnectionUpdate(self, connection, extraInfo = None):
521 wx.CallAfter(self.__doPrinterConnectionUpdate, connection, extraInfo)
522 #temp = [connection.getTemperature(0)]
523 #self.temperatureGraph.addPoint(temp, [0], connection.getBedTemperature(), 0)
525 def __doPrinterConnectionUpdate(self, connection, extraInfo):
527 if self._lastUpdateTime + 0.5 > now and extraInfo is None:
529 self._lastUpdateTime = now
531 if extraInfo is not None and len(extraInfo) > 0:
532 self._addTermLog('< %s\n' % (extraInfo))
534 self._updateButtonStates()
535 onGoingPrint = connection.isPrinting() or connection.isPaused()
537 self.progress.SetValue(connection.getPrintProgress() * 1000)
539 self.progress.SetValue(0)
540 info = connection.getStatusString()
542 if self._printerConnection.getTemperature(0) is not None:
543 info += 'Temperature: %d' % (self._printerConnection.getTemperature(0))
544 if self._printerConnection.getBedTemperature() > 0:
545 info += ' Bed: %d' % (self._printerConnection.getBedTemperature())
547 self.statsText.SetLabel(info)
548 if onGoingPrint != self._isPrinting:
549 self._isPrinting = onGoingPrint
550 preventComputerFromSleeping(self, self._isPrinting)
552 def _addTermLog(self, msg):
555 def _updateButtonStates(self):
556 self.connectButton.Show(self._printerConnection.hasActiveConnection())
557 self.connectButton.Enable(not self._printerConnection.isActiveConnectionOpen() and not self._printerConnection.isActiveConnectionOpening())
558 self.pauseButton.Show(self._printerConnection.hasPause())
559 if not self._printerConnection.hasActiveConnection() or self._printerConnection.isActiveConnectionOpen():
560 self.printButton.Enable(not self._printerConnection.isPrinting() and \
561 not self._printerConnection.isPaused())
562 self.pauseButton.Enable(self._printerConnection.isPrinting())
563 self.cancelButton.Enable(self._printerConnection.isPrinting())
565 self.printButton.Enable(False)
566 self.pauseButton.Enable(False)
567 self.cancelButton.Enable(False)
568 self.errorLogButton.Show(self._printerConnection.isInErrorState())
570 class TemperatureGraph(wx.Panel):
571 def __init__(self, parent):
572 super(TemperatureGraph, self).__init__(parent)
574 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
575 self.Bind(wx.EVT_SIZE, self.OnSize)
576 self.Bind(wx.EVT_PAINT, self.OnDraw)
578 self._lastDraw = time.time() - 1.0
580 self._backBuffer = None
581 self.addPoint([0]*16, [0]*16, 0, 0)
583 def OnEraseBackground(self, e):
587 if self._backBuffer is None or self.GetSize() != self._backBuffer.GetSize():
588 self._backBuffer = wx.EmptyBitmap(*self.GetSizeTuple())
589 self.UpdateDrawing(True)
592 dc = wx.BufferedPaintDC(self, self._backBuffer)
594 def UpdateDrawing(self, force=False):
596 if (not force and now - self._lastDraw < 1.0) or self._backBuffer is None:
600 dc.SelectObject(self._backBuffer)
602 dc.SetFont(wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT))
603 w, h = self.GetSizeTuple()
604 bgLinePen = wx.Pen('#A0A0A0')
605 tempPen = wx.Pen('#FF4040')
606 tempSPPen = wx.Pen('#FFA0A0')
607 tempPenBG = wx.Pen('#FFD0D0')
608 bedTempPen = wx.Pen('#4040FF')
609 bedTempSPPen = wx.Pen('#A0A0FF')
610 bedTempPenBG = wx.Pen('#D0D0FF')
612 #Draw the background up to the current temperatures.
618 for temp, tempSP, bedTemp, bedTempSP, t in self._points:
619 x1 = int(w - (now - t))
620 for x in xrange(x0, x1 + 1):
621 for n in xrange(0, min(len(t0), len(temp))):
622 t = float(x - x0) / float(x1 - x0 + 1) * (temp[n] - t0[n]) + t0[n]
624 dc.DrawLine(x, h, x, h - (t * h / 350))
625 bt = float(x - x0) / float(x1 - x0 + 1) * (bedTemp - bt0) + bt0
626 dc.SetPen(bedTempPenBG)
627 dc.DrawLine(x, h, x, h - (bt * h / 350))
635 for x in xrange(w, 0, -30):
637 dc.DrawLine(x, 0, x, h)
639 for y in xrange(h - 1, 0, -h * 50 / 350):
641 dc.DrawLine(0, y, w, y)
642 dc.DrawText(str(tmpNr), 0, y - dc.GetFont().GetPixelSize().GetHeight())
644 dc.DrawLine(0, 0, w, 0)
645 dc.DrawLine(0, 0, 0, h)
653 for temp, tempSP, bedTemp, bedTempSP, t in self._points:
654 x1 = int(w - (now - t))
655 for x in xrange(x0, x1 + 1):
656 for n in xrange(0, min(len(t0), len(temp))):
657 t = float(x - x0) / float(x1 - x0 + 1) * (temp[n] - t0[n]) + t0[n]
658 tSP = float(x - x0) / float(x1 - x0 + 1) * (tempSP[n] - tSP0[n]) + tSP0[n]
660 dc.DrawPoint(x, h - (tSP * h / 350))
662 dc.DrawPoint(x, h - (t * h / 350))
663 bt = float(x - x0) / float(x1 - x0 + 1) * (bedTemp - bt0) + bt0
664 btSP = float(x - x0) / float(x1 - x0 + 1) * (bedTempSP - btSP0) + btSP0
665 dc.SetPen(bedTempSPPen)
666 dc.DrawPoint(x, h - (btSP * h / 350))
667 dc.SetPen(bedTempPen)
668 dc.DrawPoint(x, h - (bt * h / 350))
676 self.Refresh(eraseBackground=False)
679 if len(self._points) > 0 and (time.time() - self._points[0][4]) > w + 20:
682 def addPoint(self, temp, tempSP, bedTemp, bedTempSP):
683 if len(self._points) > 0 and time.time() - self._points[-1][4] < 0.5:
685 for n in xrange(0, len(temp)):
688 for n in xrange(0, len(tempSP)):
689 if tempSP[n] is None:
693 if bedTempSP is None:
695 self._points.append((temp[:], tempSP[:], bedTemp, bedTempSP, time.time()))
696 wx.CallAfter(self.UpdateDrawing)
699 class LogWindow(wx.Frame):
700 def __init__(self, logText):
701 super(LogWindow, self).__init__(None, title=_("Error log"))
702 self.textBox = wx.TextCtrl(self, -1, logText, style=wx.TE_MULTILINE | wx.TE_DONTWRAP | wx.TE_READONLY)
703 self.SetSize((500, 400))