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): length_text = self.lengthInput.text() width_text = self.widthInput.text() height_text = self.heightInput.text() # Box dimensions in pixels. doc_ppi = self.doc.resolution() mm = doc_ppi/25.4 multiplier = 1 if self.unitInput.currentText() == "inch": multiplier = doc_ppi if self.unitInput.currentText() == "mm": multiplier = mm length = float(length_text) * multiplier height = float(height_text) * multiplier width = float(width_text) * multiplier layer = self.doc.createVectorLayer(f"""Box {length_text}x{width_text}x{height_text}""") root = self.doc.rootNode() root.addChildNode(layer, None) # Create outline using SVG (starts from top left corner). if self.boxTypeInput.currentIndex() == 0: # Tuckbox stringy = f""" """ else: # Telescopic total_length = length + 5 * height total_width = width + 5 * height stringy = f""" """ layer.addShapesFromSvg(stringy)