From 5a0a1d41cab335e7f11604c6d79f4683855de6d7 Mon Sep 17 00:00:00 2001 From: daid Date: Wed, 2 Oct 2013 09:26:34 +0200 Subject: [PATCH] Handle webcam support a bit better, show an error when no webcam is found, and not show the webcam snapshot button if the installation has no webcam support. --- Cura/gui/printWindow.py | 81 +++++++++++++++++++--------------- Cura/gui/tools/youmagineGui.py | 12 ++++- Cura/gui/util/webcam.py | 2 +- 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/Cura/gui/printWindow.py b/Cura/gui/printWindow.py index 1e4ee6f4..4160e326 100644 --- a/Cura/gui/printWindow.py +++ b/Cura/gui/printWindow.py @@ -43,6 +43,8 @@ class printProcessMonitor(): if platform.machine() == 'i386': cmdList.insert(0, 'arch') cmdList.insert(1, '-i386') + #Save the preferences before starting the print window so we use the proper machine settings. + profile.savePreferences(profile.getPreferencePath()) self.handle = subprocess.Popen(cmdList, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.thread = threading.Thread(target=self.Monitor) self.thread.start() @@ -62,9 +64,10 @@ class printProcessMonitor(): elif line.startswith('STATE:'): self._state = line[6:] self._callCallback() + else: + print '>' + line.rstrip() except: print sys.exc_info() - #print '>' + line.rstrip() line = p.stdout.readline() line = p.stderr.readline() while len(line) > 0: @@ -127,6 +130,7 @@ class printWindow(wx.Frame): def __init__(self): super(printWindow, self).__init__(None, -1, title=_("Printing")) + t = time.time() self.machineCom = None self.gcode = None self.gcodeList = None @@ -144,10 +148,6 @@ class printWindow(wx.Frame): self.termHistoryIdx = 0 self.cam = None - if webcam.hasWebcamSupport(): - self.cam = webcam.webcam() - if not self.cam.hasCamera(): - self.cam = None self.SetSizer(wx.BoxSizer()) self.panel = wx.Panel(self) @@ -193,6 +193,7 @@ class printWindow(wx.Frame): self.sizer.Add(self.progress, pos=(7, 0), span=(1, 7), flag=wx.EXPAND) nb = wx.Notebook(self.panel) + self.tabs = nb self.sizer.Add(nb, pos=(0, 2), span=(7, 4), flag=wx.EXPAND) self.temperaturePanel = wx.Panel(nb) @@ -306,37 +307,6 @@ class printWindow(wx.Frame): nb.AddPage(self.termPanel, _("Term")) - if self.cam is not None: - self.camPage = wx.Panel(nb) - sizer = wx.GridBagSizer(2, 2) - self.camPage.SetSizer(sizer) - - self.timelapsEnable = wx.CheckBox(self.camPage, -1, _("Enable timelapse movie recording")) - self.timelapsSavePath = wx.TextCtrl(self.camPage, -1, os.path.expanduser('~/timelaps_' + datetime.datetime.now().strftime('%Y-%m-%d_%H:%M') + '.mpg')) - sizer.Add(self.timelapsEnable, pos=(0, 0), span=(1, 2), flag=wx.EXPAND) - sizer.Add(self.timelapsSavePath, pos=(1, 0), span=(1, 2), flag=wx.EXPAND) - - pages = self.cam.propertyPages() - self.cam.buttons = [self.timelapsEnable, self.timelapsSavePath] - for page in pages: - button = wx.Button(self.camPage, -1, page) - button.index = pages.index(page) - sizer.Add(button, pos=(2, pages.index(page))) - button.Bind(wx.EVT_BUTTON, self.OnPropertyPageButton) - self.cam.buttons.append(button) - - self.campreviewEnable = wx.CheckBox(self.camPage, -1, _("Show preview")) - sizer.Add(self.campreviewEnable, pos=(3, 0), span=(1, 2), flag=wx.EXPAND) - - self.camPreview = wx.Panel(self.camPage) - sizer.Add(self.camPreview, pos=(4, 0), span=(1, 2), flag=wx.EXPAND) - - nb.AddPage(self.camPage, _("Camera")) - self.camPreview.timer = wx.Timer(self) - self.Bind(wx.EVT_TIMER, self.OnCameraTimer, self.camPreview.timer) - self.camPreview.timer.Start(500) - self.camPreview.Bind(wx.EVT_ERASE_BACKGROUND, self.OnCameraEraseBackground) - self.sizer.AddGrowableRow(6) self.sizer.AddGrowableCol(3) @@ -372,6 +342,10 @@ class printWindow(wx.Frame): self._thread.daemon = True self._thread.start() + if webcam.hasWebcamSupport(): + #Need to call the camera class on the GUI thread, or else it won't work. Shame as it hangs the GUI for about 2 seconds. + wx.CallAfter(self._webcamCheck) + def _stdinMonitor(self): while True: line = sys.stdin.readline().rstrip() @@ -379,6 +353,41 @@ class printWindow(wx.Frame): if not self.LoadGCodeFile(line[5:]): print 'LOADFAILED\n' + def _webcamCheck(self): + self.cam = webcam.webcam() + if self.cam.hasCamera(): + self.camPage = wx.Panel(self.tabs) + sizer = wx.GridBagSizer(2, 2) + self.camPage.SetSizer(sizer) + + self.timelapsEnable = wx.CheckBox(self.camPage, -1, _("Enable timelapse movie recording")) + self.timelapsSavePath = wx.TextCtrl(self.camPage, -1, os.path.expanduser('~/timelaps_' + datetime.datetime.now().strftime('%Y-%m-%d_%H:%M') + '.mpg')) + sizer.Add(self.timelapsEnable, pos=(0, 0), span=(1, 2), flag=wx.EXPAND) + sizer.Add(self.timelapsSavePath, pos=(1, 0), span=(1, 2), flag=wx.EXPAND) + + pages = self.cam.propertyPages() + self.cam.buttons = [self.timelapsEnable, self.timelapsSavePath] + for page in pages: + button = wx.Button(self.camPage, -1, page) + button.index = pages.index(page) + sizer.Add(button, pos=(2, pages.index(page))) + button.Bind(wx.EVT_BUTTON, self.OnPropertyPageButton) + self.cam.buttons.append(button) + + self.campreviewEnable = wx.CheckBox(self.camPage, -1, _("Show preview")) + sizer.Add(self.campreviewEnable, pos=(3, 0), span=(1, 2), flag=wx.EXPAND) + + self.camPreview = wx.Panel(self.camPage) + sizer.Add(self.camPreview, pos=(4, 0), span=(1, 2), flag=wx.EXPAND) + + self.tabs.AddPage(self.camPage, _("Camera")) + self.camPreview.timer = wx.Timer(self) + self.Bind(wx.EVT_TIMER, self.OnCameraTimer, self.camPreview.timer) + self.camPreview.timer.Start(500) + self.camPreview.Bind(wx.EVT_ERASE_BACKGROUND, self.OnCameraEraseBackground) + else: + self.cam = None + def OnCameraTimer(self, e): if not self.campreviewEnable.GetValue(): return diff --git a/Cura/gui/tools/youmagineGui.py b/Cura/gui/tools/youmagineGui.py index bdf6f9cd..94d85014 100644 --- a/Cura/gui/tools/youmagineGui.py +++ b/Cura/gui/tools/youmagineGui.py @@ -288,6 +288,8 @@ class newDesignWindow(wx.Frame): self._imageScroll.GetSizer().Add(self._addImageButton) self._snapshotButton = wx.Button(self._imageScroll, -1, _("Webcam..."), size=(70,52)) self._imageScroll.GetSizer().Add(self._snapshotButton) + if not webcam.hasWebcamSupport(): + self._snapshotButton.Hide() self._imageScroll.Fit() self._imageScroll.SetupScrolling(scroll_x=True, scroll_y=False) self._imageScroll.SetMinSize((20, self._imageScroll.GetSize()[1] + wx.SystemSettings_GetMetric(wx.SYS_HSCROLL_Y))) @@ -360,7 +362,12 @@ class newDesignWindow(wx.Frame): dlg.Destroy() def OnTakeImage(self, e): - webcamPhotoWindow(self).Show() + w = webcamPhotoWindow(self) + if w.hasCamera(): + w.Show() + else: + w.Destroy() + wx.MessageBox(_("No webcam found on your system"), _("Webcam error"), wx.OK | wx.ICON_ERROR) def _addImage(self, image): wxImage = None @@ -444,6 +451,9 @@ class webcamPhotoWindow(wx.Frame): self._takeImageTimer.Start(200) + def hasCamera(self): + return self._cam.hasCamera() + def OnCameraChange(self, e): self._cam.setActiveCamera(self._cameraSelect.GetSelection()) diff --git a/Cura/gui/util/webcam.py b/Cura/gui/util/webcam.py index f491b2cc..7b007d9e 100644 --- a/Cura/gui/util/webcam.py +++ b/Cura/gui/util/webcam.py @@ -27,7 +27,7 @@ except: win32vidcap = None def hasWebcamSupport(): - if cv == None and win32vidcap == None: + if cv is None and win32vidcap is None: return False if not os.path.exists(getFFMPEGpath()): return False -- 2.30.2