6 """ |
6 """ |
7 Module implementing the icon editor main window. |
7 Module implementing the icon editor main window. |
8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
|
11 import contextlib |
11 |
12 |
12 from PyQt5.QtCore import ( |
13 from PyQt5.QtCore import ( |
13 pyqtSignal, Qt, QSize, QSignalMapper, QFileInfo, QFile, QEvent |
14 pyqtSignal, Qt, QSize, QSignalMapper, QFileInfo, QFile, QEvent |
14 ) |
15 ) |
15 from PyQt5.QtGui import ( |
16 from PyQt5.QtGui import ( |
51 eric (boolean) |
52 eric (boolean) |
52 @param initShortcutsOnly flag indicating to just initialize the |
53 @param initShortcutsOnly flag indicating to just initialize the |
53 keyboard shortcuts (boolean) |
54 keyboard shortcuts (boolean) |
54 @param project reference to the project object (Project) |
55 @param project reference to the project object (Project) |
55 """ |
56 """ |
56 super(IconEditorWindow, self).__init__(parent) |
57 super().__init__(parent) |
57 self.setObjectName("eric6_icon_editor") |
58 self.setObjectName("eric6_icon_editor") |
58 |
59 |
59 self.fromEric = fromEric |
60 self.fromEric = fromEric |
60 self.initShortcutsOnly = initShortcutsOnly |
61 self.initShortcutsOnly = initShortcutsOnly |
61 self.setWindowIcon(UI.PixmapCache.getIcon("iconEditor")) |
62 self.setWindowIcon(UI.PixmapCache.getIcon("iconEditor")) |
152 } |
153 } |
153 |
154 |
154 inputFormats = [] |
155 inputFormats = [] |
155 readFormats = QImageReader.supportedImageFormats() |
156 readFormats = QImageReader.supportedImageFormats() |
156 for readFormat in readFormats: |
157 for readFormat in readFormats: |
157 try: |
158 with contextlib.suppress(KeyError): |
158 inputFormats.append(filters[bytes(readFormat).decode()]) |
159 inputFormats.append(filters[bytes(readFormat).decode()]) |
159 except KeyError: |
|
160 pass |
|
161 inputFormats.sort() |
160 inputFormats.sort() |
162 inputFormats.append(self.tr("All Files (*)")) |
161 inputFormats.append(self.tr("All Files (*)")) |
163 self.__inputFilter = ';;'.join(inputFormats) |
162 self.__inputFilter = ';;'.join(inputFormats) |
164 |
163 |
165 outputFormats = [] |
164 outputFormats = [] |
166 writeFormats = QImageWriter.supportedImageFormats() |
165 writeFormats = QImageWriter.supportedImageFormats() |
167 for writeFormat in writeFormats: |
166 for writeFormat in writeFormats: |
168 try: |
167 with contextlib.suppress(KeyError): |
169 outputFormats.append(filters[bytes(writeFormat).decode()]) |
168 outputFormats.append(filters[bytes(writeFormat).decode()]) |
170 except KeyError: |
|
171 pass |
|
172 outputFormats.sort() |
169 outputFormats.sort() |
173 self.__outputFilter = ';;'.join(outputFormats) |
170 self.__outputFilter = ';;'.join(outputFormats) |
174 |
171 |
175 self.__defaultFilter = filters['png'] |
172 self.__defaultFilter = filters['png'] |
176 |
173 |
1025 state = self.saveState() |
1022 state = self.saveState() |
1026 Preferences.setIconEditor("IconEditorState", state) |
1023 Preferences.setIconEditor("IconEditorState", state) |
1027 |
1024 |
1028 Preferences.setGeometry("IconEditorGeometry", self.saveGeometry()) |
1025 Preferences.setGeometry("IconEditorGeometry", self.saveGeometry()) |
1029 |
1026 |
1030 try: |
1027 with contextlib.suppress(ValueError): |
1031 if self.fromEric or len(self.__class__.windows) > 1: |
1028 if self.fromEric or len(self.__class__.windows) > 1: |
1032 del self.__class__.windows[ |
1029 del self.__class__.windows[ |
1033 self.__class__.windows.index(self)] |
1030 self.__class__.windows.index(self)] |
1034 except ValueError: |
|
1035 pass |
|
1036 |
1031 |
1037 if not self.fromEric: |
1032 if not self.fromEric: |
1038 Preferences.syncPreferences() |
1033 Preferences.syncPreferences() |
1039 |
1034 |
1040 evt.accept() |
1035 evt.accept() |
1064 def __openIcon(self): |
1059 def __openIcon(self): |
1065 """ |
1060 """ |
1066 Private slot to open an icon file. |
1061 Private slot to open an icon file. |
1067 """ |
1062 """ |
1068 if self.__maybeSave(): |
1063 if self.__maybeSave(): |
1069 if not self.__lastOpenPath: |
1064 if ( |
1070 if self.__project and self.__project.isOpen(): |
1065 not self.__lastOpenPath and |
1071 self.__lastOpenPath = self.__project.getProjectPath() |
1066 self.__project is not None and |
|
1067 self.__project.isOpen() |
|
1068 ): |
|
1069 self.__lastOpenPath = self.__project.getProjectPath() |
1072 |
1070 |
1073 fileName = E5FileDialog.getOpenFileNameAndFilter( |
1071 fileName = E5FileDialog.getOpenFileNameAndFilter( |
1074 self, |
1072 self, |
1075 self.tr("Open icon file"), |
1073 self.tr("Open icon file"), |
1076 self.__lastOpenPath, |
1074 self.__lastOpenPath, |
1096 """ |
1094 """ |
1097 Private slot to save the icon with a new name. |
1095 Private slot to save the icon with a new name. |
1098 |
1096 |
1099 @return flag indicating success (boolean) |
1097 @return flag indicating success (boolean) |
1100 """ |
1098 """ |
1101 if not self.__lastSavePath: |
1099 if ( |
1102 if self.__project and self.__project.isOpen(): |
1100 not self.__lastSavePath and |
1103 self.__lastSavePath = self.__project.getProjectPath() |
1101 self.__project is not None and |
|
1102 self.__project.isOpen() |
|
1103 ): |
|
1104 self.__lastSavePath = self.__project.getProjectPath() |
1104 if not self.__lastSavePath and self.__lastOpenPath: |
1105 if not self.__lastSavePath and self.__lastOpenPath: |
1105 self.__lastSavePath = self.__lastOpenPath |
1106 self.__lastSavePath = self.__lastOpenPath |
1106 |
1107 |
1107 fileName, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
1108 fileName, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
1108 self, |
1109 self, |
1221 |
1222 |
1222 @param fileName name of the file to register (string) |
1223 @param fileName name of the file to register (string) |
1223 """ |
1224 """ |
1224 self.__fileName = fileName |
1225 self.__fileName = fileName |
1225 |
1226 |
1226 if not self.__fileName: |
1227 shownName = ( |
1227 shownName = self.tr("Untitled") |
1228 self.__strippedName(self.__fileName) |
1228 else: |
1229 if self.__fileName else |
1229 shownName = self.__strippedName(self.__fileName) |
1230 self.tr("Untitled") |
|
1231 ) |
1230 |
1232 |
1231 self.setWindowTitle(self.tr("{0}[*] - {1}") |
1233 self.setWindowTitle(self.tr("{0}[*] - {1}") |
1232 .format(shownName, self.tr("Icon Editor"))) |
1234 .format(shownName, self.tr("Icon Editor"))) |
1233 |
1235 |
1234 self.setWindowModified(self.__editor.isDirty()) |
1236 self.setWindowModified(self.__editor.isDirty()) |
1382 """ |
1384 """ |
1383 if evt.type() == QEvent.Type.Gesture: |
1385 if evt.type() == QEvent.Type.Gesture: |
1384 self.gestureEvent(evt) |
1386 self.gestureEvent(evt) |
1385 return True |
1387 return True |
1386 |
1388 |
1387 return super(IconEditorWindow, self).event(evt) |
1389 return super().event(evt) |
1388 |
1390 |
1389 def gestureEvent(self, evt): |
1391 def gestureEvent(self, evt): |
1390 """ |
1392 """ |
1391 Protected method handling gesture events. |
1393 Protected method handling gesture events. |
1392 |
1394 |