eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py

branch
jsonfiles
changeset 8028
a4f1b68c0737
parent 8026
d3eacdbcb18b
child 8143
2c730d5fd177
equal deleted inserted replaced
8027:ccbfefe5bc5a 8028:a4f1b68c0737
28 except AttributeError: 28 except AttributeError:
29 MonospacedFontsOption = QFontDialog.FontDialogOptions(0x10) 29 MonospacedFontsOption = QFontDialog.FontDialogOptions(0x10)
30 NoFontsOption = QFontDialog.FontDialogOptions(0) 30 NoFontsOption = QFontDialog.FontDialogOptions(0)
31 31
32 32
33 # TODO: add capability to export a list of selected highlighter styles
34 class EditorHighlightingStylesPage(ConfigurationPageBase, 33 class EditorHighlightingStylesPage(ConfigurationPageBase,
35 Ui_EditorHighlightingStylesPage): 34 Ui_EditorHighlightingStylesPage):
36 """ 35 """
37 Class implementing the Editor Highlighting Styles configuration page. 36 Class implementing the Editor Highlighting Styles configuration page.
38 """ 37 """
116 @param language The lexer language (string) 115 @param language The lexer language (string)
117 """ 116 """
118 self.styleElementList.clear() 117 self.styleElementList.clear()
119 self.styleGroup.setEnabled(False) 118 self.styleGroup.setEnabled(False)
120 self.lexer = None 119 self.lexer = None
121
122 self.exportCurrentButton.setEnabled(language != "")
123 self.importCurrentButton.setEnabled(language != "")
124 120
125 if not language: 121 if not language:
126 return 122 return
127 123
128 try: 124 try:
491 ####################################################################### 487 #######################################################################
492 ## Importing and exporting of styles 488 ## Importing and exporting of styles
493 ####################################################################### 489 #######################################################################
494 490
495 @pyqtSlot() 491 @pyqtSlot()
496 def on_importCurrentButton_clicked(self): 492 def on_importButton_clicked(self):
497 """ 493 """
498 Private slot to import the styles of the current lexer. 494 Private slot to import styles to be selected.
499 """ 495 """
500 self.__importStyles({self.lexer.language(): self.lexer}) 496 self.__importStyles(importAll=False)
501 497
502 @pyqtSlot() 498 @pyqtSlot()
503 def on_exportCurrentButton_clicked(self): 499 def on_exportButton_clicked(self):
504 """ 500 """
505 Private slot to export the styles of the current lexer. 501 Private slot to export styles to be selected.
506 """ 502 """
507 self.__exportStyles([self.lexer]) 503 self.__exportStyles(exportAll=False)
508 504
509 @pyqtSlot() 505 @pyqtSlot()
510 def on_importAllButton_clicked(self): 506 def on_importAllButton_clicked(self):
511 """ 507 """
512 Private slot to import the styles of all lexers. 508 Private slot to import the styles of all lexers.
513 """ 509 """
514 self.__importStyles(self.lexers) 510 self.__importStyles(importAll=True)
515 511
516 @pyqtSlot() 512 @pyqtSlot()
517 def on_exportAllButton_clicked(self): 513 def on_exportAllButton_clicked(self):
518 """ 514 """
519 Private slot to export the styles of all lexers. 515 Private slot to export the styles of all lexers.
520 """ 516 """
521 self.__exportStyles(list(self.lexers.values())) 517 self.__exportStyles(exportAll=True)
522 518
523 def __exportStyles(self, lexers): 519 def __exportStyles(self, exportAll=False):
524 """ 520 """
525 Private method to export the styles of the given lexers. 521 Private method to export the styles of selectable lexers.
526 522
527 @param lexers list of lexer objects for which to export the styles 523 @param exportAll flag indicating to export all styles without asking
528 @type list of PreferencesLexer 524 (defaults to False)
525 @type bool (optional)
529 """ 526 """
530 from eric6config import getConfig 527 from eric6config import getConfig
531 stylesDir = getConfig("ericStylesDir") 528 stylesDir = getConfig("ericStylesDir")
529
530 lexerNames = list(self.lexers.keys())
531 if not exportAll:
532 if self.lexer:
533 preselect = [self.lexer.language()]
534 else:
535 preselect = []
536 from .EditorHighlightingStylesSelectionDialog import (
537 EditorHighlightingStylesSelectionDialog)
538 dlg = EditorHighlightingStylesSelectionDialog(
539 lexerNames, forImport=False, preselect=preselect)
540 if dlg.exec() == QDialog.Accepted:
541 lexerNames = dlg.getLexerNames()
542 else:
543 # Cancelled by user
544 return
545
546 lexers = [self.lexers[name] for name in lexerNames]
532 547
533 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( 548 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
534 self, 549 self,
535 self.tr("Export Highlighting Styles"), 550 self.tr("Export Highlighting Styles"),
536 stylesDir, 551 stylesDir,
579 self.tr("<p>The highlighting styles file <b>{0}</b>" 594 self.tr("<p>The highlighting styles file <b>{0}</b>"
580 " could not be written.</p><p>Reason: {1}</p>") 595 " could not be written.</p><p>Reason: {1}</p>")
581 .format(fn, f.errorString()) 596 .format(fn, f.errorString())
582 ) 597 )
583 598
584 def __importStyles(self, lexers): 599 def __importStyles(self, importAll=False):
585 """ 600 """
586 Private method to import the styles of the given lexers. 601 Private method to import the styles of lexers to be selected.
587 602
588 @param lexers dictionary of lexer objects for which to import the 603 @param importAll flag indicating to import all styles without asking
589 styles 604 (defaults to False)
605 @type bool (optional)
590 """ 606 """
591 from eric6config import getConfig 607 from eric6config import getConfig
592 stylesDir = getConfig("ericStylesDir") 608 stylesDir = getConfig("ericStylesDir")
593 609
594 fn = E5FileDialog.getOpenFileName( 610 fn = E5FileDialog.getOpenFileName(
605 # new JSON based file 621 # new JSON based file
606 from Preferences.HighlightingStylesFile import ( 622 from Preferences.HighlightingStylesFile import (
607 HighlightingStylesFile 623 HighlightingStylesFile
608 ) 624 )
609 highlightingStylesFile = HighlightingStylesFile() 625 highlightingStylesFile = HighlightingStylesFile()
610 styles = highlightingStylesFile.readFile(fn, lexers) 626 styles = highlightingStylesFile.readFile(fn)
611 if not styles: 627 if not styles:
612 return 628 return
613 else: 629 else:
614 # old XML based file 630 # old XML based file
615 f = QFile(fn) 631 f = QFile(fn)
616 if f.open(QIODevice.ReadOnly): 632 if f.open(QIODevice.ReadOnly):
617 from E5XML.HighlightingStylesReader import ( 633 from E5XML.HighlightingStylesReader import (
618 HighlightingStylesReader 634 HighlightingStylesReader
619 ) 635 )
620 reader = HighlightingStylesReader(f, lexers) 636 reader = HighlightingStylesReader(f, self.lexers)
621 styles = reader.readXML() 637 styles = reader.readXML()
622 f.close() 638 f.close()
623 if not styles: 639 if not styles:
624 return 640 return
625 else: 641 else:
631 " be read.</p><p>Reason: {1}</p>" 647 " be read.</p><p>Reason: {1}</p>"
632 ).format(fn, f.errorString()) 648 ).format(fn, f.errorString())
633 ) 649 )
634 return 650 return
635 651
636 self.__applyStyles(styles, lexers) 652 self.__applyStyles(styles, importAll=importAll)
637 self.on_lexerLanguageComboBox_activated( 653 self.on_lexerLanguageComboBox_activated(
638 self.lexerLanguageComboBox.currentText()) 654 self.lexerLanguageComboBox.currentText())
639 655
640 def __applyStyles(self, stylesList, lexersList): 656 def __applyStyles(self, stylesList, importAll=False):
641 """ 657 """
642 Private method to apply the imported styles to this dialog. 658 Private method to apply the imported styles to this dialog.
643 659
644 @param stylesList list of imported lexer styles 660 @param stylesList list of imported lexer styles
645 @type list of dict 661 @type list of dict
646 @param lexersList list of lexers to apply the styles to 662 @param importAll flag indicating to import all styles without asking
647 @type list of PreferencesLexer 663 (defaults to False)
648 """ 664 @type bool (optional)
665 """
666 lexerNames = [d["name"]
667 for d in stylesList
668 if d["name"] in self.lexers]
669
670 if not importAll:
671 from .EditorHighlightingStylesSelectionDialog import (
672 EditorHighlightingStylesSelectionDialog)
673 dlg = EditorHighlightingStylesSelectionDialog(
674 lexerNames, forImport=True)
675 if dlg.exec() == QDialog.Accepted:
676 lexerNames = dlg.getLexerNames()
677 else:
678 # Cancelled by user
679 return
680
649 for lexerDict in stylesList: 681 for lexerDict in stylesList:
650 if lexerDict["name"] in lexersList: 682 if lexerDict["name"] in lexerNames:
651 lexer = lexersList[lexerDict["name"]] 683 lexer = self.lexers[lexerDict["name"]]
652 for styleDict in lexerDict["styles"]: 684 for styleDict in lexerDict["styles"]:
653 style = styleDict["style"] 685 style = styleDict["style"]
654 substyle = styleDict["substyle"] 686 substyle = styleDict["substyle"]
655 lexer.setColor(QColor(styleDict["color"]), style, substyle) 687 lexer.setColor(QColor(styleDict["color"]), style, substyle)
656 lexer.setPaper(QColor(styleDict["paper"]), style, substyle) 688 lexer.setPaper(QColor(styleDict["paper"]), style, substyle)

eric ide

mercurial