"""
     qtgui/FontSelector.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2001-2008 University of York, CCLRC
     Copyright (C) 2009-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.
"""
from PyQt4 import QtCore, QtGui
import os,sys
from xml.dom import minidom

class MGFontSelector(QtGui.QWidget):
    def setFont(self,font):
        self.setFontFamily(font.family())
        self.setFontSize(font.pointSize())
        self.setFontBold(font.bold())
        self.setFontItalic(font.italic())
        self.setFontUnderline(font.underline())

    def setFontSize(self,value):
        self.size_spin.setValue(int(value))

    def setFontFamily(self,value):
        #print "setFontFamily",value
        self.family_combo.setCurrentFont(QtGui.QFont(value))

    def setFontBold(self,value):
        if value == 0:
          self.bold_check.setCheckState(QtCore.Qt.Unchecked)
        else:
          self.bold_check.setCheckState(QtCore.Qt.Checked)

    def setFontItalic(self,value):
        if value == 0:
          self.italic_check.setCheckState(QtCore.Qt.Unchecked)
        else:
          self.italic_check.setCheckState(QtCore.Qt.Checked)

    def setFontUnderline(self,value):
        if value == 0:
          self.underline_check.setCheckState(QtCore.Qt.Unchecked)
        else:
          self.underline_check.setCheckState(QtCore.Qt.Checked)

    def getFont(self):
        font = QtGui.QFont(self.fontFamily(),self.fontSize())
        font.setUnderline(self.isUnderlined())
        font.setBold(self.isFontBold())
        font.setItalic(self.isFontItalic())
        return font

    def fontFamily(self):
        return str(self.family_combo.currentFont().family())

    def fontSize(self):
        return self.size_spin.value()

    def isFontBold(self):
        return self.bold_check.checkState()

    def isFontItalic(self):
        return self.italic_check.checkState()

    def isUnderlined(self):
        return self.underline_check.checkState()

    def SizeChanged(self,size):
        self.FontChanged(self.family_combo.currentFont())

    def CheckBoxChanged(self,font):
        self.FontChanged(self.family_combo.currentFont())

    def FontChanged(self,font):
        font.setBold(self.bold_check.checkState())
        font.setItalic(self.italic_check.checkState())
        font.setUnderline(self.underline_check.checkState())
        self.family_combo.setFont(font)
        self.setFontSize(self.size_spin.value())
	self.emit(QtCore.SIGNAL("fontChanged"),(self))

    def setParams(self,params):
      if params.has_key('family'):
        self.setFontFamily(QtCore.QString(params['family']))
      if params.has_key('size'):
        self.setFontSize(params['size'])
      if params.has_key('weight'):
        self.setFontBold(params.get('weight')=='bold')
      if params.has_key('slant'):
        self.setFontItalic(params.get('slant')=='i')
      if params.has_key('underline'):
        self.setFontUnderline(params['underline'])

    def getParams(self):
      pars = {}
      pars['family'] = self.fontFamily()
      pars['size'] = self.fontSize()
      if self.isFontBold():
        pars['weight'] = 'bold'
      else:
        pars['weight'] = 'medium'
      if self.isFontItalic():
        pars['slant'] = 'i'
      else:
        pars['slant'] = 'r'
      pars['underline'] = self.isUnderlined()
      return pars


    def __init__(self,title='',min_size=8,max_size=100,parent=None):
        QtGui.QWidget.__init__(self,parent)
        layout = QtGui.QHBoxLayout(self)
        self.label = QtGui.QLabel(title)
        layout.addWidget(self.label)
        layout.addStretch(1)
        self.size_spin = QtGui.QSpinBox()
        self.size_spin.setRange(min_size,max_size)
        layout.addWidget(self.size_spin)
        self.family_combo = QtGui.QFontComboBox()
        layout.addWidget(self.family_combo)
        self.bold_check = QtGui.QCheckBox('Bold')
        self.italic_check = QtGui.QCheckBox('Italic')
        self.underline_check = QtGui.QCheckBox('Underline')
        layout.addWidget(self.bold_check)
        layout.addWidget(self.italic_check)
        layout.addWidget(self.underline_check)
        layout.addStretch(5)
        self.setLayout(layout)

        self.connect(self.family_combo,QtCore.SIGNAL("currentFontChanged(const QFont&)"),self.FontChanged)
        self.connect(self.bold_check,QtCore.SIGNAL("stateChanged(int)"),self.CheckBoxChanged)
        self.connect(self.italic_check,QtCore.SIGNAL("stateChanged(int)"),self.CheckBoxChanged)
        self.connect(self.underline_check,QtCore.SIGNAL("stateChanged(int)"),self.CheckBoxChanged)
        self.connect(self.size_spin,QtCore.SIGNAL("valueChanged(int)"),self.SizeChanged)
