"""
     python/ui/Application.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 dataobj import Dependency

def getparams():
  for app in Application.application_insts:
    Application.saved_params[app.name] = app.getparams()
  #print 'Application.getparams',Application.saved_params
  return Application.saved_params

def setparams(params):
  #print 'Application.setparams',params
  Application.saved_params = {}
  for key,value in params.items():
    Application.saved_params[key] = value
  
class Application(Dependency):

  application_names = []
  application_insts = []
  saved_params = {}

#--------------------------------------------------------------------
  def __init__(self,name=''):
#--------------------------------------------------------------------
    self.name = name
    Application.application_names.append(name)
    Application.application_insts.append(self)
    self.objinsts = []
    self.objnames = []    
    Dependency.__init__(self)
    #print 'Application.init ',self.name,Application.application_names,Application.saved_params.keys()
    if Application.saved_params.has_key(self.name):
      #print 'Application.init ',self.name,Application.saved_params[self.name]
      self.setparams(Application.saved_params[self.name])
    
#--------------------------------------------------------------------
  def delete(self):
#--------------------------------------------------------------------
    if self.application_insts.count(self):
      ii = self.application_insts.index(self)
      self.application_insts.pop(ii)
      self.application_names.pop(ii)
    Dependency.delete(self)
    #Remove all dependencies
    self.remove_all_dependency()
    if len (self.objinsts) > 0:
      #Make an independent list or Python get confused by
      #items disappearing from objinst
      allobj = []
      for item in self.objinsts: allobj.append(item)
      for obj in allobj:
        obj.delete()

#-------------------------------------------------------------------
  def getparams(self):
#--------------------------------------------------------------------
    return {}
  
#--------------------------------------------------------------------
  def setparams(self,params,**kw):
#--------------------------------------------------------------------
    #print 'Application.setparams',params
    params.update(kw)
    for key,value in params.items():
      setattr(self,key,value)
    
    
#---------------------------------------------------------------------------
  def close(self):
#---------------------------------------------------------------------------
    # Exit the application but do not delete
    self.currently_open = 0

    # Remove graphical objects
    if len (self.objinsts) > 0:
      #Make an independent list or Python get confused by
      #items disappearing from objinst
      allobj = []
      for item in self.objinsts: allobj.append(item)
      for obj in allobj:
        obj.delete()

  def Exit(self):
    pass

  def draw(self):
    pass
  
#-----------------------------------------------------------------------
  def add_dispobj(self,dispobj):
#-----------------------------------------------------------------------
    self.objnames.append(dispobj.name)
    self.objinsts.append(dispobj)


