diff --git a/ibridgesgui/__main__.py b/ibridgesgui/__main__.py index ac1057d5..02b23297 100755 --- a/ibridgesgui/__main__.py +++ b/ibridgesgui/__main__.py @@ -12,9 +12,9 @@ from ibridgesgui.browser import Browser from ibridgesgui.config import get_log_level, init_logger, set_log_level -from ibridgesgui.gui_utils import UI_FILE_DIR from ibridgesgui.info import Info from ibridgesgui.login import Login +from ibridgesgui.logviewer import LogViewer from ibridgesgui.popup_widgets import CheckConfig from ibridgesgui.search import Search from ibridgesgui.sync import Sync @@ -34,10 +34,9 @@ class MainMenu(PyQt6.QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, app_name): """Initialise the main window.""" super().__init__() - if getattr(sys, "frozen", False): - super().setupUi(self) - else: - PyQt6.uic.loadUi(UI_FILE_DIR / "MainMenu.ui", self) + super().setupUi(self) + + app.aboutToQuit.connect(self.close_event) self.logger = logging.getLogger(app_name) @@ -49,6 +48,7 @@ def __init__(self, app_name): "tabSync": self.init_sync_tab, "tabSearch": self.init_search_tab, "tabInfo": self.init_info_tab, + "tabLog": self.init_log_tab } self.session = None @@ -61,6 +61,7 @@ def __init__(self, app_name): self.action_check_configuration.triggered.connect(self.inspect_env_file) self.tab_widget.setCurrentIndex(0) + def disconnect(self): """Close iRODS session.""" self.error_label.clear() @@ -109,6 +110,11 @@ def exit(self): else: pass + def close_event(self): + """Close program properly if main window is closed.""" + self.disconnect() + sys.exit() + def setup_tabs(self): """Init tab view.""" for tab_fun in self.ui_tabs_lookup.values(): @@ -125,6 +131,11 @@ def init_info_tab(self): irods_info = Info(self.session) self.tab_widget.addTab(irods_info, "Info") + def init_log_tab(self): + """Create log tab.""" + ibridges_log = LogViewer(self.logger) + self.tab_widget.addTab(ibridges_log, "Logs") + def init_browser_tab(self): """Create browser.""" self.irods_browser = Browser(self.session, self.app_name) @@ -159,8 +170,8 @@ def main(): if log_level is not None: init_logger(THIS_APPLICATION, log_level) else: - set_log_level("error") - init_logger(THIS_APPLICATION, "error") + set_log_level("debug") + init_logger(THIS_APPLICATION, "debug") main_widget = PyQt6.QtWidgets.QStackedWidget() main_app = MainMenu(THIS_APPLICATION) main_widget.addWidget(main_app) diff --git a/ibridgesgui/logviewer.py b/ibridgesgui/logviewer.py new file mode 100644 index 00000000..70dfb782 --- /dev/null +++ b/ibridgesgui/logviewer.py @@ -0,0 +1,50 @@ +"""Logging tab.""" + +import logging +import sys + +import PyQt6 + +from ibridgesgui.config import CONFIG_DIR +from ibridgesgui.gui_utils import UI_FILE_DIR +from ibridgesgui.ui_files.tabLogging import Ui_tabLogging + + +class QTextEditLogger(logging.Handler, PyQt6.QtCore.QObject): + """Logging in a Qt text browser.""" + + append_plain_text = PyQt6.QtCore.pyqtSignal(str) + + def __init__(self, text_browser): + """Initialise.""" + super().__init__() + PyQt6.QtCore.QObject.__init__(self) + self.widget = text_browser + self.widget.setReadOnly(True) + self.append_plain_text.connect(self.widget.insertPlainText) + + def emit(self, record): + """Emit when new logging accurs.""" + msg = self.format(record)+"\n" + self.append_plain_text.emit(msg) + + +class LogViewer(PyQt6.QtWidgets.QWidget, Ui_tabLogging): + """Set iBridges logging in GUI.""" + + def __init__(self, logger): + """Initialise the tab.""" + super().__init__() + if getattr(sys, "frozen", False): + super().setupUi(self) + else: + PyQt6.uic.loadUi(UI_FILE_DIR / "tabLogging.ui", self) + + self.logger = logger + self.log_label.setText(str(CONFIG_DIR)) + self.log_text = QTextEditLogger(self.log_browser) + self.log_text.setFormatter( + logging.Formatter( + '%(asctime)s %(levelname)s %(module)s %(funcName)s %(message)s')) + self.logger.addHandler(self.log_text) + self.logger.setLevel(logging.DEBUG) diff --git a/ibridgesgui/ui_files/tabLogging.py b/ibridgesgui/ui_files/tabLogging.py new file mode 100644 index 00000000..d1651dfb --- /dev/null +++ b/ibridgesgui/ui_files/tabLogging.py @@ -0,0 +1,49 @@ +# Form implementation generated from reading ui file 'ibridgesgui/ui_files/tabLogging.ui' +# +# Created by: PyQt6 UI code generator 6.4.2 +# +# WARNING: Any manual changes made to this file will be lost when pyuic6 is +# run again. Do not edit this file unless you know what you are doing. + + +from PyQt6 import QtCore, QtGui, QtWidgets + + +class Ui_tabLogging(object): + def setupUi(self, Form): + Form.setObjectName("Form") + Form.resize(400, 244) + Form.setStyleSheet("QWidget\n" +"{\n" +" background-color: rgb(211,211,211);\n" +" color: rgb(88, 88, 90);\n" +" selection-background-color: rgb(21, 165, 137);\n" +" selection-color: rgb(245, 244, 244)\n" +"}\n" +"\n" +"QLabel\n" +"{\n" +" background-color: rgb(211,211,211);\n" +"}\n" +"\n" +"QTextBrowser\n" +"{\n" +" background-color: rgb(245, 244, 244)\n" +"}\n" +"") + self.verticalLayout = QtWidgets.QVBoxLayout(Form) + self.verticalLayout.setObjectName("verticalLayout") + self.log_label = QtWidgets.QLabel(parent=Form) + self.log_label.setObjectName("log_label") + self.verticalLayout.addWidget(self.log_label) + self.log_browser = QtWidgets.QTextBrowser(parent=Form) + self.log_browser.setObjectName("log_browser") + self.verticalLayout.addWidget(self.log_browser) + + self.retranslateUi(Form) + QtCore.QMetaObject.connectSlotsByName(Form) + + def retranslateUi(self, Form): + _translate = QtCore.QCoreApplication.translate + Form.setWindowTitle(_translate("Form", "Form")) + self.log_label.setText(_translate("Form", "Log file")) diff --git a/ibridgesgui/ui_files/tabLogging.ui b/ibridgesgui/ui_files/tabLogging.ui new file mode 100644 index 00000000..a346768b --- /dev/null +++ b/ibridgesgui/ui_files/tabLogging.ui @@ -0,0 +1,51 @@ + + + Form + + + + 0 + 0 + 400 + 244 + + + + Form + + + QWidget +{ + background-color: rgb(211,211,211); + color: rgb(88, 88, 90); + selection-background-color: rgb(21, 165, 137); + selection-color: rgb(245, 244, 244) +} + +QLabel +{ + background-color: rgb(211,211,211); +} + +QTextBrowser +{ + background-color: rgb(245, 244, 244) +} + + + + + + + Log file + + + + + + + + + + +