QScintilla/Exporters/ExporterRTF.py

changeset 12
1d8dd9706f46
parent 0
de9c2efb9d02
child 13
1af94a91f439
equal deleted inserted replaced
11:b0996e4a289e 12:1d8dd9706f46
14 14
15 from PyQt4.QtCore import * 15 from PyQt4.QtCore import *
16 from PyQt4.QtGui import * 16 from PyQt4.QtGui import *
17 from PyQt4.Qsci import QsciScintilla 17 from PyQt4.Qsci import QsciScintilla
18 18
19 from ExporterBase import ExporterBase 19 from .ExporterBase import ExporterBase
20 20
21 import Preferences 21 import Preferences
22 22
23 class ExporterRTF(ExporterBase): 23 class ExporterRTF(ExporterBase):
24 """ 24 """
143 else: 143 else:
144 fgColour = self.editor.color() 144 fgColour = self.editor.color()
145 bgColour = self.editor.paper() 145 bgColour = self.editor.paper()
146 146
147 try: 147 try:
148 f = open(filename, "wb") 148 f = open(filename, "w")
149 149
150 styles = {} 150 styles = {}
151 fonts = {} 151 fonts = {}
152 colors = {} 152 colors = {}
153 lastStyle = "" 153 lastStyle = ""
167 istyle > QsciScintilla.STYLE_LASTPREDEFINED): 167 istyle > QsciScintilla.STYLE_LASTPREDEFINED):
168 if lex.description(istyle): 168 if lex.description(istyle):
169 font = lex.font(istyle) 169 font = lex.font(istyle)
170 if wysiwyg: 170 if wysiwyg:
171 fontKey = None 171 fontKey = None
172 for key, value in fonts.items(): 172 for key, value in list(fonts.items()):
173 if value.lower() == font.family().lower(): 173 if value.lower() == font.family().lower():
174 fontKey = key 174 fontKey = key
175 break 175 break
176 if fontKey is None: 176 if fontKey is None:
177 fonts[fontCount] = font.family() 177 fonts[fontCount] = font.family()
190 lastStyle += self.RTF_SETFONTSIZE + \ 190 lastStyle += self.RTF_SETFONTSIZE + \
191 "%d" % fontsize 191 "%d" % fontsize
192 192
193 sColour = lex.color(istyle) 193 sColour = lex.color(istyle)
194 sColourKey = None 194 sColourKey = None
195 for key, value in colors.items(): 195 for key, value in list(colors.items()):
196 if value == sColour: 196 if value == sColour:
197 sColourKey = key 197 sColourKey = key
198 break 198 break
199 if sColourKey is None: 199 if sColourKey is None:
200 colors[colorCount] = sColour 200 colors[colorCount] = sColour
202 colorCount += 1 202 colorCount += 1
203 lastStyle += self.RTF_SETCOLOR + "%d" % sColourKey 203 lastStyle += self.RTF_SETCOLOR + "%d" % sColourKey
204 204
205 sColour = lex.paper(istyle) 205 sColour = lex.paper(istyle)
206 sColourKey = None 206 sColourKey = None
207 for key, value in colors.items(): 207 for key, value in list(colors.items()):
208 if value == sColour: 208 if value == sColour:
209 sColourKey = key 209 sColourKey = key
210 break 210 break
211 if sColourKey is None: 211 if sColourKey is None:
212 colors[colorCount] = sColour 212 colors[colorCount] = sColour
238 self.RTF_SETCOLOR + "0" + \ 238 self.RTF_SETCOLOR + "0" + \
239 self.RTF_SETBACKGROUND + "1" + \ 239 self.RTF_SETBACKGROUND + "1" + \
240 self.RTF_BOLD_OFF + self.RTF_ITALIC_OFF 240 self.RTF_BOLD_OFF + self.RTF_ITALIC_OFF
241 241
242 f.write(self.RTF_FONTDEFCLOSE + self.RTF_COLORDEFOPEN) 242 f.write(self.RTF_FONTDEFCLOSE + self.RTF_COLORDEFOPEN)
243 for value in colors.values(): 243 for value in list(colors.values()):
244 f.write(self.RTF_COLORDEF % \ 244 f.write(self.RTF_COLORDEF % \
245 (value.red(), value.green(), value.blue())) 245 (value.red(), value.green(), value.blue()))
246 f.write(self.RTF_COLORDEFCLOSE) 246 f.write(self.RTF_COLORDEFCLOSE)
247 f.write(self.RTF_INFOOPEN + self.RTF_COMMENT) 247 f.write(self.RTF_INFOOPEN + self.RTF_COMMENT)
248 f.write(time.strftime(self.RTF_CREATED)) 248 f.write(time.strftime(self.RTF_CREATED))
325 prevCR = ch == '\r' 325 prevCR = ch == '\r'
326 pos += 1 326 pos += 1
327 327
328 f.write(self.RTF_BODYCLOSE) 328 f.write(self.RTF_BODYCLOSE)
329 f.close() 329 f.close()
330 except IOError, err: 330 except IOError as err:
331 QApplication.restoreOverrideCursor() 331 QApplication.restoreOverrideCursor()
332 QMessageBox.critical(self.editor, 332 QMessageBox.critical(self.editor,
333 self.trUtf8("Export source"), 333 self.trUtf8("Export source"),
334 self.trUtf8( 334 self.trUtf8(
335 """<p>The source could not be exported to <b>{0}</b>.</p>""" 335 """<p>The source could not be exported to <b>{0}</b>.</p>"""
336 """<p>Reason: {1}</p>""")\ 336 """<p>Reason: {1}</p>""")\
337 .format(filename, unicode(err)), 337 .format(filename, str(err)),
338 QMessageBox.StandardButtons(\ 338 QMessageBox.StandardButtons(\
339 QMessageBox.Ok)) 339 QMessageBox.Ok))
340 finally: 340 finally:
341 QApplication.restoreOverrideCursor() 341 QApplication.restoreOverrideCursor()

eric ide

mercurial