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

     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.
"""


from PyQt4 import QtGui,QtCore
from global_definitions import *
import sys,os
import guiUtils,mgWidgets,MGSimpleDialog
import functools
import ExponentialSlider

def slabAndFog():
      gui = MAINWINDOW().findChild(slabAndFogGUI)
      if not gui: gui = slabAndFogGUI(MAINWINDOW())

      gui.UnClose()
      #gui.raise_()


class FrontBackStartEndExpSlider(QtGui.QWidget):
	def __init__(self,parent=None):
		QtGui.QWidget.__init__(self,parent)
		layout = QtGui.QVBoxLayout()
                sliderLayout = QtGui.QGridLayout()
                sliderLayout.setContentsMargins(0,0,0,0)
                sliderLayout.setSpacing(2)
		self.thicknessSlider = ExponentialSlider.ExponentialSlider(reversed=True,labels=(QtCore.QCoreApplication.translate("QtGui.QWidget",'Denser'),QtCore.QCoreApplication.translate("QtGui.QWidget",'Less dense')))
		self.thicknessSlider.setRange(1e-3,2000)
		label = QtGui.QLabel(self.tr("Density"))
		sliderLayout.addWidget(label,0,0)
		sliderLayout.addWidget(self.thicknessSlider,0,1)
		self.positionSlider = ExponentialSlider.SignedExponentialSlider(reversed=False,labels=(QtCore.QCoreApplication.translate("QtGui.QWidget",'Closer'),QtCore.QCoreApplication.translate("QtGui.QWidget",'Further')))
		self.positionSlider.setRange(1e-3,2000)
		label = QtGui.QLabel(self.tr("Start"))
		sliderLayout.addWidget(label,1,0)
		sliderLayout.addWidget(self.positionSlider,1,1)
		layout.addLayout(sliderLayout)
		self.setLayout(layout)
		self.connect(self.positionSlider,QtCore.SIGNAL('sliderReleased'),self.valuesChanged)
		#self.connect(self.positionSlider,QtCore.SIGNAL('sliderMoved'),self.valuesChanged)
		self.connect(self.positionSlider,QtCore.SIGNAL('sliderValueChanged'),self.valuesChanged)
		self.connect(self.positionSlider,QtCore.SIGNAL('textEditingFinished'),self.valuesChanged)
		self.connect(self.positionSlider,QtCore.SIGNAL('textChanged'),self.valuesChanged)
		self.connect(self.thicknessSlider,QtCore.SIGNAL('sliderReleased'),self.valuesChanged)
		#self.connect(self.thicknessSlider,QtCore.SIGNAL('sliderMoved'),self.valuesChanged)
		self.connect(self.thicknessSlider,QtCore.SIGNAL('sliderValueChanged'),self.valuesChanged)
		self.connect(self.thicknessSlider,QtCore.SIGNAL('textEditingFinished'),self.valuesChanged)
		self.connect(self.thicknessSlider,QtCore.SIGNAL('textChanged'),self.valuesChanged)

	def valuesChanged(self,**args):
		self.emit(QtCore.SIGNAL('ValuesChanged'))

	def getNear(self):
		return self.positionSlider.value()

	def getFar(self):
		return self.positionSlider.value()+self.thicknessSlider.value()

	def setNearFar(self,near,far):
		self.positionSlider.setValue(near)
		self.thicknessSlider.setValue(far-near)
		pass

class FrontBackStartEnd(QtGui.QWidget):
	def __init__(self,parent=None,frontLabel='front',backLabel='back',widthLabel='width',posLabel='position'):
		QtGui.QWidget.__init__(self,parent)
		layout = QtGui.QGridLayout()
		layout.setContentsMargins(2,2,2,2)
		layout.setSpacing(2)
		widget = QtGui.QLabel(self.tr(frontLabel))
		layout.addWidget(widget,0,0)
		self.near = QtGui.QDoubleSpinBox()
		self.near.setRange(-10000.0,10000.0)
		self.far = QtGui.QDoubleSpinBox()
		self.far.setRange(-10000.0,10000.0)
		layout.addWidget(self.near,0,1)
		widget = QtGui.QLabel(self.tr(backLabel))
		layout.addWidget(widget,0,2)
		layout.addWidget(self.far,0,3)
		widget = QtGui.QLabel(self.tr(widthLabel))
		layout.addWidget(widget,1,0)
		self.width = QtGui.QDoubleSpinBox()
		self.width.setRange(0,10000.0)
		self.position = QtGui.QDoubleSpinBox()
		self.position.setRange(-10000.0,10000.0)
		layout.addWidget(self.width,1,1)
		widget = QtGui.QLabel(self.tr(posLabel))
		layout.addWidget(widget,1,2)
		layout.addWidget(self.position,1,3)
		self.setLayout(layout)

		self.connect(self.near,QtCore.SIGNAL("valueChanged(double)"),functools.partial(self.valuesChanged,"near"))
		self.connect(self.far,QtCore.SIGNAL("valueChanged(double)"),functools.partial(self.valuesChanged,"far"))
		self.connect(self.width,QtCore.SIGNAL("valueChanged(double)"),functools.partial(self.valuesChanged,"width"))
		self.connect(self.position,QtCore.SIGNAL("valueChanged(double)"),functools.partial(self.valuesChanged,"position"))

	def valuesChanged(self,name):
		self.disconnectSignals()
		try:
			if name == "near" or name == "far":
				self.calcAndSetWidthPosition()
				if self.near.value() > self.far.value():
					if name == "near":
						self.far.setValue(self.near.value())
					else:
						self.near.setValue(self.far.value())
			if name == "width" or name == "position":
				self.calcAndSetNearFar()
			self.emit(QtCore.SIGNAL('ValuesChanged'))
		except:
			pass
		self.connectSignals()

	def disconnectSignals(self):
		self.near.blockSignals(True)
		self.far.blockSignals(True)
		self.width.blockSignals(True)
		self.position.blockSignals(True)

	def connectSignals(self):
		self.near.blockSignals(False)
		self.far.blockSignals(False)
		self.width.blockSignals(False)
		self.position.blockSignals(False)

	def getWidth(self):
		return self.width.value()

	def getPosition(self):
		return self.position.value()

	def getNear(self):
		return self.near.value()

	def getFar(self):
		return self.far.value()

	def setNear(self,near):
		self.disconnectSignals()
		try:
			self.near.setValue(near)
			if self.near.value() > self.far.value():
				self.far.setValue(self.near.value())
			self.calcAndSetWidthPosition()
		except:
			pass
		self.connectSignals()

	def setFar(self,far):
		self.disconnectSignals()
		try:
			self.far.setValue(far)
			if self.near.value() > self.far.value():
				self.near.setValue(self.far.value())
			self.calcAndSetWidthPosition()
		except:
			pass
		self.connectSignals()

	def setNearFar(self,near,far):
		self.disconnectSignals()
		try:
			self.near.setValue(near)
			self.far.setValue(far)
			if self.near.value() > self.far.value():
				self.near.setValue(self.near.value())
			self.calcAndSetWidthPosition()
		except:
			pass
		self.connectSignals()

	def setWidth(self,width):
		self.disconnectSignals()
		try:
			self.width.setValue(width)
			self.calcAndSetNearFar()
		except:
			pass
		self.connectSignals()

	def setPosition(self,position):
		self.disconnectSignals()
		try:
			self.position.setValue(position)
			self.calcAndSetNearFar()
		except:
			pass
		self.connectSignals()

	def setWidthPosition(self,width,position):
		self.disconnectSignals()
		try:
			self.width.setValue(width)
			self.position.setValue(position)
			self.calcAndSetNearFar()
		except:
			pass
		self.connectSignals()

	def calcAndSetNearFar(self):
		self.disconnectSignals()
		try:
			near = self.position.value() - self.width.value()/2.
			far = self.position.value() + self.width.value()/2.
			self.near.setValue(near)
			self.far.setValue(far)
		except:
			pass
		self.connectSignals()

	def calcAndSetWidthPosition(self):
		self.disconnectSignals()
		try:
			position = (self.far.value() + self.near.value())/2.
			width = (self.far.value() - self.near.value())
			self.position.setValue(position)
			self.width.setValue(width)
		except:
			pass
		self.connectSignals()

#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------------------------------------------------
class slabAndFogGUI(MGSimpleDialog.MGSimpleDockDialog):
#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------------------------------------------------

    def minimumSizeHint(self):
      if sys.platform == "darwin":
        return QtCore.QSize(260,300)
      return QtCore.QSize(260,310)
  
#-------------------------------------------------------------------
    def __init__(self,parent=None):
#-------------------------------------------------------------------
        MGSimpleDialog.MGSimpleDockDialog.__init__(self,parent,defaultDockArea=QtCore.Qt.RightDockWidgetArea,tabify=False)
        self.setWindowTitle('Clip and fog')
        self.setObjectName('slab_n_fog')

        #self.setMinimumSize(QtCore.QSize(600,600))
        layout = QtGui.QVBoxLayout()

	line_layout = QtGui.QHBoxLayout()
        self.fog_on = QtGui.QCheckBox(self.tr('Depth cue fog'),self)
        line_layout.addWidget(self.fog_on)
	layout.addLayout(line_layout)
	line_layout = QtGui.QHBoxLayout()
	#self.fog_fb = FrontBackStartEnd(frontLabel='start',backLabel='end',widthLabel='width',posLabel='position')
	self.fog_fb = FrontBackStartEndExpSlider()
	self.fog_fb.setContentsMargins(0,0,0,0)
        line_layout.addWidget(self.fog_fb)
	layout.addLayout(line_layout)
	#layout.addSpacing(2)


	line_layout = QtGui.QHBoxLayout()
        self.slab_on = QtGui.QCheckBox(self.tr('Clipping with'),self)
        line_layout.addWidget(self.slab_on)
	line_layout.setContentsMargins(0,0,0,0)
	layout.addLayout(line_layout)
	line_layout = QtGui.QHBoxLayout()
	self.slab_fb = FrontBackStartEnd(frontLabel='front',backLabel='back',widthLabel='width',posLabel='position')
	line_layout.setContentsMargins(0,0,0,0)
        line_layout.addWidget(self.slab_fb)
	layout.addLayout(line_layout)
	line_layout = QtGui.QHBoxLayout()
        self.clip_cap = QtGui.QCheckBox(self.tr('Cap perfect spheres'),self)
        customClipButton = QtGui.QPushButton(self.tr('Custom planes..'),self)
        line_layout.addWidget(self.clip_cap)
        line_layout.addWidget(customClipButton)
	line_layout.setContentsMargins(0,0,0,0)
	layout.addLayout(line_layout)

	layout.addStretch(2)
	layout.setContentsMargins(5,5,5,5)
        self.setLayout(layout)

        self.connect(GLWIDGETS()[0],QtCore.SIGNAL('SlabChanged'),self.reset)
        self.connect(GLWIDGETS()[0],QtCore.SIGNAL('FogChanged'),self.reset)
        self.connect(self.fog_on,QtCore.SIGNAL('clicked()'),self.apply)
        self.connect(self.slab_on,QtCore.SIGNAL('clicked()'),self.apply)
        self.connect(self.clip_cap,QtCore.SIGNAL('clicked()'),self.apply)
        self.connect(self.fog_fb,QtCore.SIGNAL("ValuesChanged"),self.apply)
        self.connect(self.slab_fb,QtCore.SIGNAL("ValuesChanged"),self.apply)
        self.connect(customClipButton,QtCore.SIGNAL("clicked(bool)"),self.customClipGuiDraw)

        self.reset()

#-------------------------------------------------------------------
    def customClipGuiDraw(self,checked=False):
#-------------------------------------------------------------------
        dialog = QtGui.QDialog(self)
        dialog.setWindowTitle(self.tr("Custom clipping planes"))
        layout = QtGui.QGridLayout(dialog)
        dv = QtGui.QDoubleValidator(self)

        rowCount = layout.rowCount()
        label = QtGui.QLabel(self.tr("On"),self)
        layout.addWidget(label,0,1)
        label = QtGui.QLabel(self.tr("Plane"),self)
        layout.addWidget(label,0,2,1,4)

        def changed(**args):
          customClipPlanes = []
          for i in range(1,5):
            plane = {}
            plane["visible"] = int(layout.itemAtPosition(i,1).widget().isChecked())
            thePlane = []
            for j in range(2,6):
              try:
                thePlane.append(float(layout.itemAtPosition(i,j).widget().text()))
              except:
                thePlane.append(0)
            plane["plane"] = tuple(thePlane)
            customClipPlanes.append(plane)

          if len(GLWIDGETS())>0:
            GLWIDGETS()[0].setCustomClipPlanes(customClipPlanes)
            GLWIDGETS()[0].updateGL()

        for i in range(4):
           rowCount = layout.rowCount()
           label = QtGui.QLabel(str(i+1),self)
           layout.addWidget(label,rowCount,0)
           viscb = QtGui.QCheckBox(self)
           viscb.clicked.connect(changed)
           layout.addWidget(viscb,rowCount,1)
           a = QtGui.QLineEdit(self)
           a.setValidator(dv)
           a.setText("0.0")
           a.textEdited.connect(changed)
           layout.addWidget(a,rowCount,2)
           b = QtGui.QLineEdit(self)
           b.setValidator(dv)
           b.setText("0.0")
           b.textEdited.connect(changed)
           layout.addWidget(b,rowCount,3)
           c = QtGui.QLineEdit(self)
           c.setValidator(dv)
           c.setText("0.0")
           c.textEdited.connect(changed)
           layout.addWidget(c,rowCount,4)
           d = QtGui.QLineEdit(self)
           d.setValidator(dv)
           d.setText("0.0")
           d.textEdited.connect(changed)
           layout.addWidget(d,rowCount,5)

        dbb = QtGui.QDialogButtonBox(self)
        pb = dbb.addButton(QtGui.QDialogButtonBox.Close)
        self.connect(pb,QtCore.SIGNAL("clicked(bool)"),dialog.close)

        if len(GLWIDGETS())>0:
          if hasattr(GLWIDGETS()[0],"customClipPlanes"):
             pl = GLWIDGETS()[0].customClipPlanes
             for i in range(len(pl)):
               layout.itemAtPosition(i+1,1).widget().blockSignals(True)
               if pl[i].has_key("visible") and pl[i]["visible"]:
                 layout.itemAtPosition(i+1,1).widget().setChecked(True)
               else:
                 layout.itemAtPosition(i+1,1).widget().setChecked(False)
               layout.itemAtPosition(i+1,1).widget().blockSignals(False)
               if pl[i].has_key("plane") and len(pl[i]["plane"])==4:
                 layout.itemAtPosition(i+1,2).widget().blockSignals(True)
                 layout.itemAtPosition(i+1,3).widget().blockSignals(True)
                 layout.itemAtPosition(i+1,4).widget().blockSignals(True)
                 layout.itemAtPosition(i+1,5).widget().blockSignals(True)
                 layout.itemAtPosition(i+1,2).widget().setText(str(pl[i]["plane"][0]))
                 layout.itemAtPosition(i+1,3).widget().setText(str(pl[i]["plane"][1]))
                 layout.itemAtPosition(i+1,4).widget().setText(str(pl[i]["plane"][2]))
                 layout.itemAtPosition(i+1,5).widget().setText(str(pl[i]["plane"][3]))
                 layout.itemAtPosition(i+1,2).widget().blockSignals(False)
                 layout.itemAtPosition(i+1,3).widget().blockSignals(False)
                 layout.itemAtPosition(i+1,4).widget().blockSignals(False)
                 layout.itemAtPosition(i+1,5).widget().blockSignals(False)

        dialog.setLayout(layout)

        layout.addWidget(dbb)
        dialog.exec_()

#-------------------------------------------------------------------
    def apply(self):
#-------------------------------------------------------------------
      for widget in GLWIDGETS():
         widget.blockSignals(True)

      params = self.getParams()
      for widget in GLWIDGETS():
        widget.setFog(params['fog_on'])
        widget.setFogNearAndFar(params['fog_near'],params['fog_far'])
        widget.setSlab(params['slab_on'],params['slab_width'],params['slab_offset'],clip_cap=params['clip_cap'])
        
      MAINWINDOW().updateGLWindows()
      MAINWINDOW().GLLeftMouseReleased()

      for widget in GLWIDGETS():
         widget.blockSignals(False)
#

#-------------------------------------------------------------------
    def setParams(self,params={},**kw):
#-------------------------------------------------------------------

      params.update(kw)
      for key in ['fog_on','slab_on','clip_cap']:
        if params.has_key(key):
          getattr(self,key).setChecked(params[key])

      if params.has_key("fog_near") and params.has_key("fog_far") and hasattr(self,"fog_fb"):
        self.fog_fb.setNearFar(params["fog_near"],params["fog_far"])
      if params.has_key("slab_width") and params.has_key("slab_offset") and hasattr(self,"slab_fb"):
        self.slab_fb.setWidthPosition(params["slab_width"],params["slab_offset"])

#-------------------------------------------------------------------
    def getParams(self):
#-------------------------------------------------------------------
      pars = {}
      for key in ['fog_on','slab_on','clip_cap']:
	if hasattr(self,key):
          pars[key] = getattr(self,key).isChecked()
      if hasattr(self,"slab_fb"):
         pars["slab_width"] = self.slab_fb.getWidth()
         pars["slab_offset"] = self.slab_fb.getPosition()
      if hasattr(self,"fog_fb"):
         pars["fog_near"] = self.fog_fb.getNear()
         pars["fog_far"] = self.fog_fb.getFar()
      return pars

#-------------------------------------------------------------------
    def reset(self):
#-------------------------------------------------------------------
      
      self.fog_on.blockSignals(True)
      self.slab_on.blockSignals(True)
      self.clip_cap.blockSignals(True)
      self.fog_fb.blockSignals(True)
      self.slab_fb.blockSignals(True)

      glWidget= GLWIDGETS()[0]
      self.setParams( fog_on = glWidget.fog_enabled,
                     fog_near = glWidget.fog_near,
                     fog_far = glWidget.fog_far,
                     slab_on = glWidget.slab_enabled,
                     clip_cap = glWidget.clip_cap,
                     slab_width = glWidget.slab_width,
                     slab_offset = glWidget.slab_offset )
      
      self.fog_on.blockSignals(False)
      self.slab_on.blockSignals(False)
      self.clip_cap.blockSignals(False)
      self.fog_fb.blockSignals(False)
      self.slab_fb.blockSignals(False)
