@@ -41,13 +41,11 @@ def show_console():
41
41
parent = iface .mainWindow () if iface else None
42
42
_console = PythonConsole ( parent )
43
43
_console .show () # force show even if it was restored as hidden
44
-
45
44
# set focus to the console so the user can start typing
46
45
# defer the set focus event so it works also whether the console not visible yet
47
46
QTimer .singleShot (0 , _console .activate )
48
47
else :
49
48
_console .setVisible (not _console .isVisible ())
50
-
51
49
# set focus to the console so the user can start typing
52
50
if _console .isVisible ():
53
51
_console .activate ()
@@ -89,15 +87,15 @@ class PythonConsoleWidget(QWidget):
89
87
def __init__ (self , parent = None ):
90
88
QWidget .__init__ (self , parent )
91
89
self .setWindowTitle (QCoreApplication .translate ("PythonConsole" , "Python Console" ))
92
-
90
+
93
91
self .options = optionsDialog (self )
94
92
self .helpDlg = HelpDialog (self )
95
-
93
+
96
94
self .shell = ShellScintilla (self )
97
95
self .setFocusProxy (self .shell )
98
96
self .shellOut = ShellOutputScintilla (self )
99
97
self .tabEditorWidget = EditorTabWidget (self )
100
-
98
+
101
99
##------------ UI -------------------------------
102
100
103
101
self .splitterEditor = QSplitter (self )
@@ -197,7 +195,27 @@ def __init__(self, parent=None):
197
195
self .runScriptEditorButton .setIconVisibleInMenu (True )
198
196
self .runScriptEditorButton .setToolTip (runScriptEditorBt )
199
197
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
+
201
219
##----------------Toolbar Console-------------------------------------
202
220
203
221
## Action Show Editor
@@ -241,16 +259,6 @@ def __init__(self, parent=None):
241
259
self .actionClass .setIconVisibleInMenu (True )
242
260
self .actionClass .setToolTip (actionClassBt )
243
261
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)
254
262
## Import Sextante class
255
263
loadSextanteBt = QCoreApplication .translate ("PythonConsole" , "Import Sextante class" )
256
264
self .loadSextanteButton = QAction (parent )
@@ -316,14 +324,13 @@ def __init__(self, parent=None):
316
324
self .toolBar .addAction (self .runButton )
317
325
self .toolBar .addSeparator ()
318
326
self .toolBar .addAction (self .showEditorButton )
319
- # self.toolBar.addAction(self.actionScript)
320
327
self .toolBar .addSeparator ()
321
328
self .toolBar .addAction (self .optionsButton )
322
329
self .toolBar .addAction (self .helpButton )
323
-
330
+
324
331
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);} ' )
327
334
self .toolBarEditor .setEnabled (True )
328
335
self .toolBarEditor .setFocusPolicy (Qt .NoFocus )
329
336
self .toolBarEditor .setContextMenuPolicy (Qt .DefaultContextMenu )
@@ -341,63 +348,49 @@ def __init__(self, parent=None):
341
348
self .toolBarEditor .addAction (self .copyEditorButton )
342
349
self .toolBarEditor .addAction (self .pasteEditorButton )
343
350
self .toolBarEditor .addSeparator ()
351
+ self .toolBarEditor .addAction (self .commentEditorButton )
352
+ self .toolBarEditor .addAction (self .uncommentEditorButton )
353
+ self .toolBarEditor .addSeparator ()
344
354
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)
362
355
363
356
self .widgetButton = QWidget ()
364
357
sizePolicy = QSizePolicy (QSizePolicy .Fixed , QSizePolicy .Preferred )
365
358
sizePolicy .setHorizontalStretch (0 )
366
359
sizePolicy .setVerticalStretch (0 )
367
360
sizePolicy .setHeightForWidth (self .widgetButton .sizePolicy ().hasHeightForWidth ())
368
361
self .widgetButton .setSizePolicy (sizePolicy )
369
-
362
+
370
363
self .widgetButtonEditor = QWidget (self .splitterEditor )
371
364
sizePolicy = QSizePolicy (QSizePolicy .Fixed , QSizePolicy .Preferred )
372
365
sizePolicy .setHorizontalStretch (0 )
373
366
sizePolicy .setVerticalStretch (0 )
374
367
sizePolicy .setHeightForWidth (self .widgetButtonEditor .sizePolicy ().hasHeightForWidth ())
375
368
self .widgetButtonEditor .setSizePolicy (sizePolicy )
376
-
369
+
377
370
sizePolicy = QSizePolicy (QSizePolicy .Expanding , QSizePolicy .Expanding )
378
371
sizePolicy .setHorizontalStretch (0 )
379
372
sizePolicy .setVerticalStretch (0 )
380
373
sizePolicy .setHeightForWidth (self .shellOut .sizePolicy ().hasHeightForWidth ())
381
374
self .shellOut .setSizePolicy (sizePolicy )
382
-
375
+
383
376
self .shellOut .setVerticalScrollBarPolicy (Qt .ScrollBarAsNeeded )
384
377
self .shell .setVerticalScrollBarPolicy (Qt .ScrollBarAsNeeded )
385
378
386
379
##------------ Layout -------------------------------
387
-
380
+
388
381
self .mainLayout = QGridLayout (self )
389
382
self .mainLayout .setMargin (0 )
390
383
self .mainLayout .setSpacing (0 )
391
384
self .mainLayout .addWidget (self .widgetButton , 0 , 0 , 1 , 1 )
392
385
self .mainLayout .addWidget (self .widgetButtonEditor , 0 , 1 , 1 , 1 )
393
386
self .mainLayout .addWidget (self .splitterEditor , 0 , 2 , 1 , 1 )
394
-
387
+
395
388
self .layoutEditor = QGridLayout (self .widgetEditor )
396
389
self .layoutEditor .setMargin (0 )
397
390
self .layoutEditor .setSpacing (0 )
398
391
self .layoutEditor .addWidget (self .widgetButtonEditor , 0 , 0 , 1 , 1 )
399
392
self .layoutEditor .addWidget (self .tabEditorWidget , 0 , 1 , 1 , 1 )
400
-
393
+
401
394
self .toolBarLayout = QGridLayout (self .widgetButton )
402
395
self .toolBarLayout .setMargin (0 )
403
396
self .toolBarLayout .setSpacing (0 )
@@ -406,13 +399,15 @@ def __init__(self, parent=None):
406
399
self .toolBarEditorLayout .setMargin (0 )
407
400
self .toolBarEditorLayout .setSpacing (0 )
408
401
self .toolBarEditorLayout .addWidget (self .toolBarEditor )
409
-
402
+
410
403
##------------ Add first Tab in Editor -------------------------------
411
-
404
+
412
405
#self.tabEditorWidget.newTabEditor(tabName='first', filename=None)
413
-
406
+
414
407
##------------ Signal -------------------------------
415
408
409
+ self .commentEditorButton .triggered .connect (self .commentCode )
410
+ self .uncommentEditorButton .triggered .connect (self .uncommentCode )
416
411
self .runScriptEditorButton .triggered .connect (self .runScriptEditor )
417
412
self .cutEditorButton .triggered .connect (self .cutEditor )
418
413
self .copyEditorButton .triggered .connect (self .copyEditor )
@@ -439,25 +434,31 @@ def qtCore(self):
439
434
440
435
def qtGui (self ):
441
436
self .shell .commandConsole ('qtGui' )
442
-
437
+
443
438
def toggleEditor (self , checked ):
444
439
self .widgetEditor .show () if checked else self .widgetEditor .hide ()
445
440
self .openFileButton .setEnabled (checked )
446
441
self .saveFileButton .setEnabled (checked )
447
442
self .saveAsFileButton .setEnabled (checked )
448
-
443
+
449
444
def pasteEditor (self ):
450
445
self .tabEditorWidget .currentWidget ().newEditor .paste ()
451
-
446
+
452
447
def cutEditor (self ):
453
448
self .tabEditorWidget .currentWidget ().newEditor .cut ()
454
-
449
+
455
450
def copyEditor (self ):
456
451
self .tabEditorWidget .currentWidget ().newEditor .copy ()
457
-
452
+
458
453
def runScriptEditor (self ):
459
454
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
+
461
462
# def openScriptFile(self):
462
463
# settings = QSettings()
463
464
# lastDirPath = settings.value("pythonConsole/lastDirPath").toString()
@@ -498,7 +499,7 @@ def runScriptEditor(self):
498
499
# sF.write(s)
499
500
# sF.close()
500
501
# self.callWidgetMessageBar('Script was correctly saved.')
501
-
502
+
502
503
def openScriptFile (self ):
503
504
settings = QSettings ()
504
505
lastDirPath = settings .value ("pythonConsole/lastDirPath" ).toString ()
@@ -518,15 +519,15 @@ def openScriptFile(self):
518
519
settings .setValue ("pythonConsole/lastDirPath" , QVariant (filename ))
519
520
self .tabListScript .append (filename )
520
521
self .updateTabListScript (script = None )
521
-
522
+
522
523
def saveScriptFile (self ):
523
524
tabWidget = self .tabEditorWidget .currentWidget ()
524
525
try :
525
526
tabWidget .save ()
526
527
except (IOError , OSError ), e :
527
528
QMessageBox .warning (self , "Save Error" ,
528
529
"Failed to save %s: %s" % (tabWidget .path , e ))
529
-
530
+
530
531
def saveAsScriptFile (self ):
531
532
tabWidget = self .tabEditorWidget .currentWidget ()
532
533
if tabWidget is None :
@@ -553,21 +554,21 @@ def prefChanged(self):
553
554
554
555
def callWidgetMessageBar (self , text ):
555
556
self .shellOut .widgetMessageBar (iface , text )
556
-
557
+
557
558
def callWidgetMessageBarEditor (self , text ):
558
559
self .tabEditorWidget .widgetMessageBar (iface , text )
559
-
560
+
560
561
def updateTabListScript (self , script , action = None ):
561
562
if script == 'empty' :
562
563
self .tabListScript = []
563
564
if script is not None and not action and script != 'empty' :
564
565
self .tabListScript .remove (script )
565
566
if action :
566
- self .tabListScript .append (script )
567
+ if script not in self .tabListScript :
568
+ self .tabListScript .append (script )
567
569
self .settings .setValue ("pythonConsole/tabScripts" ,
568
570
QVariant (self .tabListScript ))
569
-
570
-
571
+
571
572
if __name__ == '__main__' :
572
573
a = QApplication (sys .argv )
573
574
console = PythonConsoleWidget ()
0 commit comments