src/eric7/HexEdit/HexEditSearchReplaceWidget.py

Wed, 13 Jul 2022 14:55:47 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 13 Jul 2022 14:55:47 +0200
branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
permissions
-rw-r--r--

Reformatted the source code using the 'Black' utility.

4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
8881
54e42bc2437a Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8358
diff changeset
3 # Copyright (c) 2016 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a search and replace widget for the hex editor.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
8318
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
10 from PyQt6.QtCore import pyqtSlot, Qt, QByteArray, QRegularExpression
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
11 from PyQt6.QtGui import QRegularExpressionValidator
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
12 from PyQt6.QtWidgets import QWidget
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
8358
144a6b854f70 Sorted the eric specific extensions into packages named like the corresponding PyQt packages (i.e. EricCore,EricGui and EricWidgets).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8356
diff changeset
14 from EricGui.EricAction import EricAction
144a6b854f70 Sorted the eric specific extensions into packages named like the corresponding PyQt packages (i.e. EricCore,EricGui and EricWidgets).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8356
diff changeset
15 from EricWidgets import EricMessageBox
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 import UI.PixmapCache
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 class HexEditSearchReplaceWidget(QWidget):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 Class implementing a search and replace widget for the hex editor.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
24
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
25 def __init__(self, editor, mainWindow, replace=False, parent=None):
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
28
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 @param editor reference to the hex editor widget
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 @type HexEditWidget
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
31 @param mainWindow reference to the main window
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
32 @type HexEditMainWindow
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @param replace flag indicating a replace widget
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 @type bool
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 @param parent reference to the parent widget
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 @type QWidget
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8143
diff changeset
38 super().__init__(parent)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
39
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 self.__replace = replace
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 self.__editor = editor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
42
4660
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
43 # keep this in sync with the logic in __getContent()
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
44 self.__formatAndValidators = {
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
45 "hex": (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
46 self.tr("Hex"),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
47 QRegularExpressionValidator(QRegularExpression("[0-9a-f]*")),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
48 ),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
49 "dec": (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
50 self.tr("Dec"),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
51 QRegularExpressionValidator(QRegularExpression("[0-9]*")),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
52 ),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
53 "oct": (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
54 self.tr("Oct"),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
55 QRegularExpressionValidator(QRegularExpression("[0-7]*")),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
56 ),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
57 "bin": (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
58 self.tr("Bin"),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
59 QRegularExpressionValidator(QRegularExpression("[01]*")),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
60 ),
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
61 "iso-8859-1": (self.tr("Text"), None),
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
62 # text as latin-1/iso-8859-1
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
63 "utf-8": (self.tr("UTF-8"), None),
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
64 # text as utf-8
4660
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
65 }
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
66 formatOrder = ["hex", "dec", "oct", "bin", "iso-8859-1", "utf-8"]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
67
4660
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
68 self.__currentFindFormat = ""
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
69 self.__currentReplaceFormat = ""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
70
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
71 self.__findHistory = mainWindow.getSRHistory("search")
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 if replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 from .Ui_HexEditReplaceWidget import Ui_HexEditReplaceWidget
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
74
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
75 self.__replaceHistory = mainWindow.getSRHistory("replace")
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 self.__ui = Ui_HexEditReplaceWidget()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 from .Ui_HexEditSearchWidget import Ui_HexEditSearchWidget
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
79
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 self.__ui = Ui_HexEditSearchWidget()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 self.__ui.setupUi(self)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
82
7533
88261c96484b Removed the '.png' extension from all call to get an icon or a pixmap from the PixmapCache because this is not needed anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
83 self.__ui.closeButton.setIcon(UI.PixmapCache.getIcon("close"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
84 self.__ui.findPrevButton.setIcon(UI.PixmapCache.getIcon("1leftarrow"))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
85 self.__ui.findNextButton.setIcon(UI.PixmapCache.getIcon("1rightarrow"))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
86
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 if replace:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
88 self.__ui.replaceButton.setIcon(UI.PixmapCache.getIcon("editReplace"))
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 self.__ui.replaceSearchButton.setIcon(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
90 UI.PixmapCache.getIcon("editReplaceSearch")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
91 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
92 self.__ui.replaceAllButton.setIcon(UI.PixmapCache.getIcon("editReplaceAll"))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
93
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
94 for dataFormat in formatOrder:
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
95 formatStr, validator = self.__formatAndValidators[dataFormat]
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
96 self.__ui.findFormatCombo.addItem(formatStr, dataFormat)
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
97 if replace:
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
98 for dataFormat in formatOrder:
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
99 formatStr, validator = self.__formatAndValidators[dataFormat]
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
100 self.__ui.replaceFormatCombo.addItem(formatStr, dataFormat)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
101
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 self.__ui.findtextCombo.setCompleter(None)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 self.__ui.findtextCombo.lineEdit().returnPressed.connect(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
104 self.__findByReturnPressed
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
105 )
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 if replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 self.__ui.replacetextCombo.setCompleter(None)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 self.__ui.replacetextCombo.lineEdit().returnPressed.connect(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
109 self.on_replaceButton_clicked
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
110 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
111
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
112 self.findNextAct = EricAction(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
113 self.tr("Find Next"),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
114 self.tr("Find Next"),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
115 0,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
116 0,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
117 self,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
118 "hexEditor_search_widget_find_next",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
119 )
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 self.findNextAct.triggered.connect(self.on_findNextButton_clicked)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 self.findNextAct.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 self.__ui.findtextCombo.addAction(self.findNextAct)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
123
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
124 self.findPrevAct = EricAction(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
125 self.tr("Find Prev"),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
126 self.tr("Find Prev"),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
127 0,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
128 0,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
129 self,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
130 "hexEditor_search_widget_find_prev",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
131 )
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 self.findPrevAct.triggered.connect(self.on_findPrevButton_clicked)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 self.findPrevAct.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 self.__ui.findtextCombo.addAction(self.findPrevAct)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
135
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 self.__havefound = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
137
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
138 @pyqtSlot(int)
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
139 def on_findFormatCombo_currentIndexChanged(self, idx):
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
140 """
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
141 Private slot to handle a selection from the find format.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
142
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
143 @param idx index of the selected entry
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
144 @type int
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
145 """
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
146 if idx >= 0:
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
147 findFormat = self.__ui.findFormatCombo.itemData(idx)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
148
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
149 if findFormat != self.__currentFindFormat:
4660
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
150 txt = self.__ui.findtextCombo.currentText()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
151 newTxt = self.__convertText(txt, self.__currentFindFormat, findFormat)
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
152 self.__currentFindFormat = findFormat
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
153
4660
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
154 self.__ui.findtextCombo.setValidator(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
155 self.__formatAndValidators[findFormat][1]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
156 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
157
4660
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
158 self.__ui.findtextCombo.setEditText(newTxt)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
159
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
160 @pyqtSlot(str)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 def on_findtextCombo_editTextChanged(self, txt):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 Private slot to enable/disable the find buttons.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
164
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
165 @param txt text of the find text combo
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
166 @type str
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 if not txt:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 self.__ui.findNextButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 self.findNextAct.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 self.__ui.findPrevButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 self.findPrevAct.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 if self.__replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 self.__ui.replaceButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 self.__ui.replaceSearchButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 self.__ui.replaceAllButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 self.__ui.findNextButton.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 self.findNextAct.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 self.__ui.findPrevButton.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 self.findPrevAct.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 if self.__replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 self.__ui.replaceButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 self.__ui.replaceSearchButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 self.__ui.replaceAllButton.setEnabled(True)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
186
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
187 @pyqtSlot(int)
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
188 def on_findtextCombo_activated(self, idx):
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
189 """
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
190 Private slot to handle a selection from the find history.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
191
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
192 @param idx index of the selected entry
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
193 @type int
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
194 """
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
195 if idx >= 0:
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
196 formatIndex = self.__ui.findtextCombo.itemData(idx)
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
197 if formatIndex is not None:
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
198 self.__ui.findFormatCombo.setCurrentIndex(formatIndex)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
199
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 def __getContent(self, replace=False):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 Private method to get the contents of the find/replace combo as
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 a bytearray.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
204
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 @param replace flag indicating to retrieve the replace contents
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 @type bool
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 @return search or replace term as text and binary data
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 @rtype tuple of bytearray and str
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 if replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 textCombo = self.__ui.replacetextCombo
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 formatCombo = self.__ui.replaceFormatCombo
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 history = self.__replaceHistory
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 textCombo = self.__ui.findtextCombo
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 formatCombo = self.__ui.findFormatCombo
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 history = self.__findHistory
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
218
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 txt = textCombo.currentText()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 idx = formatCombo.currentIndex()
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
221 findFormat = formatCombo.itemData(idx)
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
222 ba = self.__text2bytearray(txt, findFormat)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
223
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 # This moves any previous occurrence of this statement to the head
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 # of the list and updates the combobox
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
226 historyEntry = (idx, txt)
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
227 if historyEntry in history:
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
228 history.remove(historyEntry)
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
229 history.insert(0, historyEntry)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 textCombo.clear()
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
231 for index, text in history:
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
232 textCombo.addItem(text, index)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
233
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 return ba, txt
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
235
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 def on_findNextButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 Private slot to find the next occurrence.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 self.findPrevNext(False)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
242
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 def on_findPrevButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 Private slot to find the previous occurrence.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 self.findPrevNext(True)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
249
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 def findPrevNext(self, prev=False):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 Public slot to find the next occurrence of the search term.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
253
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 @param prev flag indicating a backwards search
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 @type bool
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 @return flag indicating a successful search
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 @rtype bool
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 if not self.__havefound or not self.__ui.findtextCombo.currentText():
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 self.show()
6891
93f82da09f22 Fixed some code style issues detected by the new 'return' checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
261 return False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
262
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 self.__findBackwards = prev
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 ba, txt = self.__getContent()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
265
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 idx = -1
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 if len(ba) > 0:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 startIndex = self.__editor.cursorPosition() // 2
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 if prev:
7254
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
270 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
271 self.__editor.hasSelection()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
272 and startIndex == self.__editor.getSelectionEnd()
7254
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
273 ):
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 # skip to the selection start
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 startIndex = self.__editor.getSelectionBegin()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 idx = self.__editor.lastIndexOf(ba, startIndex)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 else:
7254
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
278 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
279 self.__editor.hasSelection()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
280 and startIndex == self.__editor.getSelectionBegin() - 1
7254
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
281 ):
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 # skip to the selection end
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 startIndex = self.__editor.getSelectionEnd()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 idx = self.__editor.indexOf(ba, startIndex)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
285
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 if idx >= 0:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 if self.__replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 self.__ui.replaceButton.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 self.__ui.replaceSearchButton.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 else:
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
291 EricMessageBox.information(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
292 self, self.windowTitle(), self.tr("'{0}' was not found.").format(txt)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
293 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
294
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 return idx >= 0
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
296
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 def __findByReturnPressed(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 Private slot to handle a return pressed in the find combo.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 if self.__findBackwards:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 self.findPrevNext(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 self.findPrevNext(False)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
305
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
306 @pyqtSlot(int)
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
307 def on_replaceFormatCombo_currentIndexChanged(self, idx):
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
308 """
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
309 Private slot to handle a selection from the replace format.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
310
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
311 @param idx index of the selected entry
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
312 @type int
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
313 """
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
314 if idx >= 0:
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
315 replaceFormat = self.__ui.replaceFormatCombo.itemData(idx)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
316
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
317 if replaceFormat != self.__currentReplaceFormat:
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
318 txt = self.__ui.replacetextCombo.currentText()
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
319 newTxt = self.__convertText(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
320 txt, self.__currentReplaceFormat, replaceFormat
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
321 )
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
322 self.__currentReplaceFormat = replaceFormat
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
323
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
324 self.__ui.replacetextCombo.setValidator(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
325 self.__formatAndValidators[replaceFormat][1]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
326 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
327
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
328 self.__ui.replacetextCombo.setEditText(newTxt)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
329
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
330 @pyqtSlot(int)
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
331 def on_replacetextCombo_activated(self, idx):
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
332 """
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
333 Private slot to handle a selection from the replace history.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
334
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
335 @param idx index of the selected entry
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
336 @type int
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
337 """
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
338 if idx >= 0:
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
339 formatIndex = self.__ui.replacetextCombo.itemData(idx)
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
340 if formatIndex is not None:
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
341 self.__ui.replaceFormatCombo.setCurrentIndex(formatIndex)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 def on_replaceButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 Private slot to replace one occurrence of data.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 self.__doReplace(False)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
349
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 def on_replaceSearchButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 Private slot to replace one occurrence of data and search for the next
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 one.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 self.__doReplace(True)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
357
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 def __doReplace(self, searchNext):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 Private method to replace one occurrence of data.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
361
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 @param searchNext flag indicating to search for the next occurrence
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
363 @type bool
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 # Check enabled status due to dual purpose usage of this method
7254
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
366 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
367 not self.__ui.replaceButton.isEnabled()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
368 and not self.__ui.replaceSearchButton.isEnabled()
7254
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
369 ):
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
371
4654
9dafe6905667 Fixed an issue in the hex editor search and replace widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4653
diff changeset
372 fba, ftxt = self.__getContent(False)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 rba, rtxt = self.__getContent(True)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
374
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 ok = False
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 if self.__editor.hasSelection():
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 # we did a successful search before
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 startIdx = self.__editor.getSelectionBegin()
4654
9dafe6905667 Fixed an issue in the hex editor search and replace widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4653
diff changeset
379 self.__editor.replaceByteArray(startIdx, len(fba), rba)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
380
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 if searchNext:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 ok = self.findPrevNext(self.__findBackwards)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
383
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 if not ok:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 self.__ui.replaceButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 self.__ui.replaceSearchButton.setEnabled(False)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
387
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389 def on_replaceAllButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 Private slot to replace all occurrences of data.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 replacements = 0
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
394
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 cursorPosition = self.__editor.cursorPosition()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
396
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 fba, ftxt = self.__getContent(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 rba, rtxt = self.__getContent(True)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
399
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 idx = 0
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 while idx >= 0:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 idx = self.__editor.indexOf(fba, idx)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 if idx >= 0:
4654
9dafe6905667 Fixed an issue in the hex editor search and replace widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4653
diff changeset
404 self.__editor.replaceByteArray(idx, len(fba), rba)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 idx += len(rba)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 replacements += 1
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
407
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 if replacements:
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
409 EricMessageBox.information(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
410 self,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
411 self.windowTitle(),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
412 self.tr("Replaced {0} occurrences.").format(replacements),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
413 )
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 else:
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
415 EricMessageBox.information(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
416 self,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
417 self.windowTitle(),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
418 self.tr("Nothing replaced because '{0}' was not found.").format(ftxt),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
419 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
420
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 self.__editor.setCursorPosition(cursorPosition)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 self.__editor.ensureVisible()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
423
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
424 def __showFind(self, text=""):
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 Private method to display this widget in find mode.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
427
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
428 @param text hex encoded text to be shown in the findtext edit
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
429 @type str
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 self.__replace = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
432
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 self.__ui.findtextCombo.clear()
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
434 for index, txt in self.__findHistory:
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
435 self.__ui.findtextCombo.addItem(txt, index)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
436 self.__ui.findFormatCombo.setCurrentIndex(0) # 0 is always Hex
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
437 self.on_findFormatCombo_currentIndexChanged(0)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 self.__ui.findtextCombo.setEditText(text)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 self.__ui.findtextCombo.lineEdit().selectAll()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 self.__ui.findtextCombo.setFocus()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 self.on_findtextCombo_editTextChanged(text)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
442
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 self.__havefound = True
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 self.__findBackwards = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
445
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
446 def __showReplace(self, text=""):
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 Private slot to display this widget in replace mode.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
449
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
450 @param text hex encoded text to be shown in the findtext edit
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
451 @type str
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 self.__replace = True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
454
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 self.__ui.findtextCombo.clear()
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
456 for index, txt in self.__findHistory:
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
457 self.__ui.findtextCombo.addItem(txt, index)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
458 self.__ui.findFormatCombo.setCurrentIndex(0) # 0 is always Hex
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
459 self.on_findFormatCombo_currentIndexChanged(0)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 self.__ui.findtextCombo.setEditText(text)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 self.__ui.findtextCombo.lineEdit().selectAll()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 self.__ui.findtextCombo.setFocus()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 self.on_findtextCombo_editTextChanged(text)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
464
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 self.__ui.replacetextCombo.clear()
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
466 for index, txt in self.__replaceHistory:
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
467 self.__ui.replacetextCombo.addItem(txt, index)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
468 self.__ui.replaceFormatCombo.setCurrentIndex(0) # 0 is always Hex
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
469 self.on_replaceFormatCombo_currentIndexChanged(0)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
470 self.__ui.replacetextCombo.setEditText("")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
471
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 self.__havefound = True
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 self.__findBackwards = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
474
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
475 def show(self, text=""):
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 Public slot to show the widget.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
478
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
479 @param text hex encoded text to be shown in the findtext edit
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
480 @type str
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 if self.__replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 self.__showReplace(text)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 self.__showFind(text)
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8143
diff changeset
486 super().show()
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 self.activateWindow()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
488
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 def on_closeButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 Private slot to close the widget.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 """
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
494 self.__editor.setFocus(Qt.FocusReason.OtherFocusReason)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 self.close()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 def keyPressEvent(self, event):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 Protected slot to handle key press events.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
500
4659
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
501 @param event reference to the key press event
2863d05e83c6 Improved the search/replace history handling of the hex editor and added input validators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4656
diff changeset
502 @type QKeyEvent
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 """
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
504 if event.key() == Qt.Key.Key_Escape:
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 self.close()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
506
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
507 def __convertText(self, txt, oldFormat, newFormat):
4660
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
508 """
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
509 Private method to convert text from one format into another.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
510
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
511 @param txt text to be converted
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
512 @type str
4660
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
513 @param oldFormat current format of the text
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
514 @type str
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
515 @param newFormat format to convert to
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
516 @type str
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
517 @return converted text
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
518 @rtype str
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
519 """
4687
f1d921533cc5 Little improvements to the hex editor goto widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4662
diff changeset
520 if txt and oldFormat and newFormat and oldFormat != newFormat:
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
521 # step 1: convert the text to a byte array using the old format
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
522 byteArray = self.__text2bytearray(txt, oldFormat)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
523
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
524 # step 2: convert the byte array to text using the new format
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
525 txt = self.__bytearray2text(byteArray, newFormat)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
526
4660
9096e8a2df54 Added capability to search for a UTF-8 encoded text in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4659
diff changeset
527 return txt
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
528
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
529 def __int2bytearray(self, value):
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
530 """
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
531 Private method to convert an integer to a byte array.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
532
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
533 @param value value to be converted
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
534 @type int
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
535 @return byte array for the given value
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
536 @rtype bytearray
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
537 """
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
538 ba = bytearray()
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
539 while value > 0:
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
540 value, modulus = divmod(value, 256)
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
541 ba.insert(0, modulus)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
542
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
543 return ba
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
544
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
545 def __bytearray2int(self, array):
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
546 """
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
547 Private method to convert a byte array to an integer value.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
548
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
549 @param array byte array to be converted
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
550 @type bytearray
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
551 @return integer value of the given array
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
552 @rtype int
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
553 """
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
554 value = 0
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
555 for b in array:
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
556 value = value * 256 + b
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
557
4661
bf927c5576c6 Completed the supported formats of the search and replace entry of the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4660
diff changeset
558 return value
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
559
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
560 def __text2bytearray(self, txt, dataFormat):
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
561 """
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
562 Private method to convert a text to a byte array.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
563
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
564 @param txt text to be converted
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
565 @type str
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
566 @param dataFormat format of the text
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
567 @type str
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
568 @return converted text
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
569 @rtype bytearray
7628
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7533
diff changeset
570 @exception ValueError raised to indicate an invalid dataFormat
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7533
diff changeset
571 parameter
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
572 """
7628
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7533
diff changeset
573 if dataFormat not in self.__formatAndValidators.keys():
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7533
diff changeset
574 raise ValueError("Bad value for 'dataFormat' parameter.")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
575
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
576 if dataFormat == "hex": # hex format
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
577 ba = bytearray(QByteArray.fromHex(bytes(txt, encoding="ascii")))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
578 elif dataFormat == "dec": # decimal format
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
579 ba = self.__int2bytearray(int(txt, 10))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
580 elif dataFormat == "oct": # octal format
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
581 ba = self.__int2bytearray(int(txt, 8))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
582 elif dataFormat == "bin": # binary format
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
583 ba = self.__int2bytearray(int(txt, 2))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
584 elif dataFormat == "iso-8859-1": # latin-1/iso-8859-1 text
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
585 ba = bytearray(txt, encoding="iso-8859-1")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
586 elif dataFormat == "utf-8": # utf-8 text
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
587 ba = bytearray(txt, encoding="utf-8")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
588
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
589 return ba
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
590
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
591 def __bytearray2text(self, array, dataFormat):
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
592 """
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
593 Private method to convert a byte array to a text.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
594
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
595 @param array byte array to be converted
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
596 @type bytearray
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
597 @param dataFormat format of the text
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
598 @type str
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
599 @return formatted text
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
600 @rtype str
7628
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7533
diff changeset
601 @exception ValueError raised to indicate an invalid dataFormat
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7533
diff changeset
602 parameter
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
603 """
7628
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7533
diff changeset
604 if dataFormat not in self.__formatAndValidators.keys():
f904d0eef264 Checked the reported security related issue reports generated by the new security checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7533
diff changeset
605 raise ValueError("Bad value for 'dataFormat' parameter.")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
606
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
607 if dataFormat == "hex": # hex format
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
608 txt = "{0:x}".format(self.__bytearray2int(array))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
609 elif dataFormat == "dec": # decimal format
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
610 txt = "{0:d}".format(self.__bytearray2int(array))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
611 elif dataFormat == "oct": # octal format
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
612 txt = "{0:o}".format(self.__bytearray2int(array))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
613 elif dataFormat == "bin": # binary format
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
614 txt = "{0:b}".format(self.__bytearray2int(array))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
615 elif dataFormat == "iso-8859-1": # latin-1/iso-8859-1 text
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
616 txt = str(array, encoding="iso-8859-1")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
617 elif dataFormat == "utf-8": # utf-8 text
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
618 txt = str(array, encoding="utf-8", errors="replace")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
619
4662
33e6bd4b1721 Added function to change the search/replace text according to the selected format in the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4661
diff changeset
620 return txt

eric ide

mercurial