Fix clipped combo boxes and preserve native popup layouts
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
from PySide6 import QtCore as C, QtGui as G, QtWidgets as W
|
||||
|
||||
from ranch_assistant.gui import combo, compact_layouts
|
||||
from ranch_assistant.native_widgets import ComboBox, use_native_scrollbars
|
||||
|
||||
|
||||
class ComboBoxTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.app = W.QApplication.instance() or W.QApplication([])
|
||||
use_native_scrollbars()
|
||||
|
||||
def test_popup_fits_longest_option_despite_narrow_control_and_wide_scrollbar(self):
|
||||
widget = combo([("随机30秒", 30), ("四项全S(4S)才保留", 6), ("随机2分钟", 120), ("随机5分钟", 300)])
|
||||
widget.setStyle(W.QStyleFactory.create("Windows"))
|
||||
self.assertGreaterEqual(widget.minimumSizeHint().width(), widget.fontMetrics().horizontalAdvance("四项全S(4S)才保留") + 40)
|
||||
widget.setFixedWidth(100)
|
||||
widget.show()
|
||||
try:
|
||||
self.app.processEvents()
|
||||
widget.showPopup()
|
||||
self.app.processEvents()
|
||||
view = widget.view()
|
||||
needed = max(view.fontMetrics().horizontalAdvance(widget.itemText(i)) for i in range(widget.count())) + 16
|
||||
self.assertGreaterEqual(view.viewport().width(), needed)
|
||||
finally:
|
||||
widget.hidePopup()
|
||||
widget.close()
|
||||
|
||||
def test_dynamic_options_remeasure_and_popup_stays_on_screen(self):
|
||||
widget = ComboBox()
|
||||
widget.setStyle(W.QStyleFactory.create("Windows"))
|
||||
widget.addItem("派遣")
|
||||
widget.setFixedWidth(100)
|
||||
widget.show()
|
||||
try:
|
||||
widget.showPopup()
|
||||
self.app.processEvents()
|
||||
original = widget.view().window().width()
|
||||
widget.hidePopup()
|
||||
label = "莉莉托斯市场 · 每批十连 · 保留金币并限制每日额度"
|
||||
widget.addItem(label)
|
||||
widget.showPopup()
|
||||
self.app.processEvents()
|
||||
self.assertGreater(widget.view().window().width(), original)
|
||||
self.assertGreaterEqual(widget.view().viewport().width(), widget.fontMetrics().horizontalAdvance(label) + 16)
|
||||
self.assertTrue(widget.screen().availableGeometry().contains(widget.view().window().geometry()))
|
||||
widget.hidePopup()
|
||||
widget.setItemText(1, "短选项")
|
||||
widget.showPopup()
|
||||
self.app.processEvents()
|
||||
self.assertLess(widget.view().window().width(), widget.fontMetrics().horizontalAdvance(label))
|
||||
finally:
|
||||
widget.hidePopup()
|
||||
widget.close()
|
||||
|
||||
def test_screen_wide_labels_scroll_and_hover_keeps_full_text_and_help(self):
|
||||
label = "魔物名称与编号" * 100
|
||||
widget = combo([(label, 1)])
|
||||
widget.setToolTip("保留策略说明")
|
||||
widget.setFixedWidth(100)
|
||||
widget.show()
|
||||
try:
|
||||
widget.showPopup()
|
||||
self.app.processEvents()
|
||||
self.assertLessEqual(widget.view().window().width(), widget.screen().availableGeometry().width())
|
||||
self.assertGreater(widget.view().horizontalScrollBar().maximum(), 0)
|
||||
widget.hidePopup()
|
||||
event = G.QHelpEvent(C.QEvent.Type.ToolTip, C.QPoint(5, 5), widget.mapToGlobal(C.QPoint(5, 5)))
|
||||
with patch.object(W.QToolTip, "showText") as tooltip:
|
||||
self.app.sendEvent(widget, event)
|
||||
self.assertEqual(tooltip.call_args.args[1], label + "\n保留策略说明")
|
||||
finally:
|
||||
widget.hidePopup()
|
||||
widget.close()
|
||||
|
||||
def test_compact_page_layout_keeps_short_popup_options_fully_visible(self):
|
||||
page = W.QWidget()
|
||||
layout = W.QVBoxLayout(page)
|
||||
widget = combo([("随机30秒", 30), ("随机60秒", 60), ("随机2分钟", 120), ("随机5分钟", 300)])
|
||||
layout.addWidget(widget)
|
||||
compact_layouts(page)
|
||||
page.show()
|
||||
try:
|
||||
self.app.processEvents()
|
||||
widget.showPopup()
|
||||
self.app.processEvents()
|
||||
view = widget.view()
|
||||
last = view.visualRect(view.model().index(widget.count() - 1, 0))
|
||||
self.assertLess(last.bottom(), view.viewport().height())
|
||||
finally:
|
||||
widget.hidePopup()
|
||||
page.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user