class EditableComboBox(BaseComboBox): """ Editable combo box + Validate """ def __init__(self, parent): BaseComboBox.__init__(self, parent) self.setSizeAdjustPolicy(QComboBox.AdjustToMinimumContentsLength) self.font = QFont() self.connect(self, SIGNAL("editTextChanged(QString)"), self.validate) self.connect(self, SIGNAL("activated(QString)"), lambda qstr: self.validate(qstr, editing=False)) self.set_default_style() self.tips = { True: _("Press enter to validate this entry"), False: _('This entry is incorrect') } def show_tip(self, tip=""): """Show tip""" QToolTip.showText(self.mapToGlobal(self.pos()), tip, self) def set_default_style(self): """Set widget style to default""" self.font.setBold(False) self.setFont(self.font) self.setStyleSheet("") self.show_tip() def selected(self): """Action to be executed when a valid item has been selected""" BaseComboBox.selected(self) self.set_default_style() def validate(self, qstr, editing=True): """Validate entered path""" valid = self.is_valid(qstr) if self.hasFocus() and valid is not None: self.font.setBold(True) self.setFont(self.font) if valid: self.setStyleSheet("color:rgb(50, 155, 50);") else: self.setStyleSheet("color:rgb(200, 50, 50);") if editing: # Combo box text is being modified: invalidate the entry self.show_tip(self.tips[valid]) self.emit(SIGNAL('valid(bool)'), False) else: # A new item has just been selected if valid: self.selected() else: self.emit(SIGNAL('valid(bool)'), False) else: self.set_default_style()
class EditableComboBox(BaseComboBox): """ Editable combo box + Validate """ def __init__(self, parent): BaseComboBox.__init__(self, parent) self.setSizeAdjustPolicy(QComboBox.AdjustToMinimumContentsLength) self.font = QFont() self.connect(self, SIGNAL("editTextChanged(QString)"), self.validate) self.connect(self, SIGNAL("activated(QString)"), lambda qstr: self.validate(qstr, editing=False)) self.set_default_style() self.tips = {True: _("Press enter to validate this entry"), False: _('This entry is incorrect')} def show_tip(self, tip=""): """Show tip""" QToolTip.showText(self.mapToGlobal(self.pos()), tip, self) def set_default_style(self): """Set widget style to default""" self.font.setBold(False) self.setFont(self.font) self.setStyleSheet("") self.show_tip() def selected(self): """Action to be executed when a valid item has been selected""" BaseComboBox.selected(self) self.set_default_style() def validate(self, qstr, editing=True): """Validate entered path""" valid = self.is_valid(qstr) if self.hasFocus() and valid is not None: self.font.setBold(True) self.setFont(self.font) if valid: self.setStyleSheet("color:rgb(50, 155, 50);") else: self.setStyleSheet("color:rgb(200, 50, 50);") if editing: # Combo box text is being modified: invalidate the entry self.show_tip(self.tips[valid]) self.emit(SIGNAL('valid(bool)'), False) else: # A new item has just been selected if valid: self.selected() else: self.emit(SIGNAL('valid(bool)'), False) else: self.set_default_style()
def tuple_to_qfont(tup): """ Create a QFont from tuple: (family [string], size [int], italic [bool], bold [bool]) """ if not isinstance(tup, tuple) or len(tup) != 4 \ or not font_is_installed(tup[0]) \ or not isinstance(tup[1], int) \ or not isinstance(tup[2], bool) \ or not isinstance(tup[3], bool): return None font = QFont() family, size, italic, bold = tup font.setFamily(family) font.setPointSize(size) font.setItalic(italic) font.setBold(bold) return font