#!/usr/bin/env python

"""PyQt4 port of the opengl/hellogl example from Qt v4.x"""

import sys
import math
from PyQt4 import QtCore, QtGui, QtOpenGL

try:
    from OpenGL import GL, GLUT
except ImportError:
    app = QtGui.QApplication(sys.argv)
    QtGui.QMessageBox.critical(None, "OpenGL hellogl",
                            "PyOpenGL must be installed to run this example.",
                            QtGui.QMessageBox.Ok | QtGui.QMessageBox.Default,
                            QtGui.QMessageBox.NoButton)
    sys.exit(1)



class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.glWidget = GLWidget()

        self.xSlider = self.createSlider(QtCore.SIGNAL("xRotationChanged(int)"),
                                         self.glWidget.setXRotation)
        self.ySlider = self.createSlider(QtCore.SIGNAL("yRotationChanged(int)"),
                                         self.glWidget.setYRotation)
        self.zSlider = self.createSlider(QtCore.SIGNAL("zRotationChanged(int)"),
                                         self.glWidget.setZRotation)

        mainLayout = QtGui.QHBoxLayout()
        mainLayout.addWidget(self.glWidget)
        mainLayout.addWidget(self.xSlider)
        mainLayout.addWidget(self.ySlider)
        mainLayout.addWidget(self.zSlider)
        self.setLayout(mainLayout)

        self.xSlider.setValue(348 * 16)
        self.ySlider.setValue(345 * 16)
        self.zSlider.setValue(0 * 16)

        self.setWindowTitle(self.tr("Hello GL"))

    def createSlider(self, changedSignal, setterSlot):
        slider = QtGui.QSlider(QtCore.Qt.Vertical)

        slider.setRange(0, 360 * 16)
        slider.setSingleStep(16)
        slider.setPageStep(15 * 16)
        slider.setTickInterval(15 * 16)
        slider.setTickPosition(QtGui.QSlider.TicksRight)

        self.glWidget.connect(slider, QtCore.SIGNAL("valueChanged(int)"), setterSlot)
        self.connect(self.glWidget, changedSignal, slider, QtCore.SLOT("setValue(int)"))

        return slider

