Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import sys
- from OpenGL.GL import *
- from OpenGL.GLU import *
- from PyQt4 import QtGui, QtCore
- from PyQt4.QtOpenGL import *
- class MyMainWindow(QtGui.QMainWindow):
- def __init__(self):
- QtGui.QMainWindow.__init__(self)
- widget = MyGlWidget(self)
- self.setCentralWidget(widget)
- class MyGlWidget(QGLWidget):
- def __init__(self, parent):
- QGLWidget.__init__(self, parent)
- self.setMinimumSize(500, 500)
- self.arrows = []
- def paintGL(self):
- glClearColor(1.0, 1.0, 1.0, 1.0)
- glClear(GL_COLOR_BUFFER_BIT)
- glEnable(GL_LINE_SMOOTH)
- glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
- glLineWidth(1.2)
- glPointSize(1)
- for arrow in self.arrows:
- arrow.paint()
- def resizeGL(self, w, h):
- if(h == 0):
- h = 1
- glViewport(0, 0, w, h)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- aspectRatio = float(float(w) / float(h))
- if (w <= h):
- glOrtho(0, 500.0, 0, 500.0/aspectRatio, 1.0, -1.0)
- else:
- glOrtho(0, 500.0*aspectRatio, 0, 500.0, 1.0, -1.0)
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- def mousePressEvent(self, event):
- if event.button() == QtCore.Qt.LeftButton:
- self.drawing = True
- self.current_arrow = Arrow(self)
- self.current_arrow.x1 = event.pos().x()
- self.current_arrow.y1 = event.pos().y()
- self.current_arrow.x2 = event.pos().x()
- self.current_arrow.y2 = event.pos().y()
- self.arrows.append(self.current_arrow)
- def mouseMoveEvent(self, event):
- if self.drawing == True:
- self.current_arrow.x2 = event.pos().x()
- self.current_arrow.y2 = event.pos().y()
- self.paintGL()
- def mouseReleaseEvent(self, event):
- if event.button() == QtCore.Qt.LeftButton and self.drawing:
- self.current_arrow.x2 = event.pos().x()
- self.current_arrow.y2 = event.pos().y()
- self.paintGL()
- self.drawing = False
- class Arrow(object):
- def __init__(self, view):
- self.x1 = 0
- self.y1 = 0
- self.x2 = 0
- self.y2 = 0
- self.view = view
- def paint(self):
- x1 = self.x1
- y1 = self.y1
- x2 = self.x2
- y2 = self.y2
- y1 = self.view.height() - y1
- y2 = self.view.height() - y2
- bezscale = 20
- bezmiddle = abs(x2-x1)/1.5
- glEnable(GL_MAP1_VERTEX_3)
- ctrlpoints=[[x1, y1, 0.01],
- [x1+bezscale, y1, 0.01], [x1+bezmiddle, y1, 0.01],
- [x2-bezmiddle, y2, 0.01], [x2-bezscale, y2, 0.01],
- [x2, y2, 0.01]]
- glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, ctrlpoints)
- glColor3f(0.4, 0.4, 0.4)
- glBegin(GL_LINE_STRIP)
- SEGS=20
- for p in range(0, SEGS+1):
- glEvalCoord1f(float(p)/float(SEGS))
- glEnd()
- arrow_size = 5
- glBegin(GL_POLYGON)
- glVertex2i( x2, y2 )
- glVertex2i( x2-arrow_size, y2-arrow_size )
- glVertex2i( x2-arrow_size, y2+arrow_size )
- glEnd()
- glEnable(GL_BLEND)
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
- self.view.swapBuffers()
- def main(argv):
- import sys
- app = QtGui.QApplication(sys.argv)
- window = MyMainWindow()
- window.show()
- app.exec_()
- if __name__ == "__main__":
- main(sys.argv)
Add Comment
Please, Sign In to add comment