From 48d196df11817e03ccc593e1528c80c7070b702a Mon Sep 17 00:00:00 2001 From: Marcel Date: Wed, 29 May 2024 11:42:10 +0200 Subject: [PATCH] [update] improved SearchFileByExtensionDialog widget (table, auto refresh) --- PyLoT.py | 6 ++++-- pylot/core/util/widgets.py | 34 ++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/PyLoT.py b/PyLoT.py index 71bbbe4a..26c54ed3 100755 --- a/PyLoT.py +++ b/PyLoT.py @@ -1886,6 +1886,7 @@ class MainWindow(QMainWindow): # which will read in data input twice. Therefore current tab is changed to 0 # in loadProject before calling this function. self.fill_eventbox() + #print(f'{self.get_current_event()=}') plotted = False if self.tabs.currentIndex() == 2: self.init_event_table() @@ -1920,7 +1921,6 @@ class MainWindow(QMainWindow): self.spectro_layout.addWidget(newSpectroWidget) self.spectroWidget = newSpectroWidget - def newWF(self, event=None, plot=True): ''' Load new data and plot if necessary. @@ -1971,6 +1971,8 @@ class MainWindow(QMainWindow): # ans = False settings = QSettings() + # process application events to wait for event items to appear in event box + QApplication.processEvents() curr_event = self.get_current_event() if not curr_event: print('Could not find current event. Try reload?') @@ -3632,7 +3634,7 @@ class MainWindow(QMainWindow): return True return False - def update_status(self, message, duration=5000): + def update_status(self, message, duration=10000): self.statusBar().showMessage(message, duration) if self.get_data() is not None: if not self.get_current_event() or not self.project.location: diff --git a/pylot/core/util/widgets.py b/pylot/core/util/widgets.py index 3858dce0..e0eb5cd9 100644 --- a/pylot/core/util/widgets.py +++ b/pylot/core/util/widgets.py @@ -36,7 +36,7 @@ from PySide2.QtWidgets import QAction, QApplication, QCheckBox, QComboBox, \ QGridLayout, QLabel, QLineEdit, QMessageBox, \ QTabWidget, QToolBar, QVBoxLayout, QHBoxLayout, QWidget, \ QPushButton, QFileDialog, QInputDialog -from PySide2.QtCore import QSettings, Qt, QUrl, Signal +from PySide2.QtCore import QSettings, Qt, QUrl, Signal, QTimer from PySide2.QtWebEngineWidgets import QWebEngineView as QWebView from obspy import Stream, Trace, UTCDateTime from obspy.core.util import AttribDict @@ -1579,6 +1579,9 @@ class SearchFileByExtensionDialog(QtWidgets.QDialog): self.setupUi() self.connectSignals() self.showPaths() + self.refresh_timer = QTimer(self) + self.refresh_timer.timeout.connect(self.showPaths) + self.refresh_timer.start(10000) self.resize(800, 450) @@ -1597,9 +1600,16 @@ class SearchFileByExtensionDialog(QtWidgets.QDialog): self.searchButton = QtWidgets.QPushButton('Search') self.searchButton.setVisible(False) - self.textWidget = QtWidgets.QTextEdit() - self.textWidget.setMaximumWidth(1e5) - self.textWidget.setReadOnly(True) + self.tableWidget = QtWidgets.QTableWidget() + tableWidget = self.tableWidget + tableWidget.setColumnCount(2) + tableWidget.setRowCount(len(self.events)) + tableWidget.setHorizontalHeaderLabels(('Filename', 'Last modified')) + tableWidget.setEditTriggers(tableWidget.NoEditTriggers) + tableWidget.setSortingEnabled(True) + header = tableWidget.horizontalHeader() + header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents) + header.setStretchLastSection(True) self.statusText = QtWidgets.QLabel() @@ -1608,23 +1618,31 @@ class SearchFileByExtensionDialog(QtWidgets.QDialog): self.header_layout.addWidget(self.searchButton) self.main_layout.addLayout(self.header_layout) - self.main_layout.addWidget(self.textWidget) + self.main_layout.addWidget(self.tableWidget) self.main_layout.addWidget(self.statusText) self.main_layout.addWidget(self._buttonbox) def showPaths(self): self.filepaths = [] fext = self.lineEdit.text() - for event in self.events: + self.tableWidget.clearContents() + for index, event in enumerate(self.events): filename = get_pylot_eventfile_with_extension(event, fext) if filename: self.filepaths.append(filename) - self.textWidget.append(f'{filename}') + ts = int(os.path.getmtime(filename)) + + # create QTableWidgetItems of filepath and last modification time + fname_item = QtWidgets.QTableWidgetItem(f'{filename}') + ts_item = QtWidgets.QTableWidgetItem(f'{datetime.datetime.fromtimestamp(ts)}') + self.tableWidget.setItem(index, 0, fname_item) + self.tableWidget.setItem(index, 1, ts_item) + + # TODO: Idea -> only refresh if table contents changed. Use selection to load only a subset of files if len(self.filepaths) > 0: status_text = f'Found {len(self.filepaths)} eventfiles. Do you want to load them?' else: status_text = 'Did not find any files for specified file mask.' - self.textWidget.setText('') self.statusText.setText(status_text)