31
31
import code
32
32
33
33
from qgis .core import QgsApplication
34
+ from ui_console_history_dlg import Ui_HistoryDialog
34
35
35
36
_init_commands = ["from qgis.core import *" , "import qgis.utils" ,
36
37
"from qgis.utils import iface" ]
@@ -69,6 +70,8 @@ def __init__(self, parent=None):
69
70
# Read history command file
70
71
self .readHistoryFile ()
71
72
73
+ self .historyDlg = HistoryDialog (self )
74
+
72
75
# Brace matching: enable for a brace immediately before or after
73
76
# the current position
74
77
self .setBraceMatching (QsciScintilla .SloppyBraceMatch )
@@ -107,8 +110,6 @@ def __init__(self, parent=None):
107
110
self .newShortcutCAS .setContext (Qt .WidgetShortcut )
108
111
self .newShortcutCAS .activated .connect (self .autoCompleteKeyBinding )
109
112
self .newShortcutCSS .activated .connect (self .showHistory )
110
- self .connect (self , SIGNAL ('userListActivated(int, const QString)' ),
111
- self .completion_list_selected )
112
113
113
114
def settingsShell (self ):
114
115
# Set Python lexer
@@ -129,7 +130,10 @@ def settingsShell(self):
129
130
self .setAutoCompletionSource (self .AcsNone )
130
131
131
132
def showHistory (self ):
132
- self .showUserList (1 , QStringList (self .history ))
133
+ if not self .historyDlg .isVisible ():
134
+ self .historyDlg .show ()
135
+ self .historyDlg ._reloadHistory ()
136
+ self .historyDlg .activateWindow ()
133
137
134
138
def autoCompleteKeyBinding (self ):
135
139
radioButtonSource = self .settings .value ("pythonConsole/autoCompleteSource" ).toString ()
@@ -199,17 +203,6 @@ def setLexers(self):
199
203
200
204
## TODO: show completion list for file and directory
201
205
202
- def completion_list_selected (self , id , txt ):
203
- if id == 1 :
204
- txt = unicode (txt )
205
- # get current cursor position
206
- line , pos = self .getCursorPosition ()
207
- selCmdLength = self .text (line ).length ()
208
- # select typed text
209
- self .setSelection (line , 4 , line , selCmdLength )
210
- self .removeSelectedText ()
211
- self .insert (txt )
212
-
213
206
def getText (self ):
214
207
""" Get the text as a unicode string. """
215
208
value = self .getBytes ().decode ('utf-8' )
@@ -245,11 +238,6 @@ def move_cursor_to_end(self):
245
238
self .ensureCursorVisible ()
246
239
self .ensureLineVisible (line )
247
240
248
- #def on_new_line(self):
249
- #"""On new input line"""
250
- #self.move_cursor_to_end()
251
- #self.new_input_line = False
252
-
253
241
def is_cursor_on_last_line (self ):
254
242
"""Return True if cursor is on the last line"""
255
243
cline , _ = self .getCursorPosition ()
@@ -285,10 +273,19 @@ def updateHistory(self, command):
285
273
self .historyIndex = len (self .history )
286
274
287
275
def writeHistoryFile (self ):
288
- wH = open (_historyFile , 'w' )
289
- for s in self .history :
290
- wH .write (s + '\n ' )
276
+ ok = False
277
+ try :
278
+ wH = open (_historyFile , 'w' )
279
+ for s in self .history :
280
+ wH .write (s + '\n ' )
281
+ ok = True
282
+ except :
283
+ raise
291
284
wH .close ()
285
+ if ok :
286
+ msgText = QCoreApplication .translate ('PythonConsole' ,
287
+ 'History saved successfully.' )
288
+ self .parent .callWidgetMessageBar (msgText )
292
289
293
290
def readHistoryFile (self ):
294
291
fileExist = QFile .exists (_historyFile )
@@ -301,9 +298,27 @@ def readHistoryFile(self):
301
298
else :
302
299
return
303
300
304
- def clearHistoryFile (self ):
305
- cH = open (_historyFile , 'w' )
301
+ def clearHistory (self , clearSession = False ):
302
+ if clearSession :
303
+ self .history = QStringList ()
304
+ msgText = QCoreApplication .translate ('PythonConsole' ,
305
+ 'Session and file history cleared successfully.' )
306
+ self .parent .callWidgetMessageBar (msgText )
307
+ return
308
+ ok = False
309
+ try :
310
+ cH = open (_historyFile , 'w' )
311
+ ok = True
312
+ except :
313
+ raise
306
314
cH .close ()
315
+ if ok :
316
+ msgText = QCoreApplication .translate ('PythonConsole' ,
317
+ 'History cleared successfully.' )
318
+ self .parent .callWidgetMessageBar (msgText )
319
+
320
+ def clearHistorySession (self ):
321
+ self .clearHistory (True )
307
322
308
323
def showPrevious (self ):
309
324
if self .historyIndex < len (self .history ) and not self .history .isEmpty ():
@@ -402,6 +417,25 @@ def keyPressEvent(self, e):
402
417
403
418
def contextMenuEvent (self , e ):
404
419
menu = QMenu (self )
420
+ subMenu = QMenu (menu )
421
+ titleHistoryMenu = QCoreApplication .translate ("PythonConsole" , "Command History" )
422
+ subMenu .setTitle (titleHistoryMenu )
423
+ showHistoryAction = subMenu .addAction (QCoreApplication .translate ("PythonConsole" ,
424
+ "Show" ),
425
+ self .showHistory , 'Ctrl+Shift+SPACE' )
426
+ subMenu .addSeparator ()
427
+ saveHistoryAction = subMenu .addAction (QCoreApplication .translate ("PythonConsole" ,
428
+ "Save" ),
429
+ self .writeHistoryFile )
430
+ subMenu .addSeparator ()
431
+ clearHistoryAction = subMenu .addAction (QCoreApplication .translate ("PythonConsole" ,
432
+ "Clear File" ),
433
+ self .clearHistory )
434
+ clearSessHistoryAction = subMenu .addAction (QCoreApplication .translate ("PythonConsole" ,
435
+ "Clear Session" ),
436
+ self .clearHistorySession )
437
+ menu .addMenu (subMenu )
438
+ menu .addSeparator ()
405
439
copyAction = menu .addAction (QCoreApplication .translate ("PythonConsole" ,
406
440
"Copy" ),
407
441
self .copy , QKeySequence .Copy )
@@ -460,14 +494,14 @@ def dropEvent(self, e):
460
494
def insertFromDropPaste (self , textDP ):
461
495
pasteList = str (textDP ).splitlines ()
462
496
for line in pasteList [:- 1 ]:
463
- line .replace (">>> " , "" ).replace ("... " , "" )
464
- self .insert (unicode (line ))
497
+ cleanLine = line .replace (">>> " , "" ).replace ("... " , "" )
498
+ self .insert (unicode (cleanLine ))
465
499
self .move_cursor_to_end ()
466
500
self .runCommand (unicode (self .currentCommand ()))
467
501
if pasteList [- 1 ] != "" :
468
502
line = pasteList [- 1 ]
469
- line .replace (">>> " , "" ).replace ("... " , "" )
470
- self .insert (unicode (line ))
503
+ cleanLine = line .replace (">>> " , "" ).replace ("... " , "" )
504
+ self .insert (unicode (cleanLine ))
471
505
self .move_cursor_to_end ()
472
506
473
507
def insertTextFromFile (self , listOpenFile ):
@@ -485,11 +519,9 @@ def entered(self):
485
519
self .runCommand ( unicode (self .currentCommand ()) )
486
520
self .setFocus ()
487
521
self .move_cursor_to_end ()
488
- #self.SendScintilla(QsciScintilla.SCI_EMPTYUNDOBUFFER)
489
522
490
523
def currentCommand (self ):
491
524
linenr , index = self .getCursorPosition ()
492
- #for i in range(0, linenr):
493
525
txtLength = self .text (linenr ).length ()
494
526
string = self .text ()
495
527
cmdLine = string .right (txtLength - 4 )
@@ -500,18 +532,8 @@ def runCommand(self, cmd):
500
532
self .writeCMD (cmd )
501
533
import webbrowser
502
534
self .updateHistory (cmd )
503
- if cmd in ('_save' , '_clear' , '_clearAll' , '_pyqgis' , '_api' ):
504
- if cmd == '_save' :
505
- self .writeHistoryFile ()
506
- msgText = QCoreApplication .translate ('PythonConsole' , 'History saved successfully.' )
507
- elif cmd == '_clear' :
508
- self .clearHistoryFile ()
509
- msgText = QCoreApplication .translate ('PythonConsole' , 'History cleared successfully.' )
510
- elif cmd == '_clearAll' :
511
- self .history = QStringList ()
512
- self .clearHistoryFile ()
513
- msgText = QCoreApplication .translate ('PythonConsole' , 'Session and file history cleared successfully.' )
514
- elif cmd == '_pyqgis' :
535
+ if cmd in ('_pyqgis' , '_api' ):
536
+ if cmd == '_pyqgis' :
515
537
webbrowser .open ( "http://www.qgis.org/pyqgis-cookbook/" )
516
538
elif cmd == '_api' :
517
539
webbrowser .open ( "http://www.qgis.org/api/" )
@@ -539,3 +561,32 @@ def writeCMD(self, txt):
539
561
getCmdString = self .text ()
540
562
prompt = getCmdString [0 :4 ]
541
563
sys .stdout .write (prompt + txt + '\n ' )
564
+
565
+ class HistoryDialog (QDialog , Ui_HistoryDialog ):
566
+ def __init__ (self , parent ):
567
+ QDialog .__init__ (self , parent )
568
+ self .setupUi (self )
569
+ self .parent = parent
570
+ self .setWindowTitle (QCoreApplication .translate ("PythonConsole" ,
571
+ "Python Console - Command History" ))
572
+ self .listView .setToolTip (QCoreApplication .translate ("PythonConsole" ,
573
+ "Double click on item to execute" ))
574
+ self .model = QStandardItemModel (self .listView )
575
+
576
+ self ._reloadHistory ()
577
+
578
+ self .listView .doubleClicked .connect (self ._runHistory )
579
+ self .reloadHistory .clicked .connect (self ._reloadHistory )
580
+
581
+ def _runHistory (self , item ):
582
+ cmd = item .data (Qt .DisplayRole ).toString ()
583
+ self .parent .runCommand (unicode (cmd ))
584
+
585
+ def _reloadHistory (self ):
586
+ self .model .clear ()
587
+ for i in self .parent .history :
588
+ item = QStandardItem (i )
589
+ self .model .appendRow (item )
590
+
591
+ self .listView .setModel (self .model )
592
+ self .listView .scrollToBottom ()
0 commit comments