eric7/HexEdit/HexEditSearchReplaceWidget.py

Sun, 16 May 2021 20:07:24 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 16 May 2021 20:07:24 +0200
branch
eric7
changeset 8318
962bce857696
parent 8312
800c432b34c8
child 8356
68ec9c3d4de5
permissions
-rw-r--r--

Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.

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
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7775
diff changeset
3 # Copyright (c) 2016 - 2021 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
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 from E5Gui.E5Action import E5Action
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 from E5Gui import E5MessageBox
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 """
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
24 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
25 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 Constructor
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 @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
29 @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
30 @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
31 @type HexEditMainWindow
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 @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
33 @type bool
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 @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
35 @type QWidget
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 """
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
37 super().__init__(parent)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.__replace = replace
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 self.__editor = editor
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41
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
42 # 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
43 self.__formatAndValidators = {
7775
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7628
diff changeset
44 "hex": (self.tr("Hex"), QRegularExpressionValidator(
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7628
diff changeset
45 QRegularExpression("[0-9a-f]*"))),
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7628
diff changeset
46 "dec": (self.tr("Dec"), QRegularExpressionValidator(
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7628
diff changeset
47 QRegularExpression("[0-9]*"))),
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7628
diff changeset
48 "oct": (self.tr("Oct"), QRegularExpressionValidator(
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7628
diff changeset
49 QRegularExpression("[0-7]*"))),
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7628
diff changeset
50 "bin": (self.tr("Bin"), QRegularExpressionValidator(
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7628
diff changeset
51 QRegularExpression("[01]*"))),
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
52 "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
53 # 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
54 "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
55 # 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
56 }
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
57 formatOrder = ["hex", "dec", "oct", "bin", "iso-8859-1", "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
58
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
59 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
60 self.__currentReplaceFormat = ""
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
61
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
62 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
63 if replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 from .Ui_HexEditReplaceWidget import Ui_HexEditReplaceWidget
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
65 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
66 self.__ui = Ui_HexEditReplaceWidget()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 from .Ui_HexEditSearchWidget import Ui_HexEditSearchWidget
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 self.__ui = Ui_HexEditSearchWidget()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 self.__ui.setupUi(self)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
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
72 self.__ui.closeButton.setIcon(UI.PixmapCache.getIcon("close"))
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 self.__ui.findPrevButton.setIcon(
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
74 UI.PixmapCache.getIcon("1leftarrow"))
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 self.__ui.findNextButton.setIcon(
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
76 UI.PixmapCache.getIcon("1rightarrow"))
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 if replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 self.__ui.replaceButton.setIcon(
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
80 UI.PixmapCache.getIcon("editReplace"))
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 self.__ui.replaceSearchButton.setIcon(
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
82 UI.PixmapCache.getIcon("editReplaceSearch"))
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 self.__ui.replaceAllButton.setIcon(
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
84 UI.PixmapCache.getIcon("editReplaceAll"))
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
86 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
87 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
88 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
89 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
90 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
91 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
92 self.__ui.replaceFormatCombo.addItem(formatStr, dataFormat)
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
93
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 self.__ui.findtextCombo.setCompleter(None)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 self.__ui.findtextCombo.lineEdit().returnPressed.connect(
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 self.__findByReturnPressed)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 if replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 self.__ui.replacetextCombo.setCompleter(None)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 self.__ui.replacetextCombo.lineEdit().returnPressed.connect(
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 self.on_replaceButton_clicked)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 self.findNextAct = E5Action(
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 self.tr('Find Next'),
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 self.tr('Find Next'),
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 0, 0, self, 'hexEditor_search_widget_find_next')
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 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
107 self.findNextAct.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 self.__ui.findtextCombo.addAction(self.findNextAct)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 self.findPrevAct = E5Action(
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 self.tr('Find Prev'),
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 self.tr('Find Prev'),
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 0, 0, self, 'hexEditor_search_widget_find_prev')
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 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
115 self.findPrevAct.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 self.__ui.findtextCombo.addAction(self.findPrevAct)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 self.__havefound = False
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119
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
120 @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
121 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
122 """
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
123 Private slot to handle a selection from the find format.
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
124
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
125 @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
126 @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
127 """
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
128 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
129 findFormat = self.__ui.findFormatCombo.itemData(idx)
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
130
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
131 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
132 txt = self.__ui.findtextCombo.currentText()
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
133 newTxt = self.__convertText(
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
134 txt, self.__currentFindFormat, findFormat)
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
135 self.__currentFindFormat = findFormat
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
136
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
137 self.__ui.findtextCombo.setValidator(
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
138 self.__formatAndValidators[findFormat][1])
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
139
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
140 self.__ui.findtextCombo.setEditText(newTxt)
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
141
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
142 @pyqtSlot(str)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 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
144 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 Private slot to enable/disable the find buttons.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146
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
147 @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
148 @type str
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 if not txt:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 self.__ui.findNextButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 self.findNextAct.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 self.__ui.findPrevButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 self.findPrevAct.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 if self.__replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 self.__ui.replaceButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 self.__ui.replaceSearchButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 self.__ui.replaceAllButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 self.__ui.findNextButton.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 self.findNextAct.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 self.__ui.findPrevButton.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 self.findPrevAct.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 if self.__replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 self.__ui.replaceButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 self.__ui.replaceSearchButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 self.__ui.replaceAllButton.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168
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
169 @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
170 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
171 """
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
172 Private slot to handle a selection from the find 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
173
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
174 @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
175 @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
176 """
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
177 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
178 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
179 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
180 self.__ui.findFormatCombo.setCurrentIndex(formatIndex)
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
181
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 def __getContent(self, replace=False):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 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
185 a bytearray.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 @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
188 @type bool
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 @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
190 @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
191 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 if replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 textCombo = self.__ui.replacetextCombo
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 formatCombo = self.__ui.replaceFormatCombo
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 history = self.__replaceHistory
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 textCombo = self.__ui.findtextCombo
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 formatCombo = self.__ui.findFormatCombo
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 history = self.__findHistory
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 txt = textCombo.currentText()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 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
203 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
204 ba = self.__text2bytearray(txt, findFormat)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 # 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
207 # 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
208 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
209 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
210 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
211 history.insert(0, historyEntry)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 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
213 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
214 textCombo.addItem(text, index)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 return ba, txt
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 def on_findNextButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 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
222 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 self.findPrevNext(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 def on_findPrevButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 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
229 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 self.findPrevNext(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 def findPrevNext(self, prev=False):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 Public slot to find the next occurrence of the search term.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 @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
237 @type bool
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 @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
239 @rtype bool
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 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
242 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
243 return False
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 self.__findBackwards = prev
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 ba, txt = self.__getContent()
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 idx = -1
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 if len(ba) > 0:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 startIndex = self.__editor.cursorPosition() // 2
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 if prev:
7254
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
252 if (
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
253 self.__editor.hasSelection() and
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
254 startIndex == self.__editor.getSelectionEnd()
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
255 ):
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 # skip to the selection start
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 startIndex = self.__editor.getSelectionBegin()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 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
259 else:
7254
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
260 if (
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
261 self.__editor.hasSelection() and
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
262 startIndex == self.__editor.getSelectionBegin() - 1
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
263 ):
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 # skip to the selection end
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 startIndex = self.__editor.getSelectionEnd()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 idx = self.__editor.indexOf(ba, startIndex)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 if idx >= 0:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 if self.__replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 self.__ui.replaceButton.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 self.__ui.replaceSearchButton.setEnabled(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 E5MessageBox.information(
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 self, self.windowTitle(),
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 self.tr("'{0}' was not found.").format(txt))
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 return idx >= 0
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 def __findByReturnPressed(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 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
282 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 if self.__findBackwards:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 self.findPrevNext(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 self.findPrevNext(False)
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
287
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
288 @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
289 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
290 """
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
291 Private slot to handle a selection from the replace format.
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
292
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
293 @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
294 @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
295 """
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
296 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
297 replaceFormat = self.__ui.replaceFormatCombo.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
298
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
299 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
300 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
301 newTxt = self.__convertText(
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
302 txt, self.__currentReplaceFormat, replaceFormat)
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
303 self.__currentReplaceFormat = replaceFormat
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
304
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
305 self.__ui.replacetextCombo.setValidator(
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
306 self.__formatAndValidators[replaceFormat][1])
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
307
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
308 self.__ui.replacetextCombo.setEditText(newTxt)
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
309
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
310 @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
311 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
312 """
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 Private slot to handle a selection from the replace 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
314
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
315 @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
316 @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
317 """
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
318 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
319 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
320 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
321 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
322
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 def on_replaceButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 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
327 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 self.__doReplace(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 def on_replaceSearchButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 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
334 one.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 self.__doReplace(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 def __doReplace(self, searchNext):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 Private method 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
341
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 @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
343 @type bool
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 # 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
346 if (
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
347 not self.__ui.replaceButton.isEnabled() and
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
348 not self.__ui.replaceSearchButton.isEnabled()
f00d825fbdb3 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
349 ):
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 return
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351
4654
9dafe6905667 Fixed an issue in the hex editor search and replace widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4653
diff changeset
352 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
353 rba, rtxt = self.__getContent(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 ok = False
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 if self.__editor.hasSelection():
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 # 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
358 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
359 self.__editor.replaceByteArray(startIdx, len(fba), rba)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 if searchNext:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 ok = self.findPrevNext(self.__findBackwards)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 if not ok:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 self.__ui.replaceButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 self.__ui.replaceSearchButton.setEnabled(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 def on_replaceAllButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 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
372 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 replacements = 0
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 cursorPosition = self.__editor.cursorPosition()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 fba, ftxt = self.__getContent(False)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 rba, rtxt = self.__getContent(True)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 idx = 0
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 while idx >= 0:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 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
383 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
384 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
385 idx += len(rba)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 replacements += 1
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 if replacements:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389 E5MessageBox.information(
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 self, self.windowTitle(),
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 self.tr("Replaced {0} occurrences.")
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 .format(replacements))
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 E5MessageBox.information(
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 self, self.windowTitle(),
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 self.tr("Nothing replaced because '{0}' was not found.")
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 .format(ftxt))
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 self.__editor.setCursorPosition(cursorPosition)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 self.__editor.ensureVisible()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 def __showFind(self, text=''):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 Private method to display this widget in find mode.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405
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
406 @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
407 @type str
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 self.__replace = False
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 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
412 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
413 self.__ui.findtextCombo.addItem(txt, index)
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
414 self.__ui.findFormatCombo.setCurrentIndex(0) # 0 is always Hex
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
415 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
416 self.__ui.findtextCombo.setEditText(text)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 self.__ui.findtextCombo.lineEdit().selectAll()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 self.__ui.findtextCombo.setFocus()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 self.on_findtextCombo_editTextChanged(text)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 self.__havefound = True
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 self.__findBackwards = False
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 def __showReplace(self, text=''):
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 slot to display this widget in replace mode.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
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 = True
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432
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)
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
436 self.__ui.findFormatCombo.setCurrentIndex(0) # 0 is always Hex
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)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 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
444 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
445 self.__ui.replacetextCombo.addItem(txt, index)
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
446 self.__ui.replaceFormatCombo.setCurrentIndex(0) # 0 is always Hex
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
447 self.on_replaceFormatCombo_currentIndexChanged(0)
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 self.__ui.replacetextCombo.setEditText('')
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 self.__havefound = True
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 self.__findBackwards = False
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 def show(self, text=''):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 Public slot to show the widget.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456
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
457 @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
458 @type str
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 if self.__replace:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 self.__showReplace(text)
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 else:
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 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
464 super().show()
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 self.activateWindow()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 @pyqtSlot()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 def on_closeButton_clicked(self):
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 """
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 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
471 """
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
472 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
473 self.close()
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 def keyPressEvent(self, event):
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 Protected slot to handle key press events.
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
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 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
480 @type QKeyEvent
4652
a88a2ba7a48a Added search and replace capability to the hex editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481 """
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
482 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
483 self.close()
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
484
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
485 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
486 """
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
487 Private method to convert text from one format into another.
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
488
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
489 @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
490 @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
491 @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
492 @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
493 @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
494 @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
495 @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
496 @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
497 """
4687
f1d921533cc5 Little improvements to the hex editor goto widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4662
diff changeset
498 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
499 # 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
500 byteArray = self.__text2bytearray(txt, oldFormat)
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
501
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
502 # 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
503 txt = self.__bytearray2text(byteArray, newFormat)
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
504
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
505 return txt
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
506
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 __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
508 """
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
509 Private method to convert an integer to a byte array.
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
510
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 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
512 @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
513 @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
514 @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
515 """
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
516 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
517 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
518 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
519 ba.insert(0, modulus)
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
520
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
521 return ba
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
522
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
523 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
524 """
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
525 Private method to convert a byte array to an integer 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
526
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
527 @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
528 @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
529 @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
530 @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
531 """
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
532 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
533 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
534 value = value * 256 + b
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
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 return value
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
537
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
538 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
539 """
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
540 Private method to convert a text to a byte array.
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
541
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
542 @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
543 @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
544 @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
545 @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
546 @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
547 @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
548 @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
549 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
550 """
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
551 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
552 raise ValueError("Bad value for 'dataFormat' 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
553
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
554 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
555 ba = bytearray(QByteArray.fromHex(
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
556 bytes(txt, encoding="ascii")))
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
557 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
558 ba = self.__int2bytearray(int(txt, 10))
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
559 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
560 ba = self.__int2bytearray(int(txt, 8))
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
561 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
562 ba = self.__int2bytearray(int(txt, 2))
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
563 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
564 ba = bytearray(txt, encoding="iso-8859-1")
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
565 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
566 ba = bytearray(txt, encoding="utf-8")
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
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 ba
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
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
570 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
571 """
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 Private method to convert a byte array to a 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
573
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
574 @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
575 @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
576 @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
577 @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
578 @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
579 @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
580 @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
581 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
582 """
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
583 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
584 raise ValueError("Bad value for 'dataFormat' 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
585
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
586 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
587 txt = "{0:x}".format(self.__bytearray2int(array))
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
588 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
589 txt = "{0:d}".format(self.__bytearray2int(array))
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
590 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
591 txt = "{0:o}".format(self.__bytearray2int(array))
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
592 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
593 txt = "{0:b}".format(self.__bytearray2int(array))
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
594 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
595 txt = str(array, encoding="iso-8859-1")
5587
ea526b78ee6c Started to fix code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
596 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
597 txt = str(array, encoding="utf-8", errors="replace")
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
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 txt

eric ide

mercurial