IconEditor/IconEditorWindow.py

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

eric ide

mercurial