• Source
    1. #!/usr/bin/env python
    2.  
    3. from ngl_utils.nplugins.widgets.ngl_base import NGL_Base
    4. from ngl_utils.nplugins.widgets.ngl_colors import NGL_Colors
    5. from ngl_utils.nplugins.widgets.qstyle_parser import QStyleParser
    6.  
    7. from PyQt5.QtCore import pyqtSlot, pyqtProperty, QRect, Qt
    8. from PyQt5.QtGui import QPainter, QPen, QColor
    9.  
    10.  
    11. class NGL_Greed(NGL_Base):
    12. """NGL_Greed(NGL_Base)
    13. Provides a embedded NGL library greed widget.
    14. """
    15.  
    16. def __init__(self, parent=None):
    17. """ Constructor for ngl widget """
    18. super(NGL_Greed, self).__init__(parent)
    19.  
    20. self._static = False
    21.  
    22. self._central_lines = True
    23. self._central_lines_color = QColor(Qt.red)
    24.  
    25. self._cell_width = 15
    26. self._cell_height = 15
    27.  
    28. self.setGeometry(100, 100, 150, 150)
    29. self.setStyleSheet('color: rgb(64, 64, 64);')
    30. self.update()
    31.  
    32. def paintEvent(self, event):
    33. """ Paint ngl widget event """
    34.  
    35. _width = self.geometry().width()
    36. _x_2 = _width // 2
    37.  
    38. _height = self.geometry().height()
    39. _y_2 = _height // 2
    40.  
    41. color = QStyleParser.getColor(self.styleSheet(), 'color: rgb')
    42.  
    43. p = QPainter()
    44. p.begin(self)
    45.  
    46. p.setPen(color);
    47.  
    48. # horisontal steps
    49. for x in range(_x_2, _width, self._cell_width):
    50. p.drawLine(x, 0, x, _height-1)
    51.  
    52. for x in range(_x_2, 0, -self._cell_width):
    53. p.drawLine(x, 0, x, _height-1)
    54.  
    55. # vertical steps
    56. for y in range(_y_2, _width, self._cell_height):
    57. p.drawLine(0, y, _width-1, y)
    58.  
    59. for y in range(_y_2, 0, -self._cell_height):
    60. p.drawLine(0, y, _width-1, y)
    61.  
    62.  
    63. if self._central_lines:
    64. p.setPen(self._central_lines_color);
    65. p.drawLine(_x_2, 0, _x_2, _height-1)
    66. p.drawLine(0, _y_2, _width-1, _y_2)
    67.  
    68. p.end()
    69.  
    70. def sizeHint(self):
    71. """ Return Qt sizeHint """
    72. return self.size()
    73.  
    74.  
    75. #
    76. # Provide getter and setter methods for the property.
    77. #
    78. @pyqtProperty(bool)
    79. def central_lines(self):
    80. return self._central_lines
    81.  
    82. @central_lines.setter
    83. def central_lines(self, state):
    84. self._central_lines = state
    85. self.update()
    86.  
    87. #
    88. # Provide getter and setter methods for the property.
    89. #
    90. @pyqtProperty(QColor)
    91. def central_lines_color(self):
    92. return self._central_lines_color
    93.  
    94. @central_lines_color.setter
    95. def central_lines_color(self, color):
    96. self._central_lines_color = color
    97. self.update()
    98.  
    99. #
    100. # Provide getter and setter methods for the property.
    101. #
    102. @pyqtProperty(int)
    103. def cell_width(self):
    104. return self._cell_width
    105.  
    106. @cell_width.setter
    107. def cell_width(self, width):
    108. self._cell_width = width
    109. self.update()
    110.  
    111. #
    112. # Provide getter and setter methods for the property.
    113. #
    114. @pyqtProperty(bool)
    115. def cell_height(self):
    116. return self._cell_height
    117.  
    118. @cell_height.setter
    119. def cell_height(self, height):
    120. self._cell_height = height
    121. self.update()
    122.  
    123.  
    124.  
    125. #
    126. # Generate C code for NGL
    127. #
    128. def doNGLCode(self, **kwargs):
    129. """ Generate code for NGL_Label """
    130.  
    131. # convert coordinates
    132. # g = self._ngl_geometry()
    133. g = self.geometry()
    134.  
    135. import pkg_resources
    136.  
    137. res_path = pkg_resources.resource_filename('ngl_utils', 'templates/greed.ntp')
    138. with open(res_path, 'rt') as f:
    139. template = f.read()
    140.  
    141. return template.format(
    142. pageName = self._ngl_parent_obj_name(),
    143. itemName = self.objectName(),
    144. x = g.x(),
    145. y = g.y(),
    146. width = g.width(),
    147. height = g.height(),
    148. central_lines = self.central_lines,
    149. central_lines_color = hex(NGL_Colors.fromQColor(self.central_lines_color)),
    150. cell_widht = self.cell_width,
    151. cell_height = self.cell_height,
    152. color = self._ngl_color('color: rgb')
    153. )
    154.  
    155. @staticmethod
    156. def ngl_draw(**kwargs):
    157. s = 'NGL_GUI_DrawGreed({objects}[{index}]);'
    158.  
    159. return s.format(
    160. objects = kwargs['name'],
    161. index = kwargs['index'])
    162.  
    163.  
    164.  
    165.  
    166. # if run as main program
    167. if __name__ == "__main__":
    168.  
    169. import sys
    170. from PyQt5.QtWidgets import QApplication
    171.  
    172. app = QApplication(sys.argv)
    173.  
    174. widget = NGL_Greed()
    175. widget.setObjectName('test_ngl_greed')
    176.  
    177. print(widget.doNGLCode())
    178.  
    179. widget.show()
    180.  
    181. sys.exit(app.exec_())
    182.