qt.util: HelpLabel, HelpButton, InfoButton: factor out HelpMixin
based on 3ca8854007
Co-authored-by: Calin Culianu <calin.culianu@gmail.com>
This commit is contained in:
@@ -105,19 +105,36 @@ class WWLabel(QLabel):
|
|||||||
self.setTextInteractionFlags(Qt.TextSelectableByMouse)
|
self.setTextInteractionFlags(Qt.TextSelectableByMouse)
|
||||||
|
|
||||||
|
|
||||||
class HelpLabel(QLabel):
|
class HelpMixin:
|
||||||
|
def __init__(self, help_text: str, *, help_title: str = None):
|
||||||
def __init__(self, text, help_text):
|
assert isinstance(self, QWidget), "HelpMixin must be a QWidget instance!"
|
||||||
QLabel.__init__(self, text)
|
|
||||||
self.help_text = help_text
|
self.help_text = help_text
|
||||||
|
self._help_title = help_title or _('Help')
|
||||||
|
if isinstance(self, QLabel):
|
||||||
|
self.setTextInteractionFlags(
|
||||||
|
(self.textInteractionFlags() | Qt.TextSelectableByMouse)
|
||||||
|
& ~Qt.TextSelectableByKeyboard)
|
||||||
|
|
||||||
|
def show_help(self):
|
||||||
|
custom_message_box(
|
||||||
|
icon=QMessageBox.Information,
|
||||||
|
parent=self,
|
||||||
|
title=self._help_title,
|
||||||
|
text=self.help_text,
|
||||||
|
rich_text=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class HelpLabel(HelpMixin, QLabel):
|
||||||
|
|
||||||
|
def __init__(self, text: str, help_text: str):
|
||||||
|
QLabel.__init__(self, text)
|
||||||
|
HelpMixin.__init__(self, help_text)
|
||||||
self.app = QCoreApplication.instance()
|
self.app = QCoreApplication.instance()
|
||||||
self.font = QFont()
|
self.font = self.font()
|
||||||
|
|
||||||
def mouseReleaseEvent(self, x):
|
def mouseReleaseEvent(self, x):
|
||||||
custom_message_box(icon=QMessageBox.Information,
|
self.show_help()
|
||||||
parent=self,
|
|
||||||
title=_('Help'),
|
|
||||||
text=self.help_text)
|
|
||||||
|
|
||||||
def enterEvent(self, event):
|
def enterEvent(self, event):
|
||||||
self.font.setUnderline(True)
|
self.font.setUnderline(True)
|
||||||
@@ -132,37 +149,23 @@ class HelpLabel(QLabel):
|
|||||||
return QLabel.leaveEvent(self, event)
|
return QLabel.leaveEvent(self, event)
|
||||||
|
|
||||||
|
|
||||||
class HelpButton(QToolButton):
|
class HelpButton(HelpMixin, QToolButton):
|
||||||
def __init__(self, text):
|
def __init__(self, text: str):
|
||||||
QToolButton.__init__(self)
|
QToolButton.__init__(self)
|
||||||
|
HelpMixin.__init__(self, text)
|
||||||
self.setText('?')
|
self.setText('?')
|
||||||
self.help_text = text
|
|
||||||
self.setFocusPolicy(Qt.NoFocus)
|
self.setFocusPolicy(Qt.NoFocus)
|
||||||
self.setFixedWidth(round(2.2 * char_width_in_lineedit()))
|
self.setFixedWidth(round(2.2 * char_width_in_lineedit()))
|
||||||
self.clicked.connect(self.onclick)
|
self.clicked.connect(self.show_help)
|
||||||
|
|
||||||
def onclick(self):
|
|
||||||
custom_message_box(icon=QMessageBox.Information,
|
|
||||||
parent=self,
|
|
||||||
title=_('Help'),
|
|
||||||
text=self.help_text,
|
|
||||||
rich_text=True)
|
|
||||||
|
|
||||||
|
|
||||||
class InfoButton(QPushButton):
|
class InfoButton(HelpMixin, QPushButton):
|
||||||
def __init__(self, text):
|
def __init__(self, text: str):
|
||||||
QPushButton.__init__(self, 'Info')
|
QPushButton.__init__(self, 'Info')
|
||||||
self.help_text = text
|
HelpMixin.__init__(self, text, help_title=_('Info'))
|
||||||
self.setFocusPolicy(Qt.NoFocus)
|
self.setFocusPolicy(Qt.NoFocus)
|
||||||
self.setFixedWidth(6 * char_width_in_lineedit())
|
self.setFixedWidth(6 * char_width_in_lineedit())
|
||||||
self.clicked.connect(self.onclick)
|
self.clicked.connect(self.show_help)
|
||||||
|
|
||||||
def onclick(self):
|
|
||||||
custom_message_box(icon=QMessageBox.Information,
|
|
||||||
parent=self,
|
|
||||||
title=_('Info'),
|
|
||||||
text=self.help_text,
|
|
||||||
rich_text=True)
|
|
||||||
|
|
||||||
|
|
||||||
class Buttons(QHBoxLayout):
|
class Buttons(QHBoxLayout):
|
||||||
|
|||||||
Reference in New Issue
Block a user