|
| 1 | +# -*- coding:utf-8 -*- |
| 2 | +""" |
| 3 | +/*************************************************************************** |
| 4 | +Python Conosle for QGIS |
| 5 | + ------------------- |
| 6 | +begin : 2012-09-10 |
| 7 | +copyright : (C) 2012 by Salvatore Larosa |
| 8 | +email : lrssvtml (at) gmail (dot) com |
| 9 | + ***************************************************************************/ |
| 10 | +
|
| 11 | +/*************************************************************************** |
| 12 | + * * |
| 13 | + * This program is free software; you can redistribute it and/or modify * |
| 14 | + * it under the terms of the GNU General Public License as published by * |
| 15 | + * the Free Software Foundation; either version 2 of the License, or * |
| 16 | + * (at your option) any later version. * |
| 17 | + * * |
| 18 | + ***************************************************************************/ |
| 19 | +Some portions of code were taken from https://code.google.com/p/pydee/ |
| 20 | +""" |
| 21 | + |
| 22 | +from PyQt4.QtCore import * |
| 23 | +from PyQt4.QtGui import * |
| 24 | +from PyQt4.Qsci import (QsciScintilla, |
| 25 | + QsciScintillaBase, |
| 26 | + QsciLexerPython) |
| 27 | +from console_sci import PythonEdit |
| 28 | +import sys |
| 29 | + |
| 30 | +class writeOut: |
| 31 | + def __init__(self, edit, out=None, style=None): |
| 32 | + """ |
| 33 | + This class allow to write stdout and stderr |
| 34 | + """ |
| 35 | + self.editor = edit |
| 36 | + self.out = None |
| 37 | + self.style = style |
| 38 | + |
| 39 | + def write(self, m): |
| 40 | + if self.style == "traceback": |
| 41 | + self.editor.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 1) |
| 42 | + self.editor.append(m) |
| 43 | + self.editor.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 1) |
| 44 | + else: |
| 45 | + self.editor.append(m) |
| 46 | + self.move_cursor_to_end() |
| 47 | + |
| 48 | + if self.out: |
| 49 | + self.out.write(m) |
| 50 | + |
| 51 | + def move_cursor_to_end(self): |
| 52 | + """Move cursor to end of text""" |
| 53 | + line, index = self.get_end_pos() |
| 54 | + self.editor.setCursorPosition(line, index) |
| 55 | + self.editor.ensureCursorVisible() |
| 56 | + self.editor.ensureLineVisible(line) |
| 57 | + |
| 58 | + def get_end_pos(self): |
| 59 | + """Return (line, index) position of the last character""" |
| 60 | + line = self.editor.lines() - 1 |
| 61 | + return (line, self.editor.text(line).length()) |
| 62 | + |
| 63 | + def flush(self): |
| 64 | + pass |
| 65 | + |
| 66 | +class EditorOutput(QsciScintilla): |
| 67 | + def __init__(self, parent=None): |
| 68 | + #QsciScintilla.__init__(self, parent) |
| 69 | + super(EditorOutput,self).__init__(parent) |
| 70 | + # Enable non-ascii chars for editor |
| 71 | + self.setUtf8(True) |
| 72 | + |
| 73 | + sys.stdout = writeOut(self, sys.stdout) |
| 74 | + sys.stderr = writeOut(self, sys.stderr, "traceback") |
| 75 | + |
| 76 | + self.edit = PythonEdit() |
| 77 | + self.setLexers() |
| 78 | + self.setReadOnly(True) |
| 79 | + |
| 80 | + # Set the default font |
| 81 | + font = QFont() |
| 82 | + font.setFamily('Courier') |
| 83 | + font.setFixedPitch(True) |
| 84 | + font.setPointSize(10) |
| 85 | + self.setFont(font) |
| 86 | + self.setMarginsFont(font) |
| 87 | + # Margin 0 is used for line numbers |
| 88 | + #fm = QFontMetrics(font) |
| 89 | + self.setMarginsFont(font) |
| 90 | + self.setMarginWidth(1, "00000") |
| 91 | + self.setMarginLineNumbers(1, True) |
| 92 | + self.setMarginsForegroundColor(QColor("#3E3EE3")) |
| 93 | + self.setMarginsBackgroundColor(QColor("#f9f9f9")) |
| 94 | + self.setCaretLineVisible(True) |
| 95 | + self.setCaretLineBackgroundColor(QColor("#fcf3ed")) |
| 96 | + |
| 97 | + |
| 98 | + # Folding |
| 99 | + #self.setFolding(QsciScintilla.BoxedTreeFoldStyle) |
| 100 | + #self.setFoldMarginColors(QColor("#99CC66"),QColor("#333300")) |
| 101 | + #self.setWrapMode(QsciScintilla.WrapCharacter) |
| 102 | + |
| 103 | + ## Edge Mode : does not seems to work |
| 104 | + #self.setEdgeMode(QsciScintilla.EdgeLine) |
| 105 | + #self.setEdgeColumn(80) |
| 106 | + #self.setEdgeColor(QColor("#FF0000")) |
| 107 | + |
| 108 | + self.SendScintilla(QsciScintilla.SCI_SETWRAPMODE, 2) |
| 109 | + self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0) |
| 110 | + |
| 111 | + def refreshLexerProperties(self): |
| 112 | + self.setLexers() |
| 113 | + |
| 114 | + def setLexers(self): |
| 115 | + self.lexer = QsciLexerPython() |
| 116 | + |
| 117 | + settings = QSettings() |
| 118 | + loadFont = settings.value("pythonConsole/fontfamilytext").toString() |
| 119 | + fontSize = settings.value("pythonConsole/fontsize").toInt()[0] |
| 120 | + font = QFont(loadFont) |
| 121 | + font.setFixedPitch(True) |
| 122 | + font.setPointSize(fontSize) |
| 123 | + |
| 124 | + self.lexer.setDefaultFont(font) |
| 125 | + self.lexer.setColor(Qt.red, 1) |
| 126 | + self.lexer.setColor(Qt.darkGreen, 5) |
| 127 | + self.lexer.setColor(Qt.darkBlue, 15) |
| 128 | + self.lexer.setFont(font, 1) |
| 129 | + self.lexer.setFont(font, 2) |
| 130 | + self.lexer.setFont(font, 3) |
| 131 | + self.lexer.setFont(font, 4) |
| 132 | + |
| 133 | + self.setLexer(self.lexer) |
| 134 | + |
| 135 | + def getTextFromEditor(self): |
| 136 | + text = self.text() |
| 137 | + textList = text.split("\n") |
| 138 | + return textList |
| 139 | + |
| 140 | + def clearConsole(self): |
| 141 | + #self.SendScintilla(QsciScintilla.SCI_CLEARALL) |
| 142 | + self.setText('') |
| 143 | + |
| 144 | + def contextMenuEvent(self, e): |
| 145 | + menu = QMenu(self) |
| 146 | + runAction = menu.addAction("Enter Selected") |
| 147 | + copyAction = menu.addAction("Copy CTRL+C") |
| 148 | + runAction.setEnabled(False) |
| 149 | + if self.hasSelectedText(): |
| 150 | + runAction.setEnabled(True) |
| 151 | + action = menu.exec_(self.mapToGlobal(e.pos())) |
| 152 | + if action == runAction: |
| 153 | + cmd = self.selectedText() |
| 154 | + self.edit.insertFromDropPaste(cmd) |
| 155 | + self.edit.entered() |
| 156 | + if action == copyAction: |
| 157 | + self.copy() |
| 158 | + |
| 159 | + def copy(self): |
| 160 | + """Copy text to clipboard... or keyboard interrupt""" |
| 161 | + if self.hasSelectedText(): |
| 162 | + text = unicode(self.selectedText()) |
| 163 | + text = text.replace('>>> ', '').replace('... ', '').strip() # removing prompts |
| 164 | + QApplication.clipboard().setText(text) |
| 165 | + else: |
| 166 | + self.emit(SIGNAL("keyboard_interrupt()")) |
| 167 | + |
0 commit comments