QScintilla/MiniEditor.py

changeset 12
1d8dd9706f46
parent 7
c679fb30c8f3
child 13
1af94a91f439
equal deleted inserted replaced
11:b0996e4a289e 12:1d8dd9706f46
16 from PyQt4.Qsci import QsciScintilla 16 from PyQt4.Qsci import QsciScintilla
17 17
18 from E4Gui.E4Application import e4App 18 from E4Gui.E4Application import e4App
19 from E4Gui.E4Action import E4Action, createActionGroup 19 from E4Gui.E4Action import E4Action, createActionGroup
20 20
21 import Lexers 21 from . import Lexers
22 from QsciScintillaCompat import QsciScintillaCompat, QSCINTILLA_VERSION 22 from .QsciScintillaCompat import QsciScintillaCompat, QSCINTILLA_VERSION
23 from SearchReplaceWidget import SearchReplaceWidget 23 from .SearchReplaceWidget import SearchReplaceWidget
24 24
25 import UI.PixmapCache 25 import UI.PixmapCache
26 import UI.Config 26 import UI.Config
27 27
28 from Printer import Printer 28 from .Printer import Printer
29 29
30 import Preferences 30 import Preferences
31 31
32 class MiniScintilla(QsciScintillaCompat): 32 class MiniScintilla(QsciScintillaCompat):
33 """ 33 """
43 """ 43 """
44 QsciScintillaCompat.__init__(self, parent) 44 QsciScintillaCompat.__init__(self, parent)
45 45
46 self.mw = parent 46 self.mw = parent
47 47
48 def getFileName(self):
49 """
50 Public method to return the name of the file being displayed.
51
52 @return filename of the displayed file (string)
53 """
54 return self.mw.getFileName()
55
48 def focusInEvent(self, event): 56 def focusInEvent(self, event):
49 """ 57 """
50 Protected method called when the editor receives focus. 58 Protected method called when the editor receives focus.
51 59
52 This method checks for modifications of the current file and 60 This method checks for modifications of the current file and
96 self.setWindowIcon(UI.PixmapCache.getIcon("editor.png")) 104 self.setWindowIcon(UI.PixmapCache.getIcon("editor.png"))
97 105
98 self.__textEdit = MiniScintilla(self) 106 self.__textEdit = MiniScintilla(self)
99 self.__textEdit.clearSearchIndicators = self.clearSearchIndicators 107 self.__textEdit.clearSearchIndicators = self.clearSearchIndicators
100 self.__textEdit.setSearchIndicator = self.setSearchIndicator 108 self.__textEdit.setSearchIndicator = self.setSearchIndicator
109 self.__textEdit.setUtf8(True)
101 110
102 self.srHistory = { 111 self.srHistory = {
103 "search" : [], 112 "search" : [],
104 "replace" : [] 113 "replace" : []
105 } 114 }
1495 Private method to load the given file. 1504 Private method to load the given file.
1496 1505
1497 @param fileName name of the file to load (string) 1506 @param fileName name of the file to load (string)
1498 @param filetype type of the source file (string) 1507 @param filetype type of the source file (string)
1499 """ 1508 """
1500 file= QFile(fileName) 1509 try:
1501 if not file.open(QFile.ReadOnly): 1510 f = open(fileName, 'r')
1502 QMessageBox.warning(self, self.trUtf8("eric4 Mini Editor"), 1511 except IOError as why:
1503 self.trUtf8("Cannot read file {0}:\n{1}.")\ 1512 QMessageBox.critical(self, self.trUtf8('Open File'),
1504 .format(fileName, file.errorString())) 1513 self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>'
1514 '<p>Reason: {1}</p>')
1515 .format(fileName, str(why)))
1505 return 1516 return
1506 1517
1507 input = QTextStream(file) 1518 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
1508 QApplication.setOverrideCursor(Qt.WaitCursor) 1519 try:
1509 txt = input.readAll() 1520 txt = f.read()
1521 except UnicodeDecodeError as why:
1522 QApplication.restoreOverrideCursor()
1523 QMessageBox.critical(self, self.trUtf8('Open File'),
1524 self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>'
1525 '<p>Reason: {1}</p>')
1526 .format(fileName, str(why)))
1527 return
1528 f.close()
1510 self.__textEdit.setText(txt) 1529 self.__textEdit.setText(txt)
1511 QApplication.restoreOverrideCursor() 1530 QApplication.restoreOverrideCursor()
1512 1531
1513 if filetype is None: 1532 if filetype is None:
1514 self.filetype = "" 1533 self.filetype = ""
1885 self.languagesActGrp.addAction(self.noLanguageAct) 1904 self.languagesActGrp.addAction(self.noLanguageAct)
1886 menu.addSeparator() 1905 menu.addSeparator()
1887 1906
1888 self.supportedLanguages = {} 1907 self.supportedLanguages = {}
1889 supportedLanguages = Lexers.getSupportedLanguages() 1908 supportedLanguages = Lexers.getSupportedLanguages()
1890 languages = supportedLanguages.keys() 1909 languages = sorted(list(supportedLanguages.keys()))
1891 languages.sort()
1892 for language in languages: 1910 for language in languages:
1893 if language != "Guessed": 1911 if language != "Guessed":
1894 self.supportedLanguages[language] = supportedLanguages[language][:] 1912 self.supportedLanguages[language] = supportedLanguages[language][:]
1895 act = menu.addAction(self.supportedLanguages[language][0]) 1913 act = menu.addAction(self.supportedLanguages[language][0])
1896 act.setCheckable(True) 1914 act.setCheckable(True)
2137 if not fn: 2155 if not fn:
2138 return "" 2156 return ""
2139 2157
2140 try: 2158 try:
2141 if createIt and not os.path.exists(fn): 2159 if createIt and not os.path.exists(fn):
2142 f = open(fn, "wb") 2160 f = open(fn, "w")
2143 f.close() 2161 f.close()
2144 f = open(fn, 'rb') 2162 f = open(fn, 'r')
2145 except IOError: 2163 except IOError as why:
2146 QMessageBox.critical(self, self.trUtf8('Open File'), 2164 QMessageBox.critical(self, self.trUtf8('Open File'),
2147 self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>') 2165 self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>'
2148 .format(fn)) 2166 '<p>Reason: {1}</p>')
2167 .format(fn, str(why)))
2149 raise 2168 raise
2150 2169
2151 txt = f.readline() 2170 try:
2171 txt = f.readline()
2172 except UnicodeDecodeError as why:
2173 QMessageBox.critical(self, self.trUtf8('Open File'),
2174 self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>'
2175 '<p>Reason: {1}</p>')
2176 .format(fn, str(why)))
2177 raise
2152 f.close() 2178 f.close()
2153 return txt 2179 return txt
2154 2180
2155 ########################################################## 2181 ##########################################################
2156 ## Methods needed for the search functionality 2182 ## Methods needed for the search functionality

eric ide

mercurial