# Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.View.View import View
from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
from UM.Resources import Resources
from UM.Application import Application
from UM.Math.Color import Color
from UM.Preferences import Preferences
from UM.View.Renderer import Renderer

from UM.View.GL.OpenGL import OpenGL

import math

## Standard view for mesh models. 
class SolidView(View):
    #EnabledColor = Color(1.0, 0.79, 0.14, 1.0)
    #DisabledColor = Color(0.68, 0.68, 0.68, 1.0)

    def __init__(self):
        super().__init__()

        Preferences.getInstance().addPreference("view/show_overhang", True)
        #Preferences.getInstance().preferenceChanged.connect(self._onPreferenceChanged)

        self._enabled_shader = None
        self._disabled_shader = None

    def beginRendering(self):
        scene = self.getController().getScene()
        renderer = self.getRenderer()

        if not self._enabled_shader:
            #if Preferences.getInstance().getValue("view/show_overhang"):
                #self._enabled_material = renderer.createMaterial(Resources.getPath(Resources.Shaders, "default.vert"), Resources.getPath(Resources.Shaders, "overhang.frag"))
            #else:
                #self._enabled_material = renderer.createMaterial(Resources.getPath(Resources.Shaders, "default.vert"), Resources.getPath(Resources.Shaders, "default.frag"))

            self._enabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))

            #self._enabled_material.setUniformValue("u_ambientColor", Color(0.3, 0.3, 0.3, 1.0))
            #self._enabled_material.setUniformValue("u_diffuseColor", self.EnabledColor)
            #self._enabled_material.setUniformValue("u_specularColor", Color(0.4, 0.4, 0.4, 1.0))
            #self._enabled_material.setUniformValue("u_overhangColor", Color(1.0, 0.0, 0.0, 1.0))
            #self._enabled_material.setUniformValue("u_shininess", 20.)

        if not self._disabled_shader:
            self._disabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
            self._disabled_shader.setUniformValue("u_diffuseColor", [0.68, 0.68, 0.68, 1.0])
            #self._disabled_material = renderer.createMaterial(Resources.getPath(Resources.Shaders, "default.vert"), Resources.getPath(Resources.Shaders, "default.frag"))
            #self._disabled_material.setUniformValue("u_ambientColor", Color(0.3, 0.3, 0.3, 1.0))
            #self._disabled_material.setUniformValue("u_diffuseColor", self.DisabledColor)
            #self._disabled_material.setUniformValue("u_specularColor", Color(0.4, 0.4, 0.4, 1.0))
            #self._disabled_material.setUniformValue("u_overhangColor", Color(1.0, 0.0, 0.0, 1.0))
            #self._disabled_material.setUniformValue("u_shininess", 20.)

        if Application.getInstance().getMachineManager().getActiveProfile():
            profile = Application.getInstance().getMachineManager().getActiveProfile()

            if profile.getSettingValue("support_enable") or not Preferences.getInstance().getValue("view/show_overhang"):
                angle = profile.getSettingValue("support_angle")
                if angle != None:
                    self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(90 - angle)))
            else:
                self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0)))

        for node in DepthFirstIterator(scene.getRoot()):
            if not node.render(renderer):
                if node.getMeshData() and node.isVisible():
                    # TODO: Find a better way to handle this
                    #if node.getBoundingBoxMesh():
                    #    renderer.queueNode(scene.getRoot(), mesh = node.getBoundingBoxMesh(),mode = Renderer.RenderLines)
                    if hasattr(node, "_outside_buildarea"):
                        if node._outside_buildarea:
                            renderer.queueNode(node, shader = self._disabled_shader)
                        else:
                            renderer.queueNode(node, shader = self._enabled_shader)
                    else:
                        renderer.queueNode(node, material = self._enabled_shader)
                if node.callDecoration("isGroup"):
                    renderer.queueNode(scene.getRoot(), mesh = node.getBoundingBoxMesh(),mode = Renderer.RenderLines)

    def endRendering(self):
        pass

    #def _onPreferenceChanged(self, preference):
        #if preference == "view/show_overhang": ## Todo: This a printer only setting. Should be removed from Uranium.
            #self._enabled_material = None

@@ -1044,1922 +1044,393 @@
     
-#if Preferences.getInstance().getValue(%22view/show_overhang%22):%0A                #self._enabled_material = renderer.createMaterial(Resources.getPath(Resources.Shaders, %22default.vert%22), Resources.getPath(Resources.Shaders, %22overhang.frag%22))%0A            #else:%0A                #self._enabled_material = renderer.createMaterial(Resources.getPath(Resources.Shaders, %22default.vert%22), Resources.getPath(Resources.Shaders, %22default.frag%22))%0A%0A            self._enabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, %22overhang.shader%22))%0A%0A            #self._enabled_material.setUniformValue(%22u_ambientColor%22, Color(0.3, 0.3, 0.3, 1.0))%0A            #self._enabled_material.setUniformValue(%22u_diffuseColor%22, self.EnabledColor)%0A            #self._enabled_material.setUniformValue(%22u_specularColor%22, Color(0.4, 0.4, 0.4, 1.0))%0A            #self._enabled_material.setUniformValue(%22u_overhangColor%22, Color(1.0, 0.0, 0.0, 1.0))%0A            #self._enabled_material.setUniformValue(%22u_shininess%22, 20.)%0A%0A        if not self._disabled_shader:%0A            self._disabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, %22overhang.shader%22))%0A            self._disabled_shader.setUniformValue(%22u_diffuseColor%22, %5B0.68, 0.68, 0.68, 1.0%5D)%0A            #self._disabled_material = renderer.createMaterial(Resources.getPath(Resources.Shaders, %22default.vert%22), Resources.getPath(Resources.Shaders, %22default.frag%22))%0A            #self._disabled_material.setUniformValue(%22u_ambientColor%22, Color(0.3, 0.3, 0.3, 1.0))%0A            #self._disabled_material.setUniformValue(%22u_diffuseColor%22, self.DisabledColor)%0A            #self._disabled_material.setUniformValue(%22u_specularColor%22, Color(0.4, 0.4, 0.4, 1.0))%0A            #self._disabled_material.setUniformValue(%22u_overhangColor%22, Color(1.0, 0.0, 0.0, 1.0))%0A            #self._disabled_material.setUniformValue(%22u_shininess%22, 20.
+self._enabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, %22overhang.shader%22))%0A%0A        if not self._disabled_shader:%0A            self._disabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, %22overhang.shader%22))%0A            self._disabled_shader.setUniformValue(%22u_diffuseColor%22, %5B0.68, 0.68, 0.68, 1.0%5D
 )%0A%0A 

Remove commented out code