Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ensure autocomplete correctly works for all QgsCodeEditorPython insta…
…nces
  • Loading branch information
nyalldawson committed Oct 5, 2020
1 parent 8ec7f0e commit ec2b210
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 58 deletions.
36 changes: 3 additions & 33 deletions python/console/console_editor.py
Expand Up @@ -19,10 +19,10 @@
Some portions of code were taken from https://code.google.com/p/pydee/
"""
from qgis.PyQt.QtCore import Qt, QObject, QEvent, QCoreApplication, QFileInfo, QSize, QDir, QByteArray, QJsonDocument, QUrl
from qgis.PyQt.QtGui import QFont, QFontMetrics, QColor, QKeySequence, QCursor, QFontDatabase
from qgis.PyQt.QtGui import QFont, QColor, QKeySequence
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.PyQt.Qsci import QsciScintilla, QsciStyle
from qgis.core import Qgis, QgsApplication, QgsSettings, QgsBlockingNetworkRequest
from qgis.gui import QgsMessageBar, QgsCodeEditorPython
from qgis.utils import OverrideCursor
Expand Down Expand Up @@ -102,8 +102,6 @@ def __init__(self, parent=None):

self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)

self.settingsEditor()

# Annotations
self.setAnnotationDisplay(QsciScintilla.ANNOTATION_BOXED)

Expand All @@ -120,7 +118,7 @@ def __init__(self, parent=None):
self.redoScut = QShortcut(QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_Z), self)
self.redoScut.setContext(Qt.WidgetShortcut)
self.redoScut.activated.connect(self.redo)
self.newShortcutCS.activated.connect(self.autoCompleteKeyBinding)
self.newShortcutCS.activated.connect(self.autoComplete)
self.runScut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_E), self)
self.runScut.setContext(Qt.WidgetShortcut)
self.runScut.activated.connect(self.runSelectedCode) # spellok
Expand All @@ -143,30 +141,6 @@ def __init__(self, parent=None):
def settingsEditor(self):
# Set Python lexer
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)
self.setAutoCompletionThreshold(threshold)
if autoCompEnabled:
if radioButtonSource == 'fromDoc':
self.setAutoCompletionSource(self.AcsDocument)
elif radioButtonSource == 'fromAPI':
self.setAutoCompletionSource(self.AcsAPIs)
elif radioButtonSource == 'fromDocAPI':
self.setAutoCompletionSource(self.AcsAll)
else:
self.setAutoCompletionSource(self.AcsNone)

def autoCompleteKeyBinding(self):
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI')
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled", True, type=bool)
if autoCompEnabled:
if radioButtonSource == 'fromDoc':
self.autoCompleteFromDocument()
elif radioButtonSource == 'fromAPI':
self.autoCompleteFromAPIs()
elif radioButtonSource == 'fromDocAPI':
self.autoCompleteFromAll()

def move_cursor_to_end(self):
"""Move cursor to end of text"""
Expand Down Expand Up @@ -1148,10 +1122,6 @@ def listObject(self, tab):
# pass

def refreshSettingsEditor(self):
countTab = self.count()
for i in range(countTab):
self.widget(i).newEditor.settingsEditor()

objInspectorEnabled = self.settings.value("pythonConsole/enableObjectInsp",
False, type=bool)
listObj = self.parent.objectListButton
Expand Down
26 changes: 1 addition & 25 deletions python/console/console_sci.py
Expand Up @@ -102,7 +102,7 @@ def __init__(self, parent=None):
self.newShortcutCAS = QShortcut(QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_Space), self)
self.newShortcutCSS.setContext(Qt.WidgetShortcut)
self.newShortcutCAS.setContext(Qt.WidgetShortcut)
self.newShortcutCAS.activated.connect(self.autoCompleteKeyBinding)
self.newShortcutCAS.activated.connect(self.autoComplete)
self.newShortcutCSS.activated.connect(self.showHistory)

def _setMinimumHeight(self):
Expand All @@ -114,19 +114,6 @@ def _setMinimumHeight(self):
def refreshSettingsShell(self):
# Set Python lexer
self.initializeLexer()
threshold = self.settings.value("pythonConsole/autoCompThreshold", 2, type=int)
self.setAutoCompletionThreshold(threshold)
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI')
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled", True, type=bool)
if autoCompEnabled:
if radioButtonSource == 'fromDoc':
self.setAutoCompletionSource(self.AcsDocument)
elif radioButtonSource == 'fromAPI':
self.setAutoCompletionSource(self.AcsAPIs)
elif radioButtonSource == 'fromDocAPI':
self.setAutoCompletionSource(self.AcsAll)
else:
self.setAutoCompletionSource(self.AcsNone)

# Sets minimum height for input area based of font metric
self._setMinimumHeight()
Expand All @@ -150,17 +137,6 @@ def showHistory(self):
self.historyDlg._reloadHistory()
self.historyDlg.activateWindow()

def autoCompleteKeyBinding(self):
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI')
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled", True, type=bool)
if autoCompEnabled:
if radioButtonSource == 'fromDoc':
self.autoCompleteFromDocument()
elif radioButtonSource == 'fromAPI':
self.autoCompleteFromAPIs()
elif radioButtonSource == 'fromDocAPI':
self.autoCompleteFromAll()

def commandConsole(self, commands):
if not self.is_cursor_on_last_line():
self.move_cursor_to_end()
Expand Down
9 changes: 9 additions & 0 deletions python/gui/auto_generated/qgscodeeditorpython.sip.in
Expand Up @@ -66,6 +66,15 @@ Searches the selected text in the official PyQGIS online documentation.
virtual void initializeLexer();


protected slots:

void autoComplete();
%Docstring
Triggers the autocompletion popup.

.. versionadded:: 3.16
%End

};

/************************************************************************
Expand Down
37 changes: 37 additions & 0 deletions src/gui/qgscodeeditorpython.cpp
Expand Up @@ -151,6 +151,23 @@ void QgsCodeEditorPython::initializeLexer()
}
setLexer( pyLexer );

const int threshold = settings.value( QStringLiteral( "pythonConsole/autoCompThreshold" ), 2 ).toInt();
setAutoCompletionThreshold( threshold );
if ( !settings.value( "pythonConsole/autoCompleteEnabled", true ).toBool() )
{
setAutoCompletionSource( AcsNone );
}
else
{
QString autoCompleteSource = settings.value( QStringLiteral( "pythonConsole/autoCompleteSource" ), QStringLiteral( "fromAPI" ) ).toString();
if ( autoCompleteSource == QLatin1String( "fromDoc" ) )
setAutoCompletionSource( AcsDocument );
else if ( autoCompleteSource == QLatin1String( "fromDocAPI" ) )
setAutoCompletionSource( AcsAll );
else
setAutoCompletionSource( AcsAPIs );
}

setMarginVisible( true );

// Margin 2 is used for the 'folding'
Expand All @@ -163,6 +180,26 @@ void QgsCodeEditorPython::initializeLexer()
setIndentationGuides( true );
}

void QgsCodeEditorPython::autoComplete()
{
switch ( autoCompletionSource() )
{
case AcsDocument:
autoCompleteFromDocument();
break;

case AcsAPIs:
autoCompleteFromAPIs();
break;

case AcsAll:
autoCompleteFromAll();
break;

case AcsNone:
break;
}
}

void QgsCodeEditorPython::loadAPIs( const QList<QString> &filenames )
{
Expand Down
9 changes: 9 additions & 0 deletions src/gui/qgscodeeditorpython.h
Expand Up @@ -70,6 +70,15 @@ class GUI_EXPORT QgsCodeEditorPython : public QgsCodeEditor

void initializeLexer() override;

protected slots:

/**
* Triggers the autocompletion popup.
*
* \since QGIS 3.16
*/
void autoComplete();

private:

QList<QString> mAPISFilesList;
Expand Down

0 comments on commit ec2b210

Please sign in to comment.