Skip to content

Commit 9ab8361

Browse files
committed
[pyqgis-console] added button for comment/uncomment code in side editor and minor fixes
1 parent cba952c commit 9ab8361

File tree

5 files changed

+111
-70
lines changed

5 files changed

+111
-70
lines changed
Loading
Loading

python/console/console.py

+61-60
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ def show_console():
4141
parent = iface.mainWindow() if iface else None
4242
_console = PythonConsole( parent )
4343
_console.show() # force show even if it was restored as hidden
44-
4544
# set focus to the console so the user can start typing
4645
# defer the set focus event so it works also whether the console not visible yet
4746
QTimer.singleShot(0, _console.activate)
4847
else:
4948
_console.setVisible(not _console.isVisible())
50-
5149
# set focus to the console so the user can start typing
5250
if _console.isVisible():
5351
_console.activate()
@@ -89,15 +87,15 @@ class PythonConsoleWidget(QWidget):
8987
def __init__(self, parent=None):
9088
QWidget.__init__(self, parent)
9189
self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console"))
92-
90+
9391
self.options = optionsDialog(self)
9492
self.helpDlg = HelpDialog(self)
95-
93+
9694
self.shell = ShellScintilla(self)
9795
self.setFocusProxy(self.shell)
9896
self.shellOut = ShellOutputScintilla(self)
9997
self.tabEditorWidget = EditorTabWidget(self)
100-
98+
10199
##------------ UI -------------------------------
102100

103101
self.splitterEditor = QSplitter(self)
@@ -197,7 +195,27 @@ def __init__(self, parent=None):
197195
self.runScriptEditorButton.setIconVisibleInMenu(True)
198196
self.runScriptEditorButton.setToolTip(runScriptEditorBt)
199197
self.runScriptEditorButton.setText(runScriptEditorBt)
200-
198+
## Action Run Script (subprocess)
199+
commentEditorBt = QCoreApplication.translate("PythonConsole", "Comment code")
200+
self.commentEditorButton = QAction(parent)
201+
self.commentEditorButton.setCheckable(False)
202+
self.commentEditorButton.setEnabled(True)
203+
self.commentEditorButton.setIcon(QgsApplication.getThemeIcon("console/iconCommentEditorConsole.png"))
204+
self.commentEditorButton.setMenuRole(QAction.PreferencesRole)
205+
self.commentEditorButton.setIconVisibleInMenu(True)
206+
self.commentEditorButton.setToolTip(commentEditorBt)
207+
self.commentEditorButton.setText(commentEditorBt)
208+
## Action Run Script (subprocess)
209+
uncommentEditorBt = QCoreApplication.translate("PythonConsole", "Uncomment code")
210+
self.uncommentEditorButton = QAction(parent)
211+
self.uncommentEditorButton.setCheckable(False)
212+
self.uncommentEditorButton.setEnabled(True)
213+
self.uncommentEditorButton.setIcon(QgsApplication.getThemeIcon("console/iconUncommentEditorConsole.png"))
214+
self.uncommentEditorButton.setMenuRole(QAction.PreferencesRole)
215+
self.uncommentEditorButton.setIconVisibleInMenu(True)
216+
self.uncommentEditorButton.setToolTip(uncommentEditorBt)
217+
self.uncommentEditorButton.setText(uncommentEditorBt)
218+
201219
##----------------Toolbar Console-------------------------------------
202220

