"""
     qtgui/animationGui.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.
"""
from PyQt4 import QtGui,QtCore
from global_definitions import *
import os
import mgPresentation,MGSimpleDialog

#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------------------------------------------------
class animationGui(mgPresentation.mgPresentation):
#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------------------------------------------------

#-------------------------------------------------------------------
  def __init__(self,parent,presentation=None):
#-------------------------------------------------------------------

    if not presentation:
      import os,utils,presentation
      anim_dir = os.path.join(utils.get_CCP4MG(),'default_animation.ccp4mg_presentation')
      if not os.path.exists(anim_dir):
        presentation.create_presentation_dir(anim_dir)
        p = presentation.presentation(anim_dir)
        p.pickle_movie(p.movie_file)
      else:
        p = presentation.presentation(anim_dir)
        p.unpickle_movie(p.movie_file)
      #print 'animationGui',anim_dir,p
    mgPresentation.mgPresentation.__init__(self,parent,presentation=p)

    self.setWindowTitle(self.tr("Animation: ")+self.presentation.identifier)


#-------------------------------------------------------------------
  def Close(self):
#-------------------------------------------------------------------
    #print "movieGui.Close",self.findChildren(movieTransformDetailsGui)
    mgPresentation.mgPresentation.Close(self)

#-------------------------------------------------------------------
  def getGuiDef(self,name='',pickevent=None):
#-------------------------------------------------------------------
      if name == 'menus':
        return [['action',self.tr('&Action'),'aboutToShow'],['edit',self.tr('&Edit')]]
      elif name == 'action':
        return ['animate','clear','close']
      elif name == 'edit':
        return ['preferences']
      
#-------------------------------------------------------------------
  def getActionDef(self,name,**info):
#-------------------------------------------------------------------
    import guiUtils
    if name == 'close':
      return dict ( 
        text = self.tr('Close animation'),
        tip = self.tr('Close this animation saving current status'),
        slot = self.Close  )

    def e(): return self.numberOfFrames(selected=1)>=2
    def a(): return self.presentation.animation_on
    
    if name == 'animate':
      return dict ( 
        text = self.tr('Animate'),
        tip = self.tr('Flip between selected scenes'),
        slot = guiUtils.partial( self.apply ,'animate'),
        enabled  = e,
        checkable = 1,
        checked = a )
    
    if name == 'clear':
      return dict ( 
        text = self.tr('Clear'),
        tip = self.tr('Remove all the saved scenes'),
        slot =  guiUtils.partial(self.apply,'clear') )
    
    if name == 'save':
      return dict ( 
        text = self.tr('Save animation'),
        tip = self.tr('Save this animation'),
        slot = self.saveAnimation
        )

    if name == 'preferences':
      return dict(
        text = self.tr('Animation Preferences'),
        tip = self.tr('Set time delay and order of scenes'),
        slot = self.preferencesGui )

    return mgPresentation.mgPresentation.getActionDef(self,name)
  


#-------------------------------------------------------------------
  def preferencesGui(self):
#-------------------------------------------------------------------
    if not hasattr(self,'preferences'):
      self.preferences = animationPreferencesGui(self)
      self.preferences.time.setValue(self.presentation.animation_time_delay)   
      self.preferences.mode.button(0).setChecked(1-self.presentation.animation_rock)
      self.preferences.mode.button(1).setChecked(self.presentation.animation_rock)
      self.connect(self.preferences.time,QtCore.SIGNAL('valueChanged(double)'),self.updatePreferences)
      self.connect(self.preferences.mode,QtCore.SIGNAL('buttonClicked(int)'),self.updatePreferences)

    self.preferences.show()
    
#-------------------------------------------------------------------
  def updatePreferences(self):
#-------------------------------------------------------------------
    value = self.preferences.time.value()
    #print 'animationGui.updatePreferences',value
    self.presentation.animation_time_delay = value
    mode = self.preferences.mode.checkedId()
    #print 'animationGui.updatePreferences',mode
    self.presentation.animation_rock = mode

#-------------------------------------------------------------------
  def apply(self,mode=''):
#-------------------------------------------------------------------
    #print "mgPresentation.apply",mode
    if mode == 'animate':  
      self.presentation.animate()
    elif mode == 'clear':
      self.deleteAllFrames()
    

  def saveAnimation(self):
    pass


#----------------------------------------------------------------------
#----------------------------------------------------------------------
#----------------------------------------------------------------------
class animationPreferencesGui(MGSimpleDialog.MGSimpleDialog):
#----------------------------------------------------------------------
#----------------------------------------------------------------------
#----------------------------------------------------------------------

#----------------------------------------------------------------------
  def __init__(self,parent=None):
#----------------------------------------------------------------------
    MGSimpleDialog.MGSimpleDialog.__init__(self,parent)

    #print "colourBlendGui MolData",MolData
    self.setObjectName('animation_preferences')
    self.setWindowTitle('Animation delay')    
    layout = QtGui.QVBoxLayout()
    
    line_layout = QtGui.QHBoxLayout()
    widget = QtGui.QLabel('Time delay',self)
    line_layout.addWidget(widget)
    self.time = QtGui.QDoubleSpinBox(self)
    self.time.setRange(0.1,10.0)
    self.time.setSingleStep(0.1)
    line_layout.addWidget(self.time)
    line_layout.addWidget(QtGui.QLabel('seconds'))
    layout.addLayout(line_layout)
    
    line_layout = QtGui.QHBoxLayout()
    self.mode= QtGui.QButtonGroup(self)
    widget= QtGui.QRadioButton('Cycle',self)
    line_layout.addWidget(widget)
    self.mode.addButton(widget,0)
    widget= QtGui.QRadioButton('oscillate',self)
    line_layout.addWidget(widget)
    self.mode.addButton(widget,1)
    line_layout.addWidget(QtGui.QLabel('through snapshots'))
    

    layout.addLayout(line_layout)

    line_layout = QtGui.QHBoxLayout()
    
    self.setLayout(layout)
