Skip to content

Commit d82bb77

Browse files
committed
CLN: nicer code for createEditor (for creating the array delegate)
1 parent 2757f1a commit d82bb77

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

larray_editor/arraywidget.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -227,44 +227,47 @@ def __init__(self, dtype, parent=None, font=None, minvalue=None, maxvalue=None):
227227
def createEditor(self, parent, option, index):
228228
"""Create editor widget"""
229229
model = index.model()
230-
# TODO: dtype should be taken from the model instead (or even from the actual value?)
230+
# TODO: dtype should be taken from the adapter instead. Only the adapter knows whether the dtype is per cell
231+
# (e.g. list), per column (e.g. Dataframe) or homogenous for the whole table (e.g. la.Array)
232+
# dtype = model.adapter.get_dtype(hpos, vpos)
233+
dtype = self.dtype
231234
value = model.get_value(index)
232-
if self.dtype.name == "bool":
235+
if dtype.name == "bool":
233236
# toggle value
234237
value = not value
235238
model.setData(index, value)
236239
return
237240
elif value is not np.ma.masked:
238-
minvalue, maxvalue = self.minvalue, self.maxvalue
239-
if minvalue is not None and maxvalue is not None:
240-
msg = f"value must be between {minvalue} and {maxvalue}"
241-
elif minvalue is not None:
242-
msg = f"value must be >= {minvalue}"
243-
elif maxvalue is not None:
244-
msg = f"value must be <= {maxvalue}"
245-
else:
246-
msg = None
247-
248241
# Not using a QSpinBox for integer inputs because I could not find
249242
# a way to prevent the spinbox/editor from closing if the value is
250243
# invalid. Using the builtin minimum/maximum of the spinbox works
251244
# but that provides no message so it is less clear.
252245
editor = QLineEdit(parent)
253-
if is_number(self.dtype):
254-
validator = QDoubleValidator(editor) if is_float(self.dtype) \
255-
else QIntValidator(editor)
246+
if is_number(dtype):
247+
minvalue, maxvalue = self.minvalue, self.maxvalue
248+
validator = QDoubleValidator(editor) if is_float(dtype) else QIntValidator(editor)
256249
if minvalue is not None:
257250
validator.setBottom(minvalue)
258251
if maxvalue is not None:
259252
validator.setTop(maxvalue)
260253
editor.setValidator(validator)
261254

262-
def on_editor_text_edited():
263-
if not editor.hasAcceptableInput():
264-
QToolTip.showText(editor.mapToGlobal(QPoint()), msg)
265-
else:
266-
QToolTip.hideText()
255+
if minvalue is not None and maxvalue is not None:
256+
msg = f"value must be between {minvalue} and {maxvalue}"
257+
elif minvalue is not None:
258+
msg = f"value must be >= {minvalue}"
259+
elif maxvalue is not None:
260+
msg = f"value must be <= {maxvalue}"
261+
else:
262+
msg = None
263+
267264
if msg is not None:
265+
def on_editor_text_edited():
266+
if not editor.hasAcceptableInput():
267+
QToolTip.showText(editor.mapToGlobal(QPoint()), msg)
268+
else:
269+
QToolTip.hideText()
270+
268271
editor.textEdited.connect(on_editor_text_edited)
269272

270273
editor.setFont(self.font)

0 commit comments

Comments
 (0)