Highlighting Styles: changed the import/export logic to allow the selection of lexers to be imported/exported. jsonfiles

Fri, 29 Jan 2021 19:31:47 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 29 Jan 2021 19:31:47 +0100
branch
jsonfiles
changeset 8028
a4f1b68c0737
parent 8027
ccbfefe5bc5a
child 8031
ce5858a237d2

Highlighting Styles: changed the import/export logic to allow the selection of lexers to be imported/exported.

eric6.e4p file | annotate | diff | comparison | revisions
eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py file | annotate | diff | comparison | revisions
eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.ui file | annotate | diff | comparison | revisions
eric6/Preferences/ConfigurationPages/EditorHighlightingStylesSelectionDialog.py file | annotate | diff | comparison | revisions
eric6/Preferences/ConfigurationPages/EditorHighlightingStylesSelectionDialog.ui file | annotate | diff | comparison | revisions
eric6/Preferences/HighlightingStylesFile.py file | annotate | diff | comparison | revisions
--- a/eric6.e4p	Fri Jan 29 19:30:59 2021 +0100
+++ b/eric6.e4p	Fri Jan 29 19:31:47 2021 +0100
@@ -724,6 +724,7 @@
     <Source>eric6/Preferences/ConfigurationPages/EditorGeneralPage.py</Source>
     <Source>eric6/Preferences/ConfigurationPages/EditorHighlightersPage.py</Source>
     <Source>eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py</Source>
+    <Source>eric6/Preferences/ConfigurationPages/EditorHighlightingStylesSelectionDialog.py</Source>
     <Source>eric6/Preferences/ConfigurationPages/EditorKeywordsPage.py</Source>
     <Source>eric6/Preferences/ConfigurationPages/EditorLanguageTabIndentOverrideDialog.py</Source>
     <Source>eric6/Preferences/ConfigurationPages/EditorMouseClickHandlerPage.py</Source>
@@ -1909,6 +1910,7 @@
     <Form>eric6/Preferences/ConfigurationPages/EditorGeneralPage.ui</Form>
     <Form>eric6/Preferences/ConfigurationPages/EditorHighlightersPage.ui</Form>
     <Form>eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.ui</Form>
+    <Form>eric6/Preferences/ConfigurationPages/EditorHighlightingStylesSelectionDialog.ui</Form>
     <Form>eric6/Preferences/ConfigurationPages/EditorKeywordsPage.ui</Form>
     <Form>eric6/Preferences/ConfigurationPages/EditorLanguageTabIndentOverrideDialog.ui</Form>
     <Form>eric6/Preferences/ConfigurationPages/EditorMouseClickHandlerPage.ui</Form>
--- a/eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py	Fri Jan 29 19:30:59 2021 +0100
+++ b/eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py	Fri Jan 29 19:31:47 2021 +0100
@@ -30,7 +30,6 @@
 NoFontsOption = QFontDialog.FontDialogOptions(0)
 
 
-# TODO: add capability to export a list of selected highlighter styles
 class EditorHighlightingStylesPage(ConfigurationPageBase,
                                    Ui_EditorHighlightingStylesPage):
     """
@@ -119,9 +118,6 @@
         self.styleGroup.setEnabled(False)
         self.lexer = None
         
-        self.exportCurrentButton.setEnabled(language != "")
-        self.importCurrentButton.setEnabled(language != "")
-        
         if not language:
             return
         
@@ -493,43 +489,62 @@
     #######################################################################
     
     @pyqtSlot()
-    def on_importCurrentButton_clicked(self):
+    def on_importButton_clicked(self):
         """
-        Private slot to import the styles of the current lexer.
+        Private slot to import styles to be selected.
         """
-        self.__importStyles({self.lexer.language(): self.lexer})
+        self.__importStyles(importAll=False)
     
     @pyqtSlot()
-    def on_exportCurrentButton_clicked(self):
+    def on_exportButton_clicked(self):
         """
-        Private slot to export the styles of the current lexer.
+        Private slot to export styles to be selected.
         """
-        self.__exportStyles([self.lexer])
+        self.__exportStyles(exportAll=False)
     
     @pyqtSlot()
     def on_importAllButton_clicked(self):
         """
         Private slot to import the styles of all lexers.
         """
-        self.__importStyles(self.lexers)
+        self.__importStyles(importAll=True)
     
     @pyqtSlot()
     def on_exportAllButton_clicked(self):
         """
         Private slot to export the styles of all lexers.
         """
-        self.__exportStyles(list(self.lexers.values()))
+        self.__exportStyles(exportAll=True)
     
-    def __exportStyles(self, lexers):
+    def __exportStyles(self, exportAll=False):
         """
-        Private method to export the styles of the given lexers.
+        Private method to export the styles of selectable lexers.
         
