Skip to content

Commit

Permalink
[feature][console] Add toggle comment action in the python console (#…
Browse files Browse the repository at this point in the history
…50341)

Adds a toggle comment action in the Python Console and script editors
  • Loading branch information
YoannQDQ committed Jan 6, 2023
1 parent 675933a commit 1c79b49
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 83 deletions.
54 changes: 16 additions & 38 deletions python/console/console.py
Expand Up @@ -110,7 +110,6 @@ def __init__(self, parent=None):
super().__init__(parent)
self.setObjectName("PythonConsole")
self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console"))
# self.setAllowedAreas(Qt.BottomDockWidgetArea)

self.console = PythonConsoleWidget(self)
QgsGui.instance().optionsChanged.connect(self.console.updateSettings)
Expand Down Expand Up @@ -167,13 +166,9 @@ def __init__(self, parent=None):
self.splitter.addWidget(self.shellOutWidget)
self.splitter.addWidget(self.shell)

# self.splitterEditor.addWidget(self.tabEditorWidget)

self.splitterObj = QSplitter(self.splitterEditor)
self.splitterObj.setHandleWidth(3)
self.splitterObj.setOrientation(Qt.Horizontal)
# self.splitterObj.setSizes([0, 0])
# self.splitterObj.setStretchFactor(0, 1)

self.widgetEditor = QWidget(self.splitterObj)
self.widgetFind = QWidget(self)
Expand All @@ -185,10 +180,6 @@ def __init__(self, parent=None):
self.listClassMethod.setColumnHidden(1, True)
self.listClassMethod.setAlternatingRowColors(True)

# self.splitterEditor.addWidget(self.widgetEditor)
# self.splitterObj.addWidget(self.listClassMethod)
# self.splitterObj.addWidget(self.widgetEditor)

# Hide side editor on start up
self.splitterObj.hide()
self.listClassMethod.hide()
Expand Down Expand Up @@ -286,26 +277,18 @@ def __init__(self, parent=None):
self.runScriptEditorButton.setIconVisibleInMenu(True)
self.runScriptEditorButton.setToolTip(runScriptEditorBt)
self.runScriptEditorButton.setText(runScriptEditorBt)
# Action Run Script (subprocess)
commentEditorBt = QCoreApplication.translate("PythonConsole", "Comment")
self.commentEditorButton = QAction(self)
self.commentEditorButton.setCheckable(False)
self.commentEditorButton.setEnabled(True)
self.commentEditorButton.setIcon(QgsApplication.getThemeIcon("console/iconCommentEditorConsole.svg"))
self.commentEditorButton.setMenuRole(QAction.PreferencesRole)
self.commentEditorButton.setIconVisibleInMenu(True)
self.commentEditorButton.setToolTip(commentEditorBt)
self.commentEditorButton.setText(commentEditorBt)
# Action Run Script (subprocess)
uncommentEditorBt = QCoreApplication.translate("PythonConsole", "Uncomment")
self.uncommentEditorButton = QAction(self)
self.uncommentEditorButton.setCheckable(False)
self.uncommentEditorButton.setEnabled(True)
self.uncommentEditorButton.setIcon(QgsApplication.getThemeIcon("console/iconUncommentEditorConsole.svg"))
self.uncommentEditorButton.setMenuRole(QAction.PreferencesRole)
self.uncommentEditorButton.setIconVisibleInMenu(True)
self.uncommentEditorButton.setToolTip(uncommentEditorBt)
self.uncommentEditorButton.setText(uncommentEditorBt)

# Action Toggle comment
toggleText = QCoreApplication.translate("PythonConsole", "Toggle Comment")
self.toggleCommentEditorButton = QAction(self)
self.toggleCommentEditorButton.setCheckable(False)
self.toggleCommentEditorButton.setEnabled(True)
self.toggleCommentEditorButton.setIcon(QgsApplication.getThemeIcon("console/iconCommentEditorConsole.svg"))
self.toggleCommentEditorButton.setMenuRole(QAction.PreferencesRole)
self.toggleCommentEditorButton.setIconVisibleInMenu(True)
self.toggleCommentEditorButton.setToolTip(toggleText + " <b>Ctrl+:</b>")
self.toggleCommentEditorButton.setText(toggleText)

# Action for Object browser
objList = QCoreApplication.translate("PythonConsole", "Object Inspector…")
self.objectListButton = QAction(self)
Expand Down Expand Up @@ -433,8 +416,7 @@ def __init__(self, parent=None):
self.toolBarEditor.addSeparator()
self.toolBarEditor.addAction(self.findTextButton)
self.toolBarEditor.addSeparator()
self.toolBarEditor.addAction(self.commentEditorButton)
self.toolBarEditor.addAction(self.uncommentEditorButton)
self.toolBarEditor.addAction(self.toggleCommentEditorButton)
self.toolBarEditor.addSeparator()
self.toolBarEditor.addAction(self.objectListButton)

