eric6/QScintilla/MiniEditor.py

branch
maintenance
changeset 8043
0acf98cd089a
parent 7924
8a96736d465e
parent 7969
62eff8b34a8d
child 8176
31965986ecd1
equal deleted inserted replaced
7991:866adc8c315b 8043:0acf98cd089a
45 class MiniScintilla(QsciScintillaCompat): 45 class MiniScintilla(QsciScintillaCompat):
46 """ 46 """
47 Class implementing a QsciScintillaCompat subclass for handling focus 47 Class implementing a QsciScintillaCompat subclass for handling focus
48 events. 48 events.
49 """ 49 """
50 EncloseChars = {
51 '"': '"',
52 "'": "'",
53 "(": "()",
54 ")": "()",
55 "{": "{}", # __IGNORE_WARNING_M613__
56 "}": "{}", # __IGNORE_WARNING_M613__
57 "[": "[]",
58 "]": "[]",
59 "<": "<>",
60 ">": "<>",
61 }
62
50 def __init__(self, parent=None): 63 def __init__(self, parent=None):
51 """ 64 """
52 Constructor 65 Constructor
53 66
54 @param parent parent widget (QWidget) 67 @param parent parent widget
68 @type QWidget
55 """ 69 """
56 super(MiniScintilla, self).__init__(parent) 70 super(MiniScintilla, self).__init__(parent)
57 71
58 self.mw = parent 72 self.mw = parent
59 73
60 def getFileName(self): 74 def getFileName(self):
61 """ 75 """
62 Public method to return the name of the file being displayed. 76 Public method to return the name of the file being displayed.
63 77
64 @return filename of the displayed file (string) 78 @return filename of the displayed file
79 @rtype str
65 """ 80 """
66 return self.mw.getFileName() 81 return self.mw.getFileName()
82
83 def keyPressEvent(self, ev):
84 """
85 Protected method to handle the user input a key at a time.
86
87 @param ev key event
88 @type QKeyEvent
89 """
90 def encloseSelectedText(encString):
91 """
92 Local function to enclose the current selection with some
93 characters.
94
95 @param encString string to use to enclose the selection
96 (one or two characters)
97 @type str
98 """
99 startChar = encString[0]
100 if len(encString) == 2:
101 endChar = encString[1]
102 else:
103 endChar = startChar
104
105 sline, sindex, eline, eindex = self.getSelection()
106 replaceText = startChar + self.selectedText() + endChar
107 self.beginUndoAction()
108 self.replaceSelectedText(replaceText)
109 self.endUndoAction()
110 self.setSelection(sline, sindex + 1, eline, eindex + 1)
111
112 txt = ev.text()
113
114 # See it is text to insert.
115 if len(txt) and txt >= " ":
116 if self.hasSelectedText():
117 if txt in MiniScintilla.EncloseChars:
118 encloseSelectedText(MiniScintilla.EncloseChars[txt])
119 ev.accept()
120 return
121
122 super(MiniScintilla, self).keyPressEvent(ev)
123 else:
124 ev.ignore()
67 125
68 def focusInEvent(self, event): 126 def focusInEvent(self, event):
69 """ 127 """
70 Protected method called when the editor receives focus. 128 Protected method called when the editor receives focus.
71 129
72 This method checks for modifications of the current file and 130 This method checks for modifications of the current file and
73 rereads it upon request. The cursor is placed at the current position 131 rereads it upon request. The cursor is placed at the current position
74 assuming, that it is in the vicinity of the old position after the 132 assuming, that it is in the vicinity of the old position after the
75 reread. 133 reread.
76 134
77 @param event the event object (QFocusEvent) 135 @param event the event object
136 @type QFocusEvent
78 """ 137 """
79 self.mw.editorActGrp.setEnabled(True) 138 self.mw.editorActGrp.setEnabled(True)
80 try: 139 try:
81 self.setCaretWidth(self.mw.caretWidth) 140 self.setCaretWidth(self.mw.caretWidth)
82 except AttributeError: 141 except AttributeError:
88 147
89 def focusOutEvent(self, event): 148 def focusOutEvent(self, event):
90 """ 149 """
91 Protected method called when the editor loses focus. 150 Protected method called when the editor loses focus.
92 151
93 @param event the event object (QFocusEvent) 152 @param event the event object
153 @type QFocusEvent
94 """ 154 """
95 self.mw.editorActGrp.setEnabled(False) 155 self.mw.editorActGrp.setEnabled(False)
96 self.setCaretWidth(0) 156 self.setCaretWidth(0)
97 157
98 super(MiniScintilla, self).focusOutEvent(event) 158 super(MiniScintilla, self).focusOutEvent(event)
334 """ 394 """
335 Private slot to show a little About message. 395 Private slot to show a little About message.
336 """ 396 """
337 E5MessageBox.about( 397 E5MessageBox.about(
338 self, 398 self,
339 self.tr("About eric6 Mini Editor"), 399 self.tr("About eric Mini Editor"),
340 self.tr( 400 self.tr(
341 "The eric6 Mini Editor is an editor component" 401 "The eric Mini Editor is an editor component"
342 " based on QScintilla. It may be used for simple" 402 " based on QScintilla. It may be used for simple"
343 " editing tasks, that don't need the power of" 403 " editing tasks, that don't need the power of"
344 " a full blown editor.")) 404 " a full blown editor."))
345 405
346 def __aboutQt(self): 406 def __aboutQt(self):
347 """ 407 """
348 Private slot to handle the About Qt dialog. 408 Private slot to handle the About Qt dialog.
349 """ 409 """
350 E5MessageBox.aboutQt(self, "eric6 Mini Editor") 410 E5MessageBox.aboutQt(self, "eric Mini Editor")
351 411
352 def __whatsThis(self): 412 def __whatsThis(self):
353 """ 413 """
354 Private slot called in to enter Whats This mode. 414 Private slot called in to enter Whats This mode.
355 """ 415 """
464 self.__createHelpActions() 524 self.__createHelpActions()
465 self.__createSearchActions() 525 self.__createSearchActions()
466 self.__createViewActions() 526 self.__createViewActions()
467 527
468 # read the keyboard shortcuts and make them identical to the main 528 # read the keyboard shortcuts and make them identical to the main
469 # eric6 shortcuts 529 # eric shortcuts
470 for act in self.helpActions: 530 for act in self.helpActions:
471 self.__readShortcut(act, "General") 531 self.__readShortcut(act, "General")
472 for act in self.editActions: 532 for act in self.editActions:
473 self.__readShortcut(act, "Edit") 533 self.__readShortcut(act, "Edit")
474 for act in self.fileActions: 534 for act in self.fileActions:
2504 @return flag indicating, if it is ok to continue (boolean) 2564 @return flag indicating, if it is ok to continue (boolean)
2505 """ 2565 """
2506 if self.__textEdit.isModified(): 2566 if self.__textEdit.isModified():
2507 ret = E5MessageBox.okToClearData( 2567 ret = E5MessageBox.okToClearData(
2508 self, 2568 self,
2509 self.tr("eric6 Mini Editor"), 2569 self.tr("eric Mini Editor"),
2510 self.tr("The document has unsaved changes."), 2570 self.tr("The document has unsaved changes."),
2511 self.__save) 2571 self.__save)
2512 return ret 2572 return ret
2513 return True 2573 return True
2514 2574
3182 3242
3183 @param filename filename used to determine the associated lexer 3243 @param filename filename used to determine the associated lexer
3184 language (string) 3244 language (string)
3185 @param initTextDisplay flag indicating an initialization of the text 3245 @param initTextDisplay flag indicating an initialization of the text
3186 display is required as well (boolean) 3246 display is required as well (boolean)
3187 @keyparam pyname name of the pygments lexer to use (string) 3247 @param pyname name of the pygments lexer to use (string)
3188 """ 3248 """
3189 self.__bindLexer(filename, pyname=pyname) 3249 self.__bindLexer(filename, pyname=pyname)
3190 self.__textEdit.recolor() 3250 self.__textEdit.recolor()
3191 self.__checkLanguage() 3251 self.__checkLanguage()
3192 3252
3251 """ 3311 """
3252 Private slot to set the correct lexer depending on language. 3312 Private slot to set the correct lexer depending on language.
3253 3313
3254 @param filename filename used to determine the associated lexer 3314 @param filename filename used to determine the associated lexer
3255 language (string) 3315 language (string)
3256 @keyparam pyname name of the pygments lexer to use (string) 3316 @param pyname name of the pygments lexer to use (string)
3257 """ 3317 """
3258 if ( 3318 if (
3259 self.lexer_ is not None and 3319 self.lexer_ is not None and
3260 (self.lexer_.lexer() == "container" or 3320 (self.lexer_.lexer() == "container" or
3261 self.lexer_.lexer() is None) 3321 self.lexer_.lexer() is None)
3590 """ 3650 """
3591 Public slot to jump to the beginning of a line. 3651 Public slot to jump to the beginning of a line.
3592 3652
3593 @param line line number to go to 3653 @param line line number to go to
3594 @type int 3654 @type int
3595 @keyparam pos position in line to go to 3655 @param pos position in line to go to
3596 @type int 3656 @type int
3597 """ 3657 """
3598 self.__textEdit.setCursorPosition(line - 1, pos - 1) 3658 self.__textEdit.setCursorPosition(line - 1, pos - 1)
3599 self.__textEdit.ensureLineVisible(line - 1) 3659 self.__textEdit.ensureLineVisible(line - 1)
3600 self.__textEdit.setFirstVisibleLine(line - 1) 3660 self.__textEdit.setFirstVisibleLine(line - 1)

eric ide

mercurial