7 Module implementing the hex editor main window. |
7 Module implementing the hex editor main window. |
8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
11 import contextlib |
11 import contextlib |
|
12 import pathlib |
12 |
13 |
13 from PyQt6.QtCore import ( |
14 from PyQt6.QtCore import ( |
14 pyqtSignal, pyqtSlot, QFile, QFileInfo, QSize, QCoreApplication, QLocale, |
15 pyqtSignal, pyqtSlot, QFile, QSize, QCoreApplication, QLocale, QIODevice |
15 QIODevice |
|
16 ) |
16 ) |
17 from PyQt6.QtGui import QKeySequence, QAction |
17 from PyQt6.QtGui import QKeySequence, QAction |
18 from PyQt6.QtWidgets import ( |
18 from PyQt6.QtWidgets import ( |
19 QWhatsThis, QLabel, QWidget, QVBoxLayout, QDialog, QFrame, QMenu |
19 QWhatsThis, QLabel, QWidget, QVBoxLayout, QDialog, QFrame, QMenu |
20 ) |
20 ) |
1080 self.tr("All Files (*)"), |
1080 self.tr("All Files (*)"), |
1081 EricFileDialog.DontConfirmOverwrite) |
1081 EricFileDialog.DontConfirmOverwrite) |
1082 if not fileName: |
1082 if not fileName: |
1083 return False |
1083 return False |
1084 |
1084 |
1085 if QFileInfo(fileName).exists(): |
1085 if pathlib.Path(fileName).exists(): |
1086 res = EricMessageBox.yesNo( |
1086 res = EricMessageBox.yesNo( |
1087 self, |
1087 self, |
1088 self.tr("Save binary file"), |
1088 self.tr("Save binary file"), |
1089 self.tr("<p>The file <b>{0}</b> already exists." |
1089 self.tr("<p>The file <b>{0}</b> already exists." |
1090 " Overwrite it?</p>").format(fileName), |
1090 " Overwrite it?</p>").format(fileName), |
1163 self.tr("Text Files (*.txt)"), |
1163 self.tr("Text Files (*.txt)"), |
1164 EricFileDialog.DontConfirmOverwrite) |
1164 EricFileDialog.DontConfirmOverwrite) |
1165 if not fileName: |
1165 if not fileName: |
1166 return |
1166 return |
1167 |
1167 |
1168 ext = QFileInfo(fileName).suffix() |
1168 fpath = pathlib.Path(fileName) |
1169 if not ext: |
1169 if not fpath.suffix: |
1170 ex = selectedFilter.split("(*")[1].split(")")[0] |
1170 ex = selectedFilter.split("(*")[1].split(")")[0] |
1171 if ex: |
1171 if ex: |
1172 fileName += ex |
1172 fpath = fpath.with_suffix(ex) |
1173 if QFileInfo(fileName).exists(): |
1173 if fpath.exists(): |
1174 res = EricMessageBox.yesNo( |
1174 res = EricMessageBox.yesNo( |
1175 self, |
1175 self, |
1176 self.tr("Save to readable file"), |
1176 self.tr("Save to readable file"), |
1177 self.tr("<p>The file <b>{0}</b> already exists." |
1177 self.tr("<p>The file <b>{0}</b> already exists." |
1178 " Overwrite it?</p>").format(fileName), |
1178 " Overwrite it?</p>").format(str(fpath)), |
1179 icon=EricMessageBox.Warning) |
1179 icon=EricMessageBox.Warning) |
1180 if not res: |
1180 if not res: |
1181 return |
1181 return |
1182 |
|
1183 file = QFile(fileName) |
|
1184 if not file.open(QIODevice.OpenModeFlag.WriteOnly): |
|
1185 EricMessageBox.warning( |
|
1186 self, self.tr("eric Hex Editor"), |
|
1187 self.tr("Cannot write file '{0}:\n{1}.") |
|
1188 .format(fileName, file.errorString())) |
|
1189 return |
|
1190 |
1182 |
1191 readableData = ( |
1183 readableData = ( |
1192 self.__editor.selectionToReadableString() |
1184 self.__editor.selectionToReadableString() |
1193 if selectionOnly else |
1185 if selectionOnly else |
1194 self.__editor.toReadableString() |
1186 self.__editor.toReadableString() |
1195 ) |
1187 ) |
1196 res = file.write(readableData.encode("latin1")) != -1 |
1188 try: |
1197 file.close() |
1189 with fpath.open("w", encoding="latin1") as f: |
1198 |
1190 f.write(readableData) |
1199 if not res: |
1191 except OSError as err: |
1200 EricMessageBox.warning( |
1192 EricMessageBox.warning( |
1201 self, self.tr("eric Hex Editor"), |
1193 self, self.tr("eric Hex Editor"), |
1202 self.tr("Cannot write file '{0}:\n{1}.") |
1194 self.tr("Cannot write file '{0}:\n{1}.") |
1203 .format(fileName, file.errorString())) |
1195 .format(str(fpath), str(err))) |
1204 return |
1196 return |
1205 |
1197 |
1206 self.__statusBar.showMessage(self.tr("File saved"), 2000) |
1198 self.__statusBar.showMessage(self.tr("File saved"), 2000) |
1207 |
1199 |
1208 def __saveSelectionReadable(self): |
1200 def __saveSelectionReadable(self): |
1256 @param fullFileName full pathname of the given file |
1248 @param fullFileName full pathname of the given file |
1257 @type str |
1249 @type str |
1258 @return filename part |
1250 @return filename part |
1259 @rtype str |
1251 @rtype str |
1260 """ |
1252 """ |
1261 return QFileInfo(fullFileName).fileName() |
1253 return pathlib.Path(fullFileName).name |
1262 |
1254 |
1263 def setRecentPaths(self, openPath, savePath): |
1255 def setRecentPaths(self, openPath, savePath): |
1264 """ |
1256 """ |
1265 Public method to set the last open and save paths. |
1257 Public method to set the last open and save paths. |
1266 |
1258 |
1433 formatStr.format( |
1425 formatStr.format( |
1434 idx, |
1426 idx, |
1435 Utilities.compactPath( |
1427 Utilities.compactPath( |
1436 rs, HexEditMainWindow.maxMenuFilePathLen))) |
1428 rs, HexEditMainWindow.maxMenuFilePathLen))) |
1437 act.setData(rs) |
1429 act.setData(rs) |
1438 act.setEnabled(QFileInfo(rs).exists()) |
1430 act.setEnabled(pathlib.Path(rs).exists()) |
1439 |
1431 |
1440 self.__recentMenu.addSeparator() |
1432 self.__recentMenu.addSeparator() |
1441 self.__recentMenu.addAction(self.tr('&Clear'), self.__clearRecent) |
1433 self.__recentMenu.addAction(self.tr('&Clear'), self.__clearRecent) |
1442 |
1434 |
1443 @pyqtSlot(QAction) |
1435 @pyqtSlot(QAction) |
1467 self.__recent = [] |
1459 self.__recent = [] |
1468 Preferences.Prefs.rsettings.sync() |
1460 Preferences.Prefs.rsettings.sync() |
1469 rs = Preferences.Prefs.rsettings.value(recentNameHexFiles) |
1461 rs = Preferences.Prefs.rsettings.value(recentNameHexFiles) |
1470 if rs is not None: |
1462 if rs is not None: |
1471 for f in Preferences.toList(rs): |
1463 for f in Preferences.toList(rs): |
1472 if QFileInfo(f).exists(): |
1464 if pathlib.Path(f).exists(): |
1473 self.__recent.append(f) |
1465 self.__recent.append(f) |
1474 |
1466 |
1475 def __saveRecent(self): |
1467 def __saveRecent(self): |
1476 """ |
1468 """ |
1477 Private method to save the list of recently opened files. |
1469 Private method to save the list of recently opened files. |