From 557e32129b4de9af9bc3779a4db6c0fa2763b47a Mon Sep 17 00:00:00 2001 From: Daniel Kratzert Date: Mon, 29 Jan 2024 19:59:38 +0100 Subject: [PATCH] Implement #22 * The main table has now a rightclick menu to open the respective structure folder * Also double-click opens the files directory --- docs/changes.txt | 3 +++ src/structurefinder/gui/table_view.py | 18 ++++++++++++++++-- src/structurefinder/strf.py | 10 ++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index aa9c487..66f452e 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -1,5 +1,8 @@ StructureFinder Changelog ------------------------- +* v80 A missing directory doesn't let the program crash anymore on file open actions. A double click or + right click can open the directory of the current structure (Only works on the same computer). + * v79 Improved program executable which can now be pinned to the taskbar. Updated updater executable. * v78 Improved threshold of volume prefilter to be suitable for inaccurate unit cells. * v77 The distribution architecture of the application was changed so that diff --git a/src/structurefinder/gui/table_view.py b/src/structurefinder/gui/table_view.py index 758bf58..3418f59 100644 --- a/src/structurefinder/gui/table_view.py +++ b/src/structurefinder/gui/table_view.py @@ -2,24 +2,38 @@ from PyQt5.QtCore import Qt, pyqtSignal from PyQt5.QtGui import QCursor +from structurefinder.gui.table_model import Column + class StructuresListTableView(QtWidgets.QTableView): save_excel_triggered = pyqtSignal() + open_save_path = pyqtSignal(str) def __init__(self, parent): super().__init__(parent) self.setContextMenuPolicy(Qt.DefaultContextMenu) + self.doubleClicked.connect(self._on_open_file_path) def contextMenuEvent(self, event: QtGui.QContextMenuEvent) -> None: context_menu = QtWidgets.QMenu(self) save_excel = context_menu.addAction("Save as Excel File") - save_excel.triggered.connect(lambda: self._on_save_excel(event)) context_menu.addAction(save_excel) + open_path = context_menu.addAction("Open file path") + context_menu.addAction(open_path) + save_excel.triggered.connect(self._on_save_excel) + open_path.triggered.connect(self._on_open_file_path) context_menu.popup(QCursor.pos()) - def _on_save_excel(self, event: QtGui.QContextMenuEvent): + def _on_save_excel(self): self.save_excel_triggered.emit() + def _on_open_file_path(self) -> None: + try: + path_data = self.model()._data[self.currentIndex().row()][Column.PATH] + except IndexError: + path_data = b'' + self.open_save_path.emit(path_data.decode(errors='ignore')) + def mousePressEvent(self, e: QtGui.QMouseEvent) -> None: if e.button() == Qt.RightButton: pass diff --git a/src/structurefinder/strf.py b/src/structurefinder/strf.py index 6624f15..cb1b957 100644 --- a/src/structurefinder/strf.py +++ b/src/structurefinder/strf.py @@ -179,6 +179,7 @@ def connect_signals_and_slots(self): self.ui.actionSave_Database.triggered.connect(self.save_database) self.ui.actionCopy_Unit_Cell.triggered.connect(self.copyUnitCell) self.ui.cifList_tableView.save_excel_triggered.connect(self.on_save_as_excel) + self.ui.cifList_tableView.open_save_path.connect(self.on_browse_path_from_row) # Other fields: self.ui.txtSearchEdit.textChanged.connect(self.search_text) self.ui.searchCellLineEDit.textChanged.connect(self.search_cell) @@ -325,6 +326,15 @@ def write_excel_file_from_selection(self, filename, selection): worksheet.write(row, col, item.decode('utf-8') if isinstance(item, bytes) else item) workbook.close() + def on_browse_path_from_row(self, curdir: str): + import subprocess + if sys.platform == "win" or sys.platform == "win32": + subprocess.Popen(['explorer', str(curdir)], shell=True) + if sys.platform == 'darwin': + subprocess.call(['open', curdir]) + if sys.platform == 'linux': + subprocess.call(['xdg-open', curdir]) + def show_csdentry(self, item: QModelIndex): import webbrowser sel = self.ui.CSDtreeWidget.selectionModel().selection()