From 75a50b73c2d51854d8e54ef78bc46779962e41fb Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Thu, 16 Feb 2017 09:23:28 +0100 Subject: [PATCH] Move pre-heat timer into PrinterOutputDevice If it's held inside the device that has two advantages: It's being held per-device, so switching connection doesn't stop the timer. And also, the logic is no longer in the GUI. Contributes to issue CURA-3161. --- cura/PrinterOutputDevice.py | 25 +++++++++++++++---- .../NetworkPrinterOutputDevice.py | 3 +++ resources/qml/PrintMonitor.qml | 22 ++++------------ 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/cura/PrinterOutputDevice.py b/cura/PrinterOutputDevice.py index 4a55e8a6d9..d791e50260 100644 --- a/cura/PrinterOutputDevice.py +++ b/cura/PrinterOutputDevice.py @@ -3,12 +3,11 @@ from UM.i18n import i18nCatalog from UM.OutputDevice.OutputDevice import OutputDevice -from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject +from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QTimer from PyQt5.QtWidgets import QMessageBox +from enum import IntEnum # For the connection state tracking. from UM.Settings.ContainerRegistry import ContainerRegistry - -from enum import IntEnum # For the connection state tracking. from UM.Logger import Logger from UM.Signal import signalemitter @@ -49,6 +48,9 @@ class PrinterOutputDevice(QObject, OutputDevice): self._error_text = "" self._accepts_commands = True self._preheat_bed_timeout = 900 #Default time-out for pre-heating the bed, in seconds. + self._preheat_bed_timer = QTimer() #Timer that tracks how long to preheat still. + self._preheat_bed_timer.setSingleShot(True) + self._preheat_bed_timer.timeout.connect(self.cancelPreheatBed) self._printer_state = "" self._printer_type = "unknown" @@ -214,13 +216,26 @@ class PrinterOutputDevice(QObject, OutputDevice): self._target_bed_temperature = temperature self.targetBedTemperatureChanged.emit() - ## The duration of the time-out to pre-heat the bed, in seconds. + ## The total duration of the time-out to pre-heat the bed, in seconds. # # \return The duration of the time-out to pre-heat the bed, in seconds. - @pyqtProperty(int) + @pyqtProperty(int, constant = True) def preheatBedTimeout(self): return self._preheat_bed_timeout + ## The remaining duration of the pre-heating of the bed. + # + # This is formatted in M:SS format. + # \return The duration of the time-out to pre-heat the bed, formatted. + @pyqtProperty(str) + def preheatBedRemainingTime(self): + period = self._preheat_bed_timer.remainingTime() + if period <= 0: + return "" + minutes, period = divmod(period, 60000) #60000 milliseconds in a minute. + seconds, _ = divmod(period, 1000) #1000 milliseconds in a second. + return "%d:%02d" % (minutes, seconds) + ## Time the print has been printing. # Note that timeTotal - timeElapsed should give time remaining. @pyqtProperty(float, notify = timeElapsedChanged) diff --git a/plugins/UM3NetworkPrinting/NetworkPrinterOutputDevice.py b/plugins/UM3NetworkPrinting/NetworkPrinterOutputDevice.py index 075995d39f..db80ee6c5b 100644 --- a/plugins/UM3NetworkPrinting/NetworkPrinterOutputDevice.py +++ b/plugins/UM3NetworkPrinting/NetworkPrinterOutputDevice.py @@ -262,6 +262,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): put_request = QNetworkRequest(url) put_request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") self._manager.put(put_request, data.encode()) + self._preheat_bed_timer.start(self._preheat_bed_timeout * 1000) #Times 1000 because it needs to be provided as milliseconds. ## Cancels pre-heating the heated bed of the printer. # @@ -269,6 +270,8 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): @pyqtSlot() def cancelPreheatBed(self): self.preheatBed(temperature = 0, duration = 0) + self._preheat_bed_timer.stop() + self._preheat_bed_timer.setInterval(0) ## Changes the target bed temperature on the printer. # diff --git a/resources/qml/PrintMonitor.qml b/resources/qml/PrintMonitor.qml index 6fffa0f902..3962a16fc5 100644 --- a/resources/qml/PrintMonitor.qml +++ b/resources/qml/PrintMonitor.qml @@ -288,23 +288,16 @@ Column property var endTime: new Date() //Set initial endTime to be the current date, so that the endTime has initially already passed and the timer text becomes invisible if you were to update. function update() { - var now = new Date(); - if (now.getTime() < endTime.getTime()) + preheatCountdown.text = "" + if (connectedPrinter != null) { - var remaining = endTime - now; //This is in milliseconds. - var minutes = Math.floor(remaining / 60 / 1000); - var seconds = Math.floor((remaining / 1000) % 60); - preheatCountdown.text = minutes + ":" + (seconds < 10 ? "0" + seconds : seconds); preheatCountdown.visible = true; + preheatCountdown.text = connectedPrinter.preheatBedRemainingTime; } - else + if (preheatCountdown.text == "") //Either time elapsed or not connected. { preheatCountdown.visible = false; - running = false; - if (connectedPrinter != null) - { - connectedPrinter.cancelPreheatBed() - } + stop(); } } } @@ -440,17 +433,12 @@ Column if (!preheatCountdownTimer.running) { connectedPrinter.preheatBed(preheatTemperatureInput.text, connectedPrinter.preheatBedTimeout); - var now = new Date(); - var end_time = new Date(); - end_time.setTime(now.getTime() + connectedPrinter.preheatBedTimeout * 1000); //*1000 because time is in milliseconds here. - preheatCountdownTimer.endTime = end_time; preheatCountdownTimer.start(); preheatCountdownTimer.update(); //Update once before the first timer is triggered. } else { connectedPrinter.cancelPreheatBed(); - preheatCountdownTimer.endTime = new Date(); preheatCountdownTimer.update(); } }