Source code for schrodinger.ui.qt.tooltips
from functools import lru_cache
from schrodinger.Qt import QtCore
from schrodinger.Qt import QtGui
from schrodinger.Qt import QtWidgets
from schrodinger.ui.qt import structure2d
from schrodinger.ui.qt import utils
DEFAULT_OFFSET = (2, 16)
[docs]@lru_cache(maxsize=4000)
def tooltip_for_aa(aa):
    """
    Generate a tooltip for non-standard residue amino acid.
    :param aa: non-standard residue amino acid
    :type aa: nonstandard_residues.AminoAcid
    :return: html string to be used as tooltip
    :rtype: str
    """
    image = structure2d.get_st_image_using_sketcher(aa.st)
    image_str = utils.image_to_string(image)
    if len(aa.name) <= 3:
        tooltip_top = f'{aa.description}({aa.isomer})'
    else:
        tooltip_top = f'{aa.name}({aa.isomer})'
    tooltip_bottom = f'<img src="data:image/png;base64,{image_str}">'
    return f'<html>{tooltip_top}<br/>{tooltip_bottom}</html>'
[docs]class TooltipMixin:
    """
    Mixin for converting widgets to tooltips. Contains basic functionality for
    showing the tooltip near the mouse cursor (plus an offset).
    Concrete subclasses must also inherit from `QWidget` or a subclass.
    """
[docs]    def __init__(self, *args, offset=None, **kwargs):
        """
        :param offset: offset coordinates (in pixels) from the mouse pointer
                position to draw the upper left corner of the tooltip window. If
                not supplied, will default to `DEFAULT_OFFSET`.
        :type offset: tuple[int, int] or NoneType
        """
        super().__init__(*args, **kwargs)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.ToolTip)
        self.offset = offset or DEFAULT_OFFSET
[docs]    def show(self):
        """
        Show this widget at the cursor location plus an offset.
        """
        position = QtGui.QCursor.pos()
        position.setX(position.x() + self.offset[0])
        position.setY(position.y() + self.offset[1])
        self.move(position)
        super().show()
        self.raise_()