Exemplo 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()
Exemplo n.º 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)