box_generator_extension.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from krita import *
  2. from PyQt5.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QComboBox, QCheckBox, QPushButton
  3. from PyQt5.QtCore import *
  4. class BoxGenerator(Extension):
  5. def __init__(self, parent):
  6. super().__init__(parent)
  7. # Krita.instance() exists, so do any setup work
  8. def setup(self):
  9. pass
  10. # called after setup(self)
  11. def createActions(self, window):
  12. # Create menu item in Tools > Scripts.
  13. action = window.createAction("boxgen", "Box Generator")
  14. action.triggered.connect(self.box_generator)
  15. def box_generator(self):
  16. # Get the current selected layer, called a 'node'
  17. self.doc = Krita.instance().activeDocument()
  18. if not self.doc:
  19. # Create and show a new document (A4 300ppi).
  20. self.doc = Krita.instance().createDocument(3508, 2480, "", "RGBA", "U8", "", 300.0)
  21. Krita.instance().activeWindow().addView(self.doc)
  22. # Dialog creation.
  23. newDialog = QDialog()
  24. newDialog.setWindowTitle("Box Generator")
  25. # Description row.
  26. desc = QLabel('Creates outlines for a foldable box with lid.')
  27. desc.setAlignment(Qt.AlignCenter)
  28. row0 = QHBoxLayout()
  29. row0.addWidget(desc)
  30. # Dimension row.
  31. self.lengthInput = QLineEdit('95')
  32. self.widthInput = QLineEdit('70')
  33. self.heightInput = QLineEdit('20')
  34. self.unitInput = QComboBox()
  35. self.unitInput.addItems( ['mm', 'inch', 'px'] )
  36. row1 = QHBoxLayout()
  37. row1.addWidget(QLabel('Dimensions: L:'))
  38. row1.addWidget(self.lengthInput)
  39. row1.addWidget(QLabel(' x W:'))
  40. row1.addWidget(self.widthInput)
  41. row1.addWidget(QLabel(' x H:'))
  42. row1.addWidget(self.heightInput)
  43. row1.addWidget(self.unitInput)
  44. # Decision row.
  45. self.flapCheck = QCheckBox('Flap')
  46. self.flapCheck.setChecked(True)
  47. row2 = QHBoxLayout()
  48. row2.addWidget(self.flapCheck)
  49. # Do It row.
  50. goButton = QPushButton("Create Box")
  51. goButton.setIcon( Krita.instance().icon('animation_play') )
  52. row3 = QHBoxLayout()
  53. row3.addWidget(goButton)
  54. layoutMain = QVBoxLayout()
  55. layoutMain.addLayout(row0)
  56. layoutMain.addLayout(row1)
  57. layoutMain.addLayout(row2)
  58. layoutMain.addLayout(row3)
  59. newDialog.setLayout(layoutMain)
  60. # hook up the actions.
  61. # goButton.clicked.connect( self.generateBox )
  62. # self.unitInput.currentIndexChanged.connect( self.convertCount )
  63. # show the dialog.
  64. newDialog.exec_()
  65. ##########
  66. # Slots
  67. ##########
  68. # Actually generates the box!
  69. def generateBox(self, e):
  70. # Calculate how many lines of pixels to copy.
  71. unit = self.unitInput.currentIndex()
  72. count = float(self.countInput.text())
  73. ppi = self.doc.resolution()
  74. if unit == 0: # mm
  75. count = count * (ppi / 24.5)
  76. if unit == 1: # inch
  77. count = count * ppi
  78. offset = round(float(self.offsetInput.text()))
  79. count = round(count) - offset
  80. print("Pixel bleed amount: ", count)
  81. print("Edge offset: ", offset)
  82. # Copy lines for selected sides.
  83. if self.topCheck.checkState():
  84. bds = self.layer.bounds()
  85. xpos = bds.left()
  86. ypos = bds.top() - offset
  87. len = bds.width()
  88. bleedline = self.layer.pixelData(xpos, ypos, len, 1)
  89. for c in range(1, count+1):
  90. self.layer.setPixelData(bleedline, xpos, (ypos - c), len, 1)
  91. print("Top lines cloned: ", count)
  92. if self.botCheck.checkState():
  93. bds = self.layer.bounds()
  94. xpos = bds.left()
  95. ypos = bds.bottom() + offset
  96. len = bds.width()
  97. bleedline = self.layer.pixelData(xpos, ypos, len, 1)
  98. for c in range(1, count+1):
  99. self.layer.setPixelData(bleedline, xpos, (ypos + c), len, 1)
  100. print("Bottom lines cloned: ", count)
  101. if self.leftCheck.checkState():
  102. bds = self.layer.bounds()
  103. xpos = bds.left() - offset
  104. ypos = bds.top()
  105. len = bds.height()
  106. bleedline = self.layer.pixelData(xpos, ypos, 1, len)
  107. for c in range(1, count+1):
  108. self.layer.setPixelData(bleedline, (xpos - c), ypos, 1, len)
  109. print("Left lines cloned: ", count)
  110. if self.rightCheck.checkState():
  111. bds = self.layer.bounds()
  112. xpos = bds.right() + offset
  113. ypos = bds.top()
  114. len = bds.height()
  115. bleedline = self.layer.pixelData(xpos, ypos, 1, len)
  116. for c in range(1, count+1):
  117. self.layer.setPixelData(bleedline, (xpos + c), ypos, 1, len)
  118. print("Right lines cloned: ", count)
  119. self.doc.refreshProjection()
  120. # Convert count input values to relevant units
  121. def convertCount():
  122. unitIndex = unitInput.currentIndex()
  123. print("Updated unit index: ", unitIndex)