def __init__(self, parent=None): #QsciScintilla.__init__(self, parent) super(EditorOutput,self).__init__(parent) # Enable non-ascii chars for editor self.setUtf8(True) sys.stdout = writeOut(self, sys.stdout) sys.stderr = writeOut(self, sys.stderr, "traceback") self.edit = PythonEdit() self.setLexers() self.setReadOnly(True) # Set the default font font = QFont() font.setFamily('Courier') font.setFixedPitch(True) font.setPointSize(10) self.setFont(font) self.setMarginsFont(font) # Margin 0 is used for line numbers #fm = QFontMetrics(font) self.setMarginsFont(font) self.setMarginWidth(1, "00000") self.setMarginLineNumbers(1, True) self.setMarginsForegroundColor(QColor("#3E3EE3")) self.setMarginsBackgroundColor(QColor("#f9f9f9")) self.setCaretLineVisible(True) self.setCaretLineBackgroundColor(QColor("#fcf3ed")) # Folding #self.setFolding(QsciScintilla.BoxedTreeFoldStyle) #self.setFoldMarginColors(QColor("#99CC66"),QColor("#333300")) #self.setWrapMode(QsciScintilla.WrapCharacter) ## Edge Mode : does not seems to work #self.setEdgeMode(QsciScintilla.EdgeLine) #self.setEdgeColumn(80) #self.setEdgeColor(QColor("#FF0000")) self.SendScintilla(QsciScintilla.SCI_SETWRAPMODE, 2) self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)
def __init__(self, parent=None): QWidget.__init__(self, parent) self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console")) self.widgetButton = QWidget() self.toolBar = QToolBar() self.toolBar.setEnabled(True) #self.toolBar.setFont(font) self.toolBar.setFocusPolicy(Qt.NoFocus) self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu) self.toolBar.setLayoutDirection(Qt.LeftToRight) self.toolBar.setIconSize(QSize(24, 24)) self.toolBar.setOrientation(Qt.Vertical) self.toolBar.setMovable(True) self.toolBar.setFloatable(True) #self.toolBar.setAllowedAreas(Qt.LeftToolBarArea) #self.toolBar.setAllowedAreas(Qt.RightToolBarArea) #self.toolBar.setObjectName(_fromUtf8("toolMappa")) self.b = QVBoxLayout(self.widgetButton) self.e = QHBoxLayout(self) self.e.setMargin(0) self.e.setSpacing(0) self.b.setMargin(0) ## Action for Clear button clearBt = QCoreApplication.translate("PythonConsole", "Clear console") self.clearButton = QAction(parent) self.clearButton.setCheckable(False) self.clearButton.setEnabled(True) self.clearButton.setIcon(QIcon(":/images/console/iconClearConsole.png")) self.clearButton.setMenuRole(QAction.PreferencesRole) self.clearButton.setIconVisibleInMenu(True) self.clearButton.setToolTip(clearBt) self.clearButton.setText(clearBt) ## Action for paste snippets code # self.currentLayerButton = QAction(parent) # self.currentLayerButton.setCheckable(False) # self.currentLayerButton.setEnabled(True) # self.currentLayerButton.setIcon(QIcon("icon/iconTempConsole.png")) # self.currentLayerButton.setMenuRole(QAction.PreferencesRole) # self.currentLayerButton.setIconVisibleInMenu(True) ## Action menu for class actionClassBt = QCoreApplication.translate("PythonConsole", "Import Class") self.actionClass = QAction(parent) self.actionClass.setCheckable(False) self.actionClass.setEnabled(True) self.actionClass.setIcon(QIcon(":/images/console/iconClassConsole.png")) self.actionClass.setMenuRole(QAction.PreferencesRole) self.actionClass.setIconVisibleInMenu(True) self.actionClass.setToolTip(actionClassBt) self.actionClass.setText(actionClassBt) ## Action menu Open/Save script actionScriptBt = QCoreApplication.translate("PythonConsole", "Manage Script") self.actionScript = QAction(parent) self.actionScript.setCheckable(False) self.actionScript.setEnabled(True) self.actionScript.setIcon(QIcon(":/images/console/iconScriptConsole.png")) self.actionScript.setMenuRole(QAction.PreferencesRole) self.actionScript.setIconVisibleInMenu(True) self.actionScript.setToolTip(actionScriptBt) self.actionScript.setText(actionScriptBt) ## Import Sextante class loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import Sextante class") self.loadSextanteButton = QAction(parent) self.loadSextanteButton.setCheckable(False) self.loadSextanteButton.setEnabled(True) self.loadSextanteButton.setIcon(QIcon(":/images/console/iconSextanteConsole.png")) self.loadSextanteButton.setMenuRole(QAction.PreferencesRole) self.loadSextanteButton.setIconVisibleInMenu(True) self.loadSextanteButton.setToolTip(loadSextanteBt) self.loadSextanteButton.setText(loadSextanteBt) ## Import QgisInterface class loadIfaceBt = QCoreApplication.translate("PythonConsole", "Import QgisInterface class") self.loadIfaceButton = QAction(parent) self.loadIfaceButton.setCheckable(False) self.loadIfaceButton.setEnabled(True) self.loadIfaceButton.setIcon(QIcon(":/images/console/iconIfaceConsole.png")) self.loadIfaceButton.setMenuRole(QAction.PreferencesRole) self.loadIfaceButton.setIconVisibleInMenu(True) self.loadIfaceButton.setToolTip(loadIfaceBt) self.loadIfaceButton.setText(loadIfaceBt) ## Import QtCore class loadQtCoreBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtCore class") self.loadQtCoreButton = QAction(parent) self.loadQtCoreButton.setCheckable(False) self.loadQtCoreButton.setEnabled(True) self.loadQtCoreButton.setIcon(QIcon(":/images/console/iconQtCoreConsole.png")) self.loadQtCoreButton.setMenuRole(QAction.PreferencesRole) self.loadQtCoreButton.setIconVisibleInMenu(True) self.loadQtCoreButton.setToolTip(loadQtCoreBt) self.loadQtCoreButton.setText(loadQtCoreBt) ## Import QtGui class loadQtGuiBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtGui class") self.loadQtGuiButton = QAction(parent) self.loadQtGuiButton.setCheckable(False) self.loadQtGuiButton.setEnabled(True) self.loadQtGuiButton.setIcon(QIcon(":/images/console/iconQtGuiConsole.png")) self.loadQtGuiButton.setMenuRole(QAction.PreferencesRole) self.loadQtGuiButton.setIconVisibleInMenu(True) self.loadQtGuiButton.setToolTip(loadQtGuiBt) self.loadQtGuiButton.setText(loadQtGuiBt) ## Action for Open File openFileBt = QCoreApplication.translate("PythonConsole", "Open script file") self.openFileButton = QAction(parent) self.openFileButton.setCheckable(False) self.openFileButton.setEnabled(True) self.openFileButton.setIcon(QIcon(":/images/console/iconOpenConsole.png")) self.openFileButton.setMenuRole(QAction.PreferencesRole) self.openFileButton.setIconVisibleInMenu(True) self.openFileButton.setToolTip(openFileBt) self.openFileButton.setText(openFileBt) ## Action for Save File saveFileBt = QCoreApplication.translate("PythonConsole", "Save to script file") self.saveFileButton = QAction(parent) self.saveFileButton.setCheckable(False) self.saveFileButton.setEnabled(True) self.saveFileButton.setIcon(QIcon(":/images/console/iconSaveConsole.png")) self.saveFileButton.setMenuRole(QAction.PreferencesRole) self.saveFileButton.setIconVisibleInMenu(True) self.saveFileButton.setToolTip(saveFileBt) self.saveFileButton.setText(saveFileBt) ## Action for Run script runBt = QCoreApplication.translate("PythonConsole", "Run command") self.runButton = QAction(parent) self.runButton.setCheckable(False) self.runButton.setEnabled(True) self.runButton.setIcon(QIcon(":/images/console/iconRunConsole.png")) self.runButton.setMenuRole(QAction.PreferencesRole) self.runButton.setIconVisibleInMenu(True) self.runButton.setToolTip(runBt) self.runButton.setText(runBt) ## Help action helpBt = QCoreApplication.translate("PythonConsole", "Help") self.helpButton = QAction(parent) self.helpButton.setCheckable(False) self.helpButton.setEnabled(True) self.helpButton.setIcon(QIcon(":/images/console/iconHelpConsole.png")) self.helpButton.setMenuRole(QAction.PreferencesRole) self.helpButton.setIconVisibleInMenu(True) self.helpButton.setToolTip(helpBt) self.helpButton.setText(helpBt) self.toolBar.addAction(self.clearButton) self.toolBar.addAction(self.actionClass) self.toolBar.addAction(self.actionScript) self.toolBar.addAction(self.helpButton) self.toolBar.addAction(self.runButton) ## Menu Import Class self.classMenu = QMenu(self) self.classMenu.addAction(self.loadIfaceButton) self.classMenu.addAction(self.loadSextanteButton) self.classMenu.addAction(self.loadQtCoreButton) self.classMenu.addAction(self.loadQtGuiButton) cM = self.toolBar.widgetForAction(self.actionClass) cM.setMenu(self.classMenu) cM.setPopupMode(QToolButton.InstantPopup) ## Menu Manage Script self.scriptMenu = QMenu(self) self.scriptMenu.addAction(self.openFileButton) self.scriptMenu.addAction(self.saveFileButton) sM = self.toolBar.widgetForAction(self.actionScript) sM.setMenu(self.scriptMenu) sM.setPopupMode(QToolButton.InstantPopup) self.b.addWidget(self.toolBar) self.edit = PythonEdit() self.setFocusProxy( self.edit ) self.e.addWidget(self.widgetButton) self.e.addWidget(self.edit) self.clearButton.triggered.connect(self.edit.clearConsole) #self.currentLayerButton.triggered.connect(self.cLayer) self.loadIfaceButton.triggered.connect(self.iface) self.loadSextanteButton.triggered.connect(self.sextante) self.loadQtCoreButton.triggered.connect(self.qtCore) self.loadQtGuiButton.triggered.connect(self.qtGui) self.runButton.triggered.connect(self.edit.entered) self.openFileButton.triggered.connect(self.openScriptFile) self.saveFileButton.triggered.connect(self.saveScriptFile) self.helpButton.triggered.connect(self.openHelp)
class PythonConsoleWidget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console")) self.widgetButton = QWidget() self.toolBar = QToolBar() self.toolBar.setEnabled(True) #self.toolBar.setFont(font) self.toolBar.setFocusPolicy(Qt.NoFocus) self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu) self.toolBar.setLayoutDirection(Qt.LeftToRight) self.toolBar.setIconSize(QSize(24, 24)) self.toolBar.setOrientation(Qt.Vertical) self.toolBar.setMovable(True) self.toolBar.setFloatable(True) #self.toolBar.setAllowedAreas(Qt.LeftToolBarArea) #self.toolBar.setAllowedAreas(Qt.RightToolBarArea) #self.toolBar.setObjectName(_fromUtf8("toolMappa")) self.b = QVBoxLayout(self.widgetButton) self.e = QHBoxLayout(self) self.e.setMargin(0) self.e.setSpacing(0) self.b.setMargin(0) ## Action for Clear button clearBt = QCoreApplication.translate("PythonConsole", "Clear console") self.clearButton = QAction(parent) self.clearButton.setCheckable(False) self.clearButton.setEnabled(True) self.clearButton.setIcon(QIcon(":/images/console/iconClearConsole.png")) self.clearButton.setMenuRole(QAction.PreferencesRole) self.clearButton.setIconVisibleInMenu(True) self.clearButton.setToolTip(clearBt) self.clearButton.setText(clearBt) ## Action for paste snippets code # self.currentLayerButton = QAction(parent) # self.currentLayerButton.setCheckable(False) # self.currentLayerButton.setEnabled(True) # self.currentLayerButton.setIcon(QIcon("icon/iconTempConsole.png")) # self.currentLayerButton.setMenuRole(QAction.PreferencesRole) # self.currentLayerButton.setIconVisibleInMenu(True) ## Action menu for class actionClassBt = QCoreApplication.translate("PythonConsole", "Import Class") self.actionClass = QAction(parent) self.actionClass.setCheckable(False) self.actionClass.setEnabled(True) self.actionClass.setIcon(QIcon(":/images/console/iconClassConsole.png")) self.actionClass.setMenuRole(QAction.PreferencesRole) self.actionClass.setIconVisibleInMenu(True) self.actionClass.setToolTip(actionClassBt) self.actionClass.setText(actionClassBt) ## Action menu Open/Save script actionScriptBt = QCoreApplication.translate("PythonConsole", "Manage Script") self.actionScript = QAction(parent) self.actionScript.setCheckable(False) self.actionScript.setEnabled(True) self.actionScript.setIcon(QIcon(":/images/console/iconScriptConsole.png")) self.actionScript.setMenuRole(QAction.PreferencesRole) self.actionScript.setIconVisibleInMenu(True) self.actionScript.setToolTip(actionScriptBt) self.actionScript.setText(actionScriptBt) ## Import Sextante class loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import Sextante class") self.loadSextanteButton = QAction(parent) self.loadSextanteButton.setCheckable(False) self.loadSextanteButton.setEnabled(True) self.loadSextanteButton.setIcon(QIcon(":/images/console/iconSextanteConsole.png")) self.loadSextanteButton.setMenuRole(QAction.PreferencesRole) self.loadSextanteButton.setIconVisibleInMenu(True) self.loadSextanteButton.setToolTip(loadSextanteBt) self.loadSextanteButton.setText(loadSextanteBt) ## Import QgisInterface class loadIfaceBt = QCoreApplication.translate("PythonConsole", "Import QgisInterface class") self.loadIfaceButton = QAction(parent) self.loadIfaceButton.setCheckable(False) self.loadIfaceButton.setEnabled(True) self.loadIfaceButton.setIcon(QIcon(":/images/console/iconIfaceConsole.png")) self.loadIfaceButton.setMenuRole(QAction.PreferencesRole) self.loadIfaceButton.setIconVisibleInMenu(True) self.loadIfaceButton.setToolTip(loadIfaceBt) self.loadIfaceButton.setText(loadIfaceBt) ## Import QtCore class loadQtCoreBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtCore class") self.loadQtCoreButton = QAction(parent) self.loadQtCoreButton.setCheckable(False) self.loadQtCoreButton.setEnabled(True) self.loadQtCoreButton.setIcon(QIcon(":/images/console/iconQtCoreConsole.png")) self.loadQtCoreButton.setMenuRole(QAction.PreferencesRole) self.loadQtCoreButton.setIconVisibleInMenu(True) self.loadQtCoreButton.setToolTip(loadQtCoreBt) self.loadQtCoreButton.setText(loadQtCoreBt) ## Import QtGui class loadQtGuiBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtGui class") self.loadQtGuiButton = QAction(parent) self.loadQtGuiButton.setCheckable(False) self.loadQtGuiButton.setEnabled(True) self.loadQtGuiButton.setIcon(QIcon(":/images/console/iconQtGuiConsole.png")) self.loadQtGuiButton.setMenuRole(QAction.PreferencesRole) self.loadQtGuiButton.setIconVisibleInMenu(True) self.loadQtGuiButton.setToolTip(loadQtGuiBt) self.loadQtGuiButton.setText(loadQtGuiBt) ## Action for Open File openFileBt = QCoreApplication.translate("PythonConsole", "Open script file") self.openFileButton = QAction(parent) self.openFileButton.setCheckable(False) self.openFileButton.setEnabled(True) self.openFileButton.setIcon(QIcon(":/images/console/iconOpenConsole.png")) self.openFileButton.setMenuRole(QAction.PreferencesRole) self.openFileButton.setIconVisibleInMenu(True) self.openFileButton.setToolTip(openFileBt) self.openFileButton.setText(openFileBt) ## Action for Save File saveFileBt = QCoreApplication.translate("PythonConsole", "Save to script file") self.saveFileButton = QAction(parent) self.saveFileButton.setCheckable(False) self.saveFileButton.setEnabled(True) self.saveFileButton.setIcon(QIcon(":/images/console/iconSaveConsole.png")) self.saveFileButton.setMenuRole(QAction.PreferencesRole) self.saveFileButton.setIconVisibleInMenu(True) self.saveFileButton.setToolTip(saveFileBt) self.saveFileButton.setText(saveFileBt) ## Action for Run script runBt = QCoreApplication.translate("PythonConsole", "Run command") self.runButton = QAction(parent) self.runButton.setCheckable(False) self.runButton.setEnabled(True) self.runButton.setIcon(QIcon(":/images/console/iconRunConsole.png")) self.runButton.setMenuRole(QAction.PreferencesRole) self.runButton.setIconVisibleInMenu(True) self.runButton.setToolTip(runBt) self.runButton.setText(runBt) ## Help action helpBt = QCoreApplication.translate("PythonConsole", "Help") self.helpButton = QAction(parent) self.helpButton.setCheckable(False) self.helpButton.setEnabled(True) self.helpButton.setIcon(QIcon(":/images/console/iconHelpConsole.png")) self.helpButton.setMenuRole(QAction.PreferencesRole) self.helpButton.setIconVisibleInMenu(True) self.helpButton.setToolTip(helpBt) self.helpButton.setText(helpBt) self.toolBar.addAction(self.clearButton) self.toolBar.addAction(self.actionClass) self.toolBar.addAction(self.actionScript) self.toolBar.addAction(self.helpButton) self.toolBar.addAction(self.runButton) ## Menu Import Class self.classMenu = QMenu(self) self.classMenu.addAction(self.loadIfaceButton) self.classMenu.addAction(self.loadSextanteButton) self.classMenu.addAction(self.loadQtCoreButton) self.classMenu.addAction(self.loadQtGuiButton) cM = self.toolBar.widgetForAction(self.actionClass) cM.setMenu(self.classMenu) cM.setPopupMode(QToolButton.InstantPopup) ## Menu Manage Script self.scriptMenu = QMenu(self) self.scriptMenu.addAction(self.openFileButton) self.scriptMenu.addAction(self.saveFileButton) sM = self.toolBar.widgetForAction(self.actionScript) sM.setMenu(self.scriptMenu) sM.setPopupMode(QToolButton.InstantPopup) self.b.addWidget(self.toolBar) self.edit = PythonEdit() self.setFocusProxy( self.edit ) self.e.addWidget(self.widgetButton) self.e.addWidget(self.edit) self.clearButton.triggered.connect(self.edit.clearConsole) #self.currentLayerButton.triggered.connect(self.cLayer) self.loadIfaceButton.triggered.connect(self.iface) self.loadSextanteButton.triggered.connect(self.sextante) self.loadQtCoreButton.triggered.connect(self.qtCore) self.loadQtGuiButton.triggered.connect(self.qtGui) self.runButton.triggered.connect(self.edit.entered) self.openFileButton.triggered.connect(self.openScriptFile) self.saveFileButton.triggered.connect(self.saveScriptFile) self.helpButton.triggered.connect(self.openHelp) def cLayer(self): self.edit.commandConsole('cLayer') def sextante(self): self.edit.commandConsole('sextante') def iface(self): self.edit.commandConsole('iface') def qtCore(self): self.edit.commandConsole('qtCore') def qtGui(self): self.edit.commandConsole('qtGui') def openScriptFile(self): settings = QSettings() lastDirPath = settings.value("/pythonConsole/lastDirPath").toString() scriptFile = QFileDialog.getOpenFileName( self, "Open File", lastDirPath, "Script file (*.py)") if scriptFile.isEmpty() == False: oF = open(scriptFile, 'r') listScriptFile = [] for line in oF: if line != "\n": listScriptFile.append(line) self.edit.insertTextFromFile(listScriptFile) lastDirPath = QFileInfo(scriptFile).path() settings.setValue("/pythonConsole/lastDirPath", QVariant(scriptFile)) def saveScriptFile(self): scriptFile = QFileDialog() scriptFile.setDefaultSuffix(".py") fName = scriptFile.getSaveFileName( self, "Save file", QString(), "Script file (*.py)") if fName.isEmpty() == False: filename = str(fName) if not filename.endswith(".py"): fName += ".py" sF = open(fName,'w') listText = self.edit.getTextFromEditor() is_first_line = True for s in listText: if s[0:3] in (">>>", "..."): s.replace(">>> ", "").replace("... ", "") if is_first_line: is_first_line = False else: sF.write('\n') sF.write(s) sF.close() def openHelp(self): dlg = HelpDialog() dlg.exec_() def closeEvent(self, event): self.edit.writeHistoryFile() QWidget.closeEvent(self, event)
def __init__(self, parent=None): QWidget.__init__(self, parent) self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console")) self.widgetButton = QWidget() #self.widgetEditors = QWidget() self.options = optionsDialog(self) self.helpDlg = HelpDialog(self) self.splitter = QSplitter(self) self.splitter.setOrientation(Qt.Vertical) self.splitter.setHandleWidth(3) self.splitter.setChildrenCollapsible(False) self.toolBar = QToolBar() self.toolBar.setEnabled(True) self.toolBar.setFocusPolicy(Qt.NoFocus) self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu) self.toolBar.setLayoutDirection(Qt.LeftToRight) self.toolBar.setIconSize(QSize(24, 24)) self.toolBar.setOrientation(Qt.Vertical) self.toolBar.setMovable(True) self.toolBar.setFloatable(True) self.b = QGridLayout(self.widgetButton) self.f = QGridLayout(self) self.f.setMargin(0) self.f.setSpacing(0) self.b.setMargin(0) self.b.setSpacing(0) ## Action for Clear button clearBt = QCoreApplication.translate("PythonConsole", "Clear console") self.clearButton = QAction(parent) self.clearButton.setCheckable(False) self.clearButton.setEnabled(True) self.clearButton.setIcon(QgsApplication.getThemeIcon("console/iconClearConsole.png")) self.clearButton.setMenuRole(QAction.PreferencesRole) self.clearButton.setIconVisibleInMenu(True) self.clearButton.setToolTip(clearBt) self.clearButton.setText(clearBt) ## Action for settings optionsBt = QCoreApplication.translate("PythonConsole", "Settings") self.optionsButton = QAction(parent) self.optionsButton.setCheckable(False) self.optionsButton.setEnabled(True) self.optionsButton.setIcon(QgsApplication.getThemeIcon("console/iconSettingsConsole.png")) self.optionsButton.setMenuRole(QAction.PreferencesRole) self.optionsButton.setIconVisibleInMenu(True) self.optionsButton.setToolTip(optionsBt) self.optionsButton.setText(optionsBt) ## Action menu for class actionClassBt = QCoreApplication.translate("PythonConsole", "Import Class") self.actionClass = QAction(parent) self.actionClass.setCheckable(False) self.actionClass.setEnabled(True) self.actionClass.setIcon(QgsApplication.getThemeIcon("console/iconClassConsole.png")) self.actionClass.setMenuRole(QAction.PreferencesRole) self.actionClass.setIconVisibleInMenu(True) self.actionClass.setToolTip(actionClassBt) self.actionClass.setText(actionClassBt) ## Action menu Open/Save script actionScriptBt = QCoreApplication.translate("PythonConsole", "Manage Script") self.actionScript = QAction(parent) self.actionScript.setCheckable(False) self.actionScript.setEnabled(True) self.actionScript.setIcon(QgsApplication.getThemeIcon("console/iconScriptConsole.png")) self.actionScript.setMenuRole(QAction.PreferencesRole) self.actionScript.setIconVisibleInMenu(True) self.actionScript.setToolTip(actionScriptBt) self.actionScript.setText(actionScriptBt) ## Import Sextante class loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import Sextante class") self.loadSextanteButton = QAction(parent) self.loadSextanteButton.setCheckable(False) self.loadSextanteButton.setEnabled(True) self.loadSextanteButton.setIcon(QgsApplication.getThemeIcon("console/iconSextanteConsole.png")) self.loadSextanteButton.setMenuRole(QAction.PreferencesRole) self.loadSextanteButton.setIconVisibleInMenu(True) self.loadSextanteButton.setToolTip(loadSextanteBt) self.loadSextanteButton.setText(loadSextanteBt) ## Import QtCore class loadQtCoreBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtCore class") self.loadQtCoreButton = QAction(parent) self.loadQtCoreButton.setCheckable(False) self.loadQtCoreButton.setEnabled(True) self.loadQtCoreButton.setIcon(QgsApplication.getThemeIcon("console/iconQtCoreConsole.png")) self.loadQtCoreButton.setMenuRole(QAction.PreferencesRole) self.loadQtCoreButton.setIconVisibleInMenu(True) self.loadQtCoreButton.setToolTip(loadQtCoreBt) self.loadQtCoreButton.setText(loadQtCoreBt) ## Import QtGui class loadQtGuiBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtGui class") self.loadQtGuiButton = QAction(parent) self.loadQtGuiButton.setCheckable(False) self.loadQtGuiButton.setEnabled(True) self.loadQtGuiButton.setIcon(QgsApplication.getThemeIcon("console/iconQtGuiConsole.png")) self.loadQtGuiButton.setMenuRole(QAction.PreferencesRole) self.loadQtGuiButton.setIconVisibleInMenu(True) self.loadQtGuiButton.setToolTip(loadQtGuiBt) self.loadQtGuiButton.setText(loadQtGuiBt) ## Action for Open File openFileBt = QCoreApplication.translate("PythonConsole", "Open script file") self.openFileButton = QAction(parent) self.openFileButton.setCheckable(False) self.openFileButton.setEnabled(True) self.openFileButton.setIcon(QgsApplication.getThemeIcon("console/iconOpenConsole.png")) self.openFileButton.setMenuRole(QAction.PreferencesRole) self.openFileButton.setIconVisibleInMenu(True) self.openFileButton.setToolTip(openFileBt) self.openFileButton.setText(openFileBt) ## Action for Save File saveFileBt = QCoreApplication.translate("PythonConsole", "Save to script file") self.saveFileButton = QAction(parent) self.saveFileButton.setCheckable(False) self.saveFileButton.setEnabled(True) self.saveFileButton.setIcon(QgsApplication.getThemeIcon("console/iconSaveConsole.png")) self.saveFileButton.setMenuRole(QAction.PreferencesRole) self.saveFileButton.setIconVisibleInMenu(True) self.saveFileButton.setToolTip(saveFileBt) self.saveFileButton.setText(saveFileBt) ## Action for Run script runBt = QCoreApplication.translate("PythonConsole", "Run command") self.runButton = QAction(parent) self.runButton.setCheckable(False) self.runButton.setEnabled(True) self.runButton.setIcon(QgsApplication.getThemeIcon("console/iconRunConsole.png")) self.runButton.setMenuRole(QAction.PreferencesRole) self.runButton.setIconVisibleInMenu(True) self.runButton.setToolTip(runBt) self.runButton.setText(runBt) ## Help action helpBt = QCoreApplication.translate("PythonConsole", "Help") self.helpButton = QAction(parent) self.helpButton.setCheckable(False) self.helpButton.setEnabled(True) self.helpButton.setIcon(QgsApplication.getThemeIcon("console/iconHelpConsole.png")) self.helpButton.setMenuRole(QAction.PreferencesRole) self.helpButton.setIconVisibleInMenu(True) self.helpButton.setToolTip(helpBt) self.helpButton.setText(helpBt) self.toolBar.addAction(self.clearButton) self.toolBar.addAction(self.actionClass) self.toolBar.addAction(self.actionScript) self.toolBar.addAction(self.optionsButton) self.toolBar.addAction(self.helpButton) self.toolBar.addAction(self.runButton) ## Menu Import Class self.classMenu = QMenu(self) self.classMenu.addAction(self.loadSextanteButton) self.classMenu.addAction(self.loadQtCoreButton) self.classMenu.addAction(self.loadQtGuiButton) cM = self.toolBar.widgetForAction(self.actionClass) cM.setMenu(self.classMenu) cM.setPopupMode(QToolButton.InstantPopup) ## Menu Manage Script self.scriptMenu = QMenu(self) self.scriptMenu.addAction(self.openFileButton) self.scriptMenu.addAction(self.saveFileButton) sM = self.toolBar.widgetForAction(self.actionScript) sM.setMenu(self.scriptMenu) sM.setPopupMode(QToolButton.InstantPopup) self.b.addWidget(self.toolBar) self.edit = PythonEdit(self) self.textEditOut = EditorOutput(self) self.setFocusProxy(self.edit) sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widgetButton.sizePolicy().hasHeightForWidth()) self.widgetButton.setSizePolicy(sizePolicy) self.splitter.addWidget(self.textEditOut) self.splitter.addWidget(self.edit) sizes = self.splitter.sizes() self.splitter.setSizes(sizes) self.f.addWidget(self.widgetButton, 0, 0) self.f.addWidget(self.splitter, 0, 1) sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.textEditOut.sizePolicy().hasHeightForWidth()) self.textEditOut.setSizePolicy(sizePolicy) self.textEditOut.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.edit.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.clearButton.triggered.connect(self.textEditOut.clearConsole) self.optionsButton.triggered.connect(self.openSettings) self.loadSextanteButton.triggered.connect(self.sextante) self.loadQtCoreButton.triggered.connect(self.qtCore) self.loadQtGuiButton.triggered.connect(self.qtGui) self.runButton.triggered.connect(self.edit.entered) self.openFileButton.triggered.connect(self.openScriptFile) self.saveFileButton.triggered.connect(self.saveScriptFile) self.helpButton.triggered.connect(self.openHelp) QObject.connect(self.options.buttonBox, SIGNAL("accepted()"), self.prefChanged)
class PythonConsoleWidget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console")) self.widgetButton = QWidget() #self.widgetEditors = QWidget() self.options = optionsDialog(self) self.helpDlg = HelpDialog(self) self.splitter = QSplitter(self) self.splitter.setOrientation(Qt.Vertical) self.splitter.setHandleWidth(3) self.splitter.setChildrenCollapsible(False) self.toolBar = QToolBar() self.toolBar.setEnabled(True) self.toolBar.setFocusPolicy(Qt.NoFocus) self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu) self.toolBar.setLayoutDirection(Qt.LeftToRight) self.toolBar.setIconSize(QSize(24, 24)) self.toolBar.setOrientation(Qt.Vertical) self.toolBar.setMovable(True) self.toolBar.setFloatable(True) self.b = QGridLayout(self.widgetButton) self.f = QGridLayout(self) self.f.setMargin(0) self.f.setSpacing(0) self.b.setMargin(0) self.b.setSpacing(0) ## Action for Clear button clearBt = QCoreApplication.translate("PythonConsole", "Clear console") self.clearButton = QAction(parent) self.clearButton.setCheckable(False) self.clearButton.setEnabled(True) self.clearButton.setIcon(QgsApplication.getThemeIcon("console/iconClearConsole.png")) self.clearButton.setMenuRole(QAction.PreferencesRole) self.clearButton.setIconVisibleInMenu(True) self.clearButton.setToolTip(clearBt) self.clearButton.setText(clearBt) ## Action for settings optionsBt = QCoreApplication.translate("PythonConsole", "Settings") self.optionsButton = QAction(parent) self.optionsButton.setCheckable(False) self.optionsButton.setEnabled(True) self.optionsButton.setIcon(QgsApplication.getThemeIcon("console/iconSettingsConsole.png")) self.optionsButton.setMenuRole(QAction.PreferencesRole) self.optionsButton.setIconVisibleInMenu(True) self.optionsButton.setToolTip(optionsBt) self.optionsButton.setText(optionsBt) ## Action menu for class actionClassBt = QCoreApplication.translate("PythonConsole", "Import Class") self.actionClass = QAction(parent) self.actionClass.setCheckable(False) self.actionClass.setEnabled(True) self.actionClass.setIcon(QgsApplication.getThemeIcon("console/iconClassConsole.png")) self.actionClass.setMenuRole(QAction.PreferencesRole) self.actionClass.setIconVisibleInMenu(True) self.actionClass.setToolTip(actionClassBt) self.actionClass.setText(actionClassBt) ## Action menu Open/Save script actionScriptBt = QCoreApplication.translate("PythonConsole", "Manage Script") self.actionScript = QAction(parent) self.actionScript.setCheckable(False) self.actionScript.setEnabled(True) self.actionScript.setIcon(QgsApplication.getThemeIcon("console/iconScriptConsole.png")) self.actionScript.setMenuRole(QAction.PreferencesRole) self.actionScript.setIconVisibleInMenu(True) self.actionScript.setToolTip(actionScriptBt) self.actionScript.setText(actionScriptBt) ## Import Sextante class loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import Sextante class") self.loadSextanteButton = QAction(parent) self.loadSextanteButton.setCheckable(False) self.loadSextanteButton.setEnabled(True) self.loadSextanteButton.setIcon(QgsApplication.getThemeIcon("console/iconSextanteConsole.png")) self.loadSextanteButton.setMenuRole(QAction.PreferencesRole) self.loadSextanteButton.setIconVisibleInMenu(True) self.loadSextanteButton.setToolTip(loadSextanteBt) self.loadSextanteButton.setText(loadSextanteBt) ## Import QtCore class loadQtCoreBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtCore class") self.loadQtCoreButton = QAction(parent) self.loadQtCoreButton.setCheckable(False) self.loadQtCoreButton.setEnabled(True) self.loadQtCoreButton.setIcon(QgsApplication.getThemeIcon("console/iconQtCoreConsole.png")) self.loadQtCoreButton.setMenuRole(QAction.PreferencesRole) self.loadQtCoreButton.setIconVisibleInMenu(True) self.loadQtCoreButton.setToolTip(loadQtCoreBt) self.loadQtCoreButton.setText(loadQtCoreBt) ## Import QtGui class loadQtGuiBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtGui class") self.loadQtGuiButton = QAction(parent) self.loadQtGuiButton.setCheckable(False) self.loadQtGuiButton.setEnabled(True) self.loadQtGuiButton.setIcon(QgsApplication.getThemeIcon("console/iconQtGuiConsole.png")) self.loadQtGuiButton.setMenuRole(QAction.PreferencesRole) self.loadQtGuiButton.setIconVisibleInMenu(True) self.loadQtGuiButton.setToolTip(loadQtGuiBt) self.loadQtGuiButton.setText(loadQtGuiBt) ## Action for Open File openFileBt = QCoreApplication.translate("PythonConsole", "Open script file") self.openFileButton = QAction(parent) self.openFileButton.setCheckable(False) self.openFileButton.setEnabled(True) self.openFileButton.setIcon(QgsApplication.getThemeIcon("console/iconOpenConsole.png")) self.openFileButton.setMenuRole(QAction.PreferencesRole) self.openFileButton.setIconVisibleInMenu(True) self.openFileButton.setToolTip(openFileBt) self.openFileButton.setText(openFileBt) ## Action for Save File saveFileBt = QCoreApplication.translate("PythonConsole", "Save to script file") self.saveFileButton = QAction(parent) self.saveFileButton.setCheckable(False) self.saveFileButton.setEnabled(True) self.saveFileButton.setIcon(QgsApplication.getThemeIcon("console/iconSaveConsole.png")) self.saveFileButton.setMenuRole(QAction.PreferencesRole) self.saveFileButton.setIconVisibleInMenu(True) self.saveFileButton.setToolTip(saveFileBt) self.saveFileButton.setText(saveFileBt) ## Action for Run script runBt = QCoreApplication.translate("PythonConsole", "Run command") self.runButton = QAction(parent) self.runButton.setCheckable(False) self.runButton.setEnabled(True) self.runButton.setIcon(QgsApplication.getThemeIcon("console/iconRunConsole.png")) self.runButton.setMenuRole(QAction.PreferencesRole) self.runButton.setIconVisibleInMenu(True) self.runButton.setToolTip(runBt) self.runButton.setText(runBt) ## Help action helpBt = QCoreApplication.translate("PythonConsole", "Help") self.helpButton = QAction(parent) self.helpButton.setCheckable(False) self.helpButton.setEnabled(True) self.helpButton.setIcon(QgsApplication.getThemeIcon("console/iconHelpConsole.png")) self.helpButton.setMenuRole(QAction.PreferencesRole) self.helpButton.setIconVisibleInMenu(True) self.helpButton.setToolTip(helpBt) self.helpButton.setText(helpBt) self.toolBar.addAction(self.clearButton) self.toolBar.addAction(self.actionClass) self.toolBar.addAction(self.actionScript) self.toolBar.addAction(self.optionsButton) self.toolBar.addAction(self.helpButton) self.toolBar.addAction(self.runButton) ## Menu Import Class self.classMenu = QMenu(self) self.classMenu.addAction(self.loadSextanteButton) self.classMenu.addAction(self.loadQtCoreButton) self.classMenu.addAction(self.loadQtGuiButton) cM = self.toolBar.widgetForAction(self.actionClass) cM.setMenu(self.classMenu) cM.setPopupMode(QToolButton.InstantPopup) ## Menu Manage Script self.scriptMenu = QMenu(self) self.scriptMenu.addAction(self.openFileButton) self.scriptMenu.addAction(self.saveFileButton) sM = self.toolBar.widgetForAction(self.actionScript) sM.setMenu(self.scriptMenu) sM.setPopupMode(QToolButton.InstantPopup) self.b.addWidget(self.toolBar) self.edit = PythonEdit(self) self.textEditOut = EditorOutput(self) self.setFocusProxy(self.edit) sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widgetButton.sizePolicy().hasHeightForWidth()) self.widgetButton.setSizePolicy(sizePolicy) self.splitter.addWidget(self.textEditOut) self.splitter.addWidget(self.edit) sizes = self.splitter.sizes() self.splitter.setSizes(sizes) self.f.addWidget(self.widgetButton, 0, 0) self.f.addWidget(self.splitter, 0, 1) sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.textEditOut.sizePolicy().hasHeightForWidth()) self.textEditOut.setSizePolicy(sizePolicy) self.textEditOut.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.edit.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.clearButton.triggered.connect(self.textEditOut.clearConsole) self.optionsButton.triggered.connect(self.openSettings) self.loadSextanteButton.triggered.connect(self.sextante) self.loadQtCoreButton.triggered.connect(self.qtCore) self.loadQtGuiButton.triggered.connect(self.qtGui) self.runButton.triggered.connect(self.edit.entered) self.openFileButton.triggered.connect(self.openScriptFile) self.saveFileButton.triggered.connect(self.saveScriptFile) self.helpButton.triggered.connect(self.openHelp) QObject.connect(self.options.buttonBox, SIGNAL("accepted()"), self.prefChanged) def sextante(self): self.edit.commandConsole('sextante') def qtCore(self): self.edit.commandConsole('qtCore') def qtGui(self): self.edit.commandConsole('qtGui') def openScriptFile(self): settings = QSettings() lastDirPath = settings.value("pythonConsole/lastDirPath").toString() scriptFile = QFileDialog.getOpenFileName( self, "Open File", lastDirPath, "Script file (*.py)") if scriptFile.isEmpty() == False: oF = open(scriptFile, 'r') listScriptFile = [] for line in oF: if line != "\n": listScriptFile.append(line) self.edit.insertTextFromFile(listScriptFile) lastDirPath = QFileInfo(scriptFile).path() settings.setValue("pythonConsole/lastDirPath", QVariant(scriptFile)) def saveScriptFile(self): scriptFile = QFileDialog() scriptFile.setDefaultSuffix(".py") fName = scriptFile.getSaveFileName( self, "Save file", QString(), "Script file (*.py)") if fName.isEmpty() == False: filename = str(fName) if not filename.endswith(".py"): fName += ".py" sF = open(fName,'w') listText = self.textEditOut.getTextFromEditor() is_first_line = True for s in listText: if s[0:3] in (">>>", "..."): s.replace(">>> ", "").replace("... ", "") if is_first_line: is_first_line = False else: sF.write('\n') sF.write(s) sF.close() self.callWidgetMessageBar('Script was correctly saved.') def openHelp(self): self.helpDlg.show() self.helpDlg.activateWindow() def openSettings(self): self.options.exec_() def prefChanged(self): self.edit.refreshLexerProperties() self.textEditOut.refreshLexerProperties() def callWidgetMessageBar(self, text): self.textEditOut.widgetMessageBar(iface, text)
def __init__(self, parent=None): QWidget.__init__(self, parent) self.setWindowTitle( QCoreApplication.translate("PythonConsole", "Python Console")) self.widgetButton = QWidget() #self.widgetEditors = QWidget() self.options = optionsDialog(self) self.helpDlg = HelpDialog(self) self.splitter = QSplitter(self) self.splitter.setOrientation(Qt.Vertical) self.splitter.setHandleWidth(3) self.splitter.setChildrenCollapsible(False) self.toolBar = QToolBar() self.toolBar.setEnabled(True) self.toolBar.setFocusPolicy(Qt.NoFocus) self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu) self.toolBar.setLayoutDirection(Qt.LeftToRight) self.toolBar.setIconSize(QSize(24, 24)) self.toolBar.setOrientation(Qt.Vertical) self.toolBar.setMovable(True) self.toolBar.setFloatable(True) self.b = QGridLayout(self.widgetButton) self.f = QGridLayout(self) self.f.setMargin(0) self.f.setSpacing(0) self.b.setMargin(0) self.b.setSpacing(0) ## Action for Clear button clearBt = QCoreApplication.translate("PythonConsole", "Clear console") self.clearButton = QAction(parent) self.clearButton.setCheckable(False) self.clearButton.setEnabled(True) self.clearButton.setIcon( QgsApplication.getThemeIcon("console/iconClearConsole.png")) self.clearButton.setMenuRole(QAction.PreferencesRole) self.clearButton.setIconVisibleInMenu(True) self.clearButton.setToolTip(clearBt) self.clearButton.setText(clearBt) ## Action for settings optionsBt = QCoreApplication.translate("PythonConsole", "Settings") self.optionsButton = QAction(parent) self.optionsButton.setCheckable(False) self.optionsButton.setEnabled(True) self.optionsButton.setIcon( QgsApplication.getThemeIcon("console/iconSettingsConsole.png")) self.optionsButton.setMenuRole(QAction.PreferencesRole) self.optionsButton.setIconVisibleInMenu(True) self.optionsButton.setToolTip(optionsBt) self.optionsButton.setText(optionsBt) ## Action menu for class actionClassBt = QCoreApplication.translate("PythonConsole", "Import Class") self.actionClass = QAction(parent) self.actionClass.setCheckable(False) self.actionClass.setEnabled(True) self.actionClass.setIcon( QgsApplication.getThemeIcon("console/iconClassConsole.png")) self.actionClass.setMenuRole(QAction.PreferencesRole) self.actionClass.setIconVisibleInMenu(True) self.actionClass.setToolTip(actionClassBt) self.actionClass.setText(actionClassBt) ## Action menu Open/Save script actionScriptBt = QCoreApplication.translate("PythonConsole", "Manage Script") self.actionScript = QAction(parent) self.actionScript.setCheckable(False) self.actionScript.setEnabled(True) self.actionScript.setIcon( QgsApplication.getThemeIcon("console/iconScriptConsole.png")) self.actionScript.setMenuRole(QAction.PreferencesRole) self.actionScript.setIconVisibleInMenu(True) self.actionScript.setToolTip(actionScriptBt) self.actionScript.setText(actionScriptBt) ## Import Sextante class loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import Sextante class") self.loadSextanteButton = QAction(parent) self.loadSextanteButton.setCheckable(False) self.loadSextanteButton.setEnabled(True) self.loadSextanteButton.setIcon( QgsApplication.getThemeIcon("console/iconSextanteConsole.png")) self.loadSextanteButton.setMenuRole(QAction.PreferencesRole) self.loadSextanteButton.setIconVisibleInMenu(True) self.loadSextanteButton.setToolTip(loadSextanteBt) self.loadSextanteButton.setText(loadSextanteBt) ## Import QtCore class loadQtCoreBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtCore class") self.loadQtCoreButton = QAction(parent) self.loadQtCoreButton.setCheckable(False) self.loadQtCoreButton.setEnabled(True) self.loadQtCoreButton.setIcon( QgsApplication.getThemeIcon("console/iconQtCoreConsole.png")) self.loadQtCoreButton.setMenuRole(QAction.PreferencesRole) self.loadQtCoreButton.setIconVisibleInMenu(True) self.loadQtCoreButton.setToolTip(loadQtCoreBt) self.loadQtCoreButton.setText(loadQtCoreBt) ## Import QtGui class loadQtGuiBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtGui class") self.loadQtGuiButton = QAction(parent) self.loadQtGuiButton.setCheckable(False) self.loadQtGuiButton.setEnabled(True) self.loadQtGuiButton.setIcon( QgsApplication.getThemeIcon("console/iconQtGuiConsole.png")) self.loadQtGuiButton.setMenuRole(QAction.PreferencesRole) self.loadQtGuiButton.setIconVisibleInMenu(True) self.loadQtGuiButton.setToolTip(loadQtGuiBt) self.loadQtGuiButton.setText(loadQtGuiBt) ## Action for Open File openFileBt = QCoreApplication.translate("PythonConsole", "Open script file") self.openFileButton = QAction(parent) self.openFileButton.setCheckable(False) self.openFileButton.setEnabled(True) self.openFileButton.setIcon( QgsApplication.getThemeIcon("console/iconOpenConsole.png")) self.openFileButton.setMenuRole(QAction.PreferencesRole) self.openFileButton.setIconVisibleInMenu(True) self.openFileButton.setToolTip(openFileBt) self.openFileButton.setText(openFileBt) ## Action for Save File saveFileBt = QCoreApplication.translate("PythonConsole", "Save to script file") self.saveFileButton = QAction(parent) self.saveFileButton.setCheckable(False) self.saveFileButton.setEnabled(True) self.saveFileButton.setIcon( QgsApplication.getThemeIcon("console/iconSaveConsole.png")) self.saveFileButton.setMenuRole(QAction.PreferencesRole) self.saveFileButton.setIconVisibleInMenu(True) self.saveFileButton.setToolTip(saveFileBt) self.saveFileButton.setText(saveFileBt) ## Action for Run script runBt = QCoreApplication.translate("PythonConsole", "Run command") self.runButton = QAction(parent) self.runButton.setCheckable(False) self.runButton.setEnabled(True) self.runButton.setIcon( QgsApplication.getThemeIcon("console/iconRunConsole.png")) self.runButton.setMenuRole(QAction.PreferencesRole) self.runButton.setIconVisibleInMenu(True) self.runButton.setToolTip(runBt) self.runButton.setText(runBt) ## Help action helpBt = QCoreApplication.translate("PythonConsole", "Help") self.helpButton = QAction(parent) self.helpButton.setCheckable(False) self.helpButton.setEnabled(True) self.helpButton.setIcon( QgsApplication.getThemeIcon("console/iconHelpConsole.png")) self.helpButton.setMenuRole(QAction.PreferencesRole) self.helpButton.setIconVisibleInMenu(True) self.helpButton.setToolTip(helpBt) self.helpButton.setText(helpBt) self.toolBar.addAction(self.clearButton) self.toolBar.addAction(self.actionClass) self.toolBar.addAction(self.actionScript) self.toolBar.addAction(self.optionsButton) self.toolBar.addAction(self.helpButton) self.toolBar.addAction(self.runButton) ## Menu Import Class self.classMenu = QMenu(self) self.classMenu.addAction(self.loadSextanteButton) self.classMenu.addAction(self.loadQtCoreButton) self.classMenu.addAction(self.loadQtGuiButton) cM = self.toolBar.widgetForAction(self.actionClass) cM.setMenu(self.classMenu) cM.setPopupMode(QToolButton.InstantPopup) ## Menu Manage Script self.scriptMenu = QMenu(self) self.scriptMenu.addAction(self.openFileButton) self.scriptMenu.addAction(self.saveFileButton) sM = self.toolBar.widgetForAction(self.actionScript) sM.setMenu(self.scriptMenu) sM.setPopupMode(QToolButton.InstantPopup) self.b.addWidget(self.toolBar) self.edit = PythonEdit(self) self.textEditOut = EditorOutput(self) self.setFocusProxy(self.edit) sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.widgetButton.sizePolicy().hasHeightForWidth()) self.widgetButton.setSizePolicy(sizePolicy) self.splitter.addWidget(self.textEditOut) self.splitter.addWidget(self.edit) sizes = self.splitter.sizes() self.splitter.setSizes(sizes) self.f.addWidget(self.widgetButton, 0, 0) self.f.addWidget(self.splitter, 0, 1) sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.textEditOut.sizePolicy().hasHeightForWidth()) self.textEditOut.setSizePolicy(sizePolicy) self.textEditOut.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.edit.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.clearButton.triggered.connect(self.textEditOut.clearConsole) self.optionsButton.triggered.connect(self.openSettings) self.loadSextanteButton.triggered.connect(self.sextante) self.loadQtCoreButton.triggered.connect(self.qtCore) self.loadQtGuiButton.triggered.connect(self.qtGui) self.runButton.triggered.connect(self.edit.entered) self.openFileButton.triggered.connect(self.openScriptFile) self.saveFileButton.triggered.connect(self.saveScriptFile) self.helpButton.triggered.connect(self.openHelp) QObject.connect(self.options.buttonBox, SIGNAL("accepted()"), self.prefChanged)
class EditorOutput(QsciScintilla): def __init__(self, parent=None): #QsciScintilla.__init__(self, parent) super(EditorOutput,self).__init__(parent) # Enable non-ascii chars for editor self.setUtf8(True) sys.stdout = writeOut(self, sys.stdout) sys.stderr = writeOut(self, sys.stderr, "traceback") self.edit = PythonEdit() self.setLexers() self.setReadOnly(True) # Set the default font font = QFont() font.setFamily('Courier') font.setFixedPitch(True) font.setPointSize(10) self.setFont(font) self.setMarginsFont(font) # Margin 0 is used for line numbers #fm = QFontMetrics(font) self.setMarginsFont(font) self.setMarginWidth(1, "00000") self.setMarginLineNumbers(1, True) self.setMarginsForegroundColor(QColor("#3E3EE3")) self.setMarginsBackgroundColor(QColor("#f9f9f9")) self.setCaretLineVisible(True) self.setCaretLineBackgroundColor(QColor("#fcf3ed")) # Folding #self.setFolding(QsciScintilla.BoxedTreeFoldStyle) #self.setFoldMarginColors(QColor("#99CC66"),QColor("#333300")) #self.setWrapMode(QsciScintilla.WrapCharacter) ## Edge Mode : does not seems to work #self.setEdgeMode(QsciScintilla.EdgeLine) #self.setEdgeColumn(80) #self.setEdgeColor(QColor("#FF0000")) self.SendScintilla(QsciScintilla.SCI_SETWRAPMODE, 2) self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0) def refreshLexerProperties(self): self.setLexers() def setLexers(self): self.lexer = QsciLexerPython() settings = QSettings() loadFont = settings.value("pythonConsole/fontfamilytext").toString() fontSize = settings.value("pythonConsole/fontsize").toInt()[0] font = QFont(loadFont) font.setFixedPitch(True) font.setPointSize(fontSize) self.lexer.setDefaultFont(font) self.lexer.setColor(Qt.red, 1) self.lexer.setColor(Qt.darkGreen, 5) self.lexer.setColor(Qt.darkBlue, 15) self.lexer.setFont(font, 1) self.lexer.setFont(font, 2) self.lexer.setFont(font, 3) self.lexer.setFont(font, 4) self.setLexer(self.lexer) def getTextFromEditor(self): text = self.text() textList = text.split("\n") return textList def clearConsole(self): #self.SendScintilla(QsciScintilla.SCI_CLEARALL) self.setText('') def contextMenuEvent(self, e): menu = QMenu(self) runAction = menu.addAction("Enter Selected") copyAction = menu.addAction("Copy CTRL+C") runAction.setEnabled(False) if self.hasSelectedText(): runAction.setEnabled(True) action = menu.exec_(self.mapToGlobal(e.pos())) if action == runAction: cmd = self.selectedText() self.edit.insertFromDropPaste(cmd) self.edit.entered() if action == copyAction: self.copy() def copy(self): """Copy text to clipboard... or keyboard interrupt""" if self.hasSelectedText(): text = unicode(self.selectedText()) text = text.replace('>>> ', '').replace('... ', '').strip() # removing prompts QApplication.clipboard().setText(text) else: self.emit(SIGNAL("keyboard_interrupt()"))
class PythonConsoleWidget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setWindowTitle( QCoreApplication.translate("PythonConsole", "Python Console")) self.widgetButton = QWidget() #self.widgetEditors = QWidget() self.options = optionsDialog(self) self.helpDlg = HelpDialog(self) self.splitter = QSplitter(self) self.splitter.setOrientation(Qt.Vertical) self.splitter.setHandleWidth(3) self.splitter.setChildrenCollapsible(False) self.toolBar = QToolBar() self.toolBar.setEnabled(True) self.toolBar.setFocusPolicy(Qt.NoFocus) self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu) self.toolBar.setLayoutDirection(Qt.LeftToRight) self.toolBar.setIconSize(QSize(24, 24)) self.toolBar.setOrientation(Qt.Vertical) self.toolBar.setMovable(True) self.toolBar.setFloatable(True) self.b = QGridLayout(self.widgetButton) self.f = QGridLayout(self) self.f.setMargin(0) self.f.setSpacing(0) self.b.setMargin(0) self.b.setSpacing(0) ## Action for Clear button clearBt = QCoreApplication.translate("PythonConsole", "Clear console") self.clearButton = QAction(parent) self.clearButton.setCheckable(False) self.clearButton.setEnabled(True) self.clearButton.setIcon( QgsApplication.getThemeIcon("console/iconClearConsole.png")) self.clearButton.setMenuRole(QAction.PreferencesRole) self.clearButton.setIconVisibleInMenu(True) self.clearButton.setToolTip(clearBt) self.clearButton.setText(clearBt) ## Action for settings optionsBt = QCoreApplication.translate("PythonConsole", "Settings") self.optionsButton = QAction(parent) self.optionsButton.setCheckable(False) self.optionsButton.setEnabled(True) self.optionsButton.setIcon( QgsApplication.getThemeIcon("console/iconSettingsConsole.png")) self.optionsButton.setMenuRole(QAction.PreferencesRole) self.optionsButton.setIconVisibleInMenu(True) self.optionsButton.setToolTip(optionsBt) self.optionsButton.setText(optionsBt) ## Action menu for class actionClassBt = QCoreApplication.translate("PythonConsole", "Import Class") self.actionClass = QAction(parent) self.actionClass.setCheckable(False) self.actionClass.setEnabled(True) self.actionClass.setIcon( QgsApplication.getThemeIcon("console/iconClassConsole.png")) self.actionClass.setMenuRole(QAction.PreferencesRole) self.actionClass.setIconVisibleInMenu(True) self.actionClass.setToolTip(actionClassBt) self.actionClass.setText(actionClassBt) ## Action menu Open/Save script actionScriptBt = QCoreApplication.translate("PythonConsole", "Manage Script") self.actionScript = QAction(parent) self.actionScript.setCheckable(False) self.actionScript.setEnabled(True) self.actionScript.setIcon( QgsApplication.getThemeIcon("console/iconScriptConsole.png")) self.actionScript.setMenuRole(QAction.PreferencesRole) self.actionScript.setIconVisibleInMenu(True) self.actionScript.setToolTip(actionScriptBt) self.actionScript.setText(actionScriptBt) ## Import Sextante class loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import Sextante class") self.loadSextanteButton = QAction(parent) self.loadSextanteButton.setCheckable(False) self.loadSextanteButton.setEnabled(True) self.loadSextanteButton.setIcon( QgsApplication.getThemeIcon("console/iconSextanteConsole.png")) self.loadSextanteButton.setMenuRole(QAction.PreferencesRole) self.loadSextanteButton.setIconVisibleInMenu(True) self.loadSextanteButton.setToolTip(loadSextanteBt) self.loadSextanteButton.setText(loadSextanteBt) ## Import QtCore class loadQtCoreBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtCore class") self.loadQtCoreButton = QAction(parent) self.loadQtCoreButton.setCheckable(False) self.loadQtCoreButton.setEnabled(True) self.loadQtCoreButton.setIcon( QgsApplication.getThemeIcon("console/iconQtCoreConsole.png")) self.loadQtCoreButton.setMenuRole(QAction.PreferencesRole) self.loadQtCoreButton.setIconVisibleInMenu(True) self.loadQtCoreButton.setToolTip(loadQtCoreBt) self.loadQtCoreButton.setText(loadQtCoreBt) ## Import QtGui class loadQtGuiBt = QCoreApplication.translate("PythonConsole", "Import PyQt.QtGui class") self.loadQtGuiButton = QAction(parent) self.loadQtGuiButton.setCheckable(False) self.loadQtGuiButton.setEnabled(True) self.loadQtGuiButton.setIcon( QgsApplication.getThemeIcon("console/iconQtGuiConsole.png")) self.loadQtGuiButton.setMenuRole(QAction.PreferencesRole) self.loadQtGuiButton.setIconVisibleInMenu(True) self.loadQtGuiButton.setToolTip(loadQtGuiBt) self.loadQtGuiButton.setText(loadQtGuiBt) ## Action for Open File openFileBt = QCoreApplication.translate("PythonConsole", "Open script file") self.openFileButton = QAction(parent) self.openFileButton.setCheckable(False) self.openFileButton.setEnabled(True) self.openFileButton.setIcon( QgsApplication.getThemeIcon("console/iconOpenConsole.png")) self.openFileButton.setMenuRole(QAction.PreferencesRole) self.openFileButton.setIconVisibleInMenu(True) self.openFileButton.setToolTip(openFileBt) self.openFileButton.setText(openFileBt) ## Action for Save File saveFileBt = QCoreApplication.translate("PythonConsole", "Save to script file") self.saveFileButton = QAction(parent) self.saveFileButton.setCheckable(False) self.saveFileButton.setEnabled(True) self.saveFileButton.setIcon( QgsApplication.getThemeIcon("console/iconSaveConsole.png")) self.saveFileButton.setMenuRole(QAction.PreferencesRole) self.saveFileButton.setIconVisibleInMenu(True) self.saveFileButton.setToolTip(saveFileBt) self.saveFileButton.setText(saveFileBt) ## Action for Run script runBt = QCoreApplication.translate("PythonConsole", "Run command") self.runButton = QAction(parent) self.runButton.setCheckable(False) self.runButton.setEnabled(True) self.runButton.setIcon( QgsApplication.getThemeIcon("console/iconRunConsole.png")) self.runButton.setMenuRole(QAction.PreferencesRole) self.runButton.setIconVisibleInMenu(True) self.runButton.setToolTip(runBt) self.runButton.setText(runBt) ## Help action helpBt = QCoreApplication.translate("PythonConsole", "Help") self.helpButton = QAction(parent) self.helpButton.setCheckable(False) self.helpButton.setEnabled(True) self.helpButton.setIcon( QgsApplication.getThemeIcon("console/iconHelpConsole.png")) self.helpButton.setMenuRole(QAction.PreferencesRole) self.helpButton.setIconVisibleInMenu(True) self.helpButton.setToolTip(helpBt) self.helpButton.setText(helpBt) self.toolBar.addAction(self.clearButton) self.toolBar.addAction(self.actionClass) self.toolBar.addAction(self.actionScript) self.toolBar.addAction(self.optionsButton) self.toolBar.addAction(self.helpButton) self.toolBar.addAction(self.runButton) ## Menu Import Class self.classMenu = QMenu(self) self.classMenu.addAction(self.loadSextanteButton) self.classMenu.addAction(self.loadQtCoreButton) self.classMenu.addAction(self.loadQtGuiButton) cM = self.toolBar.widgetForAction(self.actionClass) cM.setMenu(self.classMenu) cM.setPopupMode(QToolButton.InstantPopup) ## Menu Manage Script self.scriptMenu = QMenu(self) self.scriptMenu.addAction(self.openFileButton) self.scriptMenu.addAction(self.saveFileButton) sM = self.toolBar.widgetForAction(self.actionScript) sM.setMenu(self.scriptMenu) sM.setPopupMode(QToolButton.InstantPopup) self.b.addWidget(self.toolBar) self.edit = PythonEdit(self) self.textEditOut = EditorOutput(self) self.setFocusProxy(self.edit) sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.widgetButton.sizePolicy().hasHeightForWidth()) self.widgetButton.setSizePolicy(sizePolicy) self.splitter.addWidget(self.textEditOut) self.splitter.addWidget(self.edit) sizes = self.splitter.sizes() self.splitter.setSizes(sizes) self.f.addWidget(self.widgetButton, 0, 0) self.f.addWidget(self.splitter, 0, 1) sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.textEditOut.sizePolicy().hasHeightForWidth()) self.textEditOut.setSizePolicy(sizePolicy) self.textEditOut.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.edit.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.clearButton.triggered.connect(self.textEditOut.clearConsole) self.optionsButton.triggered.connect(self.openSettings) self.loadSextanteButton.triggered.connect(self.sextante) self.loadQtCoreButton.triggered.connect(self.qtCore) self.loadQtGuiButton.triggered.connect(self.qtGui) self.runButton.triggered.connect(self.edit.entered) self.openFileButton.triggered.connect(self.openScriptFile) self.saveFileButton.triggered.connect(self.saveScriptFile) self.helpButton.triggered.connect(self.openHelp) QObject.connect(self.options.buttonBox, SIGNAL("accepted()"), self.prefChanged) def sextante(self): self.edit.commandConsole('sextante') def qtCore(self): self.edit.commandConsole('qtCore') def qtGui(self): self.edit.commandConsole('qtGui') def openScriptFile(self): settings = QSettings() lastDirPath = settings.value("pythonConsole/lastDirPath").toString() scriptFile = QFileDialog.getOpenFileName(self, "Open File", lastDirPath, "Script file (*.py)") if scriptFile.isEmpty() == False: oF = open(scriptFile, 'r') listScriptFile = [] for line in oF: if line != "\n": listScriptFile.append(line) self.edit.insertTextFromFile(listScriptFile) lastDirPath = QFileInfo(scriptFile).path() settings.setValue("pythonConsole/lastDirPath", QVariant(scriptFile)) def saveScriptFile(self): scriptFile = QFileDialog() scriptFile.setDefaultSuffix(".py") fName = scriptFile.getSaveFileName(self, "Save file", QString(), "Script file (*.py)") if fName.isEmpty() == False: filename = str(fName) if not filename.endswith(".py"): fName += ".py" sF = open(fName, 'w') listText = self.textEditOut.getTextFromEditor() is_first_line = True for s in listText: if s[0:3] in (">>>", "..."): s.replace(">>> ", "").replace("... ", "") if is_first_line: is_first_line = False else: sF.write('\n') sF.write(s) sF.close() self.callWidgetMessageBar('Script was correctly saved.') def openHelp(self): self.helpDlg.show() self.helpDlg.activateWindow() def openSettings(self): self.options.exec_() def prefChanged(self): self.edit.refreshLexerProperties() self.textEditOut.refreshLexerProperties() def callWidgetMessageBar(self, text): self.textEditOut.widgetMessageBar(iface, text)
def __init__(self, parent=None): QDockWidget.__init__(self, parent) self.setObjectName("PythonConsole") #self.setAllowedAreas(Qt.BottomDockWidgetArea) self.widgetButton = QWidget() self.widgetEdit = QWidget() self.toolBar = QToolBar() self.toolBar.setEnabled(True) #self.toolBar.setFont(font) self.toolBar.setFocusPolicy(Qt.NoFocus) self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu) self.toolBar.setLayoutDirection(Qt.LeftToRight) self.toolBar.setIconSize(QSize(24, 24)) self.toolBar.setOrientation(Qt.Vertical) self.toolBar.setMovable(True) self.toolBar.setFloatable(True) #self.toolBar.setAllowedAreas(Qt.LeftToolBarArea) #self.toolBar.setAllowedAreas(Qt.RightToolBarArea) #self.toolBar.setObjectName(_fromUtf8("toolMappa")) self.b = QVBoxLayout(self.widgetButton) self.e = QHBoxLayout(self.widgetEdit) self.e.setMargin(0) self.e.setSpacing(0) self.b.setMargin(0) ## Action for Clear button clearBt = QCoreApplication.translate("PythonConsole", "Clear console") self.clearButton = QAction(parent) self.clearButton.setCheckable(False) self.clearButton.setEnabled(True) self.clearButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconClearConsole.png")) self.clearButton.setMenuRole(QAction.PreferencesRole) self.clearButton.setIconVisibleInMenu(True) self.clearButton.setToolTip(clearBt) self.clearButton.setText(clearBt) ## Action for paste snippets code # self.currentLayerButton = QAction(parent) # self.currentLayerButton.setCheckable(False) # self.currentLayerButton.setEnabled(True) # self.currentLayerButton.setIcon(QIcon("icon/iconTempConsole.png")) # self.currentLayerButton.setMenuRole(QAction.PreferencesRole) # self.currentLayerButton.setIconVisibleInMenu(True) ## Action menu for class actionClassBt = QCoreApplication.translate("PythonConsole", "Import Class") self.actionClass = QAction(parent) self.actionClass.setCheckable(False) self.actionClass.setEnabled(True) self.actionClass.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconClassConsole.png")) self.actionClass.setMenuRole(QAction.PreferencesRole) self.actionClass.setIconVisibleInMenu(True) self.actionClass.setToolTip(actionClassBt) self.actionClass.setText(actionClassBt) ## Action menu Open/Save script actionScriptBt = QCoreApplication.translate("PythonConsole", "Manage Script") self.actionScript = QAction(parent) self.actionScript.setCheckable(False) self.actionScript.setEnabled(True) self.actionScript.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconScriptConsole.png")) self.actionScript.setMenuRole(QAction.PreferencesRole) self.actionScript.setIconVisibleInMenu(True) self.actionScript.setToolTip(actionScriptBt) self.actionScript.setText(actionScriptBt) ## Import Sextante class loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import sextante class") self.loadSextanteButton = QAction(parent) self.loadSextanteButton.setCheckable(False) self.loadSextanteButton.setEnabled(True) self.loadSextanteButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconSextanteConsole.png")) self.loadSextanteButton.setMenuRole(QAction.PreferencesRole) self.loadSextanteButton.setIconVisibleInMenu(True) self.loadSextanteButton.setToolTip(loadSextanteBt) self.loadSextanteButton.setText(loadSextanteBt) ## Import QgisInterface class loadIfaceBt = QCoreApplication.translate("PythonConsole", "Import iface class") self.loadIfaceButton = QAction(parent) self.loadIfaceButton.setCheckable(False) self.loadIfaceButton.setEnabled(True) self.loadIfaceButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconIfaceConsole.png")) self.loadIfaceButton.setMenuRole(QAction.PreferencesRole) self.loadIfaceButton.setIconVisibleInMenu(True) self.loadIfaceButton.setToolTip(loadIfaceBt) self.loadIfaceButton.setText(loadIfaceBt) ## Action for Open File openFileBt = QCoreApplication.translate("PythonConsole", "Open script file") self.openFileButton = QAction(parent) self.openFileButton.setCheckable(False) self.openFileButton.setEnabled(True) self.openFileButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconOpenConsole.png")) self.openFileButton.setMenuRole(QAction.PreferencesRole) self.openFileButton.setIconVisibleInMenu(True) self.openFileButton.setToolTip(openFileBt) self.openFileButton.setText(openFileBt) ## Action for Save File saveFileBt = QCoreApplication.translate("PythonConsole", "Save to script file") self.saveFileButton = QAction(parent) self.saveFileButton.setCheckable(False) self.saveFileButton.setEnabled(True) self.saveFileButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconSaveConsole.png")) self.saveFileButton.setMenuRole(QAction.PreferencesRole) self.saveFileButton.setIconVisibleInMenu(True) self.saveFileButton.setToolTip(saveFileBt) self.saveFileButton.setText(saveFileBt) ## Action for Run script runBt = QCoreApplication.translate("PythonConsole", "Run command") self.runButton = QAction(parent) self.runButton.setCheckable(False) self.runButton.setEnabled(True) self.runButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconRunConsole.png")) self.runButton.setMenuRole(QAction.PreferencesRole) self.runButton.setIconVisibleInMenu(True) self.runButton.setToolTip(runBt) self.runButton.setText(runBt) ## Help action helpBt = QCoreApplication.translate("PythonConsole", "Help") self.helpButton = QAction(parent) self.helpButton.setCheckable(False) self.helpButton.setEnabled(True) self.helpButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconHelpConsole.png")) self.helpButton.setMenuRole(QAction.PreferencesRole) self.helpButton.setIconVisibleInMenu(True) self.helpButton.setToolTip(helpBt) self.helpButton.setText(helpBt) self.toolBar.addAction(self.clearButton) self.toolBar.addAction(self.actionClass) self.toolBar.addAction(self.actionScript) self.toolBar.addAction(self.helpButton) self.toolBar.addAction(self.runButton) ## Menu Import Class self.classMenu = QMenu(self) self.classMenu.addAction(self.loadIfaceButton) self.classMenu.addAction(self.loadSextanteButton) cM = self.toolBar.widgetForAction(self.actionClass) cM.setMenu(self.classMenu) cM.setPopupMode(QToolButton.InstantPopup) ## Menu Manage Script self.scriptMenu = QMenu(self) self.scriptMenu.addAction(self.openFileButton) self.scriptMenu.addAction(self.saveFileButton) sM = self.toolBar.widgetForAction(self.actionScript) sM.setMenu(self.scriptMenu) sM.setPopupMode(QToolButton.InstantPopup) self.b.addWidget(self.toolBar) self.edit = PythonEdit() self.setWidget(self.widgetEdit) self.e.addWidget(self.widgetButton) self.e.addWidget(self.edit) self.edit.setFocus() self.setWindowTitle( QCoreApplication.translate("PythonConsole", "Python Console")) self.clearButton.triggered.connect(self.edit.clearConsole) #self.currentLayerButton.triggered.connect(self.cLayer) self.loadIfaceButton.triggered.connect(self.iface) self.loadSextanteButton.triggered.connect(self.sextante) self.runButton.triggered.connect(self.edit.entered) self.openFileButton.triggered.connect(self.openScriptFile) self.saveFileButton.triggered.connect(self.saveScriptFile) self.helpButton.triggered.connect(self.openHelp) # try to restore position from stored main window state if not iface.mainWindow().restoreDockWidget(self): iface.mainWindow().addDockWidget(Qt.BottomDockWidgetArea, self)
class PythonConsole(QDockWidget): def __init__(self, parent=None): QDockWidget.__init__(self, parent) self.setObjectName("PythonConsole") #self.setAllowedAreas(Qt.BottomDockWidgetArea) self.widgetButton = QWidget() self.widgetEdit = QWidget() self.toolBar = QToolBar() self.toolBar.setEnabled(True) #self.toolBar.setFont(font) self.toolBar.setFocusPolicy(Qt.NoFocus) self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu) self.toolBar.setLayoutDirection(Qt.LeftToRight) self.toolBar.setIconSize(QSize(24, 24)) self.toolBar.setOrientation(Qt.Vertical) self.toolBar.setMovable(True) self.toolBar.setFloatable(True) #self.toolBar.setAllowedAreas(Qt.LeftToolBarArea) #self.toolBar.setAllowedAreas(Qt.RightToolBarArea) #self.toolBar.setObjectName(_fromUtf8("toolMappa")) self.b = QVBoxLayout(self.widgetButton) self.e = QHBoxLayout(self.widgetEdit) self.e.setMargin(0) self.e.setSpacing(0) self.b.setMargin(0) ## Action for Clear button clearBt = QCoreApplication.translate("PythonConsole", "Clear console") self.clearButton = QAction(parent) self.clearButton.setCheckable(False) self.clearButton.setEnabled(True) self.clearButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconClearConsole.png")) self.clearButton.setMenuRole(QAction.PreferencesRole) self.clearButton.setIconVisibleInMenu(True) self.clearButton.setToolTip(clearBt) self.clearButton.setText(clearBt) ## Action for paste snippets code # self.currentLayerButton = QAction(parent) # self.currentLayerButton.setCheckable(False) # self.currentLayerButton.setEnabled(True) # self.currentLayerButton.setIcon(QIcon("icon/iconTempConsole.png")) # self.currentLayerButton.setMenuRole(QAction.PreferencesRole) # self.currentLayerButton.setIconVisibleInMenu(True) ## Action menu for class actionClassBt = QCoreApplication.translate("PythonConsole", "Import Class") self.actionClass = QAction(parent) self.actionClass.setCheckable(False) self.actionClass.setEnabled(True) self.actionClass.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconClassConsole.png")) self.actionClass.setMenuRole(QAction.PreferencesRole) self.actionClass.setIconVisibleInMenu(True) self.actionClass.setToolTip(actionClassBt) self.actionClass.setText(actionClassBt) ## Action menu Open/Save script actionScriptBt = QCoreApplication.translate("PythonConsole", "Manage Script") self.actionScript = QAction(parent) self.actionScript.setCheckable(False) self.actionScript.setEnabled(True) self.actionScript.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconScriptConsole.png")) self.actionScript.setMenuRole(QAction.PreferencesRole) self.actionScript.setIconVisibleInMenu(True) self.actionScript.setToolTip(actionScriptBt) self.actionScript.setText(actionScriptBt) ## Import Sextante class loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import sextante class") self.loadSextanteButton = QAction(parent) self.loadSextanteButton.setCheckable(False) self.loadSextanteButton.setEnabled(True) self.loadSextanteButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconSextanteConsole.png")) self.loadSextanteButton.setMenuRole(QAction.PreferencesRole) self.loadSextanteButton.setIconVisibleInMenu(True) self.loadSextanteButton.setToolTip(loadSextanteBt) self.loadSextanteButton.setText(loadSextanteBt) ## Import QgisInterface class loadIfaceBt = QCoreApplication.translate("PythonConsole", "Import iface class") self.loadIfaceButton = QAction(parent) self.loadIfaceButton.setCheckable(False) self.loadIfaceButton.setEnabled(True) self.loadIfaceButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconIfaceConsole.png")) self.loadIfaceButton.setMenuRole(QAction.PreferencesRole) self.loadIfaceButton.setIconVisibleInMenu(True) self.loadIfaceButton.setToolTip(loadIfaceBt) self.loadIfaceButton.setText(loadIfaceBt) ## Action for Open File openFileBt = QCoreApplication.translate("PythonConsole", "Open script file") self.openFileButton = QAction(parent) self.openFileButton.setCheckable(False) self.openFileButton.setEnabled(True) self.openFileButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconOpenConsole.png")) self.openFileButton.setMenuRole(QAction.PreferencesRole) self.openFileButton.setIconVisibleInMenu(True) self.openFileButton.setToolTip(openFileBt) self.openFileButton.setText(openFileBt) ## Action for Save File saveFileBt = QCoreApplication.translate("PythonConsole", "Save to script file") self.saveFileButton = QAction(parent) self.saveFileButton.setCheckable(False) self.saveFileButton.setEnabled(True) self.saveFileButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconSaveConsole.png")) self.saveFileButton.setMenuRole(QAction.PreferencesRole) self.saveFileButton.setIconVisibleInMenu(True) self.saveFileButton.setToolTip(saveFileBt) self.saveFileButton.setText(saveFileBt) ## Action for Run script runBt = QCoreApplication.translate("PythonConsole", "Run command") self.runButton = QAction(parent) self.runButton.setCheckable(False) self.runButton.setEnabled(True) self.runButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconRunConsole.png")) self.runButton.setMenuRole(QAction.PreferencesRole) self.runButton.setIconVisibleInMenu(True) self.runButton.setToolTip(runBt) self.runButton.setText(runBt) ## Help action helpBt = QCoreApplication.translate("PythonConsole", "Help") self.helpButton = QAction(parent) self.helpButton.setCheckable(False) self.helpButton.setEnabled(True) self.helpButton.setIcon( QIcon( os.path.dirname(__file__) + "/iconConsole/iconHelpConsole.png")) self.helpButton.setMenuRole(QAction.PreferencesRole) self.helpButton.setIconVisibleInMenu(True) self.helpButton.setToolTip(helpBt) self.helpButton.setText(helpBt) self.toolBar.addAction(self.clearButton) self.toolBar.addAction(self.actionClass) self.toolBar.addAction(self.actionScript) self.toolBar.addAction(self.helpButton) self.toolBar.addAction(self.runButton) ## Menu Import Class self.classMenu = QMenu(self) self.classMenu.addAction(self.loadIfaceButton) self.classMenu.addAction(self.loadSextanteButton) cM = self.toolBar.widgetForAction(self.actionClass) cM.setMenu(self.classMenu) cM.setPopupMode(QToolButton.InstantPopup) ## Menu Manage Script self.scriptMenu = QMenu(self) self.scriptMenu.addAction(self.openFileButton) self.scriptMenu.addAction(self.saveFileButton) sM = self.toolBar.widgetForAction(self.actionScript) sM.setMenu(self.scriptMenu) sM.setPopupMode(QToolButton.InstantPopup) self.b.addWidget(self.toolBar) self.edit = PythonEdit() self.setWidget(self.widgetEdit) self.e.addWidget(self.widgetButton) self.e.addWidget(self.edit) self.edit.setFocus() self.setWindowTitle( QCoreApplication.translate("PythonConsole", "Python Console")) self.clearButton.triggered.connect(self.edit.clearConsole) #self.currentLayerButton.triggered.connect(self.cLayer) self.loadIfaceButton.triggered.connect(self.iface) self.loadSextanteButton.triggered.connect(self.sextante) self.runButton.triggered.connect(self.edit.entered) self.openFileButton.triggered.connect(self.openScriptFile) self.saveFileButton.triggered.connect(self.saveScriptFile) self.helpButton.triggered.connect(self.openHelp) # try to restore position from stored main window state if not iface.mainWindow().restoreDockWidget(self): iface.mainWindow().addDockWidget(Qt.BottomDockWidgetArea, self) def cLayer(self): self.edit.commandConsole('cLayer') def sextante(self): self.edit.commandConsole('sextante') def iface(self): self.edit.commandConsole('iface') def openScriptFile(self): settings = QSettings() lastDirPath = settings.value("/pythonConsole/lastDirPath").toString() scriptFile = QFileDialog.getOpenFileName(self, "Open File", lastDirPath, "Script file (*.py)") if scriptFile.isEmpty() == False: oF = open(scriptFile, 'r') listScriptFile = [] for line in oF: if line != "\n": listScriptFile.append(line) self.edit.insertTextFromFile(listScriptFile) lastDirPath = QFileInfo(scriptFile).path() settings.setValue("/pythonConsole/lastDirPath", QVariant(scriptFile)) def saveScriptFile(self): scriptFile = QFileDialog() scriptFile.setDefaultSuffix(".py") fName = scriptFile.getSaveFileName(self, "Save file", QString(), "Script file (*.py)") if fName.isEmpty() == False: filename = str(fName) if not filename.endswith(".py"): fName += ".py" sF = open(fName, 'w') listText = self.edit.getTextFromEditor() is_first_line = True for s in listText: if s[0:3] in (">>>", "..."): s.replace(">>> ", "").replace("... ", "") if is_first_line: is_first_line = False else: sF.write('\n') sF.write(s) sF.close() def openHelp(self): dlg = HelpDialog() dlg.exec_() def closeEvent(self, event): self.edit.writeHistoryFile() QWidget.closeEvent(self, event)
def __init__(self, parent=None): QDockWidget.__init__(self, parent) self.setObjectName("PythonConsole") # self.setAllowedAreas(Qt.BottomDockWidgetArea) self.widgetButton = QWidget() self.widgetEdit = QWidget() self.toolBar = QToolBar() self.toolBar.setEnabled(True) # self.toolBar.setFont(font) self.toolBar.setFocusPolicy(Qt.NoFocus) self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu) self.toolBar.setLayoutDirection(Qt.LeftToRight) self.toolBar.setIconSize(QSize(24, 24)) self.toolBar.setOrientation(Qt.Vertical) self.toolBar.setMovable(True) self.toolBar.setFloatable(True) # self.toolBar.setAllowedAreas(Qt.LeftToolBarArea) # self.toolBar.setAllowedAreas(Qt.RightToolBarArea) # self.toolBar.setObjectName(_fromUtf8("toolMappa")) self.b = QVBoxLayout(self.widgetButton) self.e = QHBoxLayout(self.widgetEdit) self.e.setMargin(0) self.e.setSpacing(0) self.b.setMargin(0) ## Action for Clear button self.clearButton = QAction(parent) self.clearButton.setCheckable(False) self.clearButton.setEnabled(True) self.clearButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconClearConsole.png")) self.clearButton.setMenuRole(QAction.PreferencesRole) self.clearButton.setIconVisibleInMenu(True) self.clearButton.setToolTip("Clear console") self.clearButton.setText("Clear console") ## Action for paste snippets code # self.currentLayerButton = QAction(parent) # self.currentLayerButton.setCheckable(False) # self.currentLayerButton.setEnabled(True) # self.currentLayerButton.setIcon(QIcon("icon/iconTempConsole.png")) # self.currentLayerButton.setMenuRole(QAction.PreferencesRole) # self.currentLayerButton.setIconVisibleInMenu(True) ## Import Sextante class self.loadSextanteButton = QAction(parent) self.loadSextanteButton.setCheckable(False) self.loadSextanteButton.setEnabled(True) self.loadSextanteButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconSextanteConsole.png")) self.loadSextanteButton.setMenuRole(QAction.PreferencesRole) self.loadSextanteButton.setIconVisibleInMenu(True) self.loadSextanteButton.setToolTip("Import sextante class") self.loadSextanteButton.setText("Import sextante class") ## Import QgisInterface class self.loadIfaceButton = QAction(parent) self.loadIfaceButton.setCheckable(False) self.loadIfaceButton.setEnabled(True) self.loadIfaceButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconTempConsole.png")) self.loadIfaceButton.setMenuRole(QAction.PreferencesRole) self.loadIfaceButton.setIconVisibleInMenu(True) self.loadIfaceButton.setToolTip("Import iface class") self.loadIfaceButton.setText("Import iface class") ## Action for Open File self.openFileButton = QAction(parent) self.openFileButton.setCheckable(False) self.openFileButton.setEnabled(True) self.openFileButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconOpenConsole.png")) self.openFileButton.setMenuRole(QAction.PreferencesRole) self.openFileButton.setIconVisibleInMenu(True) self.openFileButton.setToolTip("Open script file") self.openFileButton.setText("Open script file") ## Action for Save File self.saveFileButton = QAction(parent) self.saveFileButton.setCheckable(False) self.saveFileButton.setEnabled(True) self.saveFileButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconSaveConsole.png")) self.saveFileButton.setMenuRole(QAction.PreferencesRole) self.saveFileButton.setIconVisibleInMenu(True) self.saveFileButton.setToolTip("Save to script file") self.saveFileButton.setText("Save to script file") ## Action for Run script self.runButton = QAction(parent) self.runButton.setCheckable(False) self.runButton.setEnabled(True) self.runButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconRunConsole.png")) self.runButton.setMenuRole(QAction.PreferencesRole) self.runButton.setIconVisibleInMenu(True) self.runButton.setToolTip("Run command") self.runButton.setText("Run") ## Help action self.helpButton = QAction(parent) self.helpButton.setCheckable(False) self.helpButton.setEnabled(True) self.helpButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconHelpConsole.png")) self.helpButton.setMenuRole(QAction.PreferencesRole) self.helpButton.setIconVisibleInMenu(True) self.helpButton.setToolTip("Help") self.helpButton.setText("Help") self.toolBar.addAction(self.clearButton) # self.toolBar.addAction(self.currentLayerButton) self.toolBar.addAction(self.loadIfaceButton) self.toolBar.addAction(self.loadSextanteButton) self.toolBar.addAction(self.openFileButton) self.toolBar.addAction(self.saveFileButton) self.toolBar.addAction(self.helpButton) self.toolBar.addAction(self.runButton) self.b.addWidget(self.toolBar) self.edit = PythonEdit() self.setWidget(self.widgetEdit) self.e.addWidget(self.widgetButton) self.e.addWidget(self.edit) self.edit.setFocus() self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console")) self.clearButton.triggered.connect(self.edit.clearConsole) # self.currentLayerButton.triggered.connect(self.cLayer) self.loadIfaceButton.triggered.connect(self.iface) self.loadSextanteButton.triggered.connect(self.sextante) self.runButton.triggered.connect(self.edit.entered) self.openFileButton.triggered.connect(self.openScriptFile) self.saveFileButton.triggered.connect(self.saveScriptFile) self.helpButton.triggered.connect(self.openHelp) # try to restore position from stored main window state if not iface.mainWindow().restoreDockWidget(self): iface.mainWindow().addDockWidget(Qt.BottomDockWidgetArea, self)
class PythonConsole(QDockWidget): def __init__(self, parent=None): QDockWidget.__init__(self, parent) self.setObjectName("PythonConsole") # self.setAllowedAreas(Qt.BottomDockWidgetArea) self.widgetButton = QWidget() self.widgetEdit = QWidget() self.toolBar = QToolBar() self.toolBar.setEnabled(True) # self.toolBar.setFont(font) self.toolBar.setFocusPolicy(Qt.NoFocus) self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu) self.toolBar.setLayoutDirection(Qt.LeftToRight) self.toolBar.setIconSize(QSize(24, 24)) self.toolBar.setOrientation(Qt.Vertical) self.toolBar.setMovable(True) self.toolBar.setFloatable(True) # self.toolBar.setAllowedAreas(Qt.LeftToolBarArea) # self.toolBar.setAllowedAreas(Qt.RightToolBarArea) # self.toolBar.setObjectName(_fromUtf8("toolMappa")) self.b = QVBoxLayout(self.widgetButton) self.e = QHBoxLayout(self.widgetEdit) self.e.setMargin(0) self.e.setSpacing(0) self.b.setMargin(0) ## Action for Clear button self.clearButton = QAction(parent) self.clearButton.setCheckable(False) self.clearButton.setEnabled(True) self.clearButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconClearConsole.png")) self.clearButton.setMenuRole(QAction.PreferencesRole) self.clearButton.setIconVisibleInMenu(True) self.clearButton.setToolTip("Clear console") self.clearButton.setText("Clear console") ## Action for paste snippets code # self.currentLayerButton = QAction(parent) # self.currentLayerButton.setCheckable(False) # self.currentLayerButton.setEnabled(True) # self.currentLayerButton.setIcon(QIcon("icon/iconTempConsole.png")) # self.currentLayerButton.setMenuRole(QAction.PreferencesRole) # self.currentLayerButton.setIconVisibleInMenu(True) ## Import Sextante class self.loadSextanteButton = QAction(parent) self.loadSextanteButton.setCheckable(False) self.loadSextanteButton.setEnabled(True) self.loadSextanteButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconSextanteConsole.png")) self.loadSextanteButton.setMenuRole(QAction.PreferencesRole) self.loadSextanteButton.setIconVisibleInMenu(True) self.loadSextanteButton.setToolTip("Import sextante class") self.loadSextanteButton.setText("Import sextante class") ## Import QgisInterface class self.loadIfaceButton = QAction(parent) self.loadIfaceButton.setCheckable(False) self.loadIfaceButton.setEnabled(True) self.loadIfaceButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconTempConsole.png")) self.loadIfaceButton.setMenuRole(QAction.PreferencesRole) self.loadIfaceButton.setIconVisibleInMenu(True) self.loadIfaceButton.setToolTip("Import iface class") self.loadIfaceButton.setText("Import iface class") ## Action for Open File self.openFileButton = QAction(parent) self.openFileButton.setCheckable(False) self.openFileButton.setEnabled(True) self.openFileButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconOpenConsole.png")) self.openFileButton.setMenuRole(QAction.PreferencesRole) self.openFileButton.setIconVisibleInMenu(True) self.openFileButton.setToolTip("Open script file") self.openFileButton.setText("Open script file") ## Action for Save File self.saveFileButton = QAction(parent) self.saveFileButton.setCheckable(False) self.saveFileButton.setEnabled(True) self.saveFileButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconSaveConsole.png")) self.saveFileButton.setMenuRole(QAction.PreferencesRole) self.saveFileButton.setIconVisibleInMenu(True) self.saveFileButton.setToolTip("Save to script file") self.saveFileButton.setText("Save to script file") ## Action for Run script self.runButton = QAction(parent) self.runButton.setCheckable(False) self.runButton.setEnabled(True) self.runButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconRunConsole.png")) self.runButton.setMenuRole(QAction.PreferencesRole) self.runButton.setIconVisibleInMenu(True) self.runButton.setToolTip("Run command") self.runButton.setText("Run") ## Help action self.helpButton = QAction(parent) self.helpButton.setCheckable(False) self.helpButton.setEnabled(True) self.helpButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconHelpConsole.png")) self.helpButton.setMenuRole(QAction.PreferencesRole) self.helpButton.setIconVisibleInMenu(True) self.helpButton.setToolTip("Help") self.helpButton.setText("Help") self.toolBar.addAction(self.clearButton) # self.toolBar.addAction(self.currentLayerButton) self.toolBar.addAction(self.loadIfaceButton) self.toolBar.addAction(self.loadSextanteButton) self.toolBar.addAction(self.openFileButton) self.toolBar.addAction(self.saveFileButton) self.toolBar.addAction(self.helpButton) self.toolBar.addAction(self.runButton) self.b.addWidget(self.toolBar) self.edit = PythonEdit() self.setWidget(self.widgetEdit) self.e.addWidget(self.widgetButton) self.e.addWidget(self.edit) self.edit.setFocus() self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console")) self.clearButton.triggered.connect(self.edit.clearConsole) # self.currentLayerButton.triggered.connect(self.cLayer) self.loadIfaceButton.triggered.connect(self.iface) self.loadSextanteButton.triggered.connect(self.sextante) self.runButton.triggered.connect(self.edit.entered) self.openFileButton.triggered.connect(self.openScriptFile) self.saveFileButton.triggered.connect(self.saveScriptFile) self.helpButton.triggered.connect(self.openHelp) # try to restore position from stored main window state if not iface.mainWindow().restoreDockWidget(self): iface.mainWindow().addDockWidget(Qt.BottomDockWidgetArea, self) def cLayer(self): self.edit.commandConsole("cLayer") def sextante(self): self.edit.commandConsole("sextante") def iface(self): self.edit.commandConsole("iface") def openScriptFile(self): settings = QSettings() lastDirPath = settings.value("/pythonConsole/lastDirPath").toString() scriptFile = QFileDialog.getOpenFileName(self, "Open File", lastDirPath, "Script file (*.py)") if scriptFile.isEmpty() == False: oF = open(scriptFile, "r") listScriptFile = [] for line in oF: if line != "\n": listScriptFile.append(line) self.edit.insertTextFromFile(listScriptFile) lastDirPath = QFileInfo(scriptFile).path() settings.setValue("/pythonConsole/lastDirPath", QVariant(scriptFile)) def saveScriptFile(self): scriptFile = QFileDialog() scriptFile.setDefaultSuffix(".py") fName = scriptFile.getSaveFileName(self, "Save file", QString(), "Script file (*.py)") if fName.isEmpty() == False: filename = str(fName) if not filename.endswith(".py"): fName += ".py" sF = open(fName, "w") listText = self.edit.getTextFromEditor() is_first_line = True for s in listText: if s[0:3] in (">>>", "..."): s.replace(">>> ", "").replace("... ", "") if is_first_line: is_first_line = False else: sF.write("\n") sF.write(s) sF.close() def openHelp(self): dlg = HelpDialog() dlg.exec_() def closeEvent(self, event): QWidget.closeEvent(self, event)