Skip to content

Commit

Permalink
Move more code to QgsCodeEditorPython base class
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 5, 2020
1 parent 96db7ad commit d9782a1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
7 changes: 0 additions & 7 deletions python/console/console_base.py
Expand Up @@ -122,13 +122,6 @@ def setLexers(self):

self.setLexer(self.lexer)

def searchPyQGIS(self):
if self.hasSelectedText():
text = self.selectedText()
text = text.replace('>>> ', '').replace('... ', '').strip() # removing prompts
version = '.'.join(Qgis.QGIS_VERSION.split('.')[0:2])
QDesktopServices.openUrl(QUrl('https://qgis.org/pyqgis/' + version + '/search.html?q=' + text))


if __name__ == "__main__":
pass
2 changes: 1 addition & 1 deletion python/console/console_editor.py
Expand Up @@ -235,7 +235,7 @@ def contextMenuEvent(self, e):
self.runSelectedCode, 'Ctrl+E') # spellok
pyQGISHelpAction = menu.addAction(self.iconPyQGISHelp,
QCoreApplication.translate("PythonConsole", "Search Selected in PyQGIS docs"),
self.searchPyQGIS)
self.searchSelectedTextInPyQGISDocs)
menu.addAction(self.iconRunScript,
QCoreApplication.translate("PythonConsole", "Run Script"),
self.runScriptCode, 'Shift+Ctrl+E')
Expand Down
2 changes: 1 addition & 1 deletion python/console/console_output.py
Expand Up @@ -189,7 +189,7 @@ def contextMenuEvent(self, e):
self.clearConsole)
pyQGISHelpAction = menu.addAction(self.iconPyQGISHelp,
QCoreApplication.translate("PythonConsole", "Search Selected in PyQGIS docs"),
self.searchPyQGIS)
self.searchSelectedTextInPyQGISDocs)
menu.addSeparator()
copyAction = menu.addAction(
self.iconCopy,
Expand Down
2 changes: 1 addition & 1 deletion python/console/console_sci.py
Expand Up @@ -446,7 +446,7 @@ def contextMenuEvent(self, e):
self.paste, QKeySequence.Paste)
pyQGISHelpAction = menu.addAction(self.iconPyQGISHelp,
QCoreApplication.translate("PythonConsole", "Search Selected in PyQGIS docs"),
self.searchPyQGIS)
self.searchSelectedTextInPyQGISDocs)
copyAction.setEnabled(False)
pasteAction.setEnabled(False)
pyQGISHelpAction.setEnabled(False)
Expand Down
9 changes: 9 additions & 0 deletions python/gui/auto_generated/qgscodeeditorpython.sip.in
Expand Up @@ -50,6 +50,15 @@ Load APIs from one or more files
Load a script file

:param script: The script file to load
%End

public slots:

void searchSelectedTextInPyQGISDocs();
%Docstring
Searches the selected text in the official PyQGIS online documentation.

.. versionadded:: 3.16
%End

protected:
Expand Down
12 changes: 12 additions & 0 deletions src/gui/qgscodeeditorpython.cpp
Expand Up @@ -25,6 +25,7 @@
#include <QMessageBox>
#include <QTextStream>
#include <Qsci/qscilexerpython.h>
#include <QDesktopServices>

QgsCodeEditorPython::QgsCodeEditorPython( QWidget *parent, const QList<QString> &filenames )
: QgsCodeEditor( parent )
Expand Down Expand Up @@ -139,3 +140,14 @@ bool QgsCodeEditorPython::loadScript( const QString &script )
initializeLexer();
return true;
}

void QgsCodeEditorPython::searchSelectedTextInPyQGISDocs()
{
if ( !hasSelectedText() )
return;

QString text = selectedText();
text = text.replace( QStringLiteral( ">>> " ), QString() ).replace( QStringLiteral( "... " ), QString() ).trimmed(); // removing prompts
const QString version = QString( Qgis::version() ).split( '.' ).mid( 0, 2 ).join( '.' );
QDesktopServices::openUrl( QUrl( QStringLiteral( "https://qgis.org/pyqgis/%1/search.html?q=%2" ).arg( version, text ) ) );
}
9 changes: 9 additions & 0 deletions src/gui/qgscodeeditorpython.h
Expand Up @@ -57,6 +57,15 @@ class GUI_EXPORT QgsCodeEditorPython : public QgsCodeEditor
*/
bool loadScript( const QString &script );

public slots:

/**
* Searches the selected text in the official PyQGIS online documentation.
*
* \since QGIS 3.16
*/
void searchSelectedTextInPyQGISDocs();

protected:

void initializeLexer() override;
Expand Down

0 comments on commit d9782a1

Please sign in to comment.