def _create_buttons(self, dialog, layout):
        """ Creates the buttons. """

        if not (self.can_cancel or self.can_ok):
            return

        # Create the button.
        buttons = QtGui.QDialogButtonBox()

        if self.can_cancel:
            buttons.addButton(
                self.cancel_button_label, QtGui.QDialogButtonBox.RejectRole
            )
        if self.can_ok:
            buttons.addButton(QtGui.QDialogButtonBox.Ok)

        # TODO: hookup the buttons to our methods, this may involve subclassing from QDialog

        if self.can_cancel:
            buttons.rejected.connect(dialog.reject)
            self._connections_to_remove.append(
                (buttons.rejected, dialog.reject)
            )
        if self.can_ok:
            buttons.accepted.connect(dialog.accept)
            self._connections_to_remove.append(
                (buttons.accepted, dialog.accept)
            )

        layout.addWidget(buttons)
Example #2
0
    def __init__(self, parent, html, scale_dx, scale_dy):
        """ Initializes the object.
        """
        # Local import to avoid a WebKit dependency when one isn't needed.
        from pyface.qt import QtWebKit

        QtGui.QDialog.__init__(self, parent)
        layout = QtGui.QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)

        # Create the html control
        html_control = QtWebKit.QWebView()
        html_control.setSizePolicy(QtGui.QSizePolicy.Expanding,
                                   QtGui.QSizePolicy.Expanding)
        html_control.setHtml(html)
        layout.addWidget(html_control)

        # Create the OK button
        bbox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok,
                                      QtCore.Qt.Horizontal)
        bbox.accepted.connect(self.accept)
        layout.addWidget(bbox)

        # Position and show the dialog
        position_window(self, parent=parent)
        self.show()
 def _create_cancel_button(self, parent):
     buttons = QtGui.QDialogButtonBox()
     self._cancel_button = buttons.addButton(
         "Cancel", QtGui.QDialogButtonBox.RejectRole)
     self._cancel_button.setDefault(True)
     buttons.rejected.connect(self.cancel, type=QtCore.Qt.QueuedConnection)
     return buttons
Example #4
0
    def _create_buttons(self, dialog, layout):
        """ Creates the buttons. """

        if not (self.can_cancel or self.can_ok):
            return

        # Create the button.
        buttons = QtGui.QDialogButtonBox()

        if self.can_cancel:
            buttons.addButton(self.cancel_button_label,
                              QtGui.QDialogButtonBox.RejectRole)
        if self.can_ok:
            buttons.addButton(QtGui.QDialogButtonBox.Ok)

        # TODO: hookup the buttons to our methods, this may involve subclassing from QDialog

        if self.can_cancel:
            buttons.connect(buttons, QtCore.SIGNAL('rejected()'), dialog,
                            QtCore.SLOT('reject()'))
        if self.can_ok:
            buttons.connect(buttons, QtCore.SIGNAL('accepted()'), dialog,
                            QtCore.SLOT('accept()'))

        layout.addWidget(buttons)
Example #5
0
    def _create_contents(self, parent):
        label = QtGui.QLabel()

        if self.title == "":
            if parent.parent() is not None:
                title = parent.parent().windowTitle()
            else:
                title = ""

            # Set the title.
            self.title = "About %s" % title

        # Set the page contents.
        label.setText(self._create_html())

        # Create the button.
        buttons = QtGui.QDialogButtonBox()

        if self.ok_label:
            buttons.addButton(self.ok_label, QtGui.QDialogButtonBox.AcceptRole)
        else:
            buttons.addButton(QtGui.QDialogButtonBox.Ok)

        buttons.accepted.connect(parent.accept)
        self._connections_to_remove.append((buttons.accepted, parent.accept))

        lay = QtGui.QVBoxLayout()
        lay.addWidget(label)
        lay.addWidget(buttons)

        parent.setLayout(lay)
Example #6
0
    def _create_buttons(self, parent):
        buttons = QtGui.QDialogButtonBox()

        # 'OK' button.
        if self.ok_label:
            btn = buttons.addButton(self.ok_label,
                                    QtGui.QDialogButtonBox.AcceptRole)
        else:
            btn = buttons.addButton(QtGui.QDialogButtonBox.Ok)

        btn.setDefault(True)
        btn.clicked.connect(self.control.accept)

        # 'Cancel' button.
        if self.cancel_label:
            btn = buttons.addButton(self.cancel_label,
                                    QtGui.QDialogButtonBox.RejectRole)
        else:
            btn = buttons.addButton(QtGui.QDialogButtonBox.Cancel)

        btn.clicked.connect(self.control.reject)

        # 'Help' button.
        # FIXME v3: In the original code the only possible hook into the help
        # was to reimplement self._on_help().  However this was a private
        # method.  Obviously nobody uses the Help button.  For the moment we
        # display it but can't actually use it.
        if len(self.help_id) > 0:
            if self.help_label:
                buttons.addButton(self.help_label,
                                  QtGui.QDialogButtonBox.HelpRole)
            else:
                buttons.addButton(QtGui.QDialogButtonBox.Help)

        return buttons
