import math
from PyQt4 import QtCore, QtGui
class Tooltips(QtGui.QMainWindow):
TOOLTIPS_LINE_HEIGHT = 18
def __init__(self, parent):
super(Tooltips, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.setAttribute(QtCore.Qt.WA_DontCreateNativeAncestors)
self.setAttribute(QtCore.Qt.WA_ShowWithoutActivating)
self.hide()
self.setStyleSheet('Tooltips > QLabel {border: 1px solid #f56133; border-radius: 4px; background: #ffffff; font: normal 12px 'Microsoft Yahei'; padding: 8px;}')
self._actualWidth = 150
self._actualHeight = 50
self._offsetXShadow = 5
self._offsetYShadow = 5
self._labelText = QtGui.QLabel(self)
self._labelText.setWordWrap(True)
self._labelText.move(0, 0)
self._labelText.setText(u'')
self._labelText.setAlignment(QtCore.Qt.AlignVCenter)
def setText(self, str_text):
self._labelText.setText(str_text)
self.adjustSize()
def text(self):
return self._labelText.text()
def resize(self, *__args):
if len(__args) == 1:
if not isinstance(__args[0], QtCore.QSize):
return
self._actualWidth = __args[0].width()
self._actualHeight = __args[0].height()
super(Tooltips, self).resize(__args[0] + QtCore.QSize(self._offsetXShadow, self._offsetYShadow))
elif len(__args) == 2:
if not isinstance(self._actualWidth, int):
return
if not isinstance(self._actualHeight, int):
return
self._actualWidth = __args[0]
self._actualHeight = __args[1]
super(Tooltips, self).resize(self._actualWidth + self._offsetXShadow,
self._actualHeight + self._offsetYShadow)
def resizeEvent(self, event):
self._labelText.resize(event.size() - QtCore.QSize(self._offsetXShadow, self._offsetYShadow))
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
color = QtGui.QColor(0, 0, 0, 0)
for i in range(0, 10):
path = QtGui.QPainterPath()
path.setFillRule(QtCore.Qt.WindingFill)
path.addRect(10 - i, 10 - i, super(Tooltips, self).width() - (10 - i) * 2,
super(Tooltips, self).height() - (10 - i) * 2)
color.setAlpha(150 - math.sqrt(i) * 50)
painter.setPen(color)
painter.drawPath(path)
def width(self):
return self._actualWidth
def height(self):
return self._actualHeight
def size(self):
return QtCore.QSize(self._actualWidth, self._actualHeight)
def adjustSize(self):
orgText = self.text()
countLF = orgText.count('<br>') + 1
plainText = orgText.replace('<br>', '\n').replace(QtCore.QRegExp('<[^>]+>'), '').replace(' ', ' ')
tgtWidth = self._maxLineWidth(plainText)
tgtRect = QtCore.QSize(tgtWidth, Tooltips.TOOLTIPS_LINE_HEIGHT * countLF + 19)
if self.width() != tgtRect.width() or self.height() != tgtRect.height():
self.resize(tgtRect)
def _maxLineWidth(self, text):
maxLineWidth = 0
for line in text.split('\n'):
if line == '':
continue
tmpWidth = self._labelText.fontMetrics().width(line) + 24
if tmpWidth > maxLineWidth:
maxLineWidth = tmpWidth
return maxLineWidth
def setShadowOffset(self, int_offsetX, int_offsetY):
self._offsetXShadow = int_offsetX
self._offsetYShadow = int_offsetY
self.resize(self._actualWidth, self._actualHeight)