203221
## Action Show Editor
@@ -241,16 +259,6 @@ def __init__(self, parent=None):
241259
self.actionClass.setIconVisibleInMenu(True)
242260
self.actionClass.setToolTip(actionClassBt)
243261
self.actionClass.setText(actionClassBt)
244-
## Action menu Open/Save script
245-
# actionScriptBt = QCoreApplication.translate("PythonConsole", "Manage Script")
246-
# self.actionScript = QAction(parent)
247-
# self.actionScript.setCheckable(False)
248-
# self.actionScript.setEnabled(True)
249-
# self.actionScript.setIcon(QgsApplication.getThemeIcon("console/iconScriptConsole.png"))
250-
# self.actionScript.setMenuRole(QAction.PreferencesRole)
251-
# self.actionScript.setIconVisibleInMenu(True)
252-
# self.actionScript.setToolTip(actionScriptBt)
253-
# self.actionScript.setText(actionScriptBt)
254262
## Import Sextante class
255263
loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import Sextante class")
256264
self.loadSextanteButton = QAction(parent)
@@ -316,14 +324,13 @@ def __init__(self, parent=None):
316324
self.toolBar.addAction(self.runButton)
317325
self.toolBar.addSeparator()
318326
self.toolBar.addAction(self.showEditorButton)
319-
# self.toolBar.addAction(self.actionScript)
320327
self.toolBar.addSeparator()
321328
self.toolBar.addAction(self.optionsButton)
322329
self.toolBar.addAction(self.helpButton)
323-
330+
324331
self.toolBarEditor = QToolBar()
325-
self.toolBarEditor.setStyleSheet('background-color: rgb(200, 200, 200);\
326-
border-right: 1px solid rgb(155, 155, 155);')
332+
self.toolBarEditor.setStyleSheet('QToolBar{background-color: rgb(200, 200, 200);\
333+
border-right: 1px solid rgb(155, 155, 155);}')
327334
self.toolBarEditor.setEnabled(True)
328335
self.toolBarEditor.setFocusPolicy(Qt.NoFocus)
329336
self.toolBarEditor.setContextMenuPolicy(Qt.DefaultContextMenu)
@@ -341,63 +348,49 @@ def __init__(self, parent=None):
341348
self.toolBarEditor.addAction(self.copyEditorButton)
342349
self.toolBarEditor.addAction(self.pasteEditorButton)
343350
self.toolBarEditor.addSeparator()
351+
self.toolBarEditor.addAction(self.commentEditorButton)
352+
self.toolBarEditor.addAction(self.uncommentEditorButton)
353+
self.toolBarEditor.addSeparator()
344354
self.toolBarEditor.addAction(self.runScriptEditorButton)
345-
346-
## Menu Import Class
347-
# self.classMenu = QMenu(self)
348-
# self.classMenu.addAction(self.loadSextanteButton)
349-
# self.classMenu.addAction(self.loadQtCoreButton)
350-
# self.classMenu.addAction(self.loadQtGuiButton)
351-
# cM = self.toolBar.widgetForAction(self.actionClass)
352-
# cM.setMenu(self.classMenu)
353-
# cM.setPopupMode(QToolButton.InstantPopup)
354-
# ## Menu Manage Script
355-
# self.scriptMenu = QMenu(self)
356-
# self.scriptMenu.addAction(self.openFileButton)
357-
# self.scriptMenu.addAction(self.saveFileButton)
358-
# self.scriptMenu.addAction(self.saveAsFileButton)
359-
# sM = self.toolBar.widgetForAction(self.actionScript)
360-
# sM.setMenu(self.scriptMenu)
361-
# sM.setPopupMode(QToolButton.InstantPopup)
362355

363356
self.widgetButton = QWidget()
364357
sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred)
365358
sizePolicy.setHorizontalStretch(0)
366359
sizePolicy.setVerticalStretch(0)
367360
sizePolicy.setHeightForWidth(self.widgetButton.sizePolicy().hasHeightForWidth())
368361
self.widgetButton.setSizePolicy(sizePolicy)
369-
362+
370363
self.widgetButtonEditor = QWidget(self.splitterEditor)
371364
sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred)
372365
sizePolicy.setHorizontalStretch(0)
373366
sizePolicy.setVerticalStretch(0)
374367
sizePolicy.setHeightForWidth(self.widgetButtonEditor.sizePolicy().hasHeightForWidth())
375368
self.widgetButtonEditor.setSizePolicy(sizePolicy)
376-
369+
377370
sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
378371
sizePolicy.setHorizontalStretch(0)
379372
sizePolicy.setVerticalStretch(0)
380373
sizePolicy.setHeightForWidth(self.shellOut.sizePolicy().hasHeightForWidth())
381374
self.shellOut.setSizePolicy(sizePolicy)
382-
375+
383376
self.shellOut.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
384377
self.shell.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
385378

