コード例 #1
0
class CheckSizes(Ui_Form, QtGui.QWidget):

    def __init__(self, control, edit_mode='edit'):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

        text = 'interface: %s, preferred widget: %s, edit mode: %s' % (control.interface, control.widget, edit_mode)
        self.l_title.setText(text)

        self._control = control

        self._qcontainer = QControlContainer()
        self._qcontainer.add_control(self._control)
        self._qcontainer.create_actions(self)

#        m = getattr(scontrol, '%s_qt' % edit_mode)
        m = getattr(scontrol, 'qt_editor')

        self._qtcontrols = []

        valid_widget_shape = None
        for shape in ['vline', 'hline', 'small', 'large', 'responsive']:
            layout = getattr(self, 'l_%s' % shape)
            widget = m(self._control, shape=shape)
            if widget:
                layout.addWidget(widget)
                valid_widget_shape = shape
                self._qtcontrols.append(widget)
            else:
                layout.addWidget(QtGui.QLabel("X"))

        if valid_widget_shape:
            self._test_widget = m(self._control, shape=valid_widget_shape)
            self.layout_sample.addWidget(self._test_widget)
            self.cb_read.toggled.connect(self.on_mode_changed)
            self.cb_apply.toggled.connect(self.on_mode_changed)

        self._model = ControlModel(manager=self._qcontainer)
        self._view = ControlView()
        self._view.setModel(self._model)
        self._view.hideColumn(0)
        self.l_large_box.addWidget(self._view, 1, 1, 2, 1)

    def contextMenuEvent(self, event):
        menu = QtGui.QMenu(self)
        menu.addActions(self._qcontainer.actions())
        menu.exec_(event.pos())

    def on_mode_changed(self, state):
        autoread = self.cb_read.isChecked()
        autoapply = self.cb_apply.isChecked()
        for widget in self._qtcontrols:
            widget.autoapply(self._control, autoapply)
            widget.autoread(self._control, autoread)
コード例 #2
0
class AppletFrame(QtGui.QWidget):

    """
    """

    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()

    def fine_tune(self):
        if sys.platform == 'darwin':
            self._layout.setContentsMargins(0, 5, 0, 0)
            self.setContentsMargins(0, 5, 0, 0)
        else:
            # warning: drawin case above segfault on linux platform with Qt 4.8.6
            # but works with size==2 instead of 5
            # This is maybe due to default handle/splitters size, see splitterui module
            self._layout.setContentsMargins(0, 0, 0, 0)
            self.setContentsMargins(0, 0, 0, 0)

    def _on_prop_changed(self, prop, value):
        if prop is self._show_toolbar:
            self.show_toolbar(value)
        elif prop is self._show_title:
            self.show_title(value)

    def _create_actions(self):
        self._props.create_actions(self)

    def menu_actions(self):
        if self._applet:
            applet = self._applet()
        else:
            applet = None
        if self._edit_mode:
            actions = self._props.actions()
        else:
            actions = menu_actions(applet)
        return actions

    def contextMenuEvent(self, event):
        menu = QtGui.QMenu()
        fill_menu(menu, self.menu_actions())
        menu.exec_(event.globalPos())

    def set_edit_mode(self, edit=True):
        self._edit_mode = edit

    def set_applet(self, applet):
        self._applet = weakref.ref(applet)
        self._layout.insertWidget(1, applet)
        _plugin = plugin(applet.name, 'oalab.applet')
        self._l_title.setText(_plugin.label)
        p = QtGui.QSizePolicy
        applet.setSizePolicy(p(p.MinimumExpanding, p.MinimumExpanding))

    def applet(self):
        return self._applet

    def remove_applet(self, applet):
        self._layout.removeWidget(applet)

    def show_title(self, show=True):
        self._show_title.value = show
        self._l_title.setVisible(show)

    def show_toolbar(self, show=True):
        self._show_toolbar.value = show
        if show:
            self.fill_toolbar()
        else:
            self.clear_toolbar()

    def fill_toolbar(self):

        if self._show_toolbar.value is False:
            return

        if self._applet is None:
            return

        applet = self._applet()
        if applet is None:
            return

        # Fill toolbar
        self._menu.show()
        self.clear_toolbar()
        if hasattr(applet, 'local_toolbar_actions'):
            actions = applet.local_toolbar_actions()
        elif hasattr(applet, 'toolbar_actions'):
            actions = applet.toolbar_actions()
        else:
            actions = None
        if actions:
            self._menu.set_actions('NoName', actions)

    def clear_toolbar(self):
        if self._show_toolbar.value:
            self._menu.clear()
        else:
            self._menu.hide()

    def properties(self):
        return self._props.changed()

    def set_properties(self, properties):
        self._props.update(properties)