"""
     qtgui/CCP4Peferences.py: CCP4 Gui Project
     Copyright (C) 2011 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.
"""

'''
    Liz Potterton - create class to maintain GUIPreferences - Sept 2011
'''
from PyQt4 import QtCore,QtGui


class CPreferencesWindow(QtGui.QDialog):

  def __init__(self,parent):
    QtGui.QDialog.__init__(self,parent)
    self.setLayout(QtGui.QVBoxLayout())

    import CCP4Modules

    #print 'CPreferencesWindow.__init__',CCP4Modules.QTAPPLICATION().
   
    widgetClass = CCP4Modules.TASKMANAGER().getTaskWidgetClass('guipreferences')
    widget = widgetClass(self,layoutMode='TAB')
    prefContainer = CCP4Modules.PREFERENCES()
    widget.setContainer(prefContainer)
    widget.draw()
    self.layout().addWidget(widget)
    #except:
    #  pass
    self.buttons = QtGui.QDialogButtonBox(self)
    but = self.buttons.addButton(QtGui.QDialogButtonBox.Apply)
    self.connect(but,QtCore.SIGNAL('clicked()'),self.doApply)
    but = self.buttons.addButton(QtGui.QDialogButtonBox.Close)
    self.connect(but,QtCore.SIGNAL('clicked()'),self.close)
    line = QtGui.QHBoxLayout()
    line.addStretch(0.2)
    line.addWidget(self.buttons)
    line.addStretch(0.2)
    self.layout().addLayout(line)


  def doApply(self):
    import CCP4Preferences,CCP4Modules,CCP4ProjectManagerGui
    CCP4Preferences.CPreferences.insts.save()
    try:
      CCP4Preferences.CPreferences.insts.EXEPATHLIST.setupExeLookup()
    except:
      pass

    try:
      CCP4Modules.QTAPPLICATION().setNamedStyle(CCP4Preferences.CPreferences.insts.WINDOWS_STYLE)
    except:
      CCP4Modules.QTAPPLICATION().setNamedStyle(CCP4Preferences.CPreferences.insts.WINDOWS_STLYE)
    
    import CCP4StyleSheet,CCP4WebView,CCP4ProjectViewer
    CCP4StyleSheet.setStyleSheet()
    CCP4WebView.setGlobalSettings()

    for pV in CCP4ProjectViewer.CProjectViewer.Instances:
      try:
        pV.toolBar('project').setToolButtonStyle(int(CCP4Preferences.CPreferences.insts.TOOLBARBUTTONSSTYLE))
      except:
        pass

  def close(self):
    self.doApply()
    QtGui.QDialog.close(self)