class GLWidget(QtOpenGL.QGLWidget):
    def __init__(self, parent=None):
        QtOpenGL.QGLWidget.__init__(self, parent)

        self.object = 0
        self.xRot = 0
        self.yRot = 0
        self.zRot = 0

        self.lastPos = QtCore.QPoint()

        self.trolltechGreen = QtGui.QColor.fromCmykF(0.40, 0.0, 1.0, 0.0)
        self.trolltechPurple = QtGui.QColor.fromCmykF(0.39, 0.39, 0.0, 0.0)

    def xRotation(self):
        return self.xRot

    def yRotation(self):
        return self.yRot

    def zRotation(self):
        return self.zRot

    def minimumSizeHint(self):
        return QtCore.QSize(50, 50)

    def sizeHint(self):
        return QtCore.QSize(400, 400)

    def setXRotation(self, angle):
        angle = self.normalizeAngle(angle)
        if angle != self.xRot:
            self.xRot = angle
            self.emit(QtCore.SIGNAL("xRotationChanged(int)"), angle)
            self.updateGL()

    def setYRotation(self, angle):
        angle = self.normalizeAngle(angle)
        if angle != self.yRot:
            self.yRot = angle
            self.emit(QtCore.SIGNAL("yRotationChanged(int)"), angle)
            self.updateGL()

    def setZRotation(self, angle):
        angle = self.normalizeAngle(angle)
        if angle != self.zRot:
            self.zRot = angle
            self.emit(QtCore.SIGNAL("zRotationChanged(int)"), angle)
            self.updateGL()

    def initializeGL(self):
        self.qglClearColor(self.trolltechPurple.dark())
        self.object = self.makeObject()
        GL.glShadeModel(GL.GL_FLAT)
        GL.glEnable(GL.GL_DEPTH_TEST)
        GL.glEnable(GL.GL_CULL_FACE)
        
        return
        GL.glShadeModel(GL.GL_SMOOTH)
        GL.glEnable(GL.GL_CULL_FACE)
        GL.glEnable(GL.GL_DEPTH_TEST)
        GL.glEnable(GL.GL_LIGHTING)
        lightZeroPosition = [10.,4.,10.,1.]
        lightZeroColor = [0.8,1.0,0.8,1.0] #green tinged
        #GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightZeroPosition)
        #GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightZeroColor)
        GL.glLightf(GL.GL_LIGHT0, GL.GL_CONSTANT_ATTENUATION, 0.1)
        GL.glLightf(GL.GL_LIGHT0, GL.GL_LINEAR_ATTENUATION, 0.05)
        GL.glEnable(GL.GL_LIGHT0)

        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.gluPerspective(40.,1.,1.,40.)
        GL.glMatrixMode(GL.GL_MODELVIEW)
    
    def paintGL(self):
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glLoadIdentity()
        GL.glTranslated(-0.3, 0.3, -10.0)
        GL.glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0)
        GL.glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0)
        GL.glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0)
        GL.glCallList(self.object)

    def resizeGL(self, width, height):
        side = min(width, height)
        GL.glViewport((width - side) / 2, (height - side) / 2, side, side)

        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glLoadIdentity()
        GL.glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0)
        GL.glMatrixMode(GL.GL_MODELVIEW)

    def mousePressEvent(self, event):
        self.lastPos = QtCore.QPoint(event.pos())

    def mouseMoveEvent(self, event):
        dx = event.x() - self.lastPos.x()
        dy = event.y() - self.lastPos.y()

        if event.buttons() & QtCore.Qt.LeftButton:
            self.setXRotation(self.xRot + 8 * dy)
            self.setYRotation(self.yRot + 8 * dx)
        elif event.buttons() & QtCore.Qt.RightButton:
            self.setXRotation(self.xRot + 8 * dy)
            self.setZRotation(self.zRot + 8 * dx)

        self.lastPos = QtCore.QPoint(event.pos())

    def makeObject(self):
        genList = GL.glGenLists(1)
        GL.glNewList(genList, GL.GL_COMPILE)
        GL.glBegin(GL.GL_QUADS)

        x1 = +0.06
        y1 = -0.14
        x2 = +0.14
        y2 = -0.06
        x3 = +0.08
        y3 = +0.00
        x4 = +0.30
        y4 = +0.22
        
        # lines
        self.box( 0.00, 0.01, 0.60, 0.01, 0.60, -0.01, 0.00, -0.01, z=0.01)
        self.box( 0.01, 0.00, 0.01, -0.60, -0.01, -0.60, -0.01, 0.00, z=0.01)

        # arrows
        self.box( -0.05, -0.60,0.00, -0.65, 0.05, -0.60, 0.04, -0.60, z=-0.01)
        self.box( 0.60,-0.05 , 0.65, 0.00, 0.60, 0.05,  0.60,0.04, z=-0.01)
        
        # zet 
        self.box( -0.01, 0.01, 0.01, 0.01, 0.01, -0.01, -0.01, -0.01, z=0.01, mz=-0.60)
        
        
        #self.quad(x1, y1, x2, y2, y2, x2, y1, x1)
        #self.quad(x3, y3, x4, y4, y4, x4, y3, x3)

        #self.extrude(x1, y1, x2, y2)
        #self.extrude(x2, y2, y2, x2)
        #self.extrude(y2, x2, y1, x1)
        #self.extrude(y1, x1, x1, y1)
        #self.extrude(x3, y3, x4, y4)
        #self.extrude(x4, y4, y4, x4)
        #self.extrude(y4, x4, y3, x3)

        Pi = 3.14159265358979323846
        NumSectors = 10

        for i in []:#range(NumSectors):
            angle1 = (i * 2 * Pi) / NumSectors
            x5 = 0.30 * math.sin(angle1)
            y5 = 0.30 * math.cos(angle1)
            x6 = 0.20 * math.sin(angle1)
            y6 = 0.20 * math.cos(angle1)

            angle2 = ((i + 1) * 2 * Pi) / NumSectors
            x7 = 0.20 * math.sin(angle2)
            y7 = 0.20 * math.cos(angle2)
            x8 = 0.30 * math.sin(angle2)
            y8 = 0.30 * math.cos(angle2)

            self.quad(x5, y5, x6, y6, x7, y7, x8, y8)

            self.extrude(x6, y6, x7, y7)
            self.extrude(x8, y8, x5, y5)

        GL.glEnd()
        self.qglColor( QtGui.QColor.fromCmykF(0.39, 0.39, 0.0, 0.0) )
        GLUT.glutInit(sys.argv)
        GLUT.glutInitDisplayMode(GLUT.GLUT_DOUBLE | GLUT.GLUT_RGB | GLUT.GLUT_DEPTH)
        
        n = 0.05
        for a,b,c in [(0.24634586,  0.00479568,  0.98554564),(0.26838576,  0.03055637,  1.13824132)]:
            for i in range(10):
                m = -i * n
                GL.glTranslate(-a*m, -b*m, c*m)
                GLUT.glutSolidSphere(0.04,10,10)
        #self.drawSphere(0.01, 10, 10)
        GL.glEndList()
        return genList
        
    def drawSphere(self, r, lats, longs):
        for i in range(lats):
            lat0 = math.pi * (-0.5 + float(i - 1) / lats)
            z0   = math.sin(lat0)
            zr0  = math.cos(lat0)
    
            lat1 = math.pi * (-0.5 + float(i) / lats)
            z1   = math.sin(lat1)
            zr1  = math.cos(lat1)
    
            GL.glBegin(GL.GL_QUAD_STRIP);
            for j in range(longs):
                lng = 2.0 * math.pi * float(j - 1) / longs
                x = math.cos(lng)
                y = math.sin(lng)
        
                GL.glNormal3f(x * zr0, y * zr0, z0);
                GL.glVertex3f(x * zr0, y * zr0, z0);
                GL.glNormal3f(x * zr1, y * zr1, z1);
                GL.glVertex3f(x * zr1, y * zr1, z1);
            GL.glEnd()


    def quad(self, x1, y1, x2, y2, x3, y3, x4, y4, z=0.05):
        self.qglColor(self.trolltechGreen)

        GL.glVertex3d(x1, y1, -z)
        GL.glVertex3d(x2, y2, -z)
        GL.glVertex3d(x3, y3, -z)
        GL.glVertex3d(x4, y4, -z)

        GL.glVertex3d(x4, y4, +z)
        GL.glVertex3d(x3, y3, +z)
        GL.glVertex3d(x2, y2, +z)
        GL.glVertex3d(x1, y1, +z)

    def box(self, x1, y1, x2, y2, x3, y3, x4, y4, z=0.05, mz=None):
        self.qglColor(self.trolltechGreen)
        if not mz:
            mz = -z

        GL.glVertex3d(x1, y1, z)
        GL.glVertex3d(x2, y2, z)
        GL.glVertex3d(x3, y3, z)
        GL.glVertex3d(x4, y4, z)

        GL.glVertex3d(x4, y4, mz)
        GL.glVertex3d(x3, y3, mz)
        GL.glVertex3d(x2, y2, mz)
        GL.glVertex3d(x1, y1, mz)
        
        self.qglColor(self.trolltechGreen.dark(250 + int(100 * x1)))
        for (a1, b1), (a2, b2) in [
                ((x1, y1), (x2, y2)),
                ((x2, y2), (x3, y3)),
                ((x3, y3), (x4, y4)),
                ((x4, y4), (x1, y1)),
                 ]:
            GL.glVertex3d(a1, b1, mz)
            GL.glVertex3d(a2, b2, mz)
            GL.glVertex3d(a2, b2,  z)
            GL.glVertex3d(a1, b1,  z)
            

    def extrude(self, x1, y1, x2, y2, z=0.05):
        self.qglColor(self.trolltechGreen.dark(250 + int(100 * x1)))

        GL.glVertex3d(x1, y1, +z)
        GL.glVertex3d(x2, y2, +z)
        GL.glVertex3d(x2, y2, -z)
        GL.glVertex3d(x1, y1, -z)

    def normalizeAngle(self, angle):
        while angle < 0:
            angle += 360 * 16
        while angle > 360 * 16:
            angle -= 360 * 16
        return angle


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
