coordinates.md

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

記事に戻る

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

class Coordinates(QtWidgets.QGroupBox):
    changed = QtCore.pyqtSignal(QtCore.QModelIndex, QtCore.QModelIndex, list)
    def __init__(self):
        super().__init__()
        self.setTitle('用紙サイズ別スタンプ座標')
        self.model = QtGui.QStandardItemModel()
        self.table = QtWidgets.QTableView()
        self.table.setModel(self.model)
        self.table.setItemDelegate(Delegate())
        self.setLayout( QtWidgets.QHBoxLayout() )
        self.layout().addWidget(self.table)

        self.model.setHorizontalHeaderLabels(['原点', '用紙', 'X', 'Y'])
        self.model.insertRows(0, 5)
        self.table.setColumnWidth(0, 50)
        self.table.setColumnWidth(1, 50)

        defoult_data = [
            ['右下', 'A0', '20', '18.5'],
            ['右下', 'A1', '20', '18.5'],
            ['右下', 'A2', '20', '18.5'],
            ['右下', 'A3', '20', '18.5'],
            ['右下', 'A4', '20', '18.5']
        ]

        for r, r_data in enumerate(defoult_data):
            for c, c_data in enumerate(r_data):
                index = self.model.index(r, c)
                self.model.setData(index, c_data)
                
        self.model.dataChanged.connect(lambda topLeft, bottomRight, roles: self.changed.emit(topLeft, bottomRight, roles))
        
    def values(self, paper_size: str) -> tuple[str, float, float]:
        m = self.model

        papers = [ m.data(m.index(r, 1)) for r in range(m.rowCount())]
        if not paper_size in papers:
            return ('', -1.0, -1.0)
        
        row = papers.index(paper_size)
        origin = m.data(m.index(row, 0))
        x = float( m.data(m.index(row, 2)) )
        y = float( m.data(m.index(row, 3)) )
        return (origin, x, y)

    def to_list(self) -> list:
        output = []
        for row in range(self.model.rowCount()):
            row_data = []
            for column in range(self.model.columnCount()):
                index = self.model.index(row, column)
                data = self.model.data(index)
                row_data.append(data)
            output.append(row_data)
        return output

    def from_list(self, datas: list):
        for row_data in datas:
            items = []
            for data in row_data:
                item = QtGui.QStandardItem(str(data))
                items.append(item)
            self.model.appendRow(items)

class Delegate(QtWidgets.QStyledItemDelegate):
    def createEditor(self, parent: QtWidgets.QWidget, option: QtWidgets.QStyleOptionViewItem, index: QtCore.QModelIndex) -> QtWidgets.QWidget:
        if index.column() == 0:
            c = QtWidgets.QComboBox(parent)
            c.addItems([
                '左上', '左下', '右上', '右下'
            ])
            return c
        if index.column() == 1:
            c = QtWidgets.QComboBox(parent)
            c.addItems(['A{}'.format(i) for i in range(5)])
            return c
        return super().createEditor(parent, option, index)
        
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Coordinates()
    window.show()
    app.exec()

Share post

Related Posts

コメント