stamp_settings.md

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

記事に戻る

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

class StampSettings(QtWidgets.QGroupBox):
    changed = QtCore.pyqtSignal()
    def __init__(self):
        super().__init__()
        self.setTitle('スタンプ設定')
        
        self.radius = QtWidgets.QSpinBox()
        self.radius.setValue(20)

        self.line_width = QtWidgets.QSpinBox()
        self.line_width.setValue(2)

        self.str_u = QtWidgets.QPlainTextEdit('承認')
        self.str_u.setMaximumWidth(120)
        self.str_u.setMaximumHeight(45)

        t = datetime.now()
        now_text = '{}.{:02d}.{:02d}'.format(str(t.year)[2:], t.month, t.day)
        self.str_m = QtWidgets.QLineEdit(now_text)
        self.str_m.setMaximumWidth(120)

        self.str_b = QtWidgets.QPlainTextEdit('名前')
        self.str_b.setMaximumWidth(120)
        self.str_b.setMaximumHeight(45)

        self.fontsize_u = QtWidgets.QDoubleSpinBox()
        self.fontsize_u.setMaximumWidth(120)
        self.fontsize_u.setMaximumHeight(45)
        self.fontsize_u.setValue(9)

        self.fontsize_m = QtWidgets.QDoubleSpinBox()
        self.fontsize_m.setMaximumWidth(120)
        self.fontsize_m.setValue(9)

        self.fontsize_b = QtWidgets.QDoubleSpinBox()
        self.fontsize_b.setMaximumWidth(120)
        self.fontsize_b.setMaximumHeight(45)
        self.fontsize_b.setValue(8)

        label0 = QtWidgets.QLabel('半径')
        label1 = QtWidgets.QLabel('線の太さ')
        label2 = QtWidgets.QLabel('上段')
        label3 = QtWidgets.QLabel('中段')
        label4 = QtWidgets.QLabel('下段')
        label5 = QtWidgets.QLabel('文字')
        label6 = QtWidgets.QLabel('フォントサイズ')

        self.setLayout( QtWidgets.QGridLayout() )
        self.layout().addWidget(label0,          0, 0, 1, 1)
        self.layout().addWidget(self.radius,     0, 1, 1, 2)

        self.layout().addWidget(label1,          1, 0, 1, 1)
        self.layout().addWidget(self.line_width, 1, 1, 1, 2)

        self.layout().addWidget(label2,          3, 0, 1, 1)
        self.layout().addWidget(label3,          4, 0, 1, 1)
        self.layout().addWidget(label4,          5, 0, 1, 1)

        self.layout().addWidget(label5,          2, 1, 1, 1)
        self.layout().addWidget(self.str_u,      3, 1, 1, 1)
        self.layout().addWidget(self.str_m,      4, 1, 1, 1)
        self.layout().addWidget(self.str_b,      5, 1, 1, 1)

        self.layout().addWidget(label6,          2, 2, 1, 1)
        self.layout().addWidget(self.fontsize_u, 3, 2, 1, 1)
        self.layout().addWidget(self.fontsize_m, 4, 2, 1, 1)
        self.layout().addWidget(self.fontsize_b, 5, 2, 1, 1)

        self.setMaximumSize(235, 235)
        self.setMinimumSize(235, 235)

        self.radius.valueChanged.connect(lambda : self.changed.emit())
        self.line_width.valueChanged.connect(lambda : self.changed.emit())
        self.str_u.textChanged.connect(lambda : self.changed.emit())
        self.str_m.textChanged.connect(lambda : self.changed.emit())
        self.str_b.textChanged.connect(lambda : self.changed.emit())
        self.fontsize_u.valueChanged.connect(lambda : self.changed.emit())
        self.fontsize_m.valueChanged.connect(lambda : self.changed.emit())
        self.fontsize_b.valueChanged.connect(lambda : self.changed.emit())

    def to_dict(self) -> dict:
        output = {
            'radius' : self.radius.value(),
            'line_width' : self.line_width.value(),
            'str_u' : self.str_u.toPlainText(),
            'str_m' : self.str_m.text(),
            'str_b' : self.str_b.toPlainText(),
            'fontsize_u' : self.fontsize_u.value(),
            'fontsize_m' : self.fontsize_m.value(),
            'fontsize_b' : self.fontsize_b.value()
        }
        return output

    def from_dict(self, data: dict):
        self.radius.setValue(data['radius'])
        self.line_width.setValue(data['line_width'])
        self.str_u.setPlainText(data['str_u'])
        self.str_m.setText(data['str_m'])
        self.str_b.setPlainText(data['str_b'])
        self.fontsize_u.setValue(data['fontsize_u'])
        self.fontsize_m.setValue(data['fontsize_m'])
        self.fontsize_b.setValue(data['fontsize_b'])

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = StampSettings()
    window.show()
    app.exec()

Share post

Related Posts

コメント