2023-02-21 01:26:14 +13:00
|
|
|
from typing import Union
|
|
|
|
|
2022-06-19 06:45:36 +12:00
|
|
|
from PyQt5.QtCore import Qt
|
2023-02-16 21:47:23 +13:00
|
|
|
from PyQt5.QtGui import QFontMetrics, QResizeEvent
|
2023-02-21 01:26:14 +13:00
|
|
|
from PyQt5.QtWidgets import QLabel, QWIDGETSIZE_MAX
|
2022-06-19 06:45:36 +12:00
|
|
|
|
|
|
|
|
|
|
|
class ElideLabel(QLabel):
|
|
|
|
|
2023-02-21 01:26:14 +13:00
|
|
|
def __init__(self, text="", parent=None):
|
|
|
|
super(ElideLabel, self).__init__(parent=parent)
|
|
|
|
self.__text = text
|
|
|
|
self.__fm = QFontMetrics(self.font())
|
|
|
|
self.setFixedHeight(True)
|
|
|
|
self.setWordWrap(True)
|
|
|
|
self.setText(text)
|
2022-06-19 06:45:36 +12:00
|
|
|
|
|
|
|
def setText(self, a0: str) -> None:
|
|
|
|
self.__text = a0
|
|
|
|
self.__setElideText(a0)
|
|
|
|
|
|
|
|
def __setElideText(self, a0: str):
|
2023-02-21 01:26:14 +13:00
|
|
|
elided_text = self.__fm.elidedText(
|
2023-02-16 21:47:23 +13:00
|
|
|
a0, Qt.ElideRight,
|
2023-02-21 01:26:14 +13:00
|
|
|
self.width() - (self.contentsMargins().left() + self.contentsMargins().right())
|
2023-02-16 21:47:23 +13:00
|
|
|
)
|
2023-02-21 01:26:14 +13:00
|
|
|
self.setToolTip(self.__text if elided_text != self.__text else "")
|
2022-06-19 06:45:36 +12:00
|
|
|
super(ElideLabel, self).setText(elided_text)
|
|
|
|
|
2023-02-16 21:47:23 +13:00
|
|
|
def resizeEvent(self, a0: QResizeEvent) -> None:
|
2022-06-19 06:45:36 +12:00
|
|
|
self.__setElideText(self.__text)
|
2023-02-16 21:47:23 +13:00
|
|
|
super(ElideLabel, self).resizeEvent(a0)
|
2023-02-21 01:26:14 +13:00
|
|
|
|
|
|
|
def setFixedHeight(self, h: Union[int, bool]) -> None:
|
|
|
|
if isinstance(h, bool):
|
|
|
|
super(ElideLabel, self).setFixedHeight(self.__fm.height() if h else QWIDGETSIZE_MAX)
|
|
|
|
elif isinstance(h, int):
|
|
|
|
super(ElideLabel, self).setFixedHeight(h)
|