def open_report(self): """ Present an 'Open report' dialog to the user, load a '.report' file (as saved by OWReport) and create a new canvas window associated with the OWReport instance. """ settings = QSettings() KEY = "report/file-dialog-dir" start_dir = settings.value(KEY, "", type=str) dlg = QFileDialog( self, windowTitle=self.tr("Open Report"), acceptMode=QFileDialog.AcceptOpen, fileMode=QFileDialog.ExistingFile, ) if os.path.isdir(start_dir): dlg.setDirectory(start_dir) dlg.setWindowModality(Qt.ApplicationModal) dlg.setNameFilters(["Report (*.report)"]) def accepted(): directory = dlg.directory().absolutePath() filename = dlg.selectedFiles()[0] settings.setValue(KEY, directory) self._open_report(filename) dlg.accepted.connect(accepted) dlg.exec()
def get_save_filename(self): # pragma: no cover if sys.platform == "darwin": def remove_star(filt): return filt.replace(" (*.", " (.") else: def remove_star(filt): return filt no_ext_filters = {remove_star(f): f for f in self.valid_filters()} filename = self.initial_start_dir() while True: dlg = QFileDialog(None, "Save File", filename, ";;".join(no_ext_filters)) dlg.setAcceptMode(dlg.AcceptSave) dlg.selectNameFilter(remove_star(self.default_valid_filter())) dlg.setOption(QFileDialog.DontConfirmOverwrite) if dlg.exec() == QFileDialog.Rejected: return "", "" filename = dlg.selectedFiles()[0] selected_filter = no_ext_filters[dlg.selectedNameFilter()] filename = self._replace_extension( filename, self._extension_from_filter(selected_filter)) if not os.path.exists(filename) or QMessageBox.question( self, "Overwrite file?", f"File {os.path.split(filename)[1]} already exists.\n" "Overwrite?") == QMessageBox.Yes: return filename, selected_filter
def get_save_filename(self): # pragma: no cover if sys.platform == "darwin": def remove_star(filt): return filt.replace(" (*.", " (.") else: def remove_star(filt): return filt no_ext_filters = {remove_star(f): f for f in self._valid_filters()} filename = self._initial_start_dir() while True: dlg = QFileDialog( None, "Save File", filename, ";;".join(no_ext_filters)) dlg.setAcceptMode(dlg.AcceptSave) dlg.selectNameFilter(remove_star(self._default_valid_filter())) dlg.setOption(QFileDialog.DontConfirmOverwrite) if dlg.exec() == QFileDialog.Rejected: return "", "" filename = dlg.selectedFiles()[0] selected_filter = no_ext_filters[dlg.selectedNameFilter()] filename = self._replace_extension( filename, self._extension_from_filter(selected_filter)) if not os.path.exists(filename) or QMessageBox.question( self, "Overwrite file?", f"File {os.path.split(filename)[1]} already exists.\n" "Overwrite?") == QMessageBox.Yes: return filename, selected_filter
def get_save_filename(self): """ Open a user dialog and returns the dicrectory path. """ filename = self._initial_start_dir() while True: dlg = QFileDialog(None, "Select directory to save", filename) dlg.setFileMode(QFileDialog.Directory) dlg.setOption(QFileDialog.ShowDirsOnly) dlg.setAcceptMode(dlg.AcceptSave) dlg.setOption(QFileDialog.DontConfirmOverwrite) if dlg.exec() == QFileDialog.Rejected: return None filename = list(dlg.selectedFiles())[0] if not os.path.exists(filename) or QMessageBox.question( self, "Overwrite file?", f"Folder {os.path.split(filename)[1]} already exists.\n" "Overwrite?") == QMessageBox.Yes: return filename