from krita import * from PyQt5.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QComboBox, QCheckBox, QPushButton from PyQt5.QtCore import * class BoxGenerator(Extension): def __init__(self, parent): super().__init__(parent) # Krita.instance() exists, so do any setup work def setup(self): pass # called after setup(self) def createActions(self, window): # Create menu item in Tools > Scripts. action = window.createAction("boxgen", "Box Generator") action.triggered.connect(self.box_generator) def box_generator(self): # Get the current selected layer, called a 'node' self.doc = Krita.instance().activeDocument() if not self.doc: # Create and show a new document (A4 300ppi). self.doc = Krita.instance().createDocument(3508, 2480, "", "RGBA", "U8", "", 300.0) Krita.instance().activeWindow().addView(self.doc) # Dialog creation. newDialog = QDialog() newDialog.setWindowTitle("Box Generator") # Description row. desc = QLabel('Creates outlines for a foldable box with lid.') desc.setAlignment(Qt.AlignCenter) row0 = QHBoxLayout() row0.addWidget(desc) # Dimension row. self.lengthInput = QLineEdit('67') self.widthInput = QLineEdit('93') self.heightInput = QLineEdit('20') self.unitInput = QComboBox() self.unitInput.addItems( ['mm', 'inch', 'px'] ) row1 = QHBoxLayout() row1.addWidget(QLabel('Dimensions: L:')) row1.addWidget(self.lengthInput) row1.addWidget(QLabel(' x W:')) row1.addWidget(self.widthInput) row1.addWidget(QLabel(' x H:')) row1.addWidget(self.heightInput) row1.addWidget(self.unitInput) # Decision row. self.boxTypeInput = QComboBox() self.boxTypeInput.addItems( ['Tuckbox', 'Telescopic'] ) row2 = QHBoxLayout() row2.addWidget(QLabel('Box Type:')) row2.addWidget(self.boxTypeInput) # Do It row. goButton = QPushButton("Create Box") goButton.setIcon( Krita.instance().icon('animation_play') ) row3 = QHBoxLayout() row3.addWidget(goButton) layoutMain = QVBoxLayout() layoutMain.addLayout(row0) layoutMain.addLayout(row1) layoutMain.addLayout(row2) layoutMain.addLayout(row3) newDialog.setLayout(layoutMain) # hook up the actions. goButton.clicked.connect( self.generateBox ) # self.unitInput.currentIndexChanged.connect( self.convertCount ) # show the dialog. newDialog.exec_() ########## # Slots ########## # Actually generates the box! def generateBox(self, e): layer = self.doc.createVectorLayer("Box") root = self.doc.rootNode() root.addChildNode(layer, None) # Get dimensions in points. Points to mm conversion depends on document DPI. length_mm = float(self.lengthInput.text()) width_mm = float(self.widthInput.text()) height_mm = float(self.heightInput.text()) # Assume input is in mm. pts_per_mm = 300 / 25.4 length = length_mm * pts_per_mm height = height_mm * pts_per_mm width = width_mm * pts_per_mm # Create outline using SVG. if self.boxTypeInput.currentIndex() == 0: # Tuckbox total_length = length * 2 + height * 3 total_width = width + height * 3 leeway = pts_per_mm / 2 # Make certain flaps shorter by 0.5mm to account for paper thickness. stringy = f""" """ else: # Telescopic total_length = length + 5 * height total_width = width + 5 * height stringy = f""" """ layer.addShapesFromSvg(stringy)