Remove QgsPythonConsoleBase so that we have a single definitive Python editor class (QgsCodeEditorPython)

This commit is contained in:
Nyall Dawson 2020-10-05 19:50:25 +10:00
parent ac6ee22602
commit 8ec7f0e69c
6 changed files with 49 additions and 82 deletions

View File

@ -3,7 +3,6 @@ SET (PYTHON_OUTPUT_DIRECTORY ${QGIS_OUTPUT_DIRECTORY}/python)
SET(PY_CONSOLE_FILES
console.py
console_base.py
console_sci.py
console_settings.py
console_output.py

View File

@ -1,53 +0,0 @@
# -*- coding:utf-8 -*-
"""
/***************************************************************************
Python Console for QGIS
-------------------
begin : 2020-06-04
copyright : (C) 2020 by Richard Duivenvoorde
email : Richard Duivenvoorde (at) duif (dot) net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
Some portions of code were taken from https://code.google.com/p/pydee/
"""
from qgis.PyQt.QtCore import Qt, QUrl
from qgis.PyQt.QtGui import QColor, QFontDatabase, QDesktopServices
from qgis.PyQt.Qsci import QsciLexerPython, QsciAPIs
from qgis.core import QgsApplication, Qgis
from qgis.gui import QgsCodeEditorPython, QgsCodeEditor
import os
class QgsPythonConsoleBase(QgsCodeEditorPython):
def __init__(self, parent=None):
super().__init__(parent)
def setLexers(self):
self.api = QsciAPIs(self.lexer())
checkBoxAPI = self.settings.value("pythonConsole/preloadAPI", True, type=bool)
checkBoxPreparedAPI = self.settings.value("pythonConsole/usePreparedAPIFile", False, type=bool)
if checkBoxAPI:
pap = os.path.join(QgsApplication.pkgDataPath(), "python", "qsci_apis", "pyqgis.pap")
self.api.loadPrepared(pap)
elif checkBoxPreparedAPI:
self.api.loadPrepared(self.settings.value("pythonConsole/preparedAPIFile"))
else:
apiPath = self.settings.value("pythonConsole/userAPI", [])
for i in range(0, len(apiPath)):
self.api.load(apiPath[i])
self.api.prepare()
self.lexer().setAPIs(self.api)
if __name__ == "__main__":
pass

View File

@ -24,9 +24,8 @@ from qgis.PyQt.QtNetwork import QNetworkRequest
from qgis.PyQt.QtWidgets import QShortcut, QMenu, QApplication, QWidget, QGridLayout, QSpacerItem, QSizePolicy, QFileDialog, QTabWidget, QTreeWidgetItem, QFrame, QLabel, QToolButton, QMessageBox
from qgis.PyQt.Qsci import QsciScintilla, QsciAPIs, QsciStyle
from qgis.core import Qgis, QgsApplication, QgsSettings, QgsBlockingNetworkRequest
from qgis.gui import QgsMessageBar, QgsCodeEditor
from qgis.gui import QgsMessageBar, QgsCodeEditorPython
from qgis.utils import OverrideCursor
from .console_base import QgsPythonConsoleBase
import sys
import os
import subprocess
@ -79,12 +78,12 @@ class KeyFilter(QObject):
return QObject.eventFilter(self, obj, event)
class Editor(QgsPythonConsoleBase):
class Editor(QgsCodeEditorPython):
MARKER_NUM = 6
def __init__(self, parent=None):
super(Editor, self).__init__(parent)
super().__init__(parent)
self.parent = parent
# recent modification time
self.lastModified = 0
@ -143,7 +142,7 @@ class Editor(QgsPythonConsoleBase):
def settingsEditor(self):
# Set Python lexer
self.setLexers()
self.initializeLexer()
threshold = self.settings.value("pythonConsole/autoCompThreshold", 2, type=int)
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI')
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled", True, type=bool)

View File

@ -24,8 +24,7 @@ from qgis.PyQt.QtGui import QColor, QFont, QKeySequence, QFontDatabase
from qgis.PyQt.QtWidgets import QGridLayout, QSpacerItem, QSizePolicy, QShortcut, QMenu, QApplication
from qgis.PyQt.Qsci import QsciScintilla
from qgis.core import Qgis, QgsApplication, QgsSettings
from qgis.gui import QgsMessageBar, QgsCodeEditor
from .console_base import QgsPythonConsoleBase
from qgis.gui import QgsMessageBar, QgsCodeEditorPython
import sys
@ -94,10 +93,10 @@ class writeOut(QObject):
return False
class ShellOutputScintilla(QgsPythonConsoleBase):
class ShellOutputScintilla(QgsCodeEditorPython):
def __init__(self, parent=None):
super(ShellOutputScintilla, self).__init__(parent)
super().__init__(parent)
self.parent = parent
self.shell = self.parent.shell
@ -119,6 +118,9 @@ class ShellOutputScintilla(QgsPythonConsoleBase):
self.insertInitText()
self.refreshSettingsOutput()
self.setReadOnly(True)
self.setCaretWidth(0) # NO (blinking) caret in the output
self.setMinimumHeight(120)
@ -152,7 +154,7 @@ class ShellOutputScintilla(QgsPythonConsoleBase):
def refreshSettingsOutput(self):
# Set Python lexer
self.setLexers()
self.initializeLexer()
self.setReadOnly(True)
self.setCaretWidth(0) # NO (blinking) caret in the output

View File

@ -21,10 +21,9 @@ Some portions of code were taken from https://code.google.com/p/pydee/
from qgis.PyQt.QtCore import Qt, QByteArray, QCoreApplication, QFile, QSize
from qgis.PyQt.QtWidgets import QDialog, QMenu, QShortcut, QApplication
from qgis.PyQt.QtGui import QColor, QKeySequence, QFont, QFontMetrics, QStandardItemModel, QStandardItem, QClipboard, \
QFontDatabase
from qgis.PyQt.QtGui import QKeySequence, QFontMetrics, QStandardItemModel, QStandardItem, QClipboard
from qgis.PyQt.Qsci import QsciScintilla
from qgis.gui import QgsCodeEditor
from qgis.gui import QgsCodeEditorPython, QgsCodeEditor
import sys
import os
@ -35,7 +34,6 @@ import traceback
from qgis.core import QgsApplication, QgsSettings, Qgis
from .ui_console_history_dlg import Ui_HistoryDialogPythonConsole
from .console_base import QgsPythonConsoleBase
_init_commands = ["import sys", "import os", "import re", "import math", "from qgis.core import *",
"from qgis.gui import *", "from qgis.analysis import *", "from qgis._3d import *",
@ -46,10 +44,10 @@ _init_commands = ["import sys", "import os", "import re", "import math", "from q
_historyFile = os.path.join(QgsApplication.qgisSettingsDirPath(), "console_history.txt")
class ShellScintilla(QgsPythonConsoleBase, code.InteractiveInterpreter):
class ShellScintilla(QgsCodeEditorPython, code.InteractiveInterpreter):
def __init__(self, parent=None):
super(ShellScintilla, self).__init__(parent)
super(QgsCodeEditorPython, self).__init__(parent)
code.InteractiveInterpreter.__init__(self, locals=None)
self.parent = parent
@ -115,7 +113,7 @@ class ShellScintilla(QgsPythonConsoleBase, code.InteractiveInterpreter):
def refreshSettingsShell(self):
# Set Python lexer
self.setLexers()
self.initializeLexer()
threshold = self.settings.value("pythonConsole/autoCompThreshold", 2, type=int)
self.setAutoCompletionThreshold(threshold)
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI')

View File

@ -17,6 +17,8 @@
#include "qgscodeeditorpython.h"
#include "qgslogger.h"
#include "qgssymbollayerutils.h"
#include "qgssettings.h"
#include "qgis.h"
#include <QWidget>
#include <QString>
@ -90,15 +92,36 @@ void QgsCodeEditorPython::initializeLexer()
QsciAPIs *apis = new QsciAPIs( pyLexer );
// check if the file is a prepared apis file.
//QString mPapFileName = QFileInfo( mAPISFilesList[0] ).fileName();
//QString isPapFile = mPapFileName.right( 3 );
//QgsDebugMsg( QStringLiteral( "file extension: %1" ).arg( isPapFile ) );
QgsSettings settings;
if ( mAPISFilesList.isEmpty() )
{
mPapFile = QgsApplication::pkgDataPath() + QStringLiteral( "/python/qsci_apis/pyqgis.pap" );
apis->loadPrepared( mPapFile );
if ( settings.value( QStringLiteral( "pythonConsole/preloadAPI" ), true ).toBool() )
{
mPapFile = QgsApplication::pkgDataPath() + QStringLiteral( "/python/qsci_apis/pyqgis.pap" );
apis->loadPrepared( mPapFile );
}
else if ( settings.value( QStringLiteral( "pythonConsole/usePreparedAPIFile" ), false ).toBool() )
{
apis->loadPrepared( settings.value( QStringLiteral( "pythonConsole/preparedAPIFile" ) ).toString() );
}
else
{
const QStringList apiPaths = settings.value( QStringLiteral( "pythonConsole/userAPI" ) ).toStringList();
for ( const QString &path : apiPaths )
{
if ( !QFileInfo::exists( path ) )
{
QgsDebugMsg( QStringLiteral( "The apis file %1 was not found" ).arg( path ) );
}
else
{
apis->load( path );
}
}
apis->prepare();
pyLexer->setAPIs( apis );
}
}
else if ( mAPISFilesList.length() == 1 && mAPISFilesList[0].right( 3 ) == QLatin1String( "pap" ) )
{
@ -112,16 +135,15 @@ void QgsCodeEditorPython::initializeLexer()
}
else
{
for ( int i = 0; i < mAPISFilesList.size(); i++ )
for ( const QString &path : mAPISFilesList )
{
if ( !QFileInfo::exists( mAPISFilesList[i] ) )
if ( !QFileInfo::exists( path ) )
{
QgsDebugMsg( QStringLiteral( "The apis file %1 was not found" ).arg( mAPISFilesList.at( i ) ) );
return;
QgsDebugMsg( QStringLiteral( "The apis file %1 was not found" ).arg( path ) );
}
else
{
apis->load( mAPISFilesList[i] );
apis->load( path );
}
}
apis->prepare();