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

ENH: Add parameterize decorator utilizing pyqtgraph's parametertree #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions mily/dialogs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .parameterize import parameterize
83 changes: 83 additions & 0 deletions mily/dialogs/parameterize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from pyqtgraph.parametertree import Parameter, parameterTypes, ParameterTree
from qtpy.QtWidgets import QDialog, QVBoxLayout, QDialogButtonBox
from qtpy.QtCore import Qt


def parameterize(func):
"""
Usage:

from pyqtgraph.parametertree.parameterTypes import SimpleParameter

@parameterize
def sum(a, b):
return sum(a, b)

a = SimpleParameter(name='a', type='float', value=1)
b = SimpleParameter(name='b', type='float', value=2)

sum(a, b) # spawns a dialog; user-interaction required
"""

def func_wrapper(*args, **kwargs):
param = args_to_params(*args, **kwargs)

# query values
paramdialog = ParameterDialog(param)
paramdialog.exec_()

# extract values
args = list(args)
for i, arg in enumerate(args):
if isinstance(arg, Parameter):
args[i] = arg.value()
else:
args[i] = arg

for key, value in kwargs.items():
if isinstance(value, Parameter):
kwargs[key] = value.value()
else:
kwargs[key] = value

# make the call
return func(*args, **kwargs)

return func_wrapper


def args_to_params(*args, **kwargs):
param = parameterTypes.GroupParameter(name='Parameters')
for arg in args + tuple(kwargs.values()):
if isinstance(arg, Parameter):
param.addChild(arg)
elif isinstance(arg, (list, tuple)):
param.addChild(args_to_params(*arg))
return param


class ParameterDialog(QDialog):
def __init__(self, children, parent=None):
super(ParameterDialog, self).__init__(parent)

layout = QVBoxLayout(self)

self.paramtree = ParameterTree()
layout.addWidget(self.paramtree)

self.paramtree.setParameters(children, showTop=False)

# OK and Cancel buttons
buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal, self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)

def exec_(self, *args, **kwargs):
result = super(ParameterDialog, self).exec_()
if result != self.Accepted:
raise InterruptedError('Execution aborted by user.')