Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5792 from ynput/bugfix/number-attr-widget-multise…
Browse files Browse the repository at this point in the history
…lection

'NumberAttrWidget' shows 'Multiselection' label on multiselection
  • Loading branch information
iLLiCiTiT authored Oct 19, 2023
2 parents bffcf33 + 63e9834 commit 1ad8e94
Showing 1 changed file with 78 additions and 3 deletions.
81 changes: 78 additions & 3 deletions openpype/tools/attribute_defs/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,30 @@ def _ui_init(self):
self.main_layout.addWidget(input_widget, 0)


class ClickableLineEdit(QtWidgets.QLineEdit):
clicked = QtCore.Signal()

def __init__(self, text, parent):
super(ClickableLineEdit, self).__init__(parent)
self.setText(text)
self.setReadOnly(True)

self._mouse_pressed = False

def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self._mouse_pressed = True
super(ClickableLineEdit, self).mousePressEvent(event)

def mouseReleaseEvent(self, event):
if self._mouse_pressed:
self._mouse_pressed = False
if self.rect().contains(event.pos()):
self.clicked.emit()

super(ClickableLineEdit, self).mouseReleaseEvent(event)


class NumberAttrWidget(_BaseAttrDefWidget):
def _ui_init(self):
decimals = self.attr_def.decimals
Expand All @@ -270,34 +294,85 @@ def _ui_init(self):
input_widget.setButtonSymbols(
QtWidgets.QAbstractSpinBox.ButtonSymbols.NoButtons
)
input_line_edit = input_widget.lineEdit()
input_widget.installEventFilter(self)

multisel_widget = ClickableLineEdit("< Multiselection >", self)

input_widget.valueChanged.connect(self._on_value_change)
multisel_widget.clicked.connect(self._on_multi_click)

self._input_widget = input_widget
self._input_line_edit = input_line_edit
self._multisel_widget = multisel_widget
self._last_multivalue = None
self._multivalue = False

self.main_layout.addWidget(input_widget, 0)
self.main_layout.addWidget(multisel_widget, 0)

def _on_value_change(self, new_value):
self.value_changed.emit(new_value, self.attr_def.id)
def eventFilter(self, obj, event):
if (
self._multivalue
and obj is self._input_widget
and event.type() == QtCore.QEvent.FocusOut
):
self._set_multiselection_visible(True)
return False

def current_value(self):
return self._input_widget.value()

def set_value(self, value, multivalue=False):
self._last_multivalue = None
if multivalue:
set_value = set(value)
if None in set_value:
set_value.remove(None)
set_value.add(self.attr_def.default)

if len(set_value) > 1:
self._input_widget.setSpecialValueText("Multiselection")
self._last_multivalue = next(iter(set_value), None)
self._set_multiselection_visible(True)
self._multivalue = True
return
value = tuple(set_value)[0]

self._multivalue = False
self._set_multiselection_visible(False)

if self.current_value != value:
self._input_widget.setValue(value)

def _on_value_change(self, new_value):
self._multivalue = False
self.value_changed.emit(new_value, self.attr_def.id)

def _on_multi_click(self):
self._set_multiselection_visible(False, True)

def _set_multiselection_visible(self, visible, change_focus=False):
self._input_widget.setVisible(not visible)
self._multisel_widget.setVisible(visible)
if visible:
return

# Change value once user clicked on the input field
if self._last_multivalue is None:
value = self.attr_def.default
else:
value = self._last_multivalue
self._input_widget.blockSignals(True)
self._input_widget.setValue(value)
self._input_widget.blockSignals(False)
if not change_focus:
return
# Change focus to input field and move cursor to the end
self._input_widget.setFocus(QtCore.Qt.MouseFocusReason)
self._input_line_edit.setCursorPosition(
len(self._input_line_edit.text())
)


class TextAttrWidget(_BaseAttrDefWidget):
def _ui_init(self):
Expand Down

0 comments on commit 1ad8e94

Please sign in to comment.