999ms

Implementation of visual part of course project

Mar 9th, 2021 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4.  
  5. from OpenGL.GL import *
  6. from OpenGL.GLU import *
  7. from PyQt4 import QtGui, QtCore
  8. from PyQt4.QtOpenGL import *
  9.  
  10. class MyMainWindow(QtGui.QMainWindow):
  11.     def __init__(self):
  12.         QtGui.QMainWindow.__init__(self)
  13.         widget = MyGlWidget(self)    
  14.         self.setCentralWidget(widget)
  15.  
  16. class MyGlWidget(QGLWidget):
  17.     def __init__(self, parent):
  18.         QGLWidget.__init__(self, parent)
  19.         self.setMinimumSize(500, 500)
  20.         self.arrows = []
  21.        
  22.     def paintGL(self):
  23.         glClearColor(1.0, 1.0, 1.0, 1.0)
  24.         glClear(GL_COLOR_BUFFER_BIT)
  25.        
  26.         glEnable(GL_LINE_SMOOTH)
  27.         glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
  28.        
  29.         glLineWidth(1.2)
  30.         glPointSize(1)
  31.        
  32.         for arrow in self.arrows:
  33.             arrow.paint()
  34.        
  35.     def resizeGL(self, w, h):
  36.         if(h == 0):
  37.             h = 1
  38.         glViewport(0, 0, w, h)
  39.         glMatrixMode(GL_PROJECTION)
  40.         glLoadIdentity()
  41.         aspectRatio = float(float(w) / float(h))
  42.        
  43.         if (w <= h):
  44.             glOrtho(0, 500.0, 0, 500.0/aspectRatio, 1.0, -1.0)
  45.         else:
  46.             glOrtho(0, 500.0*aspectRatio, 0, 500.0, 1.0, -1.0)
  47.            
  48.         glMatrixMode(GL_MODELVIEW)
  49.         glLoadIdentity()
  50.        
  51.     def mousePressEvent(self, event):
  52.         if event.button() == QtCore.Qt.LeftButton:
  53.             self.drawing = True
  54.             self.current_arrow = Arrow(self)
  55.             self.current_arrow.x1 = event.pos().x()
  56.             self.current_arrow.y1 = event.pos().y()
  57.             self.current_arrow.x2 = event.pos().x()
  58.             self.current_arrow.y2 = event.pos().y()
  59.             self.arrows.append(self.current_arrow)
  60.        
  61.     def mouseMoveEvent(self, event):
  62.         if self.drawing == True:
  63.             self.current_arrow.x2 = event.pos().x()
  64.             self.current_arrow.y2 = event.pos().y()
  65.             self.paintGL()
  66.            
  67.     def mouseReleaseEvent(self, event):
  68.         if event.button() == QtCore.Qt.LeftButton and self.drawing:
  69.             self.current_arrow.x2 = event.pos().x()
  70.             self.current_arrow.y2 = event.pos().y()
  71.             self.paintGL()
  72.             self.drawing = False
  73.        
  74. class Arrow(object):
  75.     def __init__(self, view):
  76.         self.x1 = 0
  77.         self.y1 = 0
  78.         self.x2 = 0
  79.         self.y2 = 0
  80.         self.view = view
  81.    
  82.     def paint(self):
  83.         x1 = self.x1
  84.         y1 = self.y1
  85.         x2 = self.x2
  86.         y2 = self.y2
  87.        
  88.         y1 = self.view.height() - y1
  89.         y2 = self.view.height() - y2
  90.        
  91.         bezscale = 20
  92.         bezmiddle = abs(x2-x1)/1.5
  93.        
  94.         glEnable(GL_MAP1_VERTEX_3)
  95.         ctrlpoints=[[x1, y1, 0.01],
  96.             [x1+bezscale, y1, 0.01], [x1+bezmiddle, y1, 0.01],
  97.             [x2-bezmiddle, y2, 0.01], [x2-bezscale, y2, 0.01],
  98.             [x2, y2, 0.01]]
  99.        
  100.         glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, ctrlpoints)
  101.         glColor3f(0.4, 0.4, 0.4)
  102.         glBegin(GL_LINE_STRIP)
  103.         SEGS=20
  104.         for p in range(0, SEGS+1):
  105.             glEvalCoord1f(float(p)/float(SEGS))
  106.         glEnd()
  107.        
  108.         arrow_size = 5
  109.        
  110.         glBegin(GL_POLYGON)
  111.         glVertex2i( x2, y2 )
  112.         glVertex2i( x2-arrow_size, y2-arrow_size )
  113.         glVertex2i( x2-arrow_size, y2+arrow_size )
  114.         glEnd()
  115.         glEnable(GL_BLEND)
  116.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  117.         self.view.swapBuffers()
  118.  
  119. def main(argv):
  120.     import sys
  121.     app = QtGui.QApplication(sys.argv)
  122.     window = MyMainWindow()
  123.     window.show()
  124.     app.exec_()
  125.    
  126. if __name__ == "__main__":
  127.     main(sys.argv)
Add Comment
Please, Sign In to add comment