bleed_generator_docker.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. from krita import DockWidget
  2. from PyQt5.QtWidgets import QWidget, QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QComboBox, QCheckBox, QPushButton
  3. DOCKER_TITLE = 'Bleed Generator'
  4. class BleedGenerator(DockWidget):
  5. def __init__(self):
  6. super().__init__()
  7. self.setWindowTitle(DOCKER_TITLE)
  8. widget = QWidget()
  9. layout = QVBoxLayout()
  10. widget.setLayout(layout)
  11. layout.addSpacing(10)
  12. layout.addWidget(QLabel('Adds bleed to the active paint layer.'))
  13. # Count and unit inputs. Default to 24px (2mm at 300dpi).
  14. self.countInput = QLineEdit('24')
  15. self.unitInput = QComboBox()
  16. self.unitInput.addItems( ['px', 'mm', 'inch'] )
  17. row1 = QHBoxLayout()
  18. row1.addWidget(QLabel('Bleed amount:'))
  19. row1.addWidget(self.countInput)
  20. row1.addWidget(self.unitInput)
  21. layout.addLayout(row1)
  22. # Option to bleed to boundaries.
  23. self.bleedToBoundsCheck = QCheckBox('Bleed to nearest guides/boundaries')
  24. self.bleedToBoundsCheck.setChecked(False)
  25. layout.addWidget(self.bleedToBoundsCheck)
  26. # Offset input i.e. how far from edge to start copying lines.
  27. self.offsetInput = QLineEdit('0')
  28. row2 = QHBoxLayout()
  29. row2.addWidget(QLabel('Offset (0 = use edge):'))
  30. row2.addWidget(self.offsetInput)
  31. row2.addWidget(QLabel('px'))
  32. layout.addLayout(row2)
  33. # Side selection
  34. self.topCheck = QCheckBox('Top')
  35. self.topCheck.setChecked(True)
  36. self.botCheck = QCheckBox('Bottom')
  37. self.botCheck.setChecked(True)
  38. self.leftCheck = QCheckBox('Left')
  39. self.leftCheck.setChecked(True)
  40. self.rightCheck = QCheckBox('Right')
  41. self.rightCheck.setChecked(True)
  42. layout.addWidget(QLabel('Sides to bleed:'))
  43. row3 = QHBoxLayout()
  44. row3.addWidget(self.topCheck)
  45. row3.addWidget(self.botCheck)
  46. row3.addWidget(self.leftCheck)
  47. row3.addWidget(self.rightCheck)
  48. layout.addLayout(row3)
  49. # Button!
  50. layout.addSpacing(20)
  51. goButton = QPushButton("Add Bleed")
  52. goButton.setIcon( Krita.instance().icon('animation_play') )
  53. layout.addWidget(goButton)
  54. # Add a stretch to prevent the rest of the content from stretching.
  55. layout.addStretch()
  56. # Add widget to the docker.
  57. self.setWidget(widget)
  58. # Hook up the action to the button.
  59. goButton.clicked.connect( self.generateBleed )
  60. # notifies when views are added or removed
  61. # 'pass' means do not do anything
  62. def canvasChanged(self, canvas):
  63. pass
  64. ##########
  65. # Slots
  66. ##########
  67. # Actually generates the bleed.
  68. def generateBleed(self, e):
  69. # Get the current layer
  70. self.doc = Krita.instance().activeDocument()
  71. self.layer = self.doc.activeNode()
  72. if self.layer.type() != 'paintlayer':
  73. dialog = QDialog()
  74. dialog.setWindowTitle("Paint Layer Required")
  75. layout = QVBoxLayout()
  76. layout.addWidget(QLabel('Bleed generator only works on paint layers. Please select one.'))
  77. dialog.setLayout(layout)
  78. dialog.exec_()
  79. return
  80. self.offset = round(float(self.offsetInput.text()))
  81. # Calculate how many lines of pixels to copy for each side
  82. amounts = self.calculateBleedAmounts()
  83. # Copy lines for selected sides.
  84. # Bounds are fetched each time as layer dimensions change.
  85. if self.topCheck.checkState():
  86. bds = self.layer.bounds()
  87. xpos = bds.left()
  88. ypos = bds.top() - self.offset
  89. len = bds.width()
  90. bleedline = self.layer.pixelData(xpos, ypos, len, 1)
  91. for c in range(1, amounts['top']+1):
  92. self.layer.setPixelData(bleedline, xpos, (ypos - c), len, 1)
  93. if self.botCheck.checkState():
  94. bds = self.layer.bounds()
  95. xpos = bds.left()
  96. ypos = bds.bottom() + self.offset
  97. len = bds.width()
  98. bleedline = self.layer.pixelData(xpos, ypos, len, 1)
  99. for c in range(1, amounts['bottom']+1):
  100. self.layer.setPixelData(bleedline, xpos, (ypos + c), len, 1)
  101. if self.leftCheck.checkState():
  102. bds = self.layer.bounds()
  103. xpos = bds.left() - self.offset
  104. ypos = bds.top()
  105. len = bds.height()
  106. bleedline = self.layer.pixelData(xpos, ypos, 1, len)
  107. for c in range(1, amounts['left']+1):
  108. self.layer.setPixelData(bleedline, (xpos - c), ypos, 1, len)
  109. if self.rightCheck.checkState():
  110. bds = self.layer.bounds()
  111. xpos = bds.right() + self.offset
  112. ypos = bds.top()
  113. len = bds.height()
  114. bleedline = self.layer.pixelData(xpos, ypos, 1, len)
  115. for c in range(1, amounts['right']+1):
  116. self.layer.setPixelData(bleedline, (xpos + c), ypos, 1, len)
  117. # Refresh the view, or the cloned pixels will not be immediately shown.
  118. self.doc.refreshProjection()
  119. def calculateBleedAmounts(self):
  120. amounts = {}
  121. if self.bleedToBoundsCheck.checkState():
  122. # Bleed amounts determined from boundaries.
  123. # Get all boundaries.
  124. vPoints = self.doc.horizontalGuides()
  125. hPoints = self.doc.verticalGuides()
  126. vPoints.insert(0, 0)
  127. vPoints.append(self.doc.height())
  128. vPoints.sort()
  129. hPoints.insert(0, 0)
  130. hPoints.append(self.doc.width())
  131. hPoints.sort()
  132. bds = self.layer.bounds()
  133. # Calculate bleed from layer's edge to next relevant boundary.
  134. prevV = 0
  135. top = bds.top()
  136. bottom = bds.bottom() + 1
  137. for v in vPoints:
  138. v = round(v)
  139. if 'top' not in amounts and v >= top:
  140. amounts['top'] = top - prevV
  141. if 'bottom' not in amounts and v > bottom:
  142. amounts['bottom'] = v - bottom
  143. break
  144. prevV = v
  145. prevH = 0
  146. left = bds.left()
  147. right = bds.right() + 1
  148. for h in hPoints:
  149. h = round(h)
  150. if 'left' not in amounts and h >= left:
  151. amounts['left'] = left - prevH
  152. if 'right' not in amounts and h > right:
  153. amounts['right'] = h - right
  154. prevH = h
  155. else:
  156. # Bleed amounts based on the user's input.
  157. unit = self.unitInput.currentIndex()
  158. count = float(self.countInput.text())
  159. ppi = self.doc.resolution()
  160. if unit == 1: # mm
  161. count = count * (ppi / 24.5)
  162. if unit == 2: # inch
  163. count = count * ppi
  164. count = round(count) - self.offset
  165. amounts['top'] = count
  166. amounts['bottom'] = count
  167. amounts['left'] = count
  168. amounts['right'] = count
  169. return amounts