-        @param lexers list of lexer objects for which to export the styles
-        @type list of PreferencesLexer
+        @param exportAll flag indicating to export all styles without asking
+            (defaults to False)
+        @type bool (optional)
         """
         from eric6config import getConfig
         stylesDir = getConfig("ericStylesDir")
         
+        lexerNames = list(self.lexers.keys())
+        if not exportAll:
+            if self.lexer:
+                preselect = [self.lexer.language()]
+            else:
+                preselect = []
+            from .EditorHighlightingStylesSelectionDialog import (
+                EditorHighlightingStylesSelectionDialog)
+            dlg = EditorHighlightingStylesSelectionDialog(
+                lexerNames, forImport=False, preselect=preselect)
+            if dlg.exec() == QDialog.Accepted:
+                lexerNames = dlg.getLexerNames()
+            else:
+                # Cancelled by user
+                return
+        
+        lexers = [self.lexers[name] for name in lexerNames]
+        
         fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
             self,
             self.tr("Export Highlighting Styles"),
@@ -581,12 +596,13 @@
                         .format(fn, f.errorString())
                     )
     
-    def __importStyles(self, lexers):
+    def __importStyles(self, importAll=False):
         """
-        Private method to import the styles of the given lexers.
+        Private method to import the styles of lexers to be selected.
         
-        @param lexers dictionary of lexer objects for which to import the
-            styles
+        @param importAll flag indicating to import all styles without asking
+            (defaults to False)
+        @type bool (optional)
         """
         from eric6config import getConfig
         stylesDir = getConfig("ericStylesDir")
@@ -607,7 +623,7 @@
                 HighlightingStylesFile
             )
             highlightingStylesFile = HighlightingStylesFile()
-            styles = highlightingStylesFile.readFile(fn, lexers)
+            styles = highlightingStylesFile.readFile(fn)
             if not styles:
                 return
         else:
@@ -617,7 +633,7 @@
                 from E5XML.HighlightingStylesReader import (
                     HighlightingStylesReader
                 )
-                reader = HighlightingStylesReader(f, lexers)
+                reader = HighlightingStylesReader(f, self.lexers)
                 styles = reader.readXML()
                 f.close()
                 if not styles:
@@ -633,22 +649,38 @@
                 )
                 return
         
-        self.__applyStyles(styles, lexers)
+        self.__applyStyles(styles, importAll=importAll)
         self.on_lexerLanguageComboBox_activated(
             self.lexerLanguageComboBox.currentText())
     
-    def __applyStyles(self, stylesList, lexersList):
+    def __applyStyles(self, stylesList, importAll=False):
         """
         Private method to apply the imported styles to this dialog.
         
         @param stylesList list of imported lexer styles
         @type list of dict
