885 if cleanIndex >= 0: |
881 if cleanIndex >= 0: |
886 self.__undoStack.setIndex(cleanIndex) |
882 self.__undoStack.setIndex(cleanIndex) |
887 self.setCursorPosition(self.__chunks.pos() * 2) |
883 self.setCursorPosition(self.__chunks.pos() * 2) |
888 self.__refresh() |
884 self.__refresh() |
889 |
885 |
890 def editorCommand(self, cmd): |
|
891 """ |
|
892 Public method to execute an editor command sent by the eric |
|
893 view manager. |
|
894 |
|
895 @param cmd QScintilla command |
|
896 @type int |
|
897 """ |
|
898 if self.__embedded: |
|
899 from PyQt5.Qsci import QsciScintilla |
|
900 |
|
901 # Cursor movements |
|
902 if cmd == QsciScintilla.SCI_CHARLEFT: |
|
903 self.moveCursorToPreviousChar() |
|
904 elif cmd == QsciScintilla.SCI_CHARRIGHT: |
|
905 self.moveCursorToNextChar() |
|
906 elif cmd == QsciScintilla.SCI_LINEEND: |
|
907 self.moveCursorToEndOfLine() |
|
908 elif cmd == QsciScintilla.SCI_VCHOME: |
|
909 self.moveCursorToStartOfLine() |
|
910 elif cmd == QsciScintilla.SCI_LINEUP: |
|
911 self.moveCursorToPreviousLine() |
|
912 elif cmd == QsciScintilla.SCI_LINEDOWN: |
|
913 self.moveCursorToNextLine() |
|
914 elif cmd == QsciScintilla.SCI_PAGEDOWN: |
|
915 self.moveCursorToNextPage() |
|
916 elif cmd == QsciScintilla.SCI_PAGEUP: |
|
917 self.moveCursorToPreviousPage() |
|
918 elif cmd == QsciScintilla.SCI_DOCUMENTEND: |
|
919 self.moveCursorToEndOfDocument() |
|
920 elif cmd == QsciScintilla.SCI_DOCUMENTSTART: |
|
921 self.moveCursorToStartOfDocument() |
|
922 |
|
923 # Selection commands |
|
924 elif cmd == QsciScintilla.SCI_CHARRIGHTEXTEND: |
|
925 self.selectNextChar() |
|
926 elif cmd == QsciScintilla.SCI_CHARLEFTEXTEND: |
|
927 self.selectPreviousChar() |
|
928 elif cmd == QsciScintilla.SCI_LINEENDEXTEND: |
|
929 self.selectToEndOfLine() |
|
930 elif cmd == QsciScintilla.SCI_VCHOMEEXTEND: |
|
931 self.selectToStartOfLine() |
|
932 elif cmd == QsciScintilla.SCI_LINEUPEXTEND: |
|
933 self.selectPreviousLine() |
|
934 elif cmd == QsciScintilla.SCI_LINEDOWNEXTEND: |
|
935 self.selectNextLine() |
|
936 elif cmd == QsciScintilla.SCI_PAGEDOWNEXTEND: |
|
937 self.selectNextPage() |
|
938 elif cmd == QsciScintilla.SCI_PAGEUPEXTEND: |
|
939 self.selectPreviousPage() |
|
940 elif cmd == QsciScintilla.SCI_DOCUMENTENDEXTEND: |
|
941 self.selectEndOfDocument() |
|
942 elif cmd == QsciScintilla.SCI_DOCUMENTSTARTEXTEND: |
|
943 self.selectStartOfDocument() |
|
944 elif cmd == QsciScintilla.SCI_EDITTOGGLEOVERTYPE: |
|
945 self.setOverwriteMode(not self.overwriteMode()) |
|
946 self.setCursorPosition(self.__cursorPosition) |
|
947 |
|
948 # Edit commands |
|
949 if not self.__readOnly: |
|
950 if cmd == QsciScintilla.SCI_CLEAR: |
|
951 self.deleteByte() |
|
952 elif cmd == QsciScintilla.SCI_DELETEBACK: |
|
953 self.deleteByteBack() |
|
954 |
|
955 self.__refresh() |
|
956 |
|
957 #################################################### |
886 #################################################### |
958 ## Cursor movement commands |
887 ## Cursor movement commands |
959 #################################################### |
888 #################################################### |
960 |
889 |
961 def moveCursorToNextChar(self): |
890 def moveCursorToNextChar(self): |
1252 Protected method to handle key press events. |
1181 Protected method to handle key press events. |
1253 |
1182 |
1254 @param evt reference to the key event |
1183 @param evt reference to the key event |
1255 @type QKeyEvent |
1184 @type QKeyEvent |
1256 """ |
1185 """ |
1257 if not self.__embedded: |
1186 # Cursor movements |
1258 # Cursor movements |
1187 if evt.matches(QKeySequence.MoveToNextChar): |
1259 if evt.matches(QKeySequence.MoveToNextChar): |
1188 self.moveCursorToNextChar() |
1260 self.moveCursorToNextChar() |
1189 elif evt.matches(QKeySequence.MoveToPreviousChar): |
1261 elif evt.matches(QKeySequence.MoveToPreviousChar): |
1190 self.moveCursorToPreviousChar() |
1262 self.moveCursorToPreviousChar() |
1191 elif evt.matches(QKeySequence.MoveToEndOfLine): |
1263 elif evt.matches(QKeySequence.MoveToEndOfLine): |
1192 self.moveCursorToEndOfLine() |
1264 self.moveCursorToEndOfLine() |
1193 elif evt.matches(QKeySequence.MoveToStartOfLine): |
1265 elif evt.matches(QKeySequence.MoveToStartOfLine): |
1194 self.moveCursorToStartOfLine() |
1266 self.moveCursorToStartOfLine() |
1195 elif evt.matches(QKeySequence.MoveToPreviousLine): |
1267 elif evt.matches(QKeySequence.MoveToPreviousLine): |
1196 self.moveCursorToPreviousLine() |
1268 self.moveCursorToPreviousLine() |
1197 elif evt.matches(QKeySequence.MoveToNextLine): |
1269 elif evt.matches(QKeySequence.MoveToNextLine): |
1198 self.moveCursorToNextLine() |
1270 self.moveCursorToNextLine() |
1199 elif evt.matches(QKeySequence.MoveToNextPage): |
1271 elif evt.matches(QKeySequence.MoveToNextPage): |
1200 self.moveCursorToNextPage() |
1272 self.moveCursorToNextPage() |
1201 elif evt.matches(QKeySequence.MoveToPreviousPage): |
1273 elif evt.matches(QKeySequence.MoveToPreviousPage): |
1202 self.moveCursorToPreviousPage() |
1274 self.moveCursorToPreviousPage() |
1203 elif evt.matches(QKeySequence.MoveToEndOfDocument): |
1275 elif evt.matches(QKeySequence.MoveToEndOfDocument): |
1204 self.moveCursorToEndOfDocument() |
1276 self.moveCursorToEndOfDocument() |
1205 elif evt.matches(QKeySequence.MoveToStartOfDocument): |
1277 elif evt.matches(QKeySequence.MoveToStartOfDocument): |
1206 self.moveCursorToStartOfDocument() |
1278 self.moveCursorToStartOfDocument() |
1207 |
|
1208 # Selection commands |
|
1209 elif evt.matches(QKeySequence.SelectAll): |
|
1210 self.selectAll() |
|
1211 elif evt.matches(QKeySequence.SelectNextChar): |
|
1212 self.selectNextChar() |
|
1213 elif evt.matches(QKeySequence.SelectPreviousChar): |
|
1214 self.selectPreviousChar() |
|
1215 elif evt.matches(QKeySequence.SelectEndOfLine): |
|
1216 self.selectToEndOfLine() |
|
1217 elif evt.matches(QKeySequence.SelectStartOfLine): |
|
1218 self.selectToStartOfLine() |
|
1219 elif evt.matches(QKeySequence.SelectPreviousLine): |
|
1220 self.selectPreviousLine() |
|
1221 elif evt.matches(QKeySequence.SelectNextLine): |
|
1222 self.selectNextLine() |
|
1223 elif evt.matches(QKeySequence.SelectNextPage): |
|
1224 self.selectNextPage() |
|
1225 elif evt.matches(QKeySequence.SelectPreviousPage): |
|
1226 self.selectPreviousPage() |
|
1227 elif evt.matches(QKeySequence.SelectEndOfDocument): |
|
1228 self.selectEndOfDocument() |
|
1229 elif evt.matches(QKeySequence.SelectStartOfDocument): |
|
1230 self.selectStartOfDocument() |
|
1231 |
|
1232 # Edit commands |
|
1233 elif evt.matches(QKeySequence.Copy): |
|
1234 self.copy() |
|
1235 elif evt.key() == Qt.Key_Insert and \ |
|
1236 evt.modifiers() == Qt.NoModifier: |
|
1237 self.setOverwriteMode(not self.overwriteMode()) |
|
1238 self.setCursorPosition(self.__cursorPosition) |
|
1239 |
|
1240 elif not self.__readOnly: |
|
1241 if evt.matches(QKeySequence.Cut): |
|
1242 self.cut() |
|
1243 elif evt.matches(QKeySequence.Paste): |
|
1244 self.paste() |
|
1245 elif evt.matches(QKeySequence.Delete): |
|
1246 self.deleteByte() |
|
1247 elif evt.key() == Qt.Key_Backspace and \ |
|
1248 evt.modifiers() == Qt.NoModifier: |
|
1249 self.deleteByteBack() |
|
1250 elif evt.matches(QKeySequence.Undo): |
|
1251 self.undo() |
|
1252 elif evt.matches(QKeySequence.Redo): |
|
1253 self.redo() |
1279 |
1254 |
1280 # Selection commands |
1255 elif QApplication.keyboardModifiers() in [ |
1281 elif evt.matches(QKeySequence.SelectAll): |
1256 Qt.NoModifier, Qt.KeypadModifier]: |
1282 self.selectAll() |
1257 # some hex input |
1283 elif evt.matches(QKeySequence.SelectNextChar): |
1258 key = evt.text() |
1284 self.selectNextChar() |
1259 if key and key in "0123456789abcdef": |
1285 elif evt.matches(QKeySequence.SelectPreviousChar): |
1260 if self.hasSelection(): |
1286 self.selectPreviousChar() |
1261 if self.__overwriteMode: |
1287 elif evt.matches(QKeySequence.SelectEndOfLine): |
1262 length = self.getSelectionLength() |
1288 self.selectToEndOfLine() |
1263 self.replaceByteArray( |
1289 elif evt.matches(QKeySequence.SelectStartOfLine): |
1264 self.getSelectionBegin(), length, |
1290 self.selectToStartOfLine() |
1265 bytearray(length)) |
1291 elif evt.matches(QKeySequence.SelectPreviousLine): |
1266 else: |
1292 self.selectPreviousLine() |
1267 self.remove(self.getSelectionBegin(), |
1293 elif evt.matches(QKeySequence.SelectNextLine): |
1268 self.getSelectionLength()) |
1294 self.selectNextLine() |
1269 self.__bPosCurrent = self.getSelectionBegin() |
1295 elif evt.matches(QKeySequence.SelectNextPage): |
1270 self.setCursorPosition(2 * self.__bPosCurrent) |
1296 self.selectNextPage() |
1271 self.__resetSelection(2 * self.__bPosCurrent) |
1297 elif evt.matches(QKeySequence.SelectPreviousPage): |
1272 |
1298 self.selectPreviousPage() |
1273 # if in insert mode, insert a byte |
1299 elif evt.matches(QKeySequence.SelectEndOfDocument): |
1274 if not self.__overwriteMode: |
1300 self.selectEndOfDocument() |
1275 if (self.__cursorPosition % 2) == 0: |
1301 elif evt.matches(QKeySequence.SelectStartOfDocument): |
1276 self.insert(self.__bPosCurrent, 0) |
1302 self.selectStartOfDocument() |
1277 |
1303 |
1278 # change content |
1304 # Edit commands |
1279 if self.__chunks.size() > 0: |
1305 elif evt.matches(QKeySequence.Copy): |
1280 hexValue = self.__toHex( |
1306 self.copy() |
1281 self.__chunks.data(self.__bPosCurrent, 1)) |
1307 elif evt.key() == Qt.Key_Insert and \ |
1282 if (self.__cursorPosition % 2) == 0: |
1308 evt.modifiers() == Qt.NoModifier: |
1283 hexValue[0] = ord(key) |
1309 self.setOverwriteMode(not self.overwriteMode()) |
1284 else: |
1310 self.setCursorPosition(self.__cursorPosition) |
1285 hexValue[1] = ord(key) |
1311 |
1286 self.replace(self.__bPosCurrent, |
1312 if not self.__readOnly: |
1287 self.__fromHex(hexValue)[0]) |
1313 if evt.matches(QKeySequence.Cut): |
1288 |
1314 self.cut() |
1289 self.setCursorPosition(self.__cursorPosition + 1) |
1315 elif evt.matches(QKeySequence.Paste): |
1290 self.__resetSelection(self.__cursorPosition) |
1316 self.paste() |
|
1317 elif evt.matches(QKeySequence.Delete): |
|
1318 self.deleteByte() |
|
1319 elif evt.key() == Qt.Key_Backspace and \ |
|
1320 evt.modifiers() == Qt.NoModifier: |
|
1321 self.deleteByteBack() |
|
1322 elif evt.matches(QKeySequence.Undo): |
|
1323 self.undo() |
|
1324 elif evt.matches(QKeySequence.Redo): |
|
1325 self.redo() |
|
1326 |
|
1327 if not self.__readOnly and \ |
|
1328 QApplication.keyboardModifiers() in [ |
|
1329 Qt.NoModifier, Qt.KeypadModifier]: |
|
1330 # some hex input |
|
1331 key = evt.text() |
|
1332 if key and key in "0123456789abcdef": |
|
1333 if self.hasSelection(): |
|
1334 if self.__overwriteMode: |
|
1335 length = self.getSelectionLength() |
|
1336 self.replaceByteArray( |
|
1337 self.getSelectionBegin(), length, |
|
1338 bytearray(length)) |
|
1339 else: |
1291 else: |
1340 self.remove(self.getSelectionBegin(), |
1292 return |
1341 self.getSelectionLength()) |
1293 else: |
1342 self.__bPosCurrent = self.getSelectionBegin() |
1294 return |
1343 self.setCursorPosition(2 * self.__bPosCurrent) |
1295 else: |
1344 self.__resetSelection(2 * self.__bPosCurrent) |
1296 return |
1345 |
1297 else: |
1346 # if in insert mode, insert a byte |
1298 return |
1347 if not self.__overwriteMode: |
|
1348 if (self.__cursorPosition % 2) == 0: |
|
1349 self.insert(self.__bPosCurrent, 0) |
|
1350 |
|
1351 # change content |
|
1352 if self.__chunks.size() > 0: |
|
1353 hexValue = self.__toHex( |
|
1354 self.__chunks.data(self.__bPosCurrent, 1)) |
|
1355 if (self.__cursorPosition % 2) == 0: |
|
1356 hexValue[0] = ord(key) |
|
1357 else: |
|
1358 hexValue[1] = ord(key) |
|
1359 self.replace(self.__bPosCurrent, |
|
1360 self.__fromHex(hexValue)[0]) |
|
1361 |
|
1362 self.setCursorPosition(self.__cursorPosition + 1) |
|
1363 self.__resetSelection(self.__cursorPosition) |
|
1364 # TODO: handle pressing keyboard modifier only by not calling __referesh |
|
1365 |
1299 |
1366 self.__refresh() |
1300 self.__refresh() |
1367 |
1301 |
1368 def mouseMoveEvent(self, evt): |
1302 def mouseMoveEvent(self, evt): |
1369 """ |
1303 """ |