"""
     qtgui/TransparencyDialog.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2001-2008 University of York, CCLRC
     Copyright (C) 2009 University of York

     This library is free software: you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public License
     version 3, modified in accordance with the provisions of the 
     license to address the requirements of UK law.
 
     You should have received a copy of the modified GNU Lesser General 
     Public License along with this library.  If not, copies may be 
     downloaded from http://www.ccp4.ac.uk/ccp4license.php
 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Lesser General Public License for more details.
"""
"""
Turn on/off transparency and alter object alphas.
"""
from PyQt4 import QtCore, QtGui, QtOpenGL
import MGSimpleDialog,mgWidgets
import MGApplication
import global_definitions
import rebuild



class TransparencyDialog(MGSimpleDialog.MGSimpleDialog):

    def handleViewChanged(self,args=None):
        #print 'TransparencyDialog.handleViewChanged'
        #if not(self.onoff.isChecked()): return
	rebuild.UpdateDisplay()

    def handleDTChanged(self):
        import sip
	child = self.objectLayout.takeAt(0)
	while child:
		if child:
			sip.delete(child.widget())
			sip.delete(child)
			child = self.objectLayout.takeAt(0)
	dispobjs = global_definitions.get_dispobj()
	for obj in dispobjs:
          if obj.canBeTransparent():
                obj_name = obj.name + ' ' + obj.get_label()
		try:
                  #obj_alpha = obj.graphmod.obj.GetAlpha()
                  obj_alpha = obj.opacity
                  #print 'TransparencyDialog.handleDTChanged',obj_name,obj_alpha
                  #obj_name = a.split('(')[1].split(',')[1].split(')')[0].replace("'","")
                  label = QtGui.QLabel(obj_name)
                  slider = mgWidgets.mgSlider(QtCore.Qt.Horizontal)
                  slider.setRange(0.0,1.0)
                  slider.setSingleStep(0.02)
                  slider.setValue(obj_alpha)
                  slider.slider.setTracking(1)
                  self.objectLayout.addWidget(label,self.objectLayout.rowCount(),0)
                  self.objectLayout.addWidget(slider,self.objectLayout.rowCount()-1,1)
                  #self.connect(slider.slider,QtCore.SIGNAL("doubleSliderMoved"),obj.graphmod.obj.SetAlpha)
                  #self.connect(slider.slider,QtCore.SIGNAL("doubleSliderMoved"),self.handleViewChanged)
		  import functools
                  #self.connect(slider,QtCore.SIGNAL("sliderReleased()"),functools.partial(self.setObjAlpha,obj.graphmod.obj,slider))
                  self.connect(slider,QtCore.SIGNAL("sliderReleased()"),functools.partial(self.setObjAlpha,obj,slider))
                  self.connect(slider,QtCore.SIGNAL("sliderReleased()"),self.handleViewChanged)
		except:
                  pass
		
    def setObjAlpha(self,object,slider):
	    #object.SetAlpha(slider.value())
            object.set_opacity(slider.value())

    def handleRadioChanged(self,checked=False):
	glWidgets = global_definitions.GLWIDGETS()
	for glWidget in glWidgets:
		glWidget.setTransparency(checked)
	rebuild.UpdateDisplay()	

    def UnClose(self):
        self.show()
        self.raise_()

    def Close(self):
        self.hide()

    def __init__(self,parent=None):
	MGSimpleDialog.MGSimpleDialog.__init__(self,parent)
        margin = 0
        
        self.menu = "Display"
	self.setWindowTitle(self.tr("Transparency"))
        self.setSizeGripEnabled(0)
        #self.CreateMenuEntry()
	layout = QtGui.QVBoxLayout()
	glWidgets = global_definitions.GLWIDGETS()
	self.onoff = QtGui.QRadioButton(self.tr("Use slow proper depth-sorted transparency"),self)
        self.onoff.setChecked(glWidgets[0].transparency())
	layout.addWidget(self.onoff)
	self.connect(self.onoff,QtCore.SIGNAL("clicked(bool)"),self.handleRadioChanged)
	dt = global_definitions.DISPLAYTABLE()
	self.connect(dt,QtCore.SIGNAL("DisplayTableChanged"),self.handleDTChanged)
	self.objectLayout = QtGui.QGridLayout()
        self.objectLayout.setContentsMargins(margin,margin,margin,margin)
        self.objectLayout.setSpacing(margin)
	gb = QtGui.QGroupBox(self.tr("Object opacities"))
	layout.addSpacing(10)
	layout.addWidget(gb)
	gb.setLayout(self.objectLayout)
	self.setLayout(layout)
        #self.Close()
        self.handleDTChanged()
