from krita import DockWidget from PyQt5.QtWidgets import QWidget, QDialog, QVBoxLayout, QHBoxLayout, QLabel, QComboBox, QPushButton from PyQt5.QtCore import QByteArray DOCKER_TITLE = 'Button Shy PNP Expander' class BsPnpExpander(DockWidget): def __init__(self): super().__init__() self.setWindowTitle(DOCKER_TITLE) widget = QWidget() layout = QVBoxLayout() widget.setLayout(layout) layout.addSpacing(10) layout.addWidget(QLabel('Expand the Button Shy 6-card PNP layout to 8-cards')) # Expand Direction. Do we put the 2 new cards on the left or right? self.expandDir = QComboBox() dirs = ['Right', 'Left'] self.expandDir.addItems(dirs) row0 = QHBoxLayout() row0.addWidget(QLabel('Expand Direction:')) row0.addWidget(self.expandDir) layout.addLayout(row0) # Button (Shy)! layout.addSpacing(20) goButton = QPushButton("Expand") goButton.setIcon( Krita.instance().icon('animation_play') ) layout.addWidget(goButton) # Add a stretch to prevent the rest of the content from stretching. layout.addStretch() # Add widget to the docker. self.setWidget(widget) # Hook up the action to the button. goButton.clicked.connect( self.expandBsPnp ) # notifies when views are added or removed # 'pass' means do not do anything def canvasChanged(self, canvas): pass ########## # Slots ########## # Actually expands the PNP. def expandBsPnp(self, e): # dialog = QDialog() # dialog.setWindowTitle("DEBUG") # layout = QVBoxLayout() # dialog.setLayout(layout) # Get the current layer. doc = Krita.instance().activeDocument() layer = doc.activeNode() if layer.type() != 'paintlayer': dialog = QDialog() dialog.setWindowTitle("Paint Layer Required") layout = QVBoxLayout() layout.addWidget(QLabel('Expander only works on paint layers. Please select one.')) dialog.setLayout(layout) dialog.exec_() return direction = self.expandDir.currentText() layerPos = layer.position() # Move layer 411 pixels left or right xDiff = 411 if direction == 'Left' else -411 layer.move(layerPos.x() + xDiff, layerPos.y()) # Fill in empty document area with white overlapHCrop = 341 # x-offset to overwrite old horizontal crop marks. whitePixel = b'\xff\xff\xff\xff' bds = layer.bounds() fillX = 0 if direction == 'Left' else bds.right() - overlapHCrop fillY = 0 fillWidth = bds.left() + overlapHCrop if direction == 'Left' else doc.width() - fillX fillHeight = doc.height() whiteFill = QByteArray(whitePixel * fillWidth * fillHeight) layer.setPixelData(whiteFill, fillX, fillY, fillWidth, fillHeight) # Create horizontal cropmarks at image edges. # positions 106 1153 1228 2276. 61px long. blackPixel = b'\x00\x00\x00\xff' cropMarkWidth = 61 cropMarkHeight = 2 cropMark = QByteArray(blackPixel * cropMarkWidth * cropMarkHeight) ySet = [106, 1153, 1228, 2276] for x in [0, doc.width() - cropMarkWidth]: for y in ySet: layer.setPixelData(cropMark, x, y, cropMarkWidth, cropMarkHeight) # Create vertical cropmarks for new card spaces # positions 96 848 2562 3314. 37px long. cropMarkWidth = 2 cropMarkHeight = 37 cropMark = QByteArray(blackPixel * cropMarkWidth * cropMarkHeight) xSet = [96, 868] if direction == 'Left' else [2562, 3314] for y in [0, doc.height() - cropMarkHeight]: for x in xSet: layer.setPixelData(cropMark, x, y, cropMarkWidth, cropMarkHeight) # Place guides on crop marks for new card spaces vList = doc.verticalGuides() hList = doc.horizontalGuides() vList.clear() hList.clear() for x in xSet: vList.append(x + 1) for y in ySet: hList.append(y + 1) doc.setVerticalGuides(vList) doc.setHorizontalGuides(hList) doc.setGuidesLocked(True) doc.setGuidesVisible(True) # Refresh the view, or the moved will not be immediately reflected. doc.refreshProjection()