3e827f4ece
Word wrapping allows ElideLabel to resize first inside flexible sized areas such as the contents widget of a scrollarea. This allows the contents widget to properly resize itself to avoid horizontal scrolling. Word-wrapping also enables the widget to resize vertically. To avoid that ElideLabel is set to a fixed height based on font metrics. An overloaded method `setFixedHeight` has been added to disable it when desired. EglSyncGroup: Replace estimated path label with a ElideLabel because the displayed message was expanding the scrollarea.
38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
from typing import Union
|
|
|
|
from PyQt5.QtCore import Qt
|
|
from PyQt5.QtGui import QFontMetrics, QResizeEvent
|
|
from PyQt5.QtWidgets import QLabel, QWIDGETSIZE_MAX
|
|
|
|
|
|
class ElideLabel(QLabel):
|
|
|
|
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)
|
|
|
|
def setText(self, a0: str) -> None:
|
|
self.__text = a0
|
|
self.__setElideText(a0)
|
|
|
|
def __setElideText(self, a0: str):
|
|
elided_text = self.__fm.elidedText(
|
|
a0, Qt.ElideRight,
|
|
self.width() - (self.contentsMargins().left() + self.contentsMargins().right())
|
|
)
|
|
self.setToolTip(self.__text if elided_text != self.__text else "")
|
|
super(ElideLabel, self).setText(elided_text)
|
|
|
|
def resizeEvent(self, a0: QResizeEvent) -> None:
|
|
self.__setElideText(self.__text)
|
|
super(ElideLabel, self).resizeEvent(a0)
|
|
|
|
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)
|