Ejemplo n.º 1
0
 def getDialog(cls, name, parent=None):
     """
     Generates a dialog for this class widget and returns it.
     
     :param      parent | <QtGui.QWidget> || None
     
     :return     <QtGui.QDialog>
     """
     key = '_{0}__{1}_dialog'.format(cls.__name__, name)
     dlgref = getattr(cls, key, None)
     
     if dlgref is not None:
         dlg = dlgref()
         if dlg:
             return dlg
         
     if parent is None:
         parent = QApplication.activeWindow()
     
     dlg = QDialog(parent)
     
     # create widget
     widget = cls(dlg)
     dlg.__dict__['_mainwidget'] = widget
     widget.layout().setContentsMargins(0, 0, 0, 0)
     
     # create buttons
     opts    = QDialogButtonBox.Save | QDialogButtonBox.Cancel
     buttons = QDialogButtonBox(opts, Qt.Horizontal, dlg)
     
     # create layout
     layout = QVBoxLayout()
     layout.addWidget(widget)
     layout.addWidget(buttons)
     dlg.setLayout(layout)
     dlg.resize(widget.minimumSize() + QSize(15, 15))
     widget.resizeRequested.connect(dlg.adjustSize)
     
     # create connections
     buttons.accepted.connect(widget.save)
     buttons.rejected.connect(dlg.reject)
     widget.saved.connect(dlg.accept)
     widget.setFocus()
     
     dlg.adjustSize()
     if parent and parent.window():
         center = parent.window().geometry().center()
         dlg.move(center.x() - dlg.width() / 2.0,
                  center.y() - dlg.height() / 2.0)
     
     setattr(cls, key, weakref.ref(dlg))
     return dlg
Ejemplo n.º 2
0
 def edit( parent, template, actions = None ):
     """
     Prompts the user to edit the menu template with the given actions. \
     If no actions are supplied, then the actions from the parent will \
     be used.
     
     :param      parent   | <QWidget>
                 template | <str>
                 actions  | {<str> name: <QAction>, .. } || None
     
     :return     (<str> template, <bool> accepted)
     """
     # collect the potential actions from the widget
     if ( actions is None ):
         actions = {}
         for action in parent.actions():
             key = nativestring(action.objectName())
             if ( not key ):
                 key = nativestring(action.text()).replace('&', '')
             
             if ( key ):
                 actions[key] = action
     
     if ( not actions ):
         return ('', False)
     
     dlg = QDialog(parent)
     dlg.setWindowTitle('Edit Menu')
     
     widget = XMenuTemplateWidget(dlg)
     widget.setActions(actions)
     widget.setMenuTemplate(template)
     widget.layout().setContentsMargins(0, 0, 0, 0)
     
     opts = QDialogButtonBox.Save | QDialogButtonBox.Cancel
     btns = QDialogButtonBox(opts, Qt.Horizontal, dlg)
     
     btns.accepted.connect( dlg.accept )
     btns.rejected.connect( dlg.reject )
     
     layout = QVBoxLayout()
     layout.addWidget(widget)
     layout.addWidget(btns)
     
     dlg.setLayout(layout)
     dlg.adjustSize()
     dlg.resize(650, 400)
     
     if ( dlg.exec_() ):
         return (widget.menuTemplate(), True)
     return ('', False)
Ejemplo n.º 3
0
    def edit(parent, template, actions=None):
        """
        Prompts the user to edit the menu template with the given actions. \
        If no actions are supplied, then the actions from the parent will \
        be used.
        
        :param      parent   | <QWidget>
                    template | <str>
                    actions  | {<str> name: <QAction>, .. } || None
        
        :return     (<str> template, <bool> accepted)
        """
        # collect the potential actions from the widget
        if (actions is None):
            actions = {}
            for action in parent.actions():
                key = nativestring(action.objectName())
                if (not key):
                    key = nativestring(action.text()).replace('&', '')

                if (key):
                    actions[key] = action

        if (not actions):
            return ('', False)

        dlg = QDialog(parent)
        dlg.setWindowTitle('Edit Menu')

        widget = XMenuTemplateWidget(dlg)
        widget.setActions(actions)
        widget.setMenuTemplate(template)
        widget.layout().setContentsMargins(0, 0, 0, 0)

        opts = QDialogButtonBox.Save | QDialogButtonBox.Cancel
        btns = QDialogButtonBox(opts, Qt.Horizontal, dlg)

        btns.accepted.connect(dlg.accept)
        btns.rejected.connect(dlg.reject)

        layout = QVBoxLayout()
        layout.addWidget(widget)
        layout.addWidget(btns)

        dlg.setLayout(layout)
        dlg.adjustSize()
        dlg.resize(650, 400)

        if (dlg.exec_()):
            return (widget.menuTemplate(), True)
        return ('', False)