bs_pnp_expander_docker.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from krita import DockWidget
  2. from PyQt5.QtWidgets import QWidget, QDialog, QVBoxLayout, QHBoxLayout, QLabel, QComboBox, QPushButton
  3. from PyQt5.QtCore import QByteArray
  4. DOCKER_TITLE = 'Button Shy PNP Expander'
  5. class BsPnpExpander(DockWidget):
  6. def __init__(self):
  7. super().__init__()
  8. self.setWindowTitle(DOCKER_TITLE)
  9. widget = QWidget()
  10. layout = QVBoxLayout()
  11. widget.setLayout(layout)
  12. layout.addSpacing(10)
  13. layout.addWidget(QLabel('Expand the Button Shy 6-card PNP layout to 8-cards'))
  14. # Expand Direction. Do we put the 2 new cards on the left or right?
  15. self.expandDir = QComboBox()
  16. dirs = ['Right', 'Left']
  17. self.expandDir.addItems(dirs)
  18. row0 = QHBoxLayout()
  19. row0.addWidget(QLabel('Expand Direction:'))
  20. row0.addWidget(self.expandDir)
  21. layout.addLayout(row0)
  22. # Button (Shy)!
  23. layout.addSpacing(20)
  24. goButton = QPushButton("Expand")
  25. goButton.setIcon( Krita.instance().icon('animation_play') )
  26. layout.addWidget(goButton)
  27. # Add a stretch to prevent the rest of the content from stretching.
  28. layout.addStretch()
  29. # Add widget to the docker.
  30. self.setWidget(widget)
  31. # Hook up the action to the button.
  32. goButton.clicked.connect( self.expandBsPnp )
  33. # notifies when views are added or removed
  34. # 'pass' means do not do anything
  35. def canvasChanged(self, canvas):
  36. pass
  37. ##########
  38. # Slots
  39. ##########
  40. # Actually expands the PNP.
  41. def expandBsPnp(self, e):
  42. # dialog = QDialog()
  43. # dialog.setWindowTitle("DEBUG")
  44. # layout = QVBoxLayout()
  45. # dialog.setLayout(layout)
  46. # Get the current layer.
  47. doc = Krita.instance().activeDocument()
  48. layer = doc.activeNode()
  49. if layer.type() != 'paintlayer':
  50. dialog = QDialog()
  51. dialog.setWindowTitle("Paint Layer Required")
  52. layout = QVBoxLayout()
  53. layout.addWidget(QLabel('Expander only works on paint layers. Please select one.'))
  54. dialog.setLayout(layout)
  55. dialog.exec_()
  56. return
  57. direction = self.expandDir.currentText()
  58. layerPos = layer.position()
  59. # Move layer 411 pixels left or right
  60. xDiff = 411 if direction == 'Left' else -411
  61. layer.move(layerPos.x() + xDiff, layerPos.y())
  62. # Fill in empty document area with white
  63. overlapHCrop = 341 # x-offset to overwrite old horizontal crop marks.
  64. whitePixel = b'\xff\xff\xff\xff'
  65. bds = layer.bounds()
  66. fillX = 0 if direction == 'Left' else bds.right() - overlapHCrop
  67. fillY = 0
  68. fillWidth = bds.left() + overlapHCrop if direction == 'Left' else doc.width() - fillX
  69. fillHeight = doc.height()
  70. whiteFill = QByteArray(whitePixel * fillWidth * fillHeight)
  71. layer.setPixelData(whiteFill, fillX, fillY, fillWidth, fillHeight)
  72. # Create horizontal cropmarks at image edges.
  73. # positions 106 1153 1228 2276. 61px long.
  74. blackPixel = b'\x00\x00\x00\xff'
  75. cropMarkWidth = 61
  76. cropMarkHeight = 2
  77. cropMark = QByteArray(blackPixel * cropMarkWidth * cropMarkHeight)
  78. ySet = [106, 1153, 1228, 2276]
  79. for x in [0, doc.width() - cropMarkWidth]:
  80. for y in ySet:
  81. layer.setPixelData(cropMark, x, y, cropMarkWidth, cropMarkHeight)
  82. # Create vertical cropmarks for new card spaces
  83. # positions 96 848 2562 3314. 37px long.
  84. cropMarkWidth = 2
  85. cropMarkHeight = 37
  86. cropMark = QByteArray(blackPixel * cropMarkWidth * cropMarkHeight)
  87. xSet = [96, 868] if direction == 'Left' else [2562, 3314]
  88. for y in [0, doc.height() - cropMarkHeight]:
  89. for x in xSet:
  90. layer.setPixelData(cropMark, x, y, cropMarkWidth, cropMarkHeight)
  91. # Place guides on crop marks for new card spaces
  92. vList = doc.verticalGuides()
  93. hList = doc.horizontalGuides()
  94. vList.clear()
  95. hList.clear()
  96. for x in xSet:
  97. vList.append(x + 1)
  98. for y in ySet:
  99. hList.append(y + 1)
  100. doc.setVerticalGuides(vList)
  101. doc.setHorizontalGuides(hList)
  102. doc.setGuidesLocked(True)
  103. doc.setGuidesVisible(True)
  104. # Refresh the view, or the moved will not be immediately reflected.
  105. doc.refreshProjection()