"""
     python/ui/GUI.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2001-2008 University of York, CCLRC
     Copyright (C) 2009-2010 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 global_definitions import *
from PyQt4 import QtCore,QtGui


class GUI(QtCore.QObject):
  insts = []

#------------------------------------------------------------------
  def __init__( self ):
#------------------------------------------------------------------
    GUI.insts = self
    QtCore.QObject.__init__(self,MAINWINDOW())

#-------------------------------------------------------------------
  def SetCursor ( self,cursor='' ):
#-------------------------------------------------------------------
    #print "SetCursor",cursor
    self.Send("mg SetCursor "+cursor)
  

#--------------------------------------------------------------------
  def WarningMessage (self,text,help='',target='',title='',**kw):
#--------------------------------------------------------------------
    import os, sys
    if sys.argv.count('-quit'):
	    print text
	    return
    
    if not help:
      QtGui.QMessageBox.warning(None,title,text)
    else:
      reply = QtGui.QMessageBox.warning(None,title,text,QtGui.QMessageBox.Ok|QtGui.QMessageBox.Help)
      if reply == QtGui.QMessageBox.Help:
        HELPBROWSER().loadMgHelp(file=help+'.html')

      
#----------------------------------------------------------------------
  def handle_ListData_pick ( self, pick_dataobj='',line='', \
                             mode='single',**keywords):
#----------------------------------------------------------------------
    #print "handle_ListData_pick",line
    import dataobj
    import re
    import string
    
    # Beware pick_dataobj is potentially a list of objects - eg from
    # picking a listing of a vector file
    pick_dataobj_list = string.split(pick_dataobj)
    if pick_dataobj_list:
      dataobj_list=[]
      for item in pick_dataobj_list:
        obj = dataobj.get_dataobj(name=item)
        if obj: dataobj_list.append(obj[0]) 
    else:
      dataobj_list=dataobj.get_dataobj(object_type='MolData')
    #print "handle_ListData_pick dataobj_list",dataobj_list
    cid = ''
    for obj in dataobj_list:
      if obj.object_type == 'MolData':
        if re.match('ATOM|HETATM|ANISOU',line):
          cid = obj.pdbATOM2cid(line)
        else:
          cid = obj.find_cid_in_line(line=line)
      if not cid: return
      #print "cid",cid
      ifcentre = ['single','double'].index(mode)
      obj.centre_on(selection=cid,ifcentre=ifcentre)


#--------------------------------------------------------------------
  def About(self,**keywords):
#--------------------------------------------------------------------
    import utils,os,string
    about = 'CCP4 Molecular Graphics Program - presentation graphics and structure analysis\\n' + \
            'Copyright (C) 2001-2008 University of York, CCLRC\\n' + \
             'Copyright (C) 2009 University of York\\n' + \
            'http://www.ysbl.york.ac.uk/~ccp4mg\\n\\n'
    try:
      f = utils.openFile(os.path.join(os.environ["CCP4MG"],'lib','TIMESTAMP'),'r')
      if f:
        timestamp = string.split(f.read(),'\n')
        f.close()
        for line in timestamp: about = about + utils.tclSafe(line) + "\\n"
    except:
      pass

    #print "about",about
    self.WarningMessage(about,title="About CCP4mg")


