QScintilla/MiniEditor.py

changeset 45
9a18f4dbb493
parent 42
23b45a742e17
child 53
c3eb7cc1ff8b
equal deleted inserted replaced
44:fe5cd20cb0eb 45:9a18f4dbb493
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 Utilities
30 import Preferences 31 import Preferences
31 32
32 class MiniScintilla(QsciScintillaCompat): 33 class MiniScintilla(QsciScintillaCompat):
33 """ 34 """
34 Class implementing a QsciScintillaCompat subclass for handling focus events. 35 Class implementing a QsciScintillaCompat subclass for handling focus events.
105 106
106 self.__textEdit = MiniScintilla(self) 107 self.__textEdit = MiniScintilla(self)
107 self.__textEdit.clearSearchIndicators = self.clearSearchIndicators 108 self.__textEdit.clearSearchIndicators = self.clearSearchIndicators
108 self.__textEdit.setSearchIndicator = self.setSearchIndicator 109 self.__textEdit.setSearchIndicator = self.setSearchIndicator
109 self.__textEdit.setUtf8(True) 110 self.__textEdit.setUtf8(True)
111 self.encoding = Preferences.getEditor("DefaultEncoding")
110 112
111 self.srHistory = { 113 self.srHistory = {
112 "search" : [], 114 "search" : [],
113 "replace" : [] 115 "replace" : []
114 } 116 }
1504 Private method to load the given file. 1506 Private method to load the given file.
1505 1507
1506 @param fileName name of the file to load (string) 1508 @param fileName name of the file to load (string)
1507 @param filetype type of the source file (string) 1509 @param filetype type of the source file (string)
1508 """ 1510 """
1511 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
1512
1509 try: 1513 try:
1510 f = open(fileName, 'r') 1514 txt, self.encoding = Utilities.readEncodedFile(fileName)
1511 except IOError as why: 1515 except (UnicodeDecodeError, IOError) as why:
1512 QMessageBox.critical(self, self.trUtf8('Open File'),
1513 self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>'
1514 '<p>Reason: {1}</p>')
1515 .format(fileName, str(why)))
1516 return
1517
1518 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
1519 try:
1520 txt = f.read()
1521 except UnicodeDecodeError as why:
1522 QApplication.restoreOverrideCursor() 1516 QApplication.restoreOverrideCursor()
1523 QMessageBox.critical(self, self.trUtf8('Open File'), 1517 QMessageBox.critical(self, self.trUtf8('Open File'),
1524 self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>' 1518 self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>'
1525 '<p>Reason: {1}</p>') 1519 '<p>Reason: {1}</p>')
1526 .format(fileName, str(why))) 1520 .format(fileName, str(why)))
1521 QApplication.restoreOverrideCursor()
1527 return 1522 return
1528 f.close() 1523
1529 self.__textEdit.setText(txt) 1524 self.__textEdit.setText(txt)
1530 QApplication.restoreOverrideCursor() 1525 QApplication.restoreOverrideCursor()
1531 1526
1532 if filetype is None: 1527 if filetype is None:
1533 self.filetype = "" 1528 self.filetype = ""
1545 Private method to save to the given file. 1540 Private method to save to the given file.
1546 1541
1547 @param fileName name of the file to save to (string) 1542 @param fileName name of the file to save to (string)
1548 @return flag indicating success (boolean) 1543 @return flag indicating success (boolean)
1549 """ 1544 """
1550 file = QFile(fileName) 1545 QApplication.setOverrideCursor(Qt.WaitCursor)
1551 if not file.open(QFile.WriteOnly): 1546 txt = self.__textEdit.text()
1552 QMessageBox.warning(self, self.trUtf8("eric5 Mini Editor"), 1547 try:
1553 self.trUtf8("Cannot write file {0}:\n{1}.")\ 1548 self.encoding = Utilities.writeEncodedFile(fileName, txt, self.encoding)
1554 .format(fileName, file.errorString())) 1549 except (IOError, Utilities.CodingError, UnicodeError) as why:
1550 QMessageBox.critical(self, self.trUtf8('Save File'),
1551 self.trUtf8('<p>The file <b>{0}</b> could not be saved.<br/>'
1552 'Reason: {1}</p>')
1553 .format(fileName, str(why)))
1554 QApplication.restoreOverrideCursor()
1555 1555
1556 self.__checkActions() 1556 self.__checkActions()
1557 1557
1558 return False 1558 return False
1559 1559
1560 out = QTextStream(file)
1561 QApplication.setOverrideCursor(Qt.WaitCursor)
1562 out << self.__textEdit.text()
1563 QApplication.restoreOverrideCursor() 1560 QApplication.restoreOverrideCursor()
1564 self.emit(SIGNAL("editorSaved")) 1561 self.emit(SIGNAL("editorSaved"))
1565 1562
1566 self.__setCurrentFile(fileName) 1563 self.__setCurrentFile(fileName)
1567 self.__statusBar.showMessage(self.trUtf8("File saved"), 2000) 1564 self.__statusBar.showMessage(self.trUtf8("File saved"), 2000)
1587 .format(shownName, self.trUtf8("Mini Editor"))) 1584 .format(shownName, self.trUtf8("Mini Editor")))
1588 1585
1589 self.__textEdit.setModified(False) 1586 self.__textEdit.setModified(False)
1590 self.setWindowModified(False) 1587 self.setWindowModified(False)
1591 1588
1592 try: 1589 self.setLanguage(self.__bindName(self.__textEdit.text(0)))
1593 line0 = self.readLine0(self.__curFile)
1594 except IOError:
1595 line0 = ""
1596 self.setLanguage(self.__bindName(line0))
1597 1590
1598 def getFileName(self): 1591 def getFileName(self):
1599 """ 1592 """
1600 Public method to return the name of the file being displayed. 1593 Public method to return the name of the file being displayed.
1601 1594
2141 elif "dmd" in line0: 2134 elif "dmd" in line0:
2142 bindName = "dummy.d" 2135 bindName = "dummy.d"
2143 self.filetype = "D" 2136 self.filetype = "D"
2144 return bindName 2137 return bindName
2145 2138
2146 def readLine0(self, fn, createIt = False):
2147 """
2148 Public slot to read the first line from a file.
2149
2150 @param fn filename to read from (string)
2151 @param createIt flag indicating the creation of a new file, if the given
2152 one doesn't exist (boolean)
2153 @return first line of the file (string)
2154 """
2155 if not fn:
2156 return ""
2157
2158 try:
2159 if createIt and not os.path.exists(fn):
2160 f = open(fn, "w")
2161 f.close()
2162 f = open(fn, 'r')
2163 except IOError as why:
2164 QMessageBox.critical(self, self.trUtf8('Open File'),
2165 self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>'
2166 '<p>Reason: {1}</p>')
2167 .format(fn, str(why)))
2168 raise
2169
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
2178 f.close()
2179 return txt
2180
2181 ########################################################## 2139 ##########################################################
2182 ## Methods needed for the search functionality 2140 ## Methods needed for the search functionality
2183 ########################################################## 2141 ##########################################################
2184 2142
2185 def getSRHistory(self, key): 2143 def getSRHistory(self, key):

eric ide

mercurial