Example #7
0
    def _create_contents(self, parent):
        label = QtGui.QLabel()
        footer = QtGui.QLabel()

        if parent.parent() is not None:
            title = parent.parent().windowTitle()
        else:
            title = ""

        # Set the title.
        self.title = "About %s" % title

        # Load the image to be displayed in the about box.
        # image = self.image.create_image()
        path = self.image.absolute_path

        # The additional strings.
        # additions = '<br />'.join(self.additions)

        # Get the version numbers.
        py_version = sys.version[0:sys.version.find("(")]
        qt_version = QtCore.__version__

        # Set the page contents.
        label.setText(_ABOUT_TEXT % (self.version_info, path))
        footer.setText(_FOOTER_TEXT % (py_version, qt_version))

        # Create the button.
        buttons = QtGui.QDialogButtonBox()

        if self.ok_label:
            buttons.addButton(self.ok_label, QtGui.QDialogButtonBox.AcceptRole)
        else:
            buttons.addButton(QtGui.QDialogButtonBox.Ok)

        # buttons.connect(buttons, QtCore.SIGNAL('accepted()'), parent, QtCore.SLOT('accept()'))
        buttons.accepted.connect(parent.accept)

        revisions = self._create_revisions()
        changes = self._create_changes()
        lay = QtGui.QVBoxLayout()

        lay.addWidget(label)

        lay.addWidget(revisions)
        lay.addWidget(changes)
        lay.addWidget(footer)
        lay.addWidget(buttons)
        parent.setLayout(lay)
    def _create_contents(self, parent):
        label = QtGui.QLabel()

        if self.title == "":
            if parent.parent() is not None:
                title = parent.parent().windowTitle()
            else:
                title = ""

            # Set the title.
            self.title = "About %s" % title

        # Load the image to be displayed in the about box.
        image = self.image.create_image()
        path = self.image.absolute_path

        # The additional strings.
        additions = '<br />'.join(self.additions)

        # Get the version numbers.
        py_version = platform.python_version()
        qt_version = QtCore.__version__

        # Set the page contents.
        label.setText(_DIALOG_TEXT % (path, additions, py_version, qt_version))

        # Create the button.
        buttons = QtGui.QDialogButtonBox()

        if self.ok_label:
            buttons.addButton(self.ok_label, QtGui.QDialogButtonBox.AcceptRole)
        else:
            buttons.addButton(QtGui.QDialogButtonBox.Ok)

        buttons.accepted.connect(parent.accept)

        lay = QtGui.QVBoxLayout()
        lay.addWidget(label)
        lay.addWidget(buttons)

        parent.setLayout(lay)
Example #9
0
    def init(self, ui, parent, style):
        """Initialise the object.
        """
        self.ui = ui
        self.control = ui.control
        view = ui.view

        revert = apply = False

        if self.control is not None:
            if hasattr(self, "revert"):
                revert = self.revert.isEnabled()

            if hasattr(self, "apply"):
                apply = self.apply.isEnabled()

            ui.reset()
        else:
            self.create_dialog(parent, style)

            # Create the 'context' copies we will need while editing:
            context = ui.context
            ui._context = context
            ui.context = self._copy_context(context)
            ui._revert = self._copy_context(context)

        self.set_icon(view.icon)

        # Convert the buttons to actions.
        buttons = [self.coerce_button(button) for button in view.buttons]
        nr_buttons = len(buttons)

        if (nr_buttons != 1) or (not self.is_button(buttons[0], "")):
            bbox = QtGui.QDialogButtonBox()

            # Create the necessary special function buttons.
            if nr_buttons == 0:
                if view.apply:
                    self.check_button(buttons, ApplyButton)
                    if view.revert:
                        self.check_button(buttons, RevertButton)
                if view.ok:
                    self.check_button(buttons, OKButton)
                if view.cancel:
                    self.check_button(buttons, CancelButton)
                if view.help:
                    self.check_button(buttons, HelpButton)

            for raw_button, button in zip(view.buttons, buttons):
                default = raw_button == view.default_button

                if self.is_button(button, "Apply"):
                    self.apply = self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.ApplyRole,
                        self._on_apply,
                        enabled=apply,
                        default=default,
                    )
                    ui.on_trait_change(
                        self._on_applyable, "modified", dispatch="ui"
                    )

                elif self.is_button(button, "Revert"):
                    self.revert = self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.ResetRole,
                        self._on_revert,
                        enabled=revert,
                        default=default,
                    )

                elif self.is_button(button, "OK"):
                    self.ok = self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.AcceptRole,
                        self.control.accept,
                        default=default,
                    )
                    ui.on_trait_change(self._on_error, "errors", dispatch="ui")

                elif self.is_button(button, "Cancel"):
                    self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.RejectRole,
                        self.control.reject,
                        default=default,
                    )

                elif self.is_button(button, "Help"):
                    self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.HelpRole,
                        self._on_help,
                        default=default,
                    )

                elif not self.is_button(button, ""):
                    self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.ActionRole,
                        default=default,
                    )

        else:
            bbox = None

        self.add_contents(panel(ui), bbox)
