"""
     tasks/edstats/CTaskEdstats.py: CCP4 GUI Project
     Copyright (C) 2014 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.
"""

"""
     Jon Agirre         2014 - Started development

"""
from PyQt4 import QtGui,QtCore
import CCP4TaskWidget
import CCP4Widgets
import multiprocessing

class CTaskEdstats(CCP4TaskWidget.CTaskWidget):

# Subclass CTaskWidget to give specific task window
  TASKNAME = 'edstats'
  TASKVERSION = 0.1
  TASKMODULE='validation'
  TASKTITLE='Analyse agreement between model and density - EDSTATS'
  SHORTTASKTITLE='EDSTATS'
  DESCRIPTION='Calculates real-space metrics for evaluating the agreement between model and density (Edstats, cfft)'
  WHATNEXT = [ 'coot_rebuild' ]
  RANK=1
  
  def __init__(self,parent):
    CCP4TaskWidget.CTaskWidget.__init__(self,parent)

  def drawContents(self):

    self.setProgramHelpFile('edstats')
    

# the input data tab starts here

    folder = self.openFolder(folderFunction='inputData',title='Input Data' )

    self.createLine( [ 'subtitle', 'Model to analyse', 'Input your model in PDB or mmCIF format' ] )
    self.openSubFrame ( frame=[True] )
    self.createLine( [ 'widget', 'XYZIN'])
    self.closeSubFrame ( )

    self.createLine( [ 'subtitle', 'Map coefficients' , 'Input your 2mFo-DFc and mFo-DFc map coefficients'  ] )
    self.openSubFrame ( frame=[True] )
    self.createLine( [ 'advice', 'Electron density map' ] )
    self.createLine( [ 'widget', 'FPHIIN1' ] )
    
    self.createLine( [ 'advice', 'Difference density map' ] )
    self.createLine( [ 'widget', 'FPHIIN2' ] )
    self.closeSubFrame ( )
    
    self.closeFolder() 

    folder = self.openFolder(folderFunction='controlParameters', title='Options' )

    self.createLine( [ 'advice', 'Specify the resolution range you would like to use' ] )
    self.createLine( [ 'label', 'Low resolution limit ', 'widget', 'RES_LOW', 'stretch', 1, 'label', 'High resolution limit ' , 'widget', 'RES_HIGH'  ] )

    self.createLine( [ 'advice', 'Map values are averaged separately for main- and side-chains' ] )
    self.createLine( [ 'label', 'Main-chain averaging is to be performed across ', 'widget', 'MAIN_AVERAGING' ] )
    self.createLine( [ 'label', 'Side-chain averaging is to be performed across ', 'widget', 'SIDE_AVERAGING' ] )
    
    self.createLine( [ 'widget', 'SCALING', 'label', 'Rescale Q-Q plot using ', 'widget', 'SCALING_TYPE' ] ) 

    self.connect ( self.container.inputData.FPHIIN1, QtCore.SIGNAL('dataChanged'), self.updateLimits )

    self.createLine( [ 'advice', 'Adjust the rejection criteria for flagging up the outliers in Coot' ] )
    self.openSubFrame ( frame=[True] )
    self.createLine( [ 'label', 'Accuracy metrics: RZ- < ', 'widget', 'SIGMA_RZ_MINUS', 'label', ' sigma', 'stretch', 1, 'label', 'RZ+ > ', 'widget', 'SIGMA_RZ_PLUS', 'label', ' sigma' ] )
    self.createLine( [ 'label', 'Precision metric: RO < ', 'widget', 'SIGMA_RO' ] )
    self.closeSubFrame ( )

    self.closeFolder() 
    self.updateLimits ( )

  def updateLimits ( self ):

    if self.container.inputData.FPHIIN1.isSet ( ):
        self.container.inputData.FPHIIN1.loadFile()
        self.container.inputData.RES_LOW.set ( '%.2f'%( self.container.inputData.FPHIIN1.fileContent.resolutionRange.low ) )
        self.container.inputData.RES_HIGH.set ( '%.2f'%( self.container.inputData.FPHIIN1.fileContent.resolutionRange.high ) )
        self.getWidget ( 'RES_LOW' ).validate ( )
        self.getWidget ( 'RES_HIGH' ).validate ( )
    return 
