diff --git a/.gitignore b/.gitignore index 5d90984aa8cd0607c9d1192fd1166eeaea1f013a..0db9e6a46b9fa4611784c4cce0d509c708f3a1d4 100644 --- a/.gitignore +++ b/.gitignore @@ -48,4 +48,5 @@ Kodebok_*.pdf # Generated SOI-files SOI_*_*_*_*.txt -SOI_*_*_*_*.json \ No newline at end of file +SOI_*_*_*_*.json +SOI_*_*_*_*.pdf diff --git a/soitool/inline_editable_soi_view.py b/soitool/inline_editable_soi_view.py index 621d06e8608a6e7ed93ec38e2d0f876e9bba3602..93146a79c97d18b49c089b95e3bda7dcb42684ad 100644 --- a/soitool/inline_editable_soi_view.py +++ b/soitool/inline_editable_soi_view.py @@ -12,6 +12,9 @@ from PySide2.QtGui import QFont, QPixmap, QBrush, QPalette, QPainter from PySide2.QtPrintSupport import QPrinter from soitool.soi import ModuleLargerThanBinError from soitool.dialog_wrappers import exec_warning_dialog +from soitool.serialize_export_import_soi import ( + generate_soi_filename, +) class InlineEditableSOIView(QScrollArea): @@ -82,7 +85,7 @@ class InlineEditableSOIView(QScrollArea): # self.launch_auto_zoom() - def produce_pdf(self, filename): + def produce_pdf(self, filename=None): """Produce PDF using QGraphicsScene. Renders the QGraphicsScene-representation of the SOI as a PDF. This @@ -98,8 +101,13 @@ class InlineEditableSOIView(QScrollArea): filename : str Name of file to store PDF in. If file exists it will be overwritten. Note that the filename should contain the extension - '.pdf' to be properly handled by operating systems. + '.pdf' to be properly handled by operating systems. If no filename + is supplied one will be generated following the same convention as + for exported JSON and TXT files """ + if filename is None: + filename = generate_soi_filename(self.soi) + ".pdf" + printer = QPrinter(QPrinter.HighResolution) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(filename) diff --git a/soitool/main_window.py b/soitool/main_window.py index 4cd761f9badc0f5a0e76a6449a85dbfc044cc163..bd835f5ce8459c76e211b84a738f9bcf94ef9d81 100644 --- a/soitool/main_window.py +++ b/soitool/main_window.py @@ -34,6 +34,13 @@ class ModuleType(Enum): MAIN_MODULE = 0 ATTACHMENT_MODULE = 1 +class ExportMedium(Enum): + """Enumerate with mediums possible for export.""" + + COMPRESSED = 0 + UNCOMPRESSED = 1 + PDF = 2 + class MainWindow(QMainWindow): """MainWindow, shell of the entire application. @@ -127,16 +134,21 @@ class MainWindow(QMainWindow): export_compressed.setShortcut("Ctrl+e") export_compressed.setStatusTip("Eksporter komprimert SOI") export_compressed.triggered.connect( - partial(self.try_export_soi, compressed=True) + partial(self.try_export_soi, medium=ExportMedium.COMPRESSED) ) # Uncompressed SOI export_uncompressed = QAction("Ukomprimert", self) export_uncompressed.setStatusTip("Eksporter ukomprimert SOI") export_uncompressed.triggered.connect( - partial(self.try_export_soi, compressed=False) + partial(self.try_export_soi, medium=ExportMedium.UNCOMPRESSED) ) + # SOI PDF + export_pdf = QAction("PDF", self) + export_pdf.setStatusTip("Eksporter til PDF klar for utskrift") + export_pdf.triggered.connect(lambda : self.try_export_soi(medium=ExportMedium.PDF)) export_serialized_soi.addAction(export_compressed) export_serialized_soi.addAction(export_uncompressed) + export_serialized_soi.addAction(export_pdf) file_menu.addMenu(export_serialized_soi) # View/edit Codebook @@ -201,7 +213,7 @@ class MainWindow(QMainWindow): self.tabs.addTab(tab, "Kodebok") self.tabs.setCurrentWidget(tab) - def try_export_soi(self, compressed=True): + def try_export_soi(self, medium=ExportMedium.COMPRESSED): """Export the SOI in the current tab. Feedback is given through a dialog if the current tab does not contain @@ -209,14 +221,28 @@ class MainWindow(QMainWindow): Parameters ---------- - compressed : bool, optional - Serialized SOI is compressed if True (default) + medium : ExportMedium + Which medium to export SOI to. Must be one of the enums in + ExportMedium + + Raises + ------ + ValueError + If export medium is unknown """ tab_widget = self.tabs.currentWidget() # If tab contains an SOI if isinstance(tab_widget, SOIWorkspaceWidget): - export_soi(tab_widget.soi, compressed) + if medium == ExportMedium.COMPRESSED: + export_soi(tab_widget.soi, True) + elif medium == ExportMedium.UNCOMPRESSED: + export_soi(tab_widget.soi, False) + elif medium == ExportMedium.PDF: + # TODO proper name + tab_widget.view.produce_pdf() + else: + raise ValueError(f"Unknown medum for export '{medium}'") else: exec_info_dialog( "Valgt tab er ingen SOI-tab", diff --git a/soitool/serialize_export_import_soi.py b/soitool/serialize_export_import_soi.py index d7856c1e2f6bd48e840f3486f093918864075abd..c195f0c6f1768d1e00ef459099b2f0f900d95e35 100644 --- a/soitool/serialize_export_import_soi.py +++ b/soitool/serialize_export_import_soi.py @@ -150,9 +150,6 @@ def export_soi(soi, compressed=True): A .txt-file is created to contain compressed SOI. A .json-file is created to contain uncompressed SOI. - The generated file-name will be on the format: "SOI_title_YYYY_mm_dd", - where title is the SOI-title. - Parameters ---------- soi: soitool.soi.SOI @@ -163,10 +160,7 @@ def export_soi(soi, compressed=True): # Serialize SOI serialized = serialize_soi(soi) - # Generate filename - title = soi.title - date = datetime.now().strftime("%Y_%m_%d") - file_name = f"SOI_{title}_{date}" + file_name = generate_soi_filename(soi) if compressed: serialized = compress(serialized) @@ -280,3 +274,21 @@ def import_soi(file_path): ) return soi + +def generate_soi_filename(soi): + """Generate filename for SOI without extension. + + Parameters + ---------- + soi : SOI + SOI to generate filename for + + Returns + ------- + str + Filename for the SOI of the format 'SOI_title_YYYY_mm_dd' + """ + title = soi.title + parsed_date = datetime.strptime(soi.date, '%Y-%m-%d') + date_string = parsed_date.strftime("%Y_%m_%d") + return f"SOI_{title}_{date_string}"