• Source
    1. #!/usr/bin/env python
    2.  
    3. from ngl_utils.nplugins.widgets.ngl_base import NGL_Base
    4. from ngl_utils.nplugins.widgets.qstyle_parser import QStyleParser
    5. from PyQt5.QtCore import pyqtSlot, pyqtProperty, QRect, Qt
    6. from PyQt5.QtGui import QPainter, QPen, QColor
    7.  
    8.  
    9. class NGL_Greed(NGL_Base):
    10. """NGL_Greed(NGL_Base)
    11. Provides a embedded NGL library greed widget.
    12. """
    13.  
    14. def __init__(self, parent=None):
    15. """ Constructor for ngl widget """
    16. super(NGL_Greed, self).__init__(parent)
    17.  
    18. self._static = False
    19.  
    20. self._central_lines = True
    21. self._central_lines_color = QColor(Qt.red)
    22.  
    23. self._cell_width = 15
    24. self._cell_height = 15
    25.  
    26. self.setGeometry(150, 150, 150, 150)
    27. self.setStyleSheet('color: rgb(64, 64, 64);')
    28. self.update()
    29.  
    30. def paintEvent(self, event):
    31. """ Paint ngl widget event """
    32.  
    33. _width = self.geometry().width()
    34. _x_2 = _width // 2
    35.  
    36. _height = self.geometry().height()
    37. _y_2 = _height // 2
    38.  
    39. color = QStyleParser.getColor(self.styleSheet(), 'color: rgb')
    40.  
    41. p = QPainter()
    42. p.begin(self)
    43.  
    44. p.setPen(color);
    45.  
    46. # horisontal steps
    47. for x in range(_x_2, _width, self._cell_width):
    48. p.drawLine(x, 0, x, _height-1)
    49.  
    50. for x in range(_x_2, 0, -self._cell_width):
    51. p.drawLine(x, 0, x, _height-1)
    52.  
    53. # vertical steps
    54. for y in range(_y_2, _width, self._cell_height):
    55. p.drawLine(0, y, _width-1, y)
    56.  
    57. for y in range(_y_2, 0, -self._cell_height):
    58. p.drawLine(0, y, _width-1, y)
    59.  
    60.  
    61. if self._central_lines:
    62. p.setPen(self._central_lines_color);
    63. p.drawLine(_x_2, 0, _x_2, _height-1)
    64. p.drawLine(0, _y_2, _width-1, _y_2)
    65.  
    66. p.end()
    67.  
    68. def sizeHint(self):
    69. """ Return Qt sizeHint """
    70. return self.size()
    71.  
    72.  
    73. #
    74. # Provide getter and setter methods for the property.
    75. #
    76. @pyqtProperty(bool)
    77. def central_lines(self):
    78. return self._central_lines
    79.  
    80. @central_lines.setter
    81. def central_lines(self, state):
    82. self._central_lines = state
    83. self.update()
    84.  
    85. #
    86. # Provide getter and setter methods for the property.
    87. #
    88. @pyqtProperty(QColor)
    89. def central_lines_color(self):
    90. return self._central_lines_color
    91.  
    92. @central_lines_color.setter
    93. def central_lines_color(self, color):
    94. self._central_lines_color = color
    95. self.update()
    96.  
    97. #
    98. # Provide getter and setter methods for the property.
    99. #
    100. @pyqtProperty(int)
    101. def cell_width(self):
    102. return self._cell_width
    103.  
    104. @cell_width.setter
    105. def cell_width(self, width):
    106. self._cell_width = width
    107. self.update()
    108.  
    109. #
    110. # Provide getter and setter methods for the property.
    111. #
    112. @pyqtProperty(bool)
    113. def cell_height(self):
    114. return self._cell_height
    115.  
    116. @cell_height.setter
    117. def cell_height(self, height):
    118. self._cell_height = height
    119. self.update()
    120.  
    121.  
    122.  
    123.  
    124.  
    125. # if run as main program
    126. if __name__ == "__main__":
    127.  
    128. import sys
    129. from PyQt5.QtWidgets import QApplication
    130.  
    131. app = QApplication(sys.argv)
    132. widget = NGL_Greed()
    133. widget.show()
    134. sys.exit(app.exec_())
    135.