"""Qt4 combobox utility classes"""


from PyQt4 import QtCore, QtGui 

#****************************************************************************
#
#****************************************************************************
class ComboBoxWithEditTipWrap(QtCore.QObject):
    """Wrapper class for comboboxes to show a tooltip if the text of the editbox
    is truncated"""

    def __init__(self, comboBox):
        QtCore.QObject.__init__(self, comboBox)

        self.connect(
            comboBox,
            QtCore.SIGNAL('currentIndexChanged(const QString &)'),
            self._adjustToolTip
            )

        comboBox.installEventFilter(self)
        self.comboBox = comboBox
                
        
    def eventFilter(self, obj, event):
        if event.type() == event.Resize:
            self._adjustToolTip(self.comboBox.currentText())
        return False
        
        
    def _adjustToolTip(self, text):
        fm = self.comboBox.fontMetrics()
        style = self.comboBox.style()
        rc = self.comboBox.contentsRect()
        rc2 = style.subControlRect(
            style.CC_ComboBox,
            QtGui.QStyleOptionComboBox(),
            style.SC_ComboBoxEditField,
            )

        cx = rc.width() + rc2.width()
        w = fm.width(text)
        if w > cx -2:
            self.comboBox.setToolTip(text)
        else:
            self.comboBox.setToolTip('')
