Esempio n. 1
0
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self._show_toolbar = Control('toolbar', interface='IBool', value=False, label='Show Toolbar')
        self._show_title = Control('title', interface='IBool', value=False, label='Show Applet Title')

        self._props = QControlContainer()
        self._props.controlValueChanged.connect(self._on_prop_changed)
        self._props.add_control(self._show_toolbar)
        self._props.add_control(self._show_title)

        self._edit_mode = False

        self._applet = None

        self._layout = QtGui.QVBoxLayout(self)

        self._l_title = QtGui.QLabel('No applet selected')
        self._l_title.hide()
        self._menu = ContextualMenu()

        p = QtGui.QSizePolicy
        self._l_title.setSizePolicy(p(p.MinimumExpanding, p.Maximum))
        self._l_title.setAlignment(QtCore.Qt.AlignVCenter)

        self._layout.setAlignment(self._l_title, QtCore.Qt.AlignVCenter)

        self._layout.addWidget(self._l_title)
        self._layout.addWidget(self._menu)

        self._create_actions()
        self.fine_tune()
Esempio n. 2
0
    def test_controls(self):

        c1 = Control('a', value=1, constraints=dict(min=1, max=2))
        assert c1.interface.min == 1
        assert c1.interface.max == 2
        assert c1.value == 1
        self.assertEqual(repr(c1.interface), 'IInt(min=1, max=2, step=1)')

        c2 = Control('a', 'IInt', constraints=dict(min=3, max=4))
        assert c2.interface.min == 3
        assert c2.interface.max == 4
        #assert c2.value == 3

        c3 = Control('a', 'IInt', constraints=dict(min=5, max=6))

        c4 = Control('a', 'IInt')
        c5 = Control('a', value=4)

        cb1 = create_control('a', value=1, constraints=dict(min=1, max=2))
        cb2 = create_control('a', 'IInt', constraints=dict(min=3, max=4))
        cb3 = create_control('a', 'IInt', value=0, constraints=dict(min=5, max=6))
        cb4 = create_control('a', 'IInt')
        cb5 = create_control('a', value=4)

        self.compare_controls(c1, cb1)
        self.compare_controls(c2, cb2)
        self.compare_controls(c3, cb3)
        self.compare_controls(c4, cb4)
        self.compare_controls(c5, cb5)
Esempio n. 3
0
    def test_serialization(self):
        c1 = Control('a', value=1,  constraints=dict(min=1, max=2))
        c2 = Control('a', 'IInt', constraints=dict(min=3, max=4))
        orig = [c1, c2]

        serializer = ControlSerializer()
        it = serializer.serialize(orig)

        deserializer = ControlDeserializer()
        controls = deserializer.deserialize(it)

        assert len(controls) == len(orig)
        for i in range(len(orig)):
            self.compare_controls(orig[i], controls[i])
Esempio n. 4
0
def Widget(option_name, value):
    """
    :return: create a widget which permit to edit the value with a label
    """
    # TODO: Currently an evaluation is used to guess value type.
    # This approach may fails for complex data
    # We need to improve option type management, for example by providing an associated map "option_name -> type".
    # We should also add constraints on values
    try:
        eval_value = ast.literal_eval(value)
    except (ValueError, SyntaxError):
        eval_value = value

    inames = guess_interface(eval_value)
    if len(inames):
        iname = inames[0]
    else:
        iname = 'IStr'

    # Dirty hack to handle int constraints on font size.
    if 'font' in option_name and iname == 'IInt':
        iname = new_interface(iname, min=5, max=200)

    control = Control(option_name, iname, eval_value)
    editor = qt_editor(control)
    return control, editor
Esempio n. 5
0
    def test_load_save(self):
        c1 = Control('a', value=1,  constraints=dict(min=1, max=2))
        c2 = Control('a', 'IStr', 'salut')
        orig = [c1, c2]

        tmp = tempdir()
        saver = ControlSaver()
        saver.save(orig, tmp / 'control.py')

        loader = ControlLoader()
        controls = loader.load(tmp / 'control.py')
        tmp.rmtree()

        assert len(controls) == len(orig)
        for i in range(len(orig)):
            self.compare_controls(orig[i], controls[i])
Esempio n. 6
0
 def control(self):
     iname = self._interfaces[self.cb_interface.currentIndex()]
     control = Control(
         self.e_name.text(),
         iname,
         widget=self.alias_to_wname[self.cb_widget.currentText()],
         constraints=self.constraints())
     self.__class__.counters[iname] = self.__class__.counters.setdefault(
         iname, 0) + 1
     return control
Esempio n. 7
0
 def decode(self, raw_data, mimetype_in, mimetype_out):
     num, letter = raw_data.split(';')
     num = int(num)
     if mimetype_out == 'custom/data':
         data = SampleCustomData(num, letter)
     elif mimetype_out == 'openalealab/control':
         from openalea.core.control import Control
         data = Control(letter, 'IInt', num)
     elif mimetype_out == 'text/plain':
         data = 'customdata: %s, %s' % (letter, num)
     elif mimetype_out == 'text/plain.verbose':
         data = 'Custom data define these values:\n  - letter: %s\n  - num: %s' % (
             letter, num)
     return data, {}
