Exemplo n.º 1
0
    def __init__(self,
                 parent=None,
                 locals=None,
                 log='',
                 fontSize=10,
                 commandWidget=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        # Setup TextBrowser
        self.ui.tb_view.setWordWrapMode(QtGui.QTextOption.WrapAnywhere)
        self.ui.tb_view.keyPressEvent = self.tb_out_keypress

        # Setup Command
        if not commandWidget:
            try:
                import scintilla_cmd
                commandWidget = scintilla_cmd.cmd_widget()
            except:
                commandWidget = None

        if commandWidget:
            # Scintilla Commandline
            self.ui.le_cmd = commandWidget

        else:
            # Line Edit
            self.ui.le_cmd = QtGui.QLineEdit()
            self.ui.le_cmd.widgetObject = QtGui.QLineEdit
            self.ui.le_cmd.type = 'qlineedit'

        self.ui.frame.layout().addWidget(self.ui.le_cmd, 0, 2, 1, 1)
        self.ui.le_cmd.keyPressEvent = self.cmdKeyPress

        # font
        if os.name == 'posix':
            font = QtGui.QFont("Monospace", fontSize)
        elif os.name == 'nt' or os.name == 'dos':
            font = QtGui.QFont("Courier New", fontSize)
        else:
            print(SystemExit, "FIXME for 'os2', 'mac', 'ce' or 'riscos'")
##            raise Exception(SystemExit, "FIXME for 'os2', 'mac', 'ce' or 'riscos'")
        font.setFixedPitch(1)
        ##        self.setFont(font)
        self.ui.tb_view.setFont(font)
        self.ui.le_cmd.setFont(font)
        self.ui.l_prompt.setFont(font)

        # geometry
        height = 40 * QtGui.QFontMetrics(font).lineSpacing()

        self.ui.tb_view.setTabStopWidth(
            QtGui.QFontMetrics(font).width('    ') - 1)

        # Setup Interpreter
        if locals == None:
            locals = {'self': self, 'scope': parent}
        self.interpreter = Interpreter(locals)

        # Exit with Ctrl+D
        if parent is None:
            self.eofKey = Qt.Key_D
        else:
            self.eofKey = None

##        self.viewers = []

        class SplitStdErr:
            def __init__(self, default_stderr, console_stderr):
                ##                self._targets=targets
                self.default_stderr = default_stderr
                self.console_stderr = console_stderr

            def write(self, line):
                self.default_stderr.write(line)
                self.console_stderr.write(line, mode=2)
##                for target in self._targets:
##                    target.write(line)

# capture all interactive input/output

        sys.stdout = self
        sys.stderr = SplitStdErr(sys.stderr, self)
        sys.stdin = self

        # last line + last incomplete lines
        self.lines = []

        # the cursor position in the last line
        self.point = 0
        # flag: the interpreter needs more input to run the last lines.
        self.more = 0
        # flag: readline() is being used for e.g. raw_input() and input()
        self.reading = 0
        # history
        self.history = []
        self.pointer = 0
        self.indent = ''
        self.prompt = '>>>'
        self.writecount = 0

        # interpreter prompt.
        try:
            sys.ps1
        except AttributeError:
            sys.ps1 = ">>> "
        try:
            sys.ps2
        except AttributeError:
            sys.ps2 = "... "

        self.write('# Python ' + '(' + str(sys.version_info.major) + '.' +
                   str(sys.version_info.minor) + '.' +
                   str(sys.version_info.micro) + ')\n',
                   mode=1)
        self.write('\n')
        # Focus to command prompt
        self.ui.le_cmd.setFocus()