IconEditor/IconEditorWindow.py

changeset 2807
73c38615aced
parent 2404
cba0ff902c2b
child 2847
1843ef6e2656
child 2957
c7f8c7561542
equal deleted inserted replaced
2806:0dc655f1655d 2807:73c38615aced
5 5
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
11
10 from PyQt4.QtCore import pyqtSignal, Qt, QSize, QSignalMapper, QFileInfo, QFile, \ 12 from PyQt4.QtCore import pyqtSignal, Qt, QSize, QSignalMapper, QFileInfo, QFile, \
11 QEvent 13 QEvent
12 from PyQt4.QtGui import QScrollArea, QPalette, QImage, QImageReader, QImageWriter, \ 14 from PyQt4.QtGui import QScrollArea, QPalette, QImage, QImageReader, QImageWriter, \
13 QKeySequence, qApp, QLabel, QDockWidget, QWhatsThis 15 QKeySequence, QLabel, QDockWidget, QWhatsThis
14 16
15 from E5Gui.E5Action import E5Action, createActionGroup 17 from E5Gui.E5Action import E5Action, createActionGroup
16 from E5Gui import E5FileDialog, E5MessageBox 18 from E5Gui import E5FileDialog, E5MessageBox
17 from E5Gui.E5MainWindow import E5MainWindow 19 from E5Gui.E5MainWindow import E5MainWindow
18 from E5Gui.E5ZoomWidget import E5ZoomWidget 20 from E5Gui.E5ZoomWidget import E5ZoomWidget
34 editorClosed = pyqtSignal() 36 editorClosed = pyqtSignal()
35 37
36 windows = [] 38 windows = []
37 39
38 def __init__(self, fileName="", parent=None, fromEric=False, 40 def __init__(self, fileName="", parent=None, fromEric=False,
39 initShortcutsOnly=False): 41 initShortcutsOnly=False, project=None):
40 """ 42 """
41 Constructor 43 Constructor
42 44
43 @param fileName name of a file to load on startup (string) 45 @param fileName name of a file to load on startup (string)
44 @param parent parent widget of this window (QWidget) 46 @param parent parent widget of this window (QWidget)
45 @keyparam fromEric flag indicating whether it was called from within 47 @keyparam fromEric flag indicating whether it was called from within
46 eric5 (boolean) 48 eric5 (boolean)
47 @keyparam initShortcutsOnly flag indicating to just initialize the keyboard 49 @keyparam initShortcutsOnly flag indicating to just initialize the keyboard
48 shortcuts (boolean) 50 shortcuts (boolean)
51 @keyparam project reference to the project object (Project)
49 """ 52 """
50 super().__init__(parent) 53 super().__init__(parent)
51 self.setObjectName("eric5_icon_editor") 54 self.setObjectName("eric5_icon_editor")
52 self.setAttribute(Qt.WA_DeleteOnClose) 55 self.setAttribute(Qt.WA_DeleteOnClose)
53 56
101 self.__setCurrentFile("") 104 self.__setCurrentFile("")
102 if fileName: 105 if fileName:
103 self.__loadIconFile(fileName) 106 self.__loadIconFile(fileName)
104 107
105 self.__checkActions() 108 self.__checkActions()
109
110 self.__project = project
111 self.__lastOpenPath = ""
112 self.__lastSavePath = ""
106 113
107 self.grabGesture(Qt.PinchGesture) 114 self.grabGesture(Qt.PinchGesture)
108 115
109 def __initFileFilters(self): 116 def __initFileFilters(self):
110 """ 117 """
271 self.exitAct.setStatusTip(self.trUtf8('Quit the icon editor')) 278 self.exitAct.setStatusTip(self.trUtf8('Quit the icon editor'))
272 self.exitAct.setWhatsThis(self.trUtf8( 279 self.exitAct.setWhatsThis(self.trUtf8(
273 """<b>Quit</b>""" 280 """<b>Quit</b>"""
274 """<p>Quit the icon editor.</p>""" 281 """<p>Quit the icon editor.</p>"""
275 )) 282 ))
276 if self.fromEric: 283 if not self.fromEric:
277 self.exitAct.triggered[()].connect(self.close) 284 self.exitAct.triggered[()].connect(self.__closeAll)
278 else:
279 self.exitAct.triggered[()].connect(qApp.closeAllWindows)
280 self.__actions.append(self.exitAct) 285 self.__actions.append(self.exitAct)
281 286
282 def __initEditActions(self): 287 def __initEditActions(self):
283 """ 288 """
284 Private method to create the Edit actions. 289 Private method to create the Edit actions.
750 menu.addAction(self.saveAct) 755 menu.addAction(self.saveAct)
751 menu.addAction(self.saveAsAct) 756 menu.addAction(self.saveAsAct)
752 menu.addSeparator() 757 menu.addSeparator()
753 menu.addAction(self.closeAct) 758 menu.addAction(self.closeAct)
754 menu.addAction(self.closeAllAct) 759 menu.addAction(self.closeAllAct)
755 menu.addSeparator() 760 if not self.fromEric:
756 menu.addAction(self.exitAct) 761 menu.addSeparator()
762 menu.addAction(self.exitAct)
757 763
758 menu = mb.addMenu(self.trUtf8("&Edit")) 764 menu = mb.addMenu(self.trUtf8("&Edit"))
759 menu.setTearOffEnabled(True) 765 menu.setTearOffEnabled(True)
760 menu.addAction(self.undoAct) 766 menu.addAction(self.undoAct)
761 menu.addAction(self.redoAct) 767 menu.addAction(self.redoAct)
817 filetb.addSeparator() 823 filetb.addSeparator()
818 filetb.addAction(self.saveAct) 824 filetb.addAction(self.saveAct)
819 filetb.addAction(self.saveAsAct) 825 filetb.addAction(self.saveAsAct)
820 filetb.addSeparator() 826 filetb.addSeparator()
821 filetb.addAction(self.closeAct) 827 filetb.addAction(self.closeAct)
822 filetb.addAction(self.exitAct) 828 if not self.fromEric:
829 filetb.addAction(self.exitAct)
823 830
824 edittb = self.addToolBar(self.trUtf8("Edit")) 831 edittb = self.addToolBar(self.trUtf8("Edit"))
825 edittb.setObjectName("EditToolBar") 832 edittb.setObjectName("EditToolBar")
826 edittb.setIconSize(UI.Config.ToolBarIconSize) 833 edittb.setIconSize(UI.Config.ToolBarIconSize)
827 edittb.addAction(self.undoAct) 834 edittb.addAction(self.undoAct)
928 Preferences.setIconEditor("IconEditorState", state) 935 Preferences.setIconEditor("IconEditorState", state)
929 936
930 Preferences.setGeometry("IconEditorGeometry", self.saveGeometry()) 937 Preferences.setGeometry("IconEditorGeometry", self.saveGeometry())
931 938
932 try: 939 try:
933 del self.__class__.windows[self.__class__.windows.index(self)] 940 if self.fromEric or len(self.__class__.windows) > 1:
941 del self.__class__.windows[self.__class__.windows.index(self)]
934 except ValueError: 942 except ValueError:
935 pass 943 pass
936 944
937 if not self.fromEric: 945 if not self.fromEric:
938 Preferences.syncPreferences() 946 Preferences.syncPreferences()
954 962
955 def __newWindow(self): 963 def __newWindow(self):
956 """ 964 """
957 Public slot called to open a new icon editor window. 965 Public slot called to open a new icon editor window.
958 """ 966 """
959 ie = IconEditorWindow(parent=self.parent(), fromEric=self.fromEric) 967 ie = IconEditorWindow(parent=self.parent(), fromEric=self.fromEric,
968 project=self.__project)
969 ie.setRecentPaths(self.__lastOpenPath, self.__lastSavePath)
960 ie.show() 970 ie.show()
961 971
962 def __openIcon(self): 972 def __openIcon(self):
963 """ 973 """
964 Private slot to open an icon file. 974 Private slot to open an icon file.
965 """ 975 """
966 if self.__maybeSave(): 976 if self.__maybeSave():
977 if not self.__lastOpenPath:
978 if self.__project and self.__project.isOpen():
979 self.__lastOpenPath = self.__project.getProjectPath()
980
967 fileName = E5FileDialog.getOpenFileNameAndFilter( 981 fileName = E5FileDialog.getOpenFileNameAndFilter(
968 self, 982 self,
969 self.trUtf8("Open icon file"), 983 self.trUtf8("Open icon file"),
970 "", 984 self.__lastOpenPath,
971 self.__inputFilter, 985 self.__inputFilter,
972 self.__defaultFilter)[0] 986 self.__defaultFilter)[0]
973 if fileName: 987 if fileName:
974 self.__loadIconFile(fileName) 988 self.__loadIconFile(fileName)
989 self.__lastOpenPath = os.path.dirname(fileName)
975 self.__checkActions() 990 self.__checkActions()
976 991
977 def __saveIcon(self): 992 def __saveIcon(self):
978 """ 993 """
979 Private slot to save the icon. 994 Private slot to save the icon.
985 1000
986 def __saveIconAs(self): 1001 def __saveIconAs(self):
987 """ 1002 """
988 Private slot to save the icon with a new name. 1003 Private slot to save the icon with a new name.
989 """ 1004 """
1005 if not self.__lastSavePath:
1006 if self.__project and self.__project.isOpen():
1007 self.__lastSavePath = self.__project.getProjectPath()
1008 if not self.__lastSavePath and self.__lastOpenPath:
1009 self.__lastSavePath = self.__lastOpenPath
1010
990 fileName, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( 1011 fileName, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
991 self, 1012 self,
992 self.trUtf8("Save icon file"), 1013 self.trUtf8("Save icon file"),
993 "", 1014 self.__lastSavePath,
994 self.__outputFilter, 1015 self.__outputFilter,
995 self.__defaultFilter, 1016 self.__defaultFilter,
996 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) 1017 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
997 if not fileName: 1018 if not fileName:
998 return False 1019 return False
1009 " Overwrite it?</p>").format(fileName), 1030 " Overwrite it?</p>").format(fileName),
1010 icon=E5MessageBox.Warning) 1031 icon=E5MessageBox.Warning)
1011 if not res: 1032 if not res:
1012 return False 1033 return False
1013 1034
1035 self.__lastSavePath = os.path.dirname(fileName)
1036
1014 return self.__saveIconFile(fileName) 1037 return self.__saveIconFile(fileName)
1015 1038
1016 def __closeAll(self): 1039 def __closeAll(self):
1017 """ 1040 """
1018 Private slot to close all other windows. 1041 Private slot to close all other windows.
1019 """ 1042 """
1020 for win in self.__class__.windows[:]: 1043 for win in self.__class__.windows[:]:
1021 if win != self: 1044 if win != self:
1022 win.close() 1045 win.close()
1046 self.close()
1023 1047
1024 def __loadIconFile(self, fileName): 1048 def __loadIconFile(self, fileName):
1025 """ 1049 """
1026 Private method to load an icon file. 1050 Private method to load an icon file.
1027 1051
1124 self.__saveIcon) 1148 self.__saveIcon)
1125 if not ret: 1149 if not ret:
1126 return False 1150 return False
1127 return True 1151 return True
1128 1152
1153 def setRecentPaths(self, openPath, savePath):
1154 """
1155 Public method to set the last open and save paths.
1156
1157 @param openPath least recently used open path (string)
1158 @param savePath least recently used save path (string)
1159 """
1160 if openPath:
1161 self.__lastOpenPath = openPath
1162 if savePath:
1163 self.__lastSavePath = savePath
1164
1129 def __checkActions(self): 1165 def __checkActions(self):
1130 """ 1166 """
1131 Private slot to check some actions for their enable/disable status. 1167 Private slot to check some actions for their enable/disable status.
1132 """ 1168 """
1133 self.saveAct.setEnabled(self.__editor.isDirty()) 1169 self.saveAct.setEnabled(self.__editor.isDirty())

eric ide

mercurial