Expand Down Expand Up @@ -527,8 +509,7 @@ def __init__(self, parent=None):

self.findTextButton.triggered.connect(self._toggleFind)
self.objectListButton.toggled.connect(self.toggleObjectListWidget)
self.commentEditorButton.triggered.connect(self.commentCode)
self.uncommentEditorButton.triggered.connect(self.uncommentCode)
self.toggleCommentEditorButton.triggered.connect(self.toggleComment)
self.runScriptEditorButton.triggered.connect(self.runScriptEditor)
self.cutEditorButton.triggered.connect(self.cutEditor)
self.copyEditorButton.triggered.connect(self.copyEditor)
Expand Down Expand Up @@ -657,11 +638,8 @@ def copyEditor(self):
def runScriptEditor(self):
self.tabEditorWidget.currentWidget().newEditor.runScriptCode()

def commentCode(self):
self.tabEditorWidget.currentWidget().newEditor.commentEditorCode(True)

def uncommentCode(self):
self.tabEditorWidget.currentWidget().newEditor.commentEditorCode(False)
def toggleComment(self):
self.tabEditorWidget.currentWidget().newEditor.toggleComment()

def openScriptFileExtEditor(self):
tabWidget = self.tabEditorWidget.currentWidget()
Expand Down
42 changes: 3 additions & 39 deletions python/console/console_editor.py
Expand Up @@ -122,12 +122,6 @@ def __init__(self, parent=None):
self.syntaxCheckScut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_4), self)
self.syntaxCheckScut.setContext(Qt.WidgetShortcut)
self.syntaxCheckScut.activated.connect(self.syntaxCheck)
self.commentScut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_3), self)
self.commentScut.setContext(Qt.WidgetShortcut)
self.commentScut.activated.connect(self.parent.pc.commentCode)
self.uncommentScut = QShortcut(QKeySequence(Qt.SHIFT + Qt.CTRL + Qt.Key_3), self)
self.uncommentScut.setContext(Qt.WidgetShortcut)
self.uncommentScut.activated.connect(self.parent.pc.uncommentCode)
self.modificationChanged.connect(self.parent.modified)
self.modificationAttempted.connect(self.fileReadOnly)

Expand Down Expand Up @@ -178,11 +172,8 @@ def contextMenuEvent(self, e):
self.selectAll, QKeySequence.SelectAll)
menu.addSeparator()
menu.addAction(QgsApplication.getThemeIcon("console/iconCommentEditorConsole.svg"),
QCoreApplication.translate("PythonConsole", "Comment"),
self.parent.pc.commentCode, 'Ctrl+3')
menu.addAction(QgsApplication.getThemeIcon("console/iconUncommentEditorConsole.svg"),
QCoreApplication.translate("PythonConsole", "Uncomment"),
self.parent.pc.uncommentCode, 'Shift+Ctrl+3')
QCoreApplication.translate("PythonConsole", "Toggle Comment"),
self.toggleComment, 'Ctrl+:')
menu.addSeparator()
gist_menu = QMenu(self)
gist_menu.setTitle(QCoreApplication.translate("PythonConsole", "Share on GitHub"))
Expand Down Expand Up @@ -338,31 +329,6 @@ def toggleFindWidget(self):
else:
self.openFindWidget()

def commentEditorCode(self, commentCheck):
self.beginUndoAction()
if self.hasSelectedText():
startLine, _, endLine, _ = self.getSelection()
for line in range(startLine, endLine + 1):
if commentCheck:
self.insertAt('#', line, 0)
else:
if not self.text(line).strip().startswith('#'):
continue
self.setSelection(line, self.indentation(line),
line, self.indentation(line) + 1)
self.removeSelectedText()
else:
line, pos = self.getCursorPosition()
if commentCheck:
self.insertAt('#', line, 0)
else:
if not self.text(line).strip().startswith('#'):
return
self.setSelection(line, self.indentation(line),
line, self.indentation(line) + 1)
self.removeSelectedText()
self.endUndoAction()

def createTempFile(self):
import tempfile
fd, path = tempfile.mkstemp()
Expand Down Expand Up @@ -547,7 +513,7 @@ def keyPressEvent(self, e):
if re.match(ptrn, txt):
self.insert(' import')
self.setCursorPosition(line, pos + 7)
QsciScintilla.keyPressEvent(self, e)
QgsCodeEditorPython.keyPressEvent(self, e)

