Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERF: create unit menu on-demand for QLineEdit #1027

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pydm/tests/widgets/test_lineedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def test_construct(qtbot, init_channel):
assert pydm_lineedit._scale == 1
assert pydm_lineedit._prec == 0
assert pydm_lineedit.showUnits == False

assert pydm_lineedit.unitMenu is None
pydm_lineedit.widget_ctx_menu()

assert isinstance(pydm_lineedit.unitMenu, QMenu) and pydm_lineedit.unitMenu.title() == "Convert Units"
assert pydm_lineedit.displayFormat == pydm_lineedit.DisplayFormat.Default
assert (pydm_lineedit._string_encoding == pydm_lineedit.app.get_string_encoding()
Expand Down
12 changes: 8 additions & 4 deletions pydm/widgets/line_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def __init__(self, parent=None, init_channel=None):
self._scale = 1

self.returnPressed.connect(self.send_value)
self.unitMenu = QMenu('Convert Units', self)
self.create_unit_options()
self.unitMenu = None
self._display_format_type = self.DisplayFormat.Default
self._string_encoding = "utf_8"
self._user_set_read_only = False # Are we *really* read only?
Expand Down Expand Up @@ -157,7 +156,6 @@ def unit_changed(self, new_unit):
"""
super(PyDMLineEdit, self).unit_changed(new_unit)
self._scale = 1
self.create_unit_options()

def create_unit_options(self):
"""
Expand All @@ -169,7 +167,11 @@ def create_unit_options(self):
:attr:`.showUnits` attribute is set to False, the menu will tell
the user that there are no available conversions
"""
self.unitMenu.clear()
if self.unitMenu is None:
self.unitMenu = QMenu('Convert Units', self)
else:
self.unitMenu.clear()

units = utilities.find_unit_options(self._unit)
if units and self._show_units:
for choice in units:
Expand Down Expand Up @@ -219,6 +221,8 @@ def widget_ctx_menu(self):
QMenu or None
If the return of this method is None a new QMenu will be created by `assemble_tools_menu`.
"""
self.create_unit_options()

menu = self.createStandardContextMenu()
menu.addSeparator()
menu.addMenu(self.unitMenu)
Expand Down
Loading