Skip to content

Instantly share code, notes, and snippets.

@LeftRadio
Created December 6, 2015 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeftRadio/494fbe50c660dbede9d7 to your computer and use it in GitHub Desktop.
Save LeftRadio/494fbe50c660dbede9d7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from ngl_utils.nplugins.widgets.ngl_base import NGL_Base
from ngl_utils.nplugins.widgets.ngl_colors import NGL_Colors
from ngl_utils.nplugins.widgets.qstyle_parser import QStyleParser
from PyQt5.QtCore import pyqtSlot, pyqtProperty, QRect, Qt
from PyQt5.QtGui import QPainter, QPen, QColor
class NGL_Greed(NGL_Base):
"""NGL_Greed(NGL_Base)
Provides a embedded NGL library greed widget.
"""
def __init__(self, parent=None):
""" Constructor for ngl widget """
super(NGL_Greed, self).__init__(parent)
self._static = False
self._central_lines = True
self._central_lines_color = QColor(Qt.red)
self._cell_width = 15
self._cell_height = 15
self.setGeometry(100, 100, 150, 150)
self.setStyleSheet('color: rgb(64, 64, 64);')
self.update()
def paintEvent(self, event):
""" Paint ngl widget event """
_width = self.geometry().width()
_x_2 = _width // 2
_height = self.geometry().height()
_y_2 = _height // 2
color = QStyleParser.getColor(self.styleSheet(), 'color: rgb')
p = QPainter()
p.begin(self)
p.setPen(color);
# horisontal steps
for x in range(_x_2, _width, self._cell_width):
p.drawLine(x, 0, x, _height-1)
for x in range(_x_2, 0, -self._cell_width):
p.drawLine(x, 0, x, _height-1)
# vertical steps
for y in range(_y_2, _width, self._cell_height):
p.drawLine(0, y, _width-1, y)
for y in range(_y_2, 0, -self._cell_height):
p.drawLine(0, y, _width-1, y)
if self._central_lines:
p.setPen(self._central_lines_color);
p.drawLine(_x_2, 0, _x_2, _height-1)
p.drawLine(0, _y_2, _width-1, _y_2)
p.end()
def sizeHint(self):
""" Return Qt sizeHint """
return self.size()
#
# Provide getter and setter methods for the property.
#
@pyqtProperty(bool)
def central_lines(self):
return self._central_lines
@central_lines.setter
def central_lines(self, state):
self._central_lines = state
self.update()
#
# Provide getter and setter methods for the property.
#
@pyqtProperty(QColor)
def central_lines_color(self):
return self._central_lines_color
@central_lines_color.setter
def central_lines_color(self, color):
self._central_lines_color = color
self.update()
#
# Provide getter and setter methods for the property.
#
@pyqtProperty(int)
def cell_width(self):
return self._cell_width
@cell_width.setter
def cell_width(self, width):
self._cell_width = width
self.update()
#
# Provide getter and setter methods for the property.
#
@pyqtProperty(bool)
def cell_height(self):
return self._cell_height
@cell_height.setter
def cell_height(self, height):
self._cell_height = height
self.update()
#
# Generate C code for NGL
#
def doNGLCode(self, **kwargs):
""" Generate code for NGL_Label """
# convert coordinates
# g = self._ngl_geometry()
g = self.geometry()
import pkg_resources
res_path = pkg_resources.resource_filename('ngl_utils', 'templates/greed.ntp')
with open(res_path, 'rt') as f:
template = f.read()
return template.format(
pageName = self._ngl_parent_obj_name(),
itemName = self.objectName(),
x = g.x(),
y = g.y(),
width = g.width(),
height = g.height(),
central_lines = self.central_lines,
central_lines_color = hex(NGL_Colors.fromQColor(self.central_lines_color)),
cell_widht = self.cell_width,
cell_height = self.cell_height,
color = self._ngl_color('color: rgb')
)
@staticmethod
def ngl_draw(**kwargs):
s = 'NGL_GUI_DrawGreed({objects}[{index}]);'
return s.format(
objects = kwargs['name'],
index = kwargs['index'])
# if run as main program
if __name__ == "__main__":
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
widget = NGL_Greed()
widget.setObjectName('test_ngl_greed')
print(widget.doNGLCode())
widget.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment