From c61c726ad2030cbab8bb46ba61d48268f8a88238 Mon Sep 17 00:00:00 2001 From: bob-white Date: Wed, 19 Jul 2017 22:05:24 -0500 Subject: [PATCH 1/4] Possible method of making Compout Controls Initial example is an IntSpinner. --- mGui/core/compound.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 mGui/core/compound.py diff --git a/mGui/core/compound.py b/mGui/core/compound.py new file mode 100644 index 0000000..6a9bc6a --- /dev/null +++ b/mGui/core/compound.py @@ -0,0 +1,34 @@ + +import weakref + +from .controls import IntField, IconTextButton +from ..forms import HorizontalStretchForm, VerticalStretchForm +from ..events import MayaEvent + +class IntSpinner(IntField): + + def __init__(self, key=None, **kwargs): + self._step = kwargs.pop('step', 1) + with HorizontalStretchForm(key='IntSpinner#') as self._root: + super(IntSpinner, self).__init__(key, **kwargs) + with VerticalStretchForm(width=24): + self._up = IconTextButton(height=12, style='iconOnly', image='caret-top') + self._down = IconTextButton(height=12, style='iconOnly', image='caret-bottom') + + self._up.command += self._inc + self._down.command += self._dec + + + @property + def step(self): + return self._step + + @step.setter + def step(self, value): + self._step = value + + def _inc(self, *args, **kwargs): + self.value += self._step + + def _dec(self, *args, **kwargs): + self.value -= self._step From 698b8d78c41850d3cd2a6374d721290f966c4196 Mon Sep 17 00:00:00 2001 From: bob-white Date: Wed, 19 Jul 2017 22:41:17 -0500 Subject: [PATCH 2/4] Tweaked how some of the parameters are parsed. Also moved to using regular buttons with silly labels for now. Seems that `IconTextButton` has a minimum width that would cause it to overflow onto the `IntField`, and I'm not entirely sure what is causing it. --- mGui/core/compound.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mGui/core/compound.py b/mGui/core/compound.py index 6a9bc6a..92d066e 100644 --- a/mGui/core/compound.py +++ b/mGui/core/compound.py @@ -1,7 +1,7 @@ import weakref -from .controls import IntField, IconTextButton +from .controls import IntField, IconTextButton, Button from ..forms import HorizontalStretchForm, VerticalStretchForm from ..events import MayaEvent @@ -9,14 +9,17 @@ class IntSpinner(IntField): def __init__(self, key=None, **kwargs): self._step = kwargs.pop('step', 1) - with HorizontalStretchForm(key='IntSpinner#') as self._root: - super(IntSpinner, self).__init__(key, **kwargs) - with VerticalStretchForm(width=24): - self._up = IconTextButton(height=12, style='iconOnly', image='caret-top') - self._down = IconTextButton(height=12, style='iconOnly', image='caret-bottom') + height = kwargs.pop('height', kwargs.pop('h', 32)) + width = kwargs.pop('width', kwargs.pop('w', 256)) + with HorizontalStretchForm(key='IntSpinner#', height=height, width=width) as self._root: + super(IntSpinner, self).__init__(key, height=height, width=width - height, **kwargs) + with VerticalStretchForm(): + self._up = Button(height=height / 2, width=height, label='^') #, style='iconOnly') #, image='caret-top') + self._down = Button(height=height / 2, width=height, label='v') #, style='iconOnly') #, image='caret-bottom') self._up.command += self._inc self._down.command += self._dec + print(self.height, self.width) @property From fadf1dabd21d95b8229eddfd7db443878e9f57cd Mon Sep 17 00:00:00 2001 From: bob-white Date: Thu, 20 Jul 2017 18:11:08 -0500 Subject: [PATCH 3/4] Another pass on an IntSpinner CompoundControl Uses `__getattr__` and `__setattr__` to defer requests to the root control. Exposes the other controls as public fields. --- mGui/core/compound.py | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/mGui/core/compound.py b/mGui/core/compound.py index 92d066e..ce43567 100644 --- a/mGui/core/compound.py +++ b/mGui/core/compound.py @@ -1,26 +1,27 @@ import weakref -from .controls import IntField, IconTextButton, Button +from .controls import IntField, IconTextButton, Button, Text from ..forms import HorizontalStretchForm, VerticalStretchForm from ..events import MayaEvent -class IntSpinner(IntField): - + +class IntSpinner(object): + def __init__(self, key=None, **kwargs): self._step = kwargs.pop('step', 1) + value = kwargs.pop('value', 0) + height = kwargs.pop('height', kwargs.pop('h', 32)) width = kwargs.pop('width', kwargs.pop('w', 256)) - with HorizontalStretchForm(key='IntSpinner#', height=height, width=width) as self._root: - super(IntSpinner, self).__init__(key, height=height, width=width - height, **kwargs) - with VerticalStretchForm(): - self._up = Button(height=height / 2, width=height, label='^') #, style='iconOnly') #, image='caret-top') - self._down = Button(height=height / 2, width=height, label='v') #, style='iconOnly') #, image='caret-bottom') + with HorizontalStretchForm(key='IntSpinner#', **kwargs) as self.root: + self.field = IntField(height=height, width=width - height, value=value) + with VerticalStretchForm() as self.form: + self.increment = Button(height=height / 2, width=height, label='^') + self.decrement = Button(height=height / 2, width=height, label='v') - self._up.command += self._inc - self._down.command += self._dec - print(self.height, self.width) - + self.increment.command += self._inc + self.decrement.command += self._dec @property def step(self): @@ -31,7 +32,18 @@ def step(self, value): self._step = value def _inc(self, *args, **kwargs): - self.value += self._step + self.field.value += self._step def _dec(self, *args, **kwargs): - self.value -= self._step + self.field.value -= self._step + + def __getattr__(self, attr): + if hasattr(self, 'root') and hasattr(self.root, attr): + return getattr(self.root, attr) + return super(IntSpinner, self).__getattribute__(attr) + + def __setattr__(self, attr, value): + if hasattr(self, 'root') and hasattr(self.root, attr): + setattr(self.root, attr, value) + else: + self.__dict__[attr] = value From 0178c436b7e296fd785aaa2651f580e57564e1bf Mon Sep 17 00:00:00 2001 From: bob-white Date: Sat, 22 Jul 2017 22:09:59 -0500 Subject: [PATCH 4/4] Some local metaclass ideas. Might just throw this out, but don't want to lose it yet. --- mGui/core/compound.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mGui/core/compound.py b/mGui/core/compound.py index ce43567..77e7dcb 100644 --- a/mGui/core/compound.py +++ b/mGui/core/compound.py @@ -6,6 +6,35 @@ from ..events import MayaEvent +class CompoundControl(type): + # Some magic will happen here? + pass + + +class IntSpinner: + __metaclass__ = CompoundControl + + _CONTROLS = { + 'root': HorizontalStretchForm, + 'field': IntField, + 'label': Text, + 'decrement': Button, + 'increment': Button, + 'form': VerticalStretchForm, + } + _HIERARCHY = { + 'root': ( + 'label', + 'field', { + 'form': ( + 'increment', + 'decrement' + ) + } + ) + } + + class IntSpinner(object): def __init__(self, key=None, **kwargs):