Esempio n. 8
0
def qt_dialog(control=None, **kwds):
    """
    You can pass control factory arguments:
        - name: Control name
        - interface: Control interface
        - value: Control value

    You can also pass qt_editor factory arguments
        - shape: widget shape
        - preferred: preferred widget
    """
    autoapply = kwds.get('autoapply', False)
    if control is None:
        control = Control(**kwds)
    widget = qt_editor(control, **kwds)
    widget.autoapply(control, autoapply)
    dialog = ModalDialog(widget)
    if dialog.exec_() == QtGui.QDialog.Accepted:
        return widget.value()
    else:
        return None
        else:
            shapes = plugin.edit_shape
        for shape, size in [
                ('small', (50, 50)),
                ('hline', (200, 30)),
                ('vline', (30, 200)),
                ('large', None),
        ]:
            if shape not in shapes:
                continue

            if iname in SAMPLE_VALUES:
                kwargs = SAMPLE_VALUES[iname]
            else:
                kwargs = {}
            control = Control('c', iname, widget=plugin.name, **kwargs)
            w_editor_class = plugin.load()
            if issubclass(w_editor_class, QtGui.QWidget):
                w_editor = w_editor_class()
            else:
                w_editor = w_editor_class.edit(control, shape=shape)
            if w_editor:
                w_editor.set(control)
                widget = QtGui.QWidget()
                widget.setAttribute(QtCore.Qt.WA_DeleteOnClose)
                layout = QtGui.QVBoxLayout(widget)
                layout.setContentsMargins(1, 1, 1, 1)
                if size:
                    widget.setMinimumSize(*size)
                    widget.resize(*size)
                    widget.setMaximumSize(*size)
Esempio n. 10
0
from openalea.core.control import Control

controls = []
from openalea.core.interface import IBool

minterface = IBool()
mcontrol = Control(u'FLAKE', minterface, True)
controls.append(mcontrol)

Esempio n. 11
0
        inames = [interface.__name__ for interface in interfaces()]
        for interface in sorted(interfaces()):
            print '\033[41m', interface, '\033[0m', interface.__module__
            if args.list_interface_widgets:
                widgets = scontrol.qt_widget_plugins(interface.__name__)
                if widgets:
                    for plugin in widgets:
                        w = plugin.load()
                        print '    \033[36m%s\033[0m\n        plugin: %s\n        widget: %s)' % (
                            plugin.name, plugin, w)
    else:
        instance = QtGui.QApplication.instance()
        if instance is None:
            app = QtGui.QApplication([])
        else:
            app = instance

        interface = new_interface(args.iname,
                                  value=eval(args.value),
                                  **eval(args.constraints))
        control = Control('a',
                          interface,
                          value=eval(args.value),
                          widget=args.widget)
        w = CheckSizes(control, sys.argv[2:])
        w.show()
        w.raise_()

        if instance is None:
            app.exec_()
Esempio n. 12
0
def widget(iname, value, shape=None, preferred=None):
    control = Control(iname.__class__.__name__, iname, value)
    return qt_editor(control, shape, preferred)
Esempio n. 13
0
def import_lpy_controls(filepath):
    if not path(filepath).isfile():
        return

    control = dict()

    f = open(filepath, 'r')
    script = f.read()
    f.close()

    if script is None: script = ""
    beginTag = LpyParsing.InitialisationBeginTag
    if not beginTag in script:
        return str(script), control
    else:
        txts = str(script).split(beginTag)
        new_script = txts[0]
        context_to_translate = txts[1]
        context = Lsystem().context()
        context.initialiseFrom(beginTag + context_to_translate)

    managers = get_managers()
    visualparameters = []
    scalars = []
    functions = []
    curves = []
    geoms = []

    lpy_code_version = 1.0
    if context.has_key('__lpy_code_version__'):
        lpy_code_version = context['__lpy_code_version__']
    if context.has_key('__scalars__'):
        scalars_ = context['__scalars__']
        scalars = [ProduceScalar(v) for v in scalars_]
    if context.has_key('__functions__') and lpy_code_version <= 1.0:
        functions = context['__functions__']
        for n, c in functions:
            c.name = n
        functions = [c for n, c in functions]
        funcmanager = managers['Function']
        geoms += [(funcmanager, func) for func in functions]
    if context.has_key('__curves__') and lpy_code_version <= 1.0:
        curves = context['__curves__']
        for n, c in curves:
            c.name = n
        curves = [c for n, c in curves]
        curvemanager = managers['Curve2D']
        geoms += [(curvemanager, curve) for curve in curves]
    if context.has_key('__parameterset__'):
        for panelinfo, objects in context['__parameterset__']:
            for typename, obj in objects:
                visualparameters.append((managers[typename], obj))

    for scalar in scalars:
        control[unicode(scalar.name)] = scalar.value
    for (manager, geom) in geoms:
        if geom != list():
            control[geom.getName()] = geom
    for (manager, geom) in visualparameters:
        if geom != list():
            control[geom.getName()] = geom

    new_controls = []
    for name, value in control.items():
        interfaces = guess_interface(value)
        if interfaces:
            new_controls.append(Control(name, interfaces[0], value))

    try:
        control["color map"] = to_color(context.turtle.getColorList())
    except AttributeError:
        pass
    else:
        new_controls.append(
            Control("color map", 'IColorList', control["color map"]))

    for control in new_controls:
        register_control(control)