Exemplo n.º 1
0
    def __init__(self, parent, exception=None, val=None):
        QtWidgets.QDialog.__init__(self, parent)
        self._ui = Ui_PythonExpressionPrompt()
        self._ui.setupUi(self)
        self._mainWindow = parent._mainWindow  # get mainWindow instance
        self._val = val  # this is the object that is currently authored
        self._oldValID = -1  # this is the ID of the current object at "_"

        self._setupConsole()  # get or create a python console widget

        # if a exception was passed as an argument, raise it so it prints.
        if exception is not None:
            print >> sys.stderr, exception

        self._ui.buttonBox.button(
            QtWidgets.QDialogButtonBox.Apply).clicked.connect(self.accept)
Exemplo n.º 2
0
    def __init__(self, parent, exception = None, val = None):
        QtGui.QDialog.__init__(self, parent)
        self._ui = Ui_PythonExpressionPrompt()
        self._ui.setupUi(self)
        self._mainWindow = parent._mainWindow     # get mainWindow instance
        self._val = val         # this is the object that is currently authored
        self._oldValID = -1     # this is the ID of the current object at "_"
        
        self._setupConsole()    # get or create a python console widget

        # if a exception was passed as an argument, raise it so it prints.
        if exception is not None:
            print >> sys.stderr, exception

        QtCore.QObject.connect(self._ui.buttonBox.button(
                               QtGui.QDialogButtonBox.Apply),
                               QtCore.SIGNAL('clicked()'),
                               self,
                               QtCore.SLOT('accept()'))
Exemplo n.º 3
0
class PythonExpressionPrompt(QtWidgets.QDialog):
    def __init__(self, parent, exception=None, val=None):
        QtWidgets.QDialog.__init__(self, parent)
        self._ui = Ui_PythonExpressionPrompt()
        self._ui.setupUi(self)
        self._mainWindow = parent._mainWindow  # get mainWindow instance
        self._val = val  # this is the object that is currently authored
        self._oldValID = -1  # this is the ID of the current object at "_"

        self._setupConsole()  # get or create a python console widget

        # if a exception was passed as an argument, raise it so it prints.
        if exception is not None:
            print >> sys.stderr, exception

        self._ui.buttonBox.button(
            QtWidgets.QDialogButtonBox.Apply).clicked.connect(self.accept)

    def _setupConsole(self):
        # get or create an instance of Myconsole
        if Myconsole.instance is None:
            Myconsole.instance = Myconsole(self)

        # setting the parent places the Myconsole widget inside this one
        Myconsole.instance.setParent(self)

        # place the console
        self._console = Myconsole.instance
        self._ui.consoleLayout.addWidget(self._console)
        self._console.setFocus()  # puts the cursor in the interpreter
        self._console.reloadConsole(self._mainWindow,
                                    self._val)  # reloads locals

        # adjust splitter size
        self._ui.splitter.setSizes((350, 150))
        self.updatePreview()  # update the preview box

        # we want to update the preview box whenver the value of "_" changes
        self._console.textChanged.connect(self.updatePreview)

    def updatePreview(self):
        # called when the text in the interpreter changes, but the preview is
        # only updated if the value of "_" has changed
        val = self._console.getLastValue()  # value of "_"

        # check if the id of the value at "_" has changed
        if id(val) != self._oldValID:
            self._ui.previewBox.setText(prettyPrint(val))
            self._oldValID = id(val)

    def accept(self):
        # called when clicking the Save button
        # grab the value of "_" and save it in self._val
        self._val = self._console.getLastValue()
        QtWidgets.QDialog.accept(self)

    def reject(self):
        # called when clicking the Close Without Saving button
        self._val = None
        QtWidgets.QDialog.reject(self)

    def exec_(self):
        # called to "execute" the dialog process
        # we override this function to make sure to return I/O to stdin and
        # stdout, and make exec_ return the last value of "_"
        QtWidgets.QDialog.exec_(self)
        return self._val

    def __del__(self):
        # we have to remove the parent from the "Myconsole" instance
        # because the parent (self) is about to be deleted.
        # not doing this will cause crashes.
        Myconsole.instance.setParent(None)

    @staticmethod
    def getValueFromPython(parent, exception=None, val=None):
        # this is the function to call to invoke this python prompt
        # it returns the last value of "_"
        prompt = PythonExpressionPrompt(parent, exception, val)
        val = prompt.exec_()
        return val
Exemplo n.º 4
0
class PythonExpressionPrompt(QtGui.QDialog):

    def __init__(self, parent, exception = None, val = None):
        QtGui.QDialog.__init__(self, parent)
        self._ui = Ui_PythonExpressionPrompt()
        self._ui.setupUi(self)
        self._mainWindow = parent._mainWindow     # get mainWindow instance
        self._val = val         # this is the object that is currently authored
        self._oldValID = -1     # this is the ID of the current object at "_"
        
        self._setupConsole()    # get or create a python console widget

        # if a exception was passed as an argument, raise it so it prints.
        if exception is not None:
            print >> sys.stderr, exception

        QtCore.QObject.connect(self._ui.buttonBox.button(
                               QtGui.QDialogButtonBox.Apply),
                               QtCore.SIGNAL('clicked()'),
                               self,
                               QtCore.SLOT('accept()'))

    def _setupConsole(self):
        # get or create an instance of Myconsole
        if Myconsole.instance is None:
            Myconsole.instance = Myconsole(self)
        
        # setting the parent places the Myconsole widget inside this one
        Myconsole.instance.setParent(self)

        # place the console
        self._console = Myconsole.instance
        self._ui.consoleLayout.addWidget(self._console)
        self._console.setFocus()    # puts the cursor in the interpreter
        self._console.reloadConsole(self._mainWindow, self._val) # reloads locals

        # adjust splitter size
        self._ui.splitter.setSizes((350,150))
        self.updatePreview()    # update the preview box

        # we want to update the preview box whenver the value of "_" changes
        QtCore.QObject.connect(self._console,
                               QtCore.SIGNAL('textChanged()'),
                               self.updatePreview)

    def updatePreview(self):
        # called when the text in the interpreter changes, but the preview is
        # only updated if the value of "_" has changed
        val = self._console.getLastValue() # value of "_"

        # check if the id of the value at "_" has changed
        if id(val) != self._oldValID:
            self._ui.previewBox.setText(prettyPrint(val))
            self._oldValID = id(val)

    def accept(self):
        # called when clicking the Save button
        # grab the value of "_" and save it in self._val
        self._val = self._console.getLastValue()
        QtGui.QDialog.accept(self)

    def reject(self):
        # called when clicking the Close Without Saving button
        self._val = None
        QtGui.QDialog.reject(self)

    def exec_(self):
        # called to "execute" the dialog process
        # we override this function to make sure to return I/O to stdin and
        # stdout, and make exec_ return the last value of "_"
        QtGui.QDialog.exec_(self)
        return self._val
    
    def __del__(self):
        # we have to remove the parent from the "Myconsole" instance
        # because the parent (self) is about to be deleted.
        # not doing this will cause crashes.
        Myconsole.instance.setParent(None)

    @staticmethod
    def getValueFromPython(parent, exception = None, val = None):
        # this is the function to call to invoke this python prompt
        # it returns the last value of "_"
        prompt = PythonExpressionPrompt(parent, exception, val)
        val = prompt.exec_()
        return val