def export_full_to_pdf(schedule_ref: Schedule, title_text: str, file_path: str, font_name: str, font_path: str, encoding: str, progress=None) -> None: """ Method for exports to PDF file """ file = QFileInfo(file_path) # pdf pdf = FPDF(orientation="L") # encoding if encoding == "UTF-8": pdf.add_font(font_name, "", font_path, uni=True) else: pdf.add_font(font_name, "", font_path) pdf.set_doc_option("core_fonts_encoding", encoding) pdf.set_font(font_name) pdf.add_page() x, y = float(pdf.get_x()), float(pdf.get_y()) w = float(pdf.w) - 2 * float(pdf.get_x()) h = float(pdf.h) - 2 * float(pdf.get_y()) - 6 pdf.set_auto_page_break(True, margin=y) title = 10 pdf.set_font_size(14) pdf.cell(w, title, txt=title_text, align="C", border=0) h -= title first_column, first_row = 4, 4 step_column = (w - first_row) / 8 step_row = (h - first_column) / 6 indexes = schedule_ref.indexes() table_widget = schedule_editor_window.ScheduleTableWidget() table_widget.set_schedule(schedule_ref) # processing week_step = 100 / 7 count = 0 i = 0 while i < 7: j = 0 while j < 9: if i == 0 and j == 0: # upper-left cell pdf.set_xy(x, y + title) pdf.cell(first_column, first_row, border=1) j += 1 elif i == 0: # top cells with time pdf.set_xy(x + first_row + step_column * (j - 1), y + title) pdf.set_font_size(8) pdf.cell(step_column, first_row, txt=TimePair.time_start_end(j - 1), align="C", border=1) j += 1 elif j == 0: # left cells with days of the week pdf.set_xy( x, y + title + first_column + step_row * (i - 1) + step_row) pdf.rotate(90) pdf.set_font_size(8) pdf.cell(step_row, first_row, txt=str(DaysOfWeek.value_of(i - 1)), align="C", border=1) pdf.rotate(0) j += 1 else: # cells inside the table pdf.set_xy(x + first_row + step_column * (j - 1), y + title + first_column + step_row * (i - 1)) simple_row = (step_row / indexes[i - 1]) prev_x, prev_y = pdf.get_x(), pdf.get_y() start_index = sum(indexes[r] for r in range(i - 1)) number_row = 0 for m in range(start_index, start_index + indexes[i - 1]): item = table_widget.item(m, j - 1) if item is not None: x_span_count = table_widget.columnSpan(m, j - 1) y_span_count = table_widget.rowSpan(m, j - 1) pdf.set_xy(prev_x, prev_y + number_row * simple_row) pdf.cell(step_column * x_span_count, simple_row * y_span_count, border=1) text = item.text() if text != "": draw_text( pdf, x + first_row + step_column * (j - 1), y + title + first_column + step_row * (i - 1) + simple_row * number_row, step_column * x_span_count, simple_row * y_span_count, text) number_row += 1 pdf.set_xy(prev_x, prev_y) j += 1 i += 1 if progress is not None: count += 1 progress.setValue(week_step * count) if progress.wasCanceled(): break pdf.output(file.absoluteFilePath())
class ScheduleEditorWindow(QMainWindow): """ Class describing the main window of the program. """ def __init__(self): super().__init__() self._schedule = Schedule() self._indexes_ref = self._schedule.indexes() self._file = None # window settings self.setWindowTitle(self.tr("Schedule Editor")) self.setMinimumSize(800, 600) # central widget settings self.table_widget = ScheduleTableWidget() self.table_widget.set_schedule(self._schedule) self.table_widget.setVerticalHeader( CustomHeaderView(Qt.Vertical, self._indexes_ref)) self.table_widget.setHorizontalHeader(CustomHeaderView(Qt.Horizontal)) self.table_widget.setWordWrap(True) self.table_widget.setContextMenuPolicy(Qt.CustomContextMenu) self.table_widget.setSelectionMode(QAbstractItemView.SingleSelection) self.table_widget.setEditTriggers(QAbstractItemView.NoEditTriggers) self.table_widget.horizontalHeader().setSectionResizeMode( QHeaderView.Stretch) self.table_widget.horizontalHeader().setMaximumHeight(25) self.table_widget.horizontalHeader().setStretchLastSection(True) self.table_widget.verticalHeader().setSectionResizeMode( QHeaderView.Fixed) self.table_widget.verticalHeader().setMaximumWidth(25) self.table_widget.verticalHeader().setStretchLastSection(True) self.table_widget.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) self.table_widget.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.table_widget.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.table_widget.setColumnCount(self._schedule.columns()) for i in range(8): item = QTableWidgetItem(TimePair.time_start_end(i)) self.table_widget.setHorizontalHeaderItem(i, item) self.table_widget.setRowCount(self._schedule.rows()) for i, day in enumerate(DaysOfWeek.to_list()): item = QTableWidgetItem(day) self.table_widget.setVerticalHeaderItem(i, item) self.setCentralWidget(self.table_widget) # menu bar settings self.menu_bar = self.menuBar() self.menu_file = self.menu_bar.addMenu(self.tr("&File")) self.action_new_file = QAction(QIcon.fromTheme("document-new"), self.tr("&New file"), self) self.action_new_file.setShortcut("Ctrl+N") self.menu_file.addAction(self.action_new_file) self.action_open = QAction(QIcon.fromTheme("document-open"), self.tr("&Open"), self) self.action_open.setShortcut("Ctrl+O") self.menu_file.addAction(self.action_open) self.action_save = QAction(QIcon.fromTheme("document-save"), self.tr("&Save"), self) self.action_save.setShortcut("Ctrl+S") self.menu_file.addAction(self.action_save) self.action_save_as = QAction(QIcon.fromTheme("document-save-as"), self.tr("Save as..."), self) self.action_save_as.setShortcut("Ctrl+S+A") self.menu_file.addAction(self.action_save_as) self.action_export = QAction(self.tr("Export"), self) self.action_export.setShortcut("Ctrl+E") self.menu_file.addAction(self.action_export) self.action_import = QAction(self.tr("Import"), self) self.action_import.setShortcut("Ctrl+I") self.menu_file.addAction(self.action_import) self.menu_file.addSeparator() self.action_settings = QAction(self.tr("Settings"), self) self.menu_file.addAction(self.action_settings) self.action_about = QAction(self.tr("About"), self) self.menu_file.addAction(self.action_about) self.action_exit = QAction(self.tr("&Quit"), self) self.action_exit.setShortcut("Ctrl+Q") self.menu_file.addAction(self.action_exit) # status bar settings self.statusBar().showMessage(self.tr("Ready!")) # connection self.action_new_file.triggered.connect(self.action_new_file_clicked) self.action_open.triggered.connect(self.action_open_clicked) self.action_save.triggered.connect(self.action_save_clicked) self.action_save_as.triggered.connect(self.action_save_as_clicked) self.action_export.triggered.connect(self.action_export_clicked) self.action_import.triggered.connect(self.action_import_clicked) self.action_settings.triggered.connect(self.action_settings_clicked) self.action_about.triggered.connect(self.action_about_clicked) self.action_exit.triggered.connect(self.close) # self.table_widget.clicked.connect(self.test) self.table_widget.doubleClicked.connect(self.cell_clicked) self.table_widget.customContextMenuRequested.connect( self.context_menu_requested) # def test(self) -> None: # """ # Method for tests. # """ # item = self.table_widget.currentItem() # if item is not None: # print(item.row(), # item.column(), # self.table_widget.rowSpan(item.row(), item.column()), # self.table_widget.columnSpan(item.row(), item.column())) def changeEvent(self, event: QEvent) -> None: if event.type() == QEvent.LanguageChange: self.setWindowTitle(self.tr("Schedule Editor")) self.menu_file.setTitle(self.tr("&File")) self.action_new_file.setText(self.tr("&New file")) self.action_open.setText(self.tr("&Open")) self.action_save.setText(self.tr("&Save")) self.action_save_as.setText(self.tr("Save as...")) self.action_export.setText(self.tr("Export")) self.action_import.setText(self.tr("Import")) self.action_settings.setText(self.tr("Settings")) self.action_about.setText(self.tr("About")) self.action_exit.setText(self.tr("&Quit")) for i, day in enumerate(DaysOfWeek.to_list()): item = QTableWidgetItem(day) self.table_widget.setVerticalHeaderItem(i, item) self.table_widget.update_schedule() else: super().changeEvent(event) def resizeEvent(self, event: QResizeEvent) -> None: self.table_widget.resize_table() super().resizeEvent(event) def context_menu_requested(self, pos: QPoint) -> None: """ Create a context menu to edit a table cell. :param pos: Menu call position """ menu = QMenu(self) action_edit = QAction(QIcon.fromTheme("accessories-text-editor"), self.tr("Edit cell"), self) action_edit.triggered.connect(self.cell_clicked) menu.addAction(action_edit) menu.popup(self.table_widget.viewport().mapToGlobal(pos)) def action_new_file_clicked(self) -> bool: """ Slot to handle file save, after changes. If the user has agreed to save / not save, then True is returned, otherwise False. """ if self._schedule.is_change(): answer = QMessageBox.warning( self, self.tr("The document has been modified"), self.tr("Do you want to save the changes you made?\n" "You changes will be lost if you don't save them"), QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel, QMessageBox.Save) if answer == QMessageBox.Save: self.action_save_clicked() elif answer == QMessageBox.Cancel: return False self._schedule.clear() self.table_widget.update_schedule() self.setWindowTitle(self.tr("Schedule Editor")) return True def action_open_clicked(self) -> None: """ Slot to handle file upload. """ if self._file is not None: if not self.action_new_file_clicked(): return path = QFileDialog.getOpenFileName( self, self.tr("Open schedule from JSON file"), ".", "JSON file (*.json)")[0] if path == "": return try: self._schedule.load(path) except AlongTwoPairsException as ex: QMessageBox.critical(self, self.tr("AlongTwoPairsException!"), str(ex)) return except Exception as ex: QMessageBox.critical(self, self.tr("Unknown error!"), str(ex)) return self._file = QFileInfo(path) self.statusBar().showMessage(self.tr("Load file: ") + path, 5000) self.setWindowTitle( self.tr("Schedule Editor [{}]").format( self._file.absoluteFilePath())) self.table_widget.update_schedule() def action_save_clicked(self) -> None: """ Slot to handle file saving. """ if self._file is None: self.action_save_as_clicked() else: self._schedule.save(self._file.absoluteFilePath()) self.setWindowTitle( self.tr("Schedule Editor [{}]").format( self._file.absoluteFilePath())) self.statusBar().showMessage( self.tr("Save file: ") + self._file.absoluteFilePath(), 5000) def action_save_as_clicked(self) -> None: """ Slot to save the file if it has not been saved before. """ path = QFileDialog.getSaveFileName( self, self.tr("Save schedule as JSON file"), "./examples", "JSON file (*.json)")[0] if path == "": return if not path.endswith(".json"): path += ".json" self._schedule.save(path) self._file = QFileInfo(path) self.setWindowTitle( self.tr("Schedule Editor [{}]").format( self._file.absoluteFilePath())) self.statusBar().showMessage( self.tr("Save file: ") + self._file.absoluteFilePath(), 5000) def action_export_clicked(self) -> None: """ Slot for schedule exports to PDF. """ exporter = ExportWindow(self._schedule, self) exporter.exec_() def action_import_clicked(self) -> None: """ Slot for schedule imports from PDF. """ importer = ImportWindow(self) importer.exec_() def action_settings_clicked(self) -> None: """ Slot for calling up the settings window. """ settings = SettingsWindow(self) settings.exec_() self.table_widget.update_schedule() def action_about_clicked(self) -> None: """ Slot display window: "About program". """ QMessageBox.information( self, self.tr("About program"), self.tr(""" <b>Stankin Schedule Editor</b> <p> The project is designed to create a weekly schedule in the form of pdf-files. <p> <p> <b>Author</b>: Nick Vereshchagin<br> <b>GitHub</b>: <a href='https://github.com/Nikololoshka/StankinScheduleEditor'> https://github.com/Nikololoshka/StankinScheduleEditor </a> </p> """)) def cell_clicked(self) -> None: """ Processes the action to change a table cell. """ day = self.table_widget.currentRow() number = self.table_widget.currentColumn() item = self.table_widget.currentItem() if item is not None: duration = item.data(Qt.UserRole) else: duration = self.table_widget.columnSpan(day, number) day, number, duration = self._schedule.normalize_index( day, number, duration) selector = PairSelectorWindow(self._schedule, day, number, duration, self) selector.pairsListChanged.connect(self.table_widget.update_schedule) selector.exec_() self.table_widget.update_schedule()