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 |
67 @param parent parent widget |
72 Protected method to handle the user input a key at a time. |
85 Protected method to handle the user input a key at a time. |
73 |
86 |
74 @param ev key event |
87 @param ev key event |
75 @type QKeyEvent |
88 @type QKeyEvent |
76 """ |
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 |
77 txt = ev.text() |
112 txt = ev.text() |
78 |
113 |
79 # See it is text to insert. |
114 # See it is text to insert. |
80 if len(txt) and txt >= " ": |
115 if len(txt) and txt >= " ": |
81 if txt in ('"', "'") and self.hasSelectedText(): |
116 if self.hasSelectedText(): |
82 sline, sindex, eline, eindex = self.getSelection() |
117 if txt in MiniScintilla.EncloseChars: |
83 replaceText = txt + self.selectedText() + txt |
118 encloseSelectedText(MiniScintilla.EncloseChars[txt]) |
84 self.beginUndoAction() |
119 ev.accept() |
85 self.replaceSelectedText(replaceText) |
120 return |
86 self.endUndoAction() |
|
87 self.setSelection(sline, sindex + 1, eline, eindex + 1) |
|
88 ev.accept() |
|
89 return |
|
90 |
121 |
91 super(MiniScintilla, self).keyPressEvent(ev) |
122 super(MiniScintilla, self).keyPressEvent(ev) |
92 else: |
123 else: |
93 ev.ignore() |
124 ev.ignore() |
94 |
125 |