QScintilla/Editor.py

changeset 3394
d1d4d79b4f11
parent 3393
080ace4829b4
child 3395
7e923cf9918f
equal deleted inserted replaced
3393:080ace4829b4 3394:d1d4d79b4f11
733 self.menuActs["NewSplit"].setEnabled(self.vm.canSplit()) 733 self.menuActs["NewSplit"].setEnabled(self.vm.canSplit())
734 self.menu.addAction( 734 self.menu.addAction(
735 UI.PixmapCache.getIcon("close.png"), 735 UI.PixmapCache.getIcon("close.png"),
736 self.tr('Close'), self.__contextClose) 736 self.tr('Close'), self.__contextClose)
737 self.menu.addSeparator() 737 self.menu.addSeparator()
738 self.reopenEncodingMenu = self.__initContextMenuReopenWithEncoding()
739 self.menuActs["Reopen"] = self.menu.addMenu(self.reopenEncodingMenu)
738 self.menuActs["Save"] = self.menu.addAction( 740 self.menuActs["Save"] = self.menu.addAction(
739 UI.PixmapCache.getIcon("fileSave.png"), 741 UI.PixmapCache.getIcon("fileSave.png"),
740 self.tr('Save'), self.__contextSave) 742 self.tr('Save'), self.__contextSave)
741 self.menu.addAction( 743 self.menu.addAction(
742 UI.PixmapCache.getIcon("fileSaveAs.png"), 744 UI.PixmapCache.getIcon("fileSaveAs.png"),
924 self.supportedEncodings[encoding] = act 926 self.supportedEncodings[encoding] = act
925 self.encodingsActGrp.addAction(act) 927 self.encodingsActGrp.addAction(act)
926 928
927 menu.triggered.connect(self.__encodingsMenuTriggered) 929 menu.triggered.connect(self.__encodingsMenuTriggered)
928 menu.aboutToShow.connect(self.__showContextMenuEncodings) 930 menu.aboutToShow.connect(self.__showContextMenuEncodings)
931
932 return menu
933
934 def __initContextMenuReopenWithEncoding(self):
935 """
936 Private method used to setup the Reopen With Encoding context sub menu.
937
938 @return reference to the generated menu (QMenu)
939 """
940 menu = QMenu(self.tr("Re-Open With Encoding"))
941 menu.setIcon(UI.PixmapCache.getIcon("open.png"))
942
943 for encoding in sorted(Utilities.supportedCodecs):
944 act = menu.addAction(encoding)
945 act.setData(encoding)
946
947 menu.triggered.connect(self.__reopenWithEncodingMenuTriggered)
929 948
930 return menu 949 return menu
931 950
932 def __initContextMenuEol(self): 951 def __initContextMenuEol(self):
933 """ 952 """
2810 self.redo() 2829 self.redo()
2811 else: 2830 else:
2812 break 2831 break
2813 # Couldn't find the unmodified state 2832 # Couldn't find the unmodified state
2814 2833
2815 def readFile(self, fn, createIt=False): 2834 def readFile(self, fn, createIt=False, encoding=""):
2816 """ 2835 """
2817 Public slot to read the text from a file. 2836 Public slot to read the text from a file.
2818 2837
2819 @param fn filename to read from (string) 2838 @param fn filename to read from (string)
2820 @param createIt flag indicating the creation of a new file, if the 2839 @keyparam createIt flag indicating the creation of a new file, if the
2821 given one doesn't exist (boolean) 2840 given one doesn't exist (boolean)
2841 @keyparam encoding encoding to be used to read the file (string)
2842 (Note: this parameter overrides encoding detection)
2822 """ 2843 """
2823 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) 2844 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
2824 2845
2825 try: 2846 try:
2826 if createIt and not os.path.exists(fn): 2847 if createIt and not os.path.exists(fn):
2827 f = open(fn, "w") 2848 f = open(fn, "w")
2828 f.close() 2849 f.close()
2829 txt, self.encoding = Utilities.readEncodedFile(fn) 2850 if encoding:
2851 txt, self.encoding = Utilities.readEncodedFileWithEncoding(
2852 fn, encoding)
2853 else:
2854 txt, self.encoding = Utilities.readEncodedFile(fn)
2830 except (UnicodeDecodeError, IOError) as why: 2855 except (UnicodeDecodeError, IOError) as why:
2831 QApplication.restoreOverrideCursor() 2856 QApplication.restoreOverrideCursor()
2832 E5MessageBox.critical( 2857 E5MessageBox.critical(
2833 self.vm, 2858 self.vm,
2834 self.tr('Open File'), 2859 self.tr('Open File'),
4673 4698
4674 def __showContextMenu(self): 4699 def __showContextMenu(self):
4675 """ 4700 """
4676 Private slot handling the aboutToShow signal of the context menu. 4701 Private slot handling the aboutToShow signal of the context menu.
4677 """ 4702 """
4703 # TODO: set enable status of self.menuActs["Reopen"]
4678 self.menuActs["Save"].setEnabled(self.isModified()) 4704 self.menuActs["Save"].setEnabled(self.isModified())
4679 self.menuActs["Undo"].setEnabled(self.isUndoAvailable()) 4705 self.menuActs["Undo"].setEnabled(self.isUndoAvailable())
4680 self.menuActs["Redo"].setEnabled(self.isRedoAvailable()) 4706 self.menuActs["Redo"].setEnabled(self.isRedoAvailable())
4681 self.menuActs["Revert"].setEnabled(self.isModified()) 4707 self.menuActs["Revert"].setEnabled(self.isModified())
4682 if not self.miniMenu: 4708 if not self.miniMenu:
4927 """ 4953 """
4928 Private slot handling the aboutToShow signal of the tools context 4954 Private slot handling the aboutToShow signal of the tools context
4929 menu. 4955 menu.
4930 """ 4956 """
4931 self.showMenu.emit("Tools", self.toolsMenu, self) 4957 self.showMenu.emit("Tools", self.toolsMenu, self)
4958
4959 def __reopenWithEncodingMenuTriggered(self, act):
4960 """
4961 Private method to handle the rereading of the file with a selected
4962 encoding.
4963
4964 @param act reference to the action that was triggered (QAction)
4965 """
4966 encoding = act.data()
4967 self.readFile(self.fileName, encoding=encoding)
4932 4968
4933 def __contextSave(self): 4969 def __contextSave(self):
4934 """ 4970 """
4935 Private slot handling the save context menu entry. 4971 Private slot handling the save context menu entry.
4936 """ 4972 """

eric ide

mercurial