386379
##------------ Layout -------------------------------
387-
380+
388381
self.mainLayout = QGridLayout(self)
389382
self.mainLayout.setMargin(0)
390383
self.mainLayout.setSpacing(0)
391384
self.mainLayout.addWidget(self.widgetButton, 0, 0, 1, 1)
392385
self.mainLayout.addWidget(self.widgetButtonEditor, 0, 1, 1, 1)
393386
self.mainLayout.addWidget(self.splitterEditor, 0, 2, 1, 1)
394-
387+
395388
self.layoutEditor = QGridLayout(self.widgetEditor)
396389
self.layoutEditor.setMargin(0)
397390
self.layoutEditor.setSpacing(0)
398391
self.layoutEditor.addWidget(self.widgetButtonEditor, 0, 0, 1, 1)
399392
self.layoutEditor.addWidget(self.tabEditorWidget, 0, 1, 1, 1)
400-
393+
401394
self.toolBarLayout = QGridLayout(self.widgetButton)
402395
self.toolBarLayout.setMargin(0)
403396
self.toolBarLayout.setSpacing(0)
@@ -406,13 +399,15 @@ def __init__(self, parent=None):
406399
self.toolBarEditorLayout.setMargin(0)
407400
self.toolBarEditorLayout.setSpacing(0)
408401
self.toolBarEditorLayout.addWidget(self.toolBarEditor)
409-
402+
410403
##------------ Add first Tab in Editor -------------------------------
411-
404+
412405
#self.tabEditorWidget.newTabEditor(tabName='first', filename=None)
413-
406+
414407
##------------ Signal -------------------------------
415408

409+
self.commentEditorButton.triggered.connect(self.commentCode)
410+
self.uncommentEditorButton.triggered.connect(self.uncommentCode)
416411
self.runScriptEditorButton.triggered.connect(self.runScriptEditor)
417412
self.cutEditorButton.triggered.connect(self.cutEditor)
418413
self.copyEditorButton.triggered.connect(self.copyEditor)
@@ -439,25 +434,31 @@ def qtCore(self):
439434

440435
def qtGui(self):
441436
self.shell.commandConsole('qtGui')
442-
437+
443438
def toggleEditor(self, checked):
444439
self.widgetEditor.show() if checked else self.widgetEditor.hide()
445440
self.openFileButton.setEnabled(checked)
446441
self.saveFileButton.setEnabled(checked)
447442
self.saveAsFileButton.setEnabled(checked)
448-
443+
449444
def pasteEditor(self):
450445
self.tabEditorWidget.currentWidget().newEditor.paste()
451-
446+
452447
def cutEditor(self):
453448
self.tabEditorWidget.currentWidget().newEditor.cut()
454-
449+
455450
def copyEditor(self):
456451
self.tabEditorWidget.currentWidget().newEditor.copy()
457-
452+
458453
def runScriptEditor(self):
459454
self.tabEditorWidget.currentWidget().newEditor.runScriptCode()
460-
455+
456+
def commentCode(self):
457+
self.tabEditorWidget.currentWidget().newEditor.commentEditorCode(True)
458+
459+
def uncommentCode(self):
460+
self.tabEditorWidget.currentWidget().newEditor.commentEditorCode(False)
461+
461462
# def openScriptFile(self):
462463
# settings = QSettings()
463464
# lastDirPath = settings.value("pythonConsole/lastDirPath").toString()
@@ -498,7 +499,7 @@ def runScriptEditor(self):
498499
# sF.write(s)
499500
# sF.close()
500501
# self.callWidgetMessageBar('Script was correctly saved.')
501-
502+
502503
def openScriptFile(self):
503504
settings = QSettings()
504505
lastDirPath = settings.value("pythonConsole/lastDirPath").toString()
@@ -518,15 +519,15 @@ def openScriptFile(self):
518519
settings.setValue("pythonConsole/lastDirPath", QVariant(filename))
519520
self.tabListScript.append(filename)
520521
self.updateTabListScript(script=None)
521-
522+
522523
def saveScriptFile(self):
523524
tabWidget = self.tabEditorWidget.currentWidget()
524525
try:
525526
tabWidget.save()
526527
except (IOError, OSError), e:
527528
QMessageBox.warning(self, "Save Error",
528529
"Failed to save %s: %s" % (tabWidget.path, e))
529-
530+
530531
def saveAsScriptFile(self):
531532
tabWidget = self.tabEditorWidget.currentWidget()
532533
if tabWidget is None:
@@ -553,21 +554,21 @@ def prefChanged(self):
553554