Example #10
0
    def __init__(self, ui, parent, is_subpanel):
        """Initialise the object.
        """
        self.ui = ui
        history = ui.history
        view = ui.view

        # Reset any existing history listeners.
        if history is not None:
            history.observe(self._on_undoable,
                            "undoable",
                            remove=True,
                            dispatch="ui")
            history.observe(self._on_redoable,
                            "redoable",
                            remove=True,
                            dispatch="ui")
            history.observe(self._on_revertable,
                            "undoable",
                            remove=True,
                            dispatch="ui")

        # Determine if we need any buttons or an 'undo' history.
        buttons = [self.coerce_button(button) for button in view.buttons]
        nr_buttons = len(buttons)
        has_buttons = not is_subpanel and (nr_buttons != 1 or
                                           not self.is_button(buttons[0], ""))

        if nr_buttons == 0:
            if view.undo:
                self.check_button(buttons, UndoButton)
            if view.revert:
                self.check_button(buttons, RevertButton)
            if view.help:
                self.check_button(buttons, HelpButton)

        if not is_subpanel and history is None:
            for button in buttons:
                if self.is_button(button, "Undo") or self.is_button(
                        button, "Revert"):
                    history = ui.history = UndoHistory()
                    break

        # Create the panel.
        self.control = panel(ui)

        # Suppress the title if this is a subpanel or if we think it should be
        # superceded by the title of an "outer" widget (eg. a dock widget).
        title = view.title
        if (is_subpanel or (isinstance(parent, QtGui.QMainWindow)
                            and not isinstance(parent.parent(), QtGui.QDialog))
                or isinstance(parent, QtGui.QTabWidget)):
            title = ""

        # Panels must be widgets as it is only the TraitsUI PyQt code that can
        # handle them being layouts as well.  Therefore create a widget if the
        # panel is not a widget or if we need a title or buttons.
        if (not isinstance(self.control, QtGui.QWidget) or title != ""
                or has_buttons):
            w = QtGui.QWidget()
            layout = QtGui.QVBoxLayout(w)
            layout.setContentsMargins(0, 0, 0, 0)

            # Handle any view title.
            if title != "":
                layout.addWidget(heading_text(None, text=view.title).control)

            if isinstance(self.control, QtGui.QWidget):
                layout.addWidget(self.control)
            elif isinstance(self.control, QtGui.QLayout):
                layout.addLayout(self.control)

            self.control = w

            # Add any buttons.
            if has_buttons:

                # Add the horizontal separator
                separator = QtGui.QFrame()
                separator.setFrameStyle(QtGui.QFrame.Sunken
                                        | QtGui.QFrame.HLine)
                separator.setFixedHeight(2)
                layout.addWidget(separator)

                # Add the special function buttons
                bbox = QtGui.QDialogButtonBox(QtCore.Qt.Horizontal)
                for button in buttons:
                    role = QtGui.QDialogButtonBox.ActionRole
                    if self.is_button(button, "Undo"):
                        self.undo = self.add_button(button, bbox, role,
                                                    self._on_undo, False,
                                                    "Undo")
                        self.redo = self.add_button(button, bbox, role,
                                                    self._on_redo, False,
                                                    "Redo")
                        history.observe(self._on_undoable,
                                        "undoable",
                                        dispatch="ui")
                        history.observe(self._on_redoable,
                                        "redoable",
                                        dispatch="ui")
                    elif self.is_button(button, "Revert"):
                        role = QtGui.QDialogButtonBox.ResetRole
                        self.revert = self.add_button(button, bbox, role,
                                                      self._on_revert, False)
                        history.observe(self._on_revertable,
                                        "undoable",
                                        dispatch="ui")
                    elif self.is_button(button, "Help"):
                        role = QtGui.QDialogButtonBox.HelpRole
                        self.add_button(button, bbox, role, self._on_help)
                    elif not self.is_button(button, ""):
                        self.add_button(button, bbox, role)
                layout.addWidget(bbox)

        # If the UI has a toolbar, should add it to the panel too
        self._add_toolbar(parent)

        # Ensure the control has a size hint reflecting the View specification.
        # Yes, this is a hack, but it's too late to repair this convoluted
        # control building process, so we do what we have to...
        self.control.sizeHint = _size_hint_wrapper(self.control.sizeHint, ui)
