eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py

branch
jsonfiles
changeset 8022
2da0139f4f91
parent 8013
c3bd65c330ed
child 8026
d3eacdbcb18b
equal deleted inserted replaced
8021:a8ba35ce81ad 8022:2da0139f4f91
4 # 4 #
5 5
6 """ 6 """
7 Module implementing the Editor Highlighting Styles configuration page. 7 Module implementing the Editor Highlighting Styles configuration page.
8 """ 8 """
9
10 import os
9 11
10 from PyQt5.QtCore import pyqtSlot, Qt, QFileInfo, QFile, QIODevice 12 from PyQt5.QtCore import pyqtSlot, Qt, QFileInfo, QFile, QIODevice
11 from PyQt5.QtGui import QPalette, QFont 13 from PyQt5.QtGui import QPalette, QFont
12 from PyQt5.QtWidgets import ( 14 from PyQt5.QtWidgets import (
13 QColorDialog, QFontDialog, QInputDialog, QMenu, QTreeWidgetItem, QDialog 15 QColorDialog, QFontDialog, QInputDialog, QMenu, QTreeWidgetItem, QDialog
26 except AttributeError: 28 except AttributeError:
27 MonospacedFontsOption = QFontDialog.FontDialogOptions(0x10) 29 MonospacedFontsOption = QFontDialog.FontDialogOptions(0x10)
28 NoFontsOption = QFontDialog.FontDialogOptions(0) 30 NoFontsOption = QFontDialog.FontDialogOptions(0)
29 31
30 32
33 # TODO: add capability to export a list of selected highlighter styles
31 class EditorHighlightingStylesPage(ConfigurationPageBase, 34 class EditorHighlightingStylesPage(ConfigurationPageBase,
32 Ui_EditorHighlightingStylesPage): 35 Ui_EditorHighlightingStylesPage):
33 """ 36 """
34 Class implementing the Editor Highlighting Styles configuration page. 37 Class implementing the Editor Highlighting Styles configuration page.
35 """ 38 """
515 """ 518 """
516 Private slot to export the styles of all lexers. 519 Private slot to export the styles of all lexers.
517 """ 520 """
518 self.__exportStyles(list(self.lexers.values())) 521 self.__exportStyles(list(self.lexers.values()))
519 522
520 # TODO: do the JSON styles
521 def __exportStyles(self, lexers): 523 def __exportStyles(self, lexers):
522 """ 524 """
523 Private method to export the styles of the given lexers. 525 Private method to export the styles of the given lexers.
524 526
525 @param lexers list of lexer objects for which to export the styles 527 @param lexers list of lexer objects for which to export the styles
528 @type list of PreferencesLexer
526 """ 529 """
527 from eric6config import getConfig 530 from eric6config import getConfig
528 stylesDir = getConfig("ericStylesDir") 531 stylesDir = getConfig("ericStylesDir")
529 532
530 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( 533 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
531 self, 534 self,
532 self.tr("Export Highlighting Styles"), 535 self.tr("Export Highlighting Styles"),
533 stylesDir, 536 stylesDir,
534 self.tr("Highlighting styles file (*.e6h)"), 537 self.tr("Highlighting Styles File (*.ehj);;"
538 "XML Highlighting Styles File (*.e6h)"),
535 "", 539 "",
536 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) 540 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
537 541
538 if not fn: 542 if not fn:
539 return 543 return
542 if not ext: 546 if not ext:
543 ex = selectedFilter.split("(*")[1].split(")")[0] 547 ex = selectedFilter.split("(*")[1].split(")")[0]
544 if ex: 548 if ex:
545 fn += ex 549 fn += ex
546 550
547 f = QFile(fn) 551 if os.path.exists(fn):
548 if f.open(QIODevice.WriteOnly): 552 ok = E5MessageBox.yesNo(
549 from E5XML.HighlightingStylesWriter import HighlightingStylesWriter
550 HighlightingStylesWriter(f, lexers).writeXML()
551 f.close()
552 else:
553 E5MessageBox.critical(
554 self, 553 self,
555 self.tr("Export Highlighting Styles"), 554 self.tr("Export Highlighting Styles"),
556 self.tr( 555 self.tr("""<p>The highlighting styles file <b>{0}</b> exists"""
557 """<p>The highlighting styles could not be exported""" 556 """ already. Overwrite it?</p>""").format(fn))
558 """ to file <b>{0}</b>.</p><p>Reason: {1}</p>""") 557 else:
559 .format(fn, f.errorString()) 558 ok = True
560 ) 559
561 560 if ok:
562 # TODO: do the JSON styles 561 if fn.endswith(".ehj"):
562 from Preferences.HighlightingStylesFile import (
563 HighlightingStylesFile
564 )
565 highlightingStylesFile = HighlightingStylesFile()
566 highlightingStylesFile.writeFile(fn, lexers)
567 else:
568 f = QFile(fn)
569 if f.open(QIODevice.WriteOnly):
570 from E5XML.HighlightingStylesWriter import (
571 HighlightingStylesWriter
572 )
573 HighlightingStylesWriter(f, lexers).writeXML()
574 f.close()
575 else:
576 E5MessageBox.critical(
577 self,
578 self.tr("Export Highlighting Styles"),
579 self.tr("<p>The highlighting styles file <b>{0}</b>"
580 " could not be written.</p><p>Reason: {1}</p>")
581 .format(fn, f.errorString())
582 )
583
563 def __importStyles(self, lexers): 584 def __importStyles(self, lexers):
564 """ 585 """
565 Private method to import the styles of the given lexers. 586 Private method to import the styles of the given lexers.
566 587
567 @param lexers dictionary of lexer objects for which to import the 588 @param lexers dictionary of lexer objects for which to import the
572 593
573 fn = E5FileDialog.getOpenFileName( 594 fn = E5FileDialog.getOpenFileName(
574 self, 595 self,
575 self.tr("Import Highlighting Styles"), 596 self.tr("Import Highlighting Styles"),
576 stylesDir, 597 stylesDir,
577 self.tr("Highlighting styles file (*.e6h *.e4h)")) 598 self.tr("Highlighting Styles File (*.ehj);;"
599 "XML Highlighting Styles File (*.e6h *.e4h)"))
578 600
579 if not fn: 601 if not fn:
580 return 602 return
581 603
582 f = QFile(fn) 604 if fn.endswith(".ehj"):
583 if f.open(QIODevice.ReadOnly): 605 # new JSON based file
584 from E5XML.HighlightingStylesReader import HighlightingStylesReader 606 from Preferences.HighlightingStylesFile import (
585 reader = HighlightingStylesReader(f, lexers) 607 HighlightingStylesFile
586 reader.readXML() 608 )
587 f.close() 609 highlightingStylesFile = HighlightingStylesFile()
610 res = highlightingStylesFile.readFile(fn, lexers)
611 if not res:
612 return
588 else: 613 else:
589 E5MessageBox.critical( 614 # old XML based file
590 self, 615 f = QFile(fn)
591 self.tr("Import Highlighting Styles"), 616 if f.open(QIODevice.ReadOnly):
592 self.tr( 617 from E5XML.HighlightingStylesReader import (
593 """<p>The highlighting styles could not be read""" 618 HighlightingStylesReader
594 """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""") 619 )
595 .format(fn, f.errorString()) 620 reader = HighlightingStylesReader(f, lexers)
596 ) 621 reader.readXML()
597 return 622 f.close()
623 else:
624 E5MessageBox.critical(
625 self,
626 self.tr("Import Highlighting Styles"),
627 self.tr(
628 "<p>The highlighting styles file <b>{0}</b> could not"
629 " be read.</p><p>Reason: {1}</p>"
630 ).format(fn, f.errorString())
631 )
632 return
598 633
599 self.on_lexerLanguageComboBox_activated( 634 self.on_lexerLanguageComboBox_activated(
600 self.lexerLanguageComboBox.currentText()) 635 self.lexerLanguageComboBox.currentText())
601 636
602 ####################################################################### 637 #######################################################################

eric ide

mercurial