def focusInEvent(self, e):
pathfile = self.parent.path
Expand All @@ -560,7 +526,6 @@ def focusInEvent(self, e):
if pathfile and self.lastModified != QFileInfo(pathfile).lastModified():
self.beginUndoAction()
self.selectAll()
# fileReplaced = self.selectedText()
self.removeSelectedText()
file = open(pathfile, "r")
fileLines = file.readlines()
Expand Down Expand Up @@ -658,7 +623,6 @@ def save(self, fileName=None):
if overwrite:
try:
permis = os.stat(path).st_mode
# self.newEditor.lastModified = QFileInfo(path).lastModified()
os.chmod(path, permis)
except:
raise
Expand Down
Expand Up @@ -62,13 +62,22 @@ Loads a ``script`` file.
Searches the selected text in the official PyQGIS online documentation.

.. versionadded:: 3.16
%End

void toggleComment();
%Docstring
Toggle comment for the selected text.

.. versionadded:: 3.30
%End

protected:

virtual void initializeLexer();


virtual void keyPressEvent( QKeyEvent *event );

protected slots:

void autoComplete();
Expand Down
3 changes: 3 additions & 0 deletions python/plugins/processing/script/ScriptEditorDialog.py
Expand Up @@ -92,6 +92,8 @@ def __init__(self, filePath=None, parent=None):
QgsApplication.getThemeIcon('/mActionIncreaseFont.svg'))
self.actionDecreaseFontSize.setIcon(
QgsApplication.getThemeIcon('/mActionDecreaseFont.svg'))
self.actionToggleComment.setIcon(
QgsApplication.getThemeIcon('console/iconCommentEditorConsole.svg'))

# Connect signals and slots
self.actionOpenScript.triggered.connect(self.openScript)
Expand All @@ -106,6 +108,7 @@ def __init__(self, filePath=None, parent=None):
self.actionFindReplace.toggled.connect(self.toggleSearchBox)
self.actionIncreaseFontSize.triggered.connect(self.editor.zoomIn)
self.actionDecreaseFontSize.triggered.connect(self.editor.zoomOut)
self.actionToggleComment.triggered.connect(self.editor.toggleComment)
self.editor.textChanged.connect(lambda: self.setHasChanged(True))

self.leFindText.returnPressed.connect(self.find)
Expand Down
11 changes: 9 additions & 2 deletions python/plugins/processing/ui/DlgScriptEditor.ui
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>721</width>
<width>600</width>
<height>578</height>
</rect>
</property>
Expand Down Expand Up @@ -120,6 +120,8 @@
<addaction name="separator"/>
<addaction name="actionIncreaseFontSize"/>
<addaction name="actionDecreaseFontSize"/>
<addaction name="separator"/>
<addaction name="actionToggleComment"/>
</widget>
<action name="actionOpenScript">
<property name="text">
Expand Down Expand Up @@ -240,11 +242,16 @@
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Find &amp;&amp; &amp;Replace</string>
</property>
<property name="shortcut">
<string>Ctrl+F</string>
</property>
</action>
<action name="actionToggleComment">
<property name="text">
<string>Find &amp;&amp; &amp;Replace</string>
<string>Toggle Comment</string>
</property>
</action>
</widget>
Expand Down
2 changes: 1 addition & 1 deletion src/app/maptools/qgsmaptoolshaperegularpolygonabstract.cpp
Expand Up @@ -21,7 +21,7 @@
#include "qgisapp.h"
#include "qgsmaptoolcapture.h"

QgsMapToolShapeRegularPolygonAbstract::QgsMapToolShapeRegularPolygonAbstract(const QString &id, QgsMapToolCapture *parentTool )
QgsMapToolShapeRegularPolygonAbstract::QgsMapToolShapeRegularPolygonAbstract( const QString &id, QgsMapToolCapture *parentTool )
: QgsMapToolShapeAbstract( id, parentTool )
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/maptools/qgsmaptoolshaperegularpolygonabstract.h
Expand Up @@ -28,7 +28,7 @@ class APP_EXPORT QgsMapToolShapeRegularPolygonAbstract: public QgsMapToolShapeAb
Q_OBJECT

public:
QgsMapToolShapeRegularPolygonAbstract(const QString &id, QgsMapToolCapture *parentTool);
QgsMapToolShapeRegularPolygonAbstract( const QString &id, QgsMapToolCapture *parentTool );

void clean() override;

Expand Down

0 comments on commit 1c79b49

Please sign in to comment.