files.md

2023/10/08 categories:Code| tags:Code|

記事に戻る

# -*- coding: utf-8 -*-
from PyQt6 import QtCore, QtGui, QtWidgets
from pathlib import Path

class Files(QtWidgets.QGroupBox):
    pdf_double_clicked = QtCore.pyqtSignal(Path)
    result_double_clicked = QtCore.pyqtSignal(int)
    def __init__(self):
        super().__init__()
        self.setTitle('ファイル')
        self.stamp_all = QtWidgets.QPushButton('一括スタンプ')
        self.clear_files = QtWidgets.QPushButton('ファイルクリア')
        self.model = QtGui.QStandardItemModel()
        self.delegate = Delegate()
        self.table = QtWidgets.QTableView()
        self.table.setModel(self.model)
        self.table.setItemDelegate(self.delegate)

        w0 = QtWidgets.QWidget()
        w0.setLayout( QtWidgets.QHBoxLayout() )
        w0.layout().addWidget( QtWidgets.QLabel('ファイルをDDして「一括スタンプ」をクリック') )
        w0.layout().addWidget(self.stamp_all)
        w0.layout().addWidget(self.clear_files)
        w0.layout().setContentsMargins(2, 2, 2, 2)
        self.setLayout( QtWidgets.QVBoxLayout() )
        self.layout().addWidget(w0)
        self.layout().addWidget(QtWidgets.QLabel('ファイル名のセルをダブルクリックしてPDFをプレビュー'))
        self.layout().addWidget(self.table)

        self.model.setHorizontalHeaderLabels((['処理結果', '用紙', 'ファイル名']))
        self.table.horizontalHeader().setStretchLastSection(True)
        self.table.setColumnWidth(0, 60)
        self.table.setColumnWidth(1, 30)

        self.delegate.pdf_double_clicked.connect(lambda pdf_path: self.pdf_double_clicked.emit(pdf_path))
        self.delegate.result_double_clicked.connect(lambda row: self.result_double_clicked.emit(row))

    def append(self, paper_size: str, p:Path):
        root = self.model.invisibleRootItem()
        c0 = QtGui.QStandardItem()
        c0.setData(Exception())
        c1 = QtGui.QStandardItem(paper_size)
        c2 = QtGui.QStandardItem(p.name)
        c2.setData(p)
        root.appendRow([c0, c1, c2])

    def paths(self) -> list[Path]:
        paths = []
        for r in range(self.model.rowCount()):
            item = self.model.item(r, 2)
            path = item.data()
            paths.append(path)
        return paths

    def set_result(self, row: int, text: str, error: Exception):
        item = self.model.item(row, 0)
        item.setText(text)
        item.setData(error)

    def set_current(self, row, column):
        index = self.model.index(row, column)
        self.table.setCurrentIndex(index)
        
class Delegate(QtWidgets.QStyledItemDelegate):
    pdf_double_clicked = QtCore.pyqtSignal(Path)
    result_double_clicked = QtCore.pyqtSignal(int)
    def createEditor(self, parent: QtWidgets.QWidget, option: QtWidgets.QStyleOptionViewItem, index: QtCore.QModelIndex) -> QtWidgets.QWidget:
        if index.column() == 0:
            self.result_double_clicked.emit( index.row() )
            return None
        if index.column() == 1:
            return None
        if index.column() == 2:
            item = index.model().item(index.row(), 2)
            path = item.data()
            self.pdf_double_clicked.emit(path)
            return None
        return super().createEditor(parent, option, index)
        
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Files()
    window.append('A3', Path('Y18801AU0Y04_rev2.pdf'))
    window.append('A3', Path('Y18801AU0Y11_rev1.pdf'))
    window.show()
    app.exec()

Share post

Related Posts

コメント