• 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. self._central_lines = True
    20. self._central_lines_color = QColor(Qt.red)
    21. self._cell_width = 15
    22. self._cell_height = 15
    23.  
    24. self.setGeometry(100, 100, 100, 100)
    25. self.setStyleSheet('color: rgb(64, 64, 64);')
    26. self.update()
    27.  
    28. def paintEvent(self, event):
    29. """ Paint ngl widget event """
    30.  
    31. _width = self.geometry().width()
    32. _height = self.geometry().height()
    33.  
    34. color = QStyleParser.getColor(self.styleSheet(), 'color: rgb')
    35.  
    36. p = QPainter()
    37. p.begin(self)
    38. p.setPen(color);
    39.  
    40. for x in range(_width):
    41. if x % self._cell_width == 0:
    42. p.drawLine(x, 0, x, _height)
    43.  
    44. for y in range(_height):
    45. if y % self._cell_height == 0:
    46. p.drawLine(0, y, _width, y)
    47.  
    48. if self._central_lines:
    49. p.setPen(self._central_lines_color);
    50. p.drawLine(_width/2, 0, _width/2, _height)
    51. p.drawLine(0, _height/2, _width, _height/2)
    52.  
    53. p.end()
    54.  
    55. def sizeHint(self):
    56. """ Return Qt sizeHint """
    57. return self.size()
    58.  
    59.  
    60. #
    61. # Provide getter and setter methods for the property.
    62. #
    63. @pyqtProperty(bool)
    64. def central_lines(self):
    65. return self._central_lines
    66.  
    67. @central_lines.setter
    68. def central_lines(self, state):
    69. self._central_lines = state
    70. self.update()
    71.  
    72. #
    73. # Provide getter and setter methods for the property.
    74. #
    75. @pyqtProperty(QColor)
    76. def central_lines_color(self):
    77. return self._central_lines_color
    78.  
    79. @central_lines_color.setter
    80. def central_lines_color(self, color):
    81. self._central_lines_color = color
    82. self.update()
    83.  
    84. #
    85. # Provide getter and setter methods for the property.
    86. #
    87. @pyqtProperty(int)
    88. def cell_width(self):
    89. return self._cell_width
    90.  
    91. @cell_width.setter
    92. def cell_width(self, width):
    93. self._cell_width = width
    94. self.update()
    95.  
    96. #
    97. # Provide getter and setter methods for the property.
    98. #
    99. @pyqtProperty(bool)
    100. def cell_height(self):
    101. return self._cell_height
    102.  
    103. @cell_height.setter
    104. def cell_height(self, height):
    105. self._cell_height = height
    106. self.update()
    107.  
    108.  
    109. # if run as main program
    110. if __name__ == "__main__":
    111.  
    112. import sys
    113. from PyQt5.QtWidgets import QApplication
    114.  
    115. app = QApplication(sys.argv)
    116. widget = NGL_Greed()
    117. widget.show()
    118. sys.exit(app.exec_())