554555
def callWidgetMessageBar(self, text):
555556
self.shellOut.widgetMessageBar(iface, text)
556-
557+
557558
def callWidgetMessageBarEditor(self, text):
558559
self.tabEditorWidget.widgetMessageBar(iface, text)
559-
560+
560561
def updateTabListScript(self, script, action=None):
561562
if script == 'empty':
562563
self.tabListScript = []
563564
if script is not None and not action and script != 'empty':
564565
self.tabListScript.remove(script)
565566
if action:
566-
self.tabListScript.append(script)
567+
if script not in self.tabListScript:
568+
self.tabListScript.append(script)
567569
self.settings.setValue("pythonConsole/tabScripts",
568570
QVariant(self.tabListScript))
569-
570-
571+
571572
if __name__ == '__main__':
572573
a = QApplication(sys.argv)
573574
console = PythonConsoleWidget()

python/console/console_editor.py

+49-5
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ def setLexers(self):
207207

208208
self.setLexer(self.lexer)
209209

210+
def move_cursor_to_end(self):
211+
"""Move cursor to end of text"""
212+
line, index = self.get_end_pos()
213+
self.setCursorPosition(line, index)
214+
self.ensureCursorVisible()
215+
self.ensureLineVisible(line)
216+
217+
def get_end_pos(self):
218+
"""Return (line, index) position of the last character"""
219+
line = self.lines() - 1
220+
return (line, self.text(line).length())
221+
210222
def contextMenuEvent(self, e):
211223
menu = QMenu(self)
212224
iconRun = QgsApplication.getThemeIcon("console/iconRunConsole.png")
@@ -320,12 +332,44 @@ def codepad(self):
320332
self.parent.pc.callWidgetMessageBarEditor(msgText + str(e.args))
321333

322334
def hideEditor(self):
323-
Ed = self.parent.pc.tabEditorWidget
335+
Ed = self.parent.pc.widgetEditor
324336
Ed.hide()
325-
self.parent.pc.openFileButton.setEnabled(False)
326-
self.parent.pc.saveFileButton.setEnabled(False)
327-
self.parent.pc.saveAsFileButton.setEnabled(False)
328-
self.parent.pc.showEditorButton.setChecked(False)
337+
338+
def commentEditorCode(self, commentCheck):
339+
if self.hasSelectedText():
340+
startLine, _, endLine, _ = self.getSelection()
341+
for line in range(startLine, endLine + 1):
342+
selCmd = self.text(line)
343+
self.setSelection(line, 0, line, selCmd.length())
344+
self.removeSelectedText()
345+
if commentCheck:
346+
self.insert('#' + selCmd)
347+
self.setCursorPosition(endLine, selCmd.length())
348+
else:
349+
if selCmd.startsWith('#'):
350+
self.insert(selCmd[1:])
351+
else:
352+
self.insert(selCmd)
353+
self.setCursorPosition(endLine, selCmd.length() - 2)
354+
355+
else:
356+
line, pos = self.getCursorPosition()
357+
selCmd = self.text(line)
358+
self.setSelection(line, 0, line, selCmd.length())
359+
self.removeSelectedText()
360+
if commentCheck:
361+
self.insert('#' + selCmd)
362+
self.setCursorPosition(line, selCmd.length())
363+
else:
364+
if selCmd.startsWith('#'):
365+
self.insert(selCmd[1:])
366+
else:
367+
self.insert(selCmd)
368+
self.setCursorPosition(line, selCmd.length() - 2)
369+
370+
371+
def uncommentEditorCode(self):
372+
pass
329373

330374
def runScriptCode(self):
331375
tabWidget = self.parent.mw.currentWidget()

python/console/console_output.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,9 @@ def hideToolBar(self):
221221
self.shell.setFocus()
222222

223223
def showEditor(self):
224-
Ed = self.parent.tabEditorWidget
224+
Ed = self.parent.widgetEditor
225225
if not Ed.isVisible():
226226
Ed.show()
227-
self.parent.openFileButton.setEnabled(True)
228-
self.parent.saveFileButton.setEnabled(True)
229-
self.parent.saveAsFileButton.setEnabled(True)
230-
self.parent.showEditorButton.setChecked(True)
231227
self.shell.setFocus()
232228

233229
def copy(self):

0 commit comments

Comments
 (0)