1from AnyQt.QtCore import Qt, QSize, QEvent 2from AnyQt.QtGui import QKeySequence 3from AnyQt.QtWidgets import QToolButton, QSizePolicy, QStyle, QToolTip 4 5from orangewidget.utils.buttons import ( 6 VariableTextPushButton, SimpleButton, 7) 8__all__ = [ 9 "VariableTextPushButton", "SimpleButton", "FixedSizeButton", 10] 11 12 13class FixedSizeButton(QToolButton): 14 15 def __init__(self, *args, defaultAction=None, **kwargs): 16 super().__init__(*args, **kwargs) 17 sh = self.sizePolicy() 18 sh.setHorizontalPolicy(QSizePolicy.Fixed) 19 sh.setVerticalPolicy(QSizePolicy.Fixed) 20 self.setSizePolicy(sh) 21 self.setAttribute(Qt.WA_WState_OwnSizePolicy, True) 22 23 if defaultAction is not None: 24 self.setDefaultAction(defaultAction) 25 26 def sizeHint(self): 27 style = self.style() 28 size = (style.pixelMetric(QStyle.PM_SmallIconSize) + 29 style.pixelMetric(QStyle.PM_ButtonMargin)) 30 return QSize(size, size) 31 32 def event(self, event: QEvent) -> bool: 33 if event.type() == QEvent.ToolTip and self.toolTip(): 34 action = self.defaultAction() 35 if action is not None and action.toolTip(): 36 text = tooltip_with_shortcut(action.toolTip(), 37 action.shortcut()) 38 QToolTip.showText(event.globalPos(), text) 39 return True 40 return super().event(event) 41 42 43def tooltip_with_shortcut(tool_tip, shortcut: QKeySequence) -> str: 44 text = [] 45 if tool_tip: 46 text.append("<span>{}</span>".format(tool_tip)) 47 if not shortcut.isEmpty(): 48 text.append("<kbd>{}</kbd>" 49 .format(shortcut.toString(QKeySequence.NativeText))) 50 return " ".join(text) 51