src/eric7/Project/FiletypeAssociationDialog.py

branch
eric7
changeset 9516
0f023e61a9b5
parent 9514
2b104ad132a4
child 9531
155b2646799a
--- a/src/eric7/Project/FiletypeAssociationDialog.py	Wed Nov 16 11:04:18 2022 +0100
+++ b/src/eric7/Project/FiletypeAssociationDialog.py	Wed Nov 16 18:11:52 2022 +0100
@@ -7,8 +7,6 @@
 Module implementing a dialog to enter filetype associations for the project.
 """
 
-import contextlib
-
 from PyQt6.QtCore import Qt, pyqtSlot
 from PyQt6.QtWidgets import QDialog, QHeaderView, QTreeWidgetItem
 
@@ -37,36 +35,27 @@
             0, Qt.SortOrder.AscendingOrder
         )
 
-        # keep these lists in sync
-        self.filetypes = [
-            "SOURCES",
-            "FORMS",
-            "TRANSLATIONS",
-            "RESOURCES",
-            "INTERFACES",
-            "PROTOCOLS",
-            "OTHERS",
-            "__IGNORE__",
-        ]
-        self.filetypeStrings = [
-            self.tr("Sources"),
-            self.tr("Forms"),
-            self.tr("Translations"),
-            self.tr("Resources"),
-            self.tr("Interfaces"),
-            self.tr("Protocols"),
-            self.tr("Others"),
-            self.tr("Ignore"),
-        ]
-        self.filetypeCombo.addItems(self.filetypeStrings)
+        self.__project = project
+
+        self.filetypeCombo.addItem("", "")
+        for fileCategory in sorted(self.__project.getFileCategories()):
+            self.filetypeCombo.addItem(
+                self.__project.getFileCategoryType(fileCategory), fileCategory
+            )
+        self.filetypeCombo.addItem(self.tr("Ignore"), "__IGNORE__")
 
-        self.project = project
-        for pattern, filetype in list(
-            self.project.getProjectData(dataKey="FILETYPES").items()
-        ):
-            with contextlib.suppress(ValueError):
-                index = self.filetypes.index(filetype)
-                self.__createItem(pattern, self.filetypeStrings[index])
+        for pattern, filetype in self.__project.getProjectData(
+            dataKey="FILETYPES"
+        ).items():
+            try:
+                self.__createItem(
+                    pattern, self.__project.getFileCategoryType(filetype), filetype
+                )
+            except KeyError:
+                if filetype == "__IGNORE__":
+                    self.__createItem(pattern, self.tr("Ignore"), "__IGNORE__")
+                else:
+                    raise  # re-raise the error if the type is not __IGNORE__
 
         self.__resort()
         self.__reformat()
@@ -89,15 +78,21 @@
         )
         self.filetypeAssociationList.header().setStretchLastSection(True)
 
-    def __createItem(self, pattern, filetype):
+    def __createItem(self, pattern, filetypeStr, fileCategory):
         """
         Private slot to create a new entry in the association list.
 
-        @param pattern pattern of the entry (string)
-        @param filetype file type of the entry (string)
-        @return reference to the newly generated entry (QTreeWidgetItem)
+        @param pattern pattern of the entry
+        @type str
+        @param filetypeStr file type user string of the entry
+        @type str
+        @param fileCategory category of the file
+        @type str
+        @return reference to the newly generated entry
+        @rtype QTreeWidgetItem
         """
-        itm = QTreeWidgetItem(self.filetypeAssociationList, [pattern, filetype])
+        itm = QTreeWidgetItem(self.filetypeAssociationList, [pattern, filetypeStr])
+        itm.setData(1, Qt.ItemDataRole.UserRole, fileCategory)
         return itm
 
     def on_filetypeAssociationList_currentItemChanged(self, itm, prevItm):
@@ -114,7 +109,7 @@
             self.deleteAssociationButton.setEnabled(False)
         else:
             self.filePatternEdit.setText(itm.text(0))
-            self.filetypeCombo.setCurrentIndex(self.filetypeCombo.findText(itm.text(1)))
+            self.filetypeCombo.setCurrentText(itm.text(1))
             self.deleteAssociationButton.setEnabled(True)
 
     @pyqtSlot()
@@ -124,6 +119,7 @@
         """
         pattern = self.filePatternEdit.text()
         filetype = self.filetypeCombo.currentText()
+        fileCategory = self.filetypeCombo.currentData()
         if pattern:
             items = self.filetypeAssociationList.findItems(
                 pattern, Qt.MatchFlag.MatchExactly, 0
@@ -133,7 +129,7 @@
                     self.filetypeAssociationList.indexOfTopLevelItem(itm)
                 )
                 del itm
-            itm = self.__createItem(pattern, filetype)
+            itm = self.__createItem(pattern, filetype, fileCategory)
             self.__resort()
             self.__reformat()
             self.filePatternEdit.clear()
@@ -160,30 +156,44 @@
         """
         Private slot to handle the textChanged signal of the pattern lineedit.
 
-        @param txt text of the lineedit (string)
+        @param txt text of the line edit (string)
         """
         if not txt:
-            self.addAssociationButton.setEnabled(False)
             self.deleteAssociationButton.setEnabled(False)
         else:
-            self.addAssociationButton.setEnabled(True)
             if len(self.filetypeAssociationList.selectedItems()) == 0:
                 self.deleteAssociationButton.setEnabled(False)
             else:
                 self.deleteAssociationButton.setEnabled(
                     self.filetypeAssociationList.selectedItems()[0].text(0) == txt
                 )
+        self.__updateAddButton()
+
+    @pyqtSlot(int)
+    def on_filetypeCombo_currentIndexChanged(self, index):
+        """
+        Private slot handling the selection of a file type.
+
+        @param index index of the selected entry
+        @type int
+        """
+        self.__updateAddButton()
+
+    def __updateAddButton(self):
+        """
+        Private method to update the enabled state of the 'add' button.
+        """
+        self.addAssociationButton.setEnabled(
+            bool(self.filePatternEdit.text()) and bool(self.filetypeCombo.currentText())
+        )
 
     def transferData(self):
         """
         Public slot to transfer the associations into the projects data
         structure.
         """
-        self.project.setProjectData({}, dataKey="FILETYPES")
+        fileTypes = {}
         for index in range(self.filetypeAssociationList.topLevelItemCount()):
             itm = self.filetypeAssociationList.topLevelItem(index)
-            pattern = itm.text(0)
-            index = self.filetypeStrings.index(itm.text(1))
-            fileTypes = self.project.getProjectData(dataKey="FILETYPES")
-            fileTypes[pattern] = self.filetypes[index]
-            self.project.setProjectData(fileTypes, dataKey="FILETYPES")
+            fileTypes[itm.text(0)] = itm.data(1, Qt.ItemDataRole.UserRole)
+        self.__project.setProjectData(fileTypes, dataKey="FILETYPES")

eric ide

mercurial