Example #11
0
    def init(self, ui, parent, style):
        """Initialise the object.

           FIXME: Note that we treat MODAL and POPUP as equivalent until we
           have an example that demonstrates how POPUP is supposed to work.
        """
        self.ui = ui
        self.control = ui.control
        view = ui.view
        history = ui.history

        if self.control is not None:
            if history is not None:
                history.on_trait_change(
                    self._on_undoable, "undoable", remove=True
                )
                history.on_trait_change(
                    self._on_redoable, "redoable", remove=True
                )
                history.on_trait_change(
                    self._on_revertable, "undoable", remove=True
                )

            ui.reset()
        else:
            self.create_dialog(parent, style)

        self.set_icon(view.icon)

        # Convert the buttons to actions.
        buttons = [self.coerce_button(button) for button in view.buttons]
        nr_buttons = len(buttons)

        no_buttons = (nr_buttons == 1) and self.is_button(buttons[0], "")

        has_buttons = (not no_buttons) and (
            (nr_buttons > 0)
            or view.undo
            or view.revert
            or view.ok
            or view.cancel
        )

        if has_buttons or (view.menubar is not None):
            if history is None:
                history = UndoHistory()
        else:
            history = None

        ui.history = history

        if (not no_buttons) and (has_buttons or view.help):
            bbox = QtGui.QDialogButtonBox()

            # Create the necessary special function buttons.
            if nr_buttons == 0:
                if view.undo:
                    self.check_button(buttons, UndoButton)
                if view.revert:
                    self.check_button(buttons, RevertButton)
                if view.ok:
                    self.check_button(buttons, OKButton)
                if view.cancel:
                    self.check_button(buttons, CancelButton)
                if view.help:
                    self.check_button(buttons, HelpButton)

            for raw_button, button in zip(view.buttons, buttons):
                default = raw_button == view.default_button

                if self.is_button(button, "Undo"):
                    self.undo = self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.ActionRole,
                        self._on_undo,
                        False,
                        default=default,
                    )
                    history.on_trait_change(
                        self._on_undoable, "undoable", dispatch="ui"
                    )
                    if history.can_undo:
                        self._on_undoable(True)

                    self.redo = self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.ActionRole,
                        self._on_redo,
                        False,
                        "Redo",
                    )
                    history.on_trait_change(
                        self._on_redoable, "redoable", dispatch="ui"
                    )
                    if history.can_redo:
                        self._on_redoable(True)

                elif self.is_button(button, "Revert"):
                    self.revert = self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.ResetRole,
                        self._on_revert,
                        False,
                        default=default,
                    )
                    history.on_trait_change(
                        self._on_revertable, "undoable", dispatch="ui"
                    )
                    if history.can_undo:
                        self._on_revertable(True)

                elif self.is_button(button, "OK"):
                    self.ok = self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.AcceptRole,
                        self.control.accept,
                        default=default,
                    )
                    ui.on_trait_change(self._on_error, "errors", dispatch="ui")

                elif self.is_button(button, "Cancel"):
                    self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.RejectRole,
                        self.control.reject,
                        default=default,
                    )

                elif self.is_button(button, "Help"):
                    self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.HelpRole,
                        self._on_help,
                        default=default,
                    )

                elif not self.is_button(button, ""):
                    self.add_button(
                        button,
                        bbox,
                        QtGui.QDialogButtonBox.ActionRole,
                        default=default,
                    )

        else:
            bbox = None

        self.add_contents(panel(ui), bbox)