-        @param lexersList list of lexers to apply the styles to
-        @type list of PreferencesLexer
+        @param importAll flag indicating to import all styles without asking
+            (defaults to False)
+        @type bool (optional)
         """
+        lexerNames = [d["name"]
+                      for d in stylesList
+                      if d["name"] in self.lexers]
+        
+        if not importAll:
+            from .EditorHighlightingStylesSelectionDialog import (
+                EditorHighlightingStylesSelectionDialog)
+            dlg = EditorHighlightingStylesSelectionDialog(
+                lexerNames, forImport=True)
+            if dlg.exec() == QDialog.Accepted:
+                lexerNames = dlg.getLexerNames()
+            else:
+                # Cancelled by user
+                return
+        
         for lexerDict in stylesList:
-            if lexerDict["name"] in lexersList:
-                lexer = lexersList[lexerDict["name"]]
+            if lexerDict["name"] in lexerNames:
+                lexer = self.lexers[lexerDict["name"]]
                 for styleDict in lexerDict["styles"]:
                     style = styleDict["style"]
                     substyle = styleDict["substyle"]
--- a/eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.ui	Fri Jan 29 19:30:59 2021 +0100
+++ b/eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.ui	Fri Jan 29 19:31:47 2021 +0100
@@ -348,12 +348,9 @@
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
-      <widget class="QPushButton" name="importCurrentButton">
-       <property name="enabled">
-        <bool>false</bool>
-       </property>
+      <widget class="QPushButton" name="importButton">
        <property name="toolTip">
-        <string>Imports all styles of the currently selected language</string>
+        <string>Imports all styles of languages to be selected</string>
        </property>
        <property name="text">
         <string>Import styles</string>
@@ -361,12 +358,9 @@
       </widget>
      </item>
      <item>
-      <widget class="QPushButton" name="exportCurrentButton">
-       <property name="enabled">
-        <bool>false</bool>
-       </property>
+      <widget class="QPushButton" name="exportButton">
        <property name="toolTip">
-        <string>Exports all styles of the currently selected language</string>
+        <string>Exports all styles of languages to be selected</string>
        </property>
        <property name="text">
         <string>Export styles</string>
@@ -415,8 +409,8 @@
   <tabstop>allFontsButton</tabstop>
   <tabstop>allEolFillButton</tabstop>
   <tabstop>allDefaultButton</tabstop>
-  <tabstop>importCurrentButton</tabstop>
-  <tabstop>exportCurrentButton</tabstop>
+  <tabstop>importButton</tabstop>
+  <tabstop>exportButton</tabstop>
   <tabstop>importAllButton</tabstop>
   <tabstop>exportAllButton</tabstop>
  </tabstops>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric6/Preferences/ConfigurationPages/EditorHighlightingStylesSelectionDialog.py	Fri Jan 29 19:31:47 2021 +0100
@@ -0,0 +1,117 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a dialog to select the styles to be imported/exported.
+"""
+
+from PyQt5.QtCore import pyqtSlot, Qt
+from PyQt5.QtWidgets import (
+    QDialog, QDialogButtonBox, QListWidgetItem, QAbstractButton
+)
+
+from .Ui_EditorHighlightingStylesSelectionDialog import (
+    Ui_EditorHighlightingStylesSelectionDialog
+)
+
+
+class EditorHighlightingStylesSelectionDialog(
+    QDialog, Ui_EditorHighlightingStylesSelectionDialog
+):
+    """
+    Class implementing a dialog to select the styles to be imported/exported.
+    """
+    def __init__(self, lexerNames, forImport, preselect=None, parent=None):
+        """
+        Constructor
+        
+        @param lexerNames list of lexer names to select from
+        @type list of str
+        @param forImport flag indicating a dialog for importing styles
+        @type bool
+        @param preselect list of lexer names to be preselected
+        @type list of str
+        @param parent reference to the parent widget
+        @type QWidget
+        """
+        super(EditorHighlightingStylesSelectionDialog, self).__init__(parent)
+        self.setupUi(self)
+        
+        self.__selectAllButton = self.buttonBox.addButton(
+            self.tr("Select All"), QDialogButtonBox.ActionRole)
+        
+        if forImport:
+            self.setWindowTitle(self.tr("Import Highlighting Styles"))
+            self.infoLabel.setText(self.tr(
+                "Select the highlighting styles to be imported"))
+        else:
+            self.setWindowTitle(self.tr("Export Highlighting Styles"))
+            self.infoLabel.setText(self.tr(
+                "Select the highlighting styles to be exported"))
+        
+        if preselect is None:
+            preselect = []
+        
+        for name in lexerNames:
+            itm = QListWidgetItem(name, self.lexersList)
+            itm.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
+            if name in preselect:
+                itm.setCheckState(Qt.Checked)
+            else:
+                itm.setCheckState(Qt.Unchecked)
+        
+        self.__updateOkButton()
+    
+    @pyqtSlot()
+    def __updateOkButton(self):
+        """
+        Private slot to update the state of the OK button.
+        """
+        for row in range(self.lexersList.count()):
+            itm = self.lexersList.item(row)
+            if itm.checkState() == Qt.Checked:
+                enable = True
+                break
+        else:
+            enable = False
+        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable)
+    
+    @pyqtSlot(QListWidgetItem)
+    def on_lexersList_itemChanged(self, item):
+        """
+        Private slot to react on changes in check state.
+        
+        @param item reference to the changed item
+        @type QListWidgetItem
+        """
+        self.__updateOkButton()
+    
+    @pyqtSlot(QAbstractButton)
+    def on_buttonBox_clicked(self, button):
+        """
+        Private slot to handle the user pressing a button.
+        
+        @param button reference to the button pressed
+        @type QAbstractButton
+        """
+        if button is self.__selectAllButton:
+            for row in range(self.lexersList.count()):
+                itm = self.lexersList.item(row)
+                itm.setCheckState(Qt.Checked)
+    
+    def getLexerNames(self):
+        """
+        Public method to get the selected lexer names.
+        
+        @return list of selected lexer names
+        @rtype list of str
+        """
+        lexerNames = []
+        for row in range(self.lexersList.count()):
+            itm = self.lexersList.item(row)
+            if itm.checkState() == Qt.Checked:
+                lexerNames.append(itm.text())
+        
+        return lexerNames
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric6/Preferences/ConfigurationPages/EditorHighlightingStylesSelectionDialog.ui	Fri Jan 29 19:31:47 2021 +0100
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>EditorHighlightingStylesSelectionDialog</class>
+ <widget class="QDialog" name="EditorHighlightingStylesSelectionDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>500</height>
+   </rect>
+  </property>
+  <property name="sizeGripEnabled">
+   <bool>true</bool>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <widget class="QLabel" name="infoLabel">
+     <property name="wordWrap">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QListWidget" name="lexersList">
+     <property name="alternatingRowColors">
+      <bool>true</bool>
+     </property>
+     <property name="sortingEnabled">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>EditorHighlightingStylesSelectionDialog</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>EditorHighlightingStylesSelectionDialog</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>
--- a/eric6/Preferences/HighlightingStylesFile.py	Fri Jan 29 19:30:59 2021 +0100
+++ b/eric6/Preferences/HighlightingStylesFile.py	Fri Jan 29 19:31:47 2021 +0100
@@ -89,16 +89,13 @@
         
         return True
     
-    def readFile(self, filename: str, lexers: dict) -> list:
+    def readFile(self, filename: str) -> list:
         """
         Public method to read the highlighting styles data from a highlighting
         styles JSON file.
         
         @param filename name of the highlighting styles file
         @type str
-        @param lexers dictionary of lexer objects for which to import the
-            styles
-        @type dict of {str: PreferencesLexer}
         @return list of read lexer style definitions
         @rtype list of dict
         """

eric ide

mercurial