def export_to_pdf(self) -> None:
        # select path

        paths = []

        if self.check_box_use_current.isChecked():
            path = QFileDialog.getSaveFileName(self, self.tr("Export to pdf"),
                                               ".", "PDF file (*.pdf)")[0]
            if path == "":
                return

            if not path.endswith(".pdf"):
                path += ".pdf"

            paths.append(path)
        else:
            for index in range(self.list_widget.count()):
                path = self.list_widget.item(index).data(Qt.UserRole)
                paths.append(path)

        # progress dialog
        progress = QProgressDialog(self.tr("Export to pdf"),
                                   self.tr("Abort exports"), 0, 100, self)
        progress.setWindowModality(Qt.WindowModal)
        progress.setMinimumDuration(2000)

        try:
            for index, path in enumerate(paths):

                if self.check_box_use_current.isChecked():
                    title_text = self.line_edit_title.text()
                    schedule = self._schedule_ref
                else:
                    title_text = QFileInfo(path).baseName()
                    schedule = Schedule()
                    schedule.load(path)
                    path = path[0:-5] + ".pdf"

                print(path)
                mode = self.combo_box_work_mode.currentIndex()
                if mode == 0:
                    export_weeks_to_pdf(
                        schedule, title_text,
                        self.check_box_add_date.isChecked(), path,
                        self.combo_box_font.currentText(),
                        self.combo_box_font.currentData(Qt.UserRole),
                        self.combo_box_encoding.currentText(),
                        self.date_edit_start.date().toPyDate(),
                        self.date_edit_end.date().toPyDate(),
                        self.combo_box_color_a.currentData(Qt.UserRole),
                        self.combo_box_color_b.currentData(Qt.UserRole),
                        progress
                        if self.check_box_use_current.isChecked() else None)
                else:
                    export_full_to_pdf(
                        schedule, title_text, path,
                        self.combo_box_font.currentText(),
                        self.combo_box_font.currentData(Qt.UserRole),
                        self.combo_box_encoding.currentText(), progress
                        if self.check_box_use_current.isChecked() else None)

                progress.setValue(int(index * 100 / len(paths)))

            # finish dialog
            progress.setValue(100)
            finish_msg_box = QMessageBox(QMessageBox.Information,
                                         self.tr("Export to pdf"),
                                         self.tr("Gone!"))
            open_folder_button = finish_msg_box.addButton(
                self.tr("Open folder"), QMessageBox.ActionRole)
            finish_msg_box.addButton(QMessageBox.Ok)
            finish_msg_box.exec_()

            if finish_msg_box.clickedButton() == open_folder_button:
                QDesktopServices.openUrl(
                    QUrl(
                        QFileInfo(paths[0] if len(paths) != 0 else ".").
                        absolutePath()))

        except UnicodeEncodeError as ex:
            QMessageBox.critical(self, self.tr("Encoding error"), str(ex))
        except Exception as ex:
            QMessageBox.critical(self, self.tr("Unknown error"), str(ex))
        progress.setValue(100)
Пример #2
0
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()