Fix clipped combo boxes and preserve native popup layouts
This commit is contained in:
@@ -3,6 +3,75 @@ from PySide6 import QtCore as C, QtGui as G, QtWidgets as W
|
||||
import time
|
||||
|
||||
|
||||
class ComboBox(W.QComboBox):
|
||||
"""Size popups for their text, independently of narrow table columns."""
|
||||
def __init__(self, parent=None):
|
||||
self._text_width = None
|
||||
super().__init__(parent)
|
||||
self.setSizeAdjustPolicy(W.QComboBox.SizeAdjustPolicy.AdjustToMinimumContentsLengthWithIcon)
|
||||
self.setMinimumContentsLength(6)
|
||||
self.setMaxVisibleItems(12)
|
||||
view = W.QListView(self)
|
||||
view.setTextElideMode(C.Qt.TextElideMode.ElideNone)
|
||||
view.setHorizontalScrollBarPolicy(C.Qt.ScrollBarPolicy.ScrollBarAsNeeded)
|
||||
view.setHorizontalScrollMode(W.QAbstractItemView.ScrollMode.ScrollPerPixel)
|
||||
self.setView(view)
|
||||
for signal in (self.model().dataChanged, self.model().rowsInserted, self.model().rowsRemoved, self.model().modelReset):
|
||||
signal.connect(self.invalidate_width)
|
||||
|
||||
def invalidate_width(self, *args):
|
||||
self._text_width = None
|
||||
self.updateGeometry()
|
||||
|
||||
def text_width(self):
|
||||
if self._text_width is None:
|
||||
self._text_width = max((self.fontMetrics().horizontalAdvance(self.itemText(i)) for i in range(self.count())), default=0)
|
||||
return self._text_width
|
||||
|
||||
def sizeHint(self):
|
||||
hint = super().sizeHint()
|
||||
hint.setWidth(max(120, hint.width(), min(360, self.text_width() + 48)))
|
||||
return hint
|
||||
|
||||
def minimumSizeHint(self):
|
||||
hint = super().minimumSizeHint()
|
||||
hint.setWidth(max(120, hint.width(), min(260, self.text_width() + 48)))
|
||||
return hint
|
||||
|
||||
def changeEvent(self, event):
|
||||
if event.type() in (C.QEvent.Type.FontChange, C.QEvent.Type.StyleChange):
|
||||
self.invalidate_width()
|
||||
super().changeEvent(event)
|
||||
|
||||
def showPopup(self):
|
||||
view = self.view()
|
||||
screen = self.screen().availableGeometry()
|
||||
scrollbar = view.verticalScrollBar().sizeHint().width()
|
||||
content = max(self.text_width() + 16, view.sizeHintForColumn(0))
|
||||
width = min(screen.width(), max(self.width(), content + scrollbar + 2 * view.frameWidth() + 8))
|
||||
popup = view.window()
|
||||
frame = popup.frameWidth() if isinstance(popup, W.QFrame) else 0
|
||||
view.setMinimumWidth(max(0, width - 2 * frame))
|
||||
super().showPopup()
|
||||
# QComboBox resets this policy while opening its popup.
|
||||
view.setHorizontalScrollBarPolicy(C.Qt.ScrollBarPolicy.ScrollBarAsNeeded)
|
||||
popup.resize(width, popup.height())
|
||||
view.doItemsLayout()
|
||||
if view.horizontalScrollBar().maximum() > 0:
|
||||
popup.resize(width, min(screen.height(), popup.height() + view.horizontalScrollBar().sizeHint().height()))
|
||||
popup.move(min(max(popup.x(), screen.left()), screen.right() - popup.width() + 1),
|
||||
min(max(popup.y(), screen.top()), screen.bottom() - popup.height() + 1))
|
||||
|
||||
def event(self, event):
|
||||
if event.type() == C.QEvent.Type.ToolTip and self.currentText():
|
||||
text = self.currentText()
|
||||
if self.toolTip() and self.toolTip() != text:
|
||||
text += "\n" + self.toolTip()
|
||||
W.QToolTip.showText(event.globalPos(), text, self)
|
||||
return True
|
||||
return super().event(event)
|
||||
|
||||
|
||||
class RowBatch(C.QObject):
|
||||
"""Create Qt rows in short event-loop slices, keeping input responsive."""
|
||||
def __init__(self, parent):
|
||||
|
||||
Reference in New Issue
Block a user