|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from PyQt4.QtCore import * |
| 4 | +from PyQt4.QtGui import * |
| 5 | + |
| 6 | +import traceback |
| 7 | +import sys |
| 8 | + |
| 9 | +from ui_qgspythondialog import Ui_QgsPythonDialog |
| 10 | + |
| 11 | +_console = None |
| 12 | + |
| 13 | + |
| 14 | +def show_console(): |
| 15 | + """ called from QGIS to open the console """ |
| 16 | + global _console |
| 17 | + if _console is None: |
| 18 | + _console = QgsPythonConsole() |
| 19 | + _console.show() |
| 20 | + _console.raise_() |
| 21 | + _console.setWindowState( _console.windowState() & ~Qt.WindowMinimized ) |
| 22 | + _console.activateWindow() |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +_old_stdout = sys.stdout |
| 27 | + |
| 28 | +class QgisOutputCatcher: |
| 29 | + def __init__(self): |
| 30 | + self.data = '' |
| 31 | + def write(self, stuff): |
| 32 | + self.data += stuff |
| 33 | + def get_and_clean_data(self): |
| 34 | + tmp = self.data |
| 35 | + self.data = '' |
| 36 | + return tmp |
| 37 | + def flush(self): |
| 38 | + pass |
| 39 | + |
| 40 | +def installConsoleHook(): |
| 41 | + sys.stdout = QgisOutputCatcher() |
| 42 | + |
| 43 | +def uninstallConsoleHook(): |
| 44 | + sys.stdout = _old_stdout |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +class ConsoleHistory(object): |
| 49 | + def __init__(self): |
| 50 | + self.cmds = [] |
| 51 | + self.pos = 0 |
| 52 | + self.active_cmd = u"" # active, not yet entered command |
| 53 | + |
| 54 | + def add_cmd(self, command): |
| 55 | + if len(command) != 0: |
| 56 | + self.cmds.append(command) |
| 57 | + self.pos = len(self.cmds) |
| 58 | + self.active_cmd = u"" |
| 59 | + |
| 60 | + def get_previous_cmd(self, current): |
| 61 | + if self.pos == 0: |
| 62 | + return None |
| 63 | + |
| 64 | + if self.pos == len(self.cmds): |
| 65 | + self.active_cmd = current |
| 66 | + else: |
| 67 | + self.cmds[self.pos] = current |
| 68 | + |
| 69 | + self.pos -= 1 |
| 70 | + return self.cmds[self.pos] |
| 71 | + |
| 72 | + def get_next_cmd(self, current): |
| 73 | + |
| 74 | + # if we're at active (last) command, don't move |
| 75 | + if self.pos == len(self.cmds): |
| 76 | + return None |
| 77 | + |
| 78 | + self.cmds[self.pos] = current |
| 79 | + self.pos += 1 |
| 80 | + |
| 81 | + if self.pos == len(self.cmds): |
| 82 | + return self.active_cmd |
| 83 | + else: |
| 84 | + return self.cmds[self.pos] |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +class QgsPythonConsole(QWidget, Ui_QgsPythonDialog): |
| 89 | + def __init__(self, parent=None): |
| 90 | + QWidget.__init__(self, parent) |
| 91 | + |
| 92 | + self.setupUi(self) |
| 93 | + |
| 94 | + # minimize button was not enabled on mac |
| 95 | + self.setWindowFlags( self.windowFlags() | Qt.WindowMinimizeButtonHint ) |
| 96 | + |
| 97 | + self.history = ConsoleHistory() |
| 98 | + |
| 99 | + self.console_globals = {} |
| 100 | + |
| 101 | + def escapeHtml(self, text): |
| 102 | + return text.replace("<", "<").replace(">", ">") |
| 103 | + |
| 104 | + @pyqtSlot() |
| 105 | + def on_pbnPrev_clicked(self): |
| 106 | + cmd = self.history.get_previous_cmd( self.getCommand() ) |
| 107 | + if cmd is not None: |
| 108 | + self.edtCmdLine.setText(cmd) |
| 109 | + |
| 110 | + @pyqtSlot() |
| 111 | + def on_pbnNext_clicked(self): |
| 112 | + cmd = self.history.get_next_cmd( self.getCommand() ) |
| 113 | + if cmd is not None: |
| 114 | + self.edtCmdLine.setText(cmd) |
| 115 | + |
| 116 | + def getCommand(self): |
| 117 | + return unicode(self.edtCmdLine.toPlainText()) |
| 118 | + |
| 119 | + def execute(self, single): |
| 120 | + command = self.getCommand() |
| 121 | + |
| 122 | + self.history.add_cmd(command) |
| 123 | + |
| 124 | + try: |
| 125 | + # run command |
| 126 | + code = compile(command, '<console>', 'single' if single else 'exec') |
| 127 | + res = eval(code, self.console_globals) |
| 128 | + result = unicode( res ) if res is not None else u'' |
| 129 | + |
| 130 | + # get standard output |
| 131 | + output = sys.stdout.get_and_clean_data() |
| 132 | + |
| 133 | + #_old_stdout.write("cmd: '"+command+"'\n") |
| 134 | + #_old_stdout.write("res: '"+result+"'\n") |
| 135 | + #_old_stdout.write("out: '"+output+"'\n") |
| 136 | + #_old_stdout.flush() |
| 137 | + |
| 138 | + # escape the result so python objects display properly and |
| 139 | + # we can still use html output to get nicely formatted display |
| 140 | + output = self.escapeHtml( output ) + self.escapeHtml( result ) |
| 141 | + if len(output) != 0: |
| 142 | + output += "<br>" |
| 143 | + |
| 144 | + except Exception, e: |
| 145 | + #lst = traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback) |
| 146 | + lst = traceback.format_exception_only(sys.exc_type, sys.exc_value) |
| 147 | + errorText = "<br>".join(map(self.escapeHtml, lst)) |
| 148 | + output = "<font color=\"red\">" + errorText + "</font><br>" |
| 149 | + |
| 150 | + s = "<b><font color=\"green\">>>></font> " + self.escapeHtml( command ) + "</b><br>" + output |
| 151 | + self.edtCmdLine.setText("") |
| 152 | + |
| 153 | + self.txtHistory.moveCursor(QTextCursor.End) |
| 154 | + self.txtHistory.insertHtml(s) |
| 155 | + self.txtHistory.moveCursor(QTextCursor.End) |
| 156 | + self.txtHistory.ensureCursorVisible() |
| 157 | + |
| 158 | + @pyqtSlot() |
| 159 | + def on_pbnExecute_clicked(self): |
| 160 | + self.execute(False) |
| 161 | + |
| 162 | + @pyqtSlot() |
| 163 | + def on_pbnEval_clicked(self): |
| 164 | + self.execute(True) |
| 165 | + |
| 166 | + def showEvent(self, event): |
| 167 | + QWidget.showEvent(self, event) |
| 168 | + installConsoleHook() |
| 169 | + |
| 170 | + def closeEvent(self, event): |
| 171 | + uninstallConsoleHook() |
| 172 | + QWidget.closeEvent(self, event) |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == '__main__': |
| 176 | + import sys |
| 177 | + a = QApplication(sys.argv) |
| 178 | + w = QgsPythonConsole() |
| 179 | + w.show() |
| 180 | + a.exec_() |
0 commit comments