From 27e688c1481fff234c49e951eb22f9c0b4c1ef45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=A9rence=20Clastres?= Date: Wed, 13 May 2020 21:47:23 +0200 Subject: [PATCH] Add Save, Clear and toggle autoscroll to the Logs tab (#1129) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New options: - Save: save the log to a file (default to carla_log.txt) - Clear: clear the console log (also accessible via CTRL+L) - Auto-Scroll (default on): chooses wether it should scroll to keep up with new log entries or stay where it is Misc: - Small carla_host.ui cleanup (from Qt Designer) - Do not show the console scrollbars when there is nothing to scroll - Add some margin to the console to give it a more "console-like" feel Signed-off-by: Térence Clastres --- resources/ui/carla_host.ui | 128 ++++++++++++++++++++++++++++------ source/frontend/carla_host.py | 58 +++++++++++++++ 2 files changed, 165 insertions(+), 21 deletions(-) diff --git a/resources/ui/carla_host.ui b/resources/ui/carla_host.ui index 799c84ad8..640a5c3db 100644 --- a/resources/ui/carla_host.ui +++ b/resources/ui/carla_host.ui @@ -7,7 +7,7 @@ 0 0 1045 - 716 + 741 @@ -67,7 +67,16 @@ 0 - + + 0 + + + 0 + + + 0 + + 0 @@ -179,21 +188,6 @@ Logs - - 0 - - - 0 - - - 0 - - - 0 - - - 1 - @@ -202,10 +196,10 @@ - Qt::ScrollBarAlwaysOn + Qt::ScrollBarAsNeeded - Qt::ScrollBarAlwaysOn + Qt::ScrollBarAsNeeded QPlainTextEdit::NoWrap @@ -218,6 +212,98 @@ + + + + true + + + + 0 + 0 + + + + + 0 + 0 + + + + + QLayout::SetDefaultConstraint + + + + + ArrowCursor + + + false + + + Qt::LeftToRight + + + false + + + Save + + + + :/16x16/document-save.svgz:/16x16/document-save.svgz + + + + 24 + 24 + + + + false + + + false + + + false + + + + + + + Clear + + + + :/16x16/edit-clear.svgz:/16x16/edit-clear.svgz + + + + 24 + 24 + + + + Ctrl+L + + + + + + + Auto-Scroll + + + true + + + + + + @@ -336,7 +422,7 @@ 0 0 1045 - 25 + 42 @@ -878,7 +964,7 @@ 0 - + false diff --git a/source/frontend/carla_host.py b/source/frontend/carla_host.py index bc207237b..ed6ee66ea 100644 --- a/source/frontend/carla_host.py +++ b/source/frontend/carla_host.py @@ -205,6 +205,12 @@ class HostWindow(QMainWindow): self.fWithCanvas = withCanvas + # ---------------------------------------------------------------------------------------------------- + # Internal stuff (logs) + + self.autoscrollOnNewLog = True + self.lastLogSliderPos = 0 + # ---------------------------------------------------------------------------------------------------- # Set up GUI (engine stopped) @@ -387,6 +393,15 @@ class HostWindow(QMainWindow): self.ui.miniCanvasPreview.setRealParent(self) self.ui.tw_miniCanvas.tabBar().hide() + # ---------------------------------------------------------------------------------------------------- + # Set up GUI (logs) + + self.ui.text_logs.textChanged.connect(self.slot_logButtonsState) + self.ui.logs_clear.clicked.connect(self.slot_logClear) + self.ui.logs_save.clicked.connect(self.slot_logSave) + self.ui.logs_autoscroll.stateChanged.connect(self.slot_toggleLogAutoscroll) + self.ui.text_logs.verticalScrollBar().valueChanged.connect(self.slot_logSliderMoved) + # ---------------------------------------------------------------------------------------------------- # Set up GUI (special stuff for Mac OS) @@ -540,6 +555,7 @@ class HostWindow(QMainWindow): # Final setup self.ui.text_logs.clear() + self.slot_logButtonsState(False) self.setProperWindowTitle() # Disable non-supported features @@ -2189,6 +2205,48 @@ class HostWindow(QMainWindow): vsb.setValue(yp * vsb.maximum()) self.updateCanvasInitialPos() + # -------------------------------------------------------------------------------------------------------- + # Logs autoscroll, save and clear + + @pyqtSlot(Qt.CheckState) + def slot_toggleLogAutoscroll(self, checked): + self.autoscrollOnNewLog = bool(checked) + if self.autoscrollOnNewLog: + self.ui.text_logs.verticalScrollBar().setValue(self.ui.text_logs.verticalScrollBar().maximum()) + + @pyqtSlot(int) + def slot_logSliderMoved(self, slider_pos): + if self.ui.text_logs.verticalScrollBar().hasTracking() or self.autoscrollOnNewLog: + self.lastLogSliderPos = slider_pos + else: + self.ui.text_logs.verticalScrollBar().setValue(self.lastLogSliderPos) + + @pyqtSlot() + def slot_logButtonsState(self, enabled=True): + self.ui.logs_clear.setEnabled(enabled) + self.ui.logs_save.setEnabled(enabled) + + @pyqtSlot() + def slot_logSave(self): + filename, _ = QFileDialog.getSaveFileName(self, self.tr("Save Logs"),os.path.join( + self.fSavedSettings[CARLA_KEY_MAIN_PROJECT_FOLDER], 'carla_log.txt')) + + if not filename: + return + + try: + with open(filename, "w") as logfile: + logfile.write(self.ui.text_logs.toPlainText()) + logfile.close() + except: + return + + @pyqtSlot() + def slot_logClear(self): + self.ui.text_logs.clear() + self.ui.text_logs.appendPlainText("======= Logs cleared ========") + self.slot_logButtonsState(False) + # -------------------------------------------------------------------------------------------------------- # Timers