Set default output dir and allow users changing it

Set the default directory for saving the file as the one from
the first document. This one will show just the directory name.
If the user changes it by choosing another directory, then show the new
directory name and its full path.
This commit is contained in:
deeplow 2022-10-07 11:24:42 +01:00
parent 4a42627f45
commit 1790231db0
No known key found for this signature in database
GPG key ID: 577982871529A52A
2 changed files with 22 additions and 17 deletions

View file

@ -22,11 +22,7 @@ log = logging.getLogger(__name__)
class MainWindow(QtWidgets.QMainWindow): class MainWindow(QtWidgets.QMainWindow):
delete_window = QtCore.Signal(str) delete_window = QtCore.Signal(str)
def __init__( def __init__(self, dangerzone: DangerzoneGui, window_id: str) -> None:
self,
dangerzone: DangerzoneGui,
window_id: str
) -> None:
super(MainWindow, self).__init__() super(MainWindow, self).__init__()
self.dangerzone = dangerzone self.dangerzone = dangerzone
self.window_id = window_id self.window_id = window_id
@ -429,34 +425,43 @@ class SettingsWidget(QtWidgets.QWidget):
else: else:
self.start_button.setEnabled(False) self.start_button.setEnabled(False)
def document_selected(self) -> None: def document_selected(self, filenames: List[str]) -> None:
pass # set the default save location as the directory for the first document
save_path = os.path.dirname(filenames[0])
save_dir = os.path.basename(save_path)
self.save_location.setText(save_dir)
def select_output_directory(self) -> None: def select_output_directory(self) -> None:
dialog = QtWidgets.QFileDialog() dialog = QtWidgets.QFileDialog()
dialog.setLabelText(QtWidgets.QFileDialog.Accept, "Select output directory") dialog.setLabelText(QtWidgets.QFileDialog.Accept, "Select output directory")
if len(self.dangerzone.get_unsafe_documents()) >= 1: if self.output_dir is None:
# pick the first document's directory as the default one # pick the first document's directory as the default one
dialog.setDirectory( unconverted_docs = self.dangerzone.get_unconverted_documents()
os.path.dirname(self.dangerzone.documents[0].input_filename) if len(unconverted_docs) >= 1:
) dialog.setDirectory(os.path.dirname(unconverted_docs[0].input_filename))
else:
# open the directory where the user last saved it
dialog.setDirectory(self.output_dir)
# allow only the selection of directories # allow only the selection of directories
dialog.setFileMode(QtWidgets.QFileDialog.DirectoryOnly) dialog.setFileMode(QtWidgets.QFileDialog.DirectoryOnly)
dialog.setOption(QtWidgets.QFileDialog.ShowDirsOnly, True) dialog.setOption(QtWidgets.QFileDialog.ShowDirsOnly, True)
if dialog.exec_() == QtWidgets.QFileDialog.Accepted: if dialog.exec_() == QtWidgets.QFileDialog.Accepted:
self.output_dir = dialog.selectedFiles()[0] selected_dir = dialog.selectedFiles()[0]
if selected_dir is not None:
self.output_dir = str(selected_dir) # type: ignore [assignment]
self.save_location.setText(selected_dir)
def start_button_clicked(self) -> None: def start_button_clicked(self) -> None:
if self.save_checkbox.checkState() == QtCore.Qt.Unchecked: if self.save_checkbox.checkState() == QtCore.Qt.Unchecked:
# If not saving, then save it to a temp file instead # If not saving, then save it to a temp file instead
for document in self.dangerzone.get_unsafe_documents(): for document in self.dangerzone.get_unconverted_documents():
(_, tmp) = tempfile.mkstemp(suffix=".pdf", prefix="dangerzone_") (_, tmp) = tempfile.mkstemp(suffix=".pdf", prefix="dangerzone_")
document.output_filename = tmp document.output_filename = tmp
else: else:
for document in self.dangerzone.get_unsafe_documents(): for document in self.dangerzone.get_unconverted_documents():
document.suffix = self.safe_extension.text() document.suffix = self.safe_extension.text()
if self.output_dir: if self.output_dir:
@ -528,7 +533,7 @@ class DocumentsListWidget(QtWidgets.QListWidget):
for filename in filenames: for filename in filenames:
self.dangerzone.add_document(filename) self.dangerzone.add_document(filename)
for document in self.dangerzone.get_unsafe_documents(): for document in self.dangerzone.get_unconverted_documents():
item = QtWidgets.QListWidgetItem() item = QtWidgets.QListWidgetItem()
item.setSizeHint(QtCore.QSize(500, 50)) item.setSizeHint(QtCore.QSize(500, 50))
widget = DocumentWidget(self.dangerzone, document) widget = DocumentWidget(self.dangerzone, document)

View file

@ -61,8 +61,8 @@ class DangerzoneCore(object):
with concurrent.futures.ThreadPoolExecutor(max_workers=max_jobs) as executor: with concurrent.futures.ThreadPoolExecutor(max_workers=max_jobs) as executor:
executor.map(convert_doc, self.documents) executor.map(convert_doc, self.documents)
def get_unsafe_documents(self) -> List[Document]: def get_unconverted_documents(self) -> List[Document]:
return [doc for doc in self.documents if doc.is_unsafe()] return [doc for doc in self.documents if doc.is_unconverted()]
def get_safe_documents(self) -> List[Document]: def get_safe_documents(self) -> List[Document]:
return [doc for doc in self.documents if doc.is_safe()] return [doc for doc in self.documents if doc.is_safe()]