4 # |
4 # |
5 |
5 |
6 """ |
6 """ |
7 Module implementing a dialog to enter filetype associations for the project. |
7 Module implementing a dialog to enter filetype associations for the project. |
8 """ |
8 """ |
9 |
|
10 import contextlib |
|
11 |
9 |
12 from PyQt6.QtCore import Qt, pyqtSlot |
10 from PyQt6.QtCore import Qt, pyqtSlot |
13 from PyQt6.QtWidgets import QDialog, QHeaderView, QTreeWidgetItem |
11 from PyQt6.QtWidgets import QDialog, QHeaderView, QTreeWidgetItem |
14 |
12 |
15 from .Ui_FiletypeAssociationDialog import Ui_FiletypeAssociationDialog |
13 from .Ui_FiletypeAssociationDialog import Ui_FiletypeAssociationDialog |
35 ) |
33 ) |
36 self.filetypeAssociationList.header().setSortIndicator( |
34 self.filetypeAssociationList.header().setSortIndicator( |
37 0, Qt.SortOrder.AscendingOrder |
35 0, Qt.SortOrder.AscendingOrder |
38 ) |
36 ) |
39 |
37 |
40 # keep these lists in sync |
38 self.__project = project |
41 self.filetypes = [ |
|
42 "SOURCES", |
|
43 "FORMS", |
|
44 "TRANSLATIONS", |
|
45 "RESOURCES", |
|
46 "INTERFACES", |
|
47 "PROTOCOLS", |
|
48 "OTHERS", |
|
49 "__IGNORE__", |
|
50 ] |
|
51 self.filetypeStrings = [ |
|
52 self.tr("Sources"), |
|
53 self.tr("Forms"), |
|
54 self.tr("Translations"), |
|
55 self.tr("Resources"), |
|
56 self.tr("Interfaces"), |
|
57 self.tr("Protocols"), |
|
58 self.tr("Others"), |
|
59 self.tr("Ignore"), |
|
60 ] |
|
61 self.filetypeCombo.addItems(self.filetypeStrings) |
|
62 |
39 |
63 self.project = project |
40 self.filetypeCombo.addItem("", "") |
64 for pattern, filetype in list( |
41 for fileCategory in sorted(self.__project.getFileCategories()): |
65 self.project.getProjectData(dataKey="FILETYPES").items() |
42 self.filetypeCombo.addItem( |
66 ): |
43 self.__project.getFileCategoryType(fileCategory), fileCategory |
67 with contextlib.suppress(ValueError): |
44 ) |
68 index = self.filetypes.index(filetype) |
45 self.filetypeCombo.addItem(self.tr("Ignore"), "__IGNORE__") |
69 self.__createItem(pattern, self.filetypeStrings[index]) |
46 |
|
47 for pattern, filetype in self.__project.getProjectData( |
|
48 dataKey="FILETYPES" |
|
49 ).items(): |
|
50 try: |
|
51 self.__createItem( |
|
52 pattern, self.__project.getFileCategoryType(filetype), filetype |
|
53 ) |
|
54 except KeyError: |
|
55 if filetype == "__IGNORE__": |
|
56 self.__createItem(pattern, self.tr("Ignore"), "__IGNORE__") |
|
57 else: |
|
58 raise # re-raise the error if the type is not __IGNORE__ |
70 |
59 |
71 self.__resort() |
60 self.__resort() |
72 self.__reformat() |
61 self.__reformat() |
73 |
62 |
74 def __resort(self): |
63 def __resort(self): |
87 self.filetypeAssociationList.header().resizeSections( |
76 self.filetypeAssociationList.header().resizeSections( |
88 QHeaderView.ResizeMode.ResizeToContents |
77 QHeaderView.ResizeMode.ResizeToContents |
89 ) |
78 ) |
90 self.filetypeAssociationList.header().setStretchLastSection(True) |
79 self.filetypeAssociationList.header().setStretchLastSection(True) |
91 |
80 |
92 def __createItem(self, pattern, filetype): |
81 def __createItem(self, pattern, filetypeStr, fileCategory): |
93 """ |
82 """ |
94 Private slot to create a new entry in the association list. |
83 Private slot to create a new entry in the association list. |
95 |
84 |
96 @param pattern pattern of the entry (string) |
85 @param pattern pattern of the entry |
97 @param filetype file type of the entry (string) |
86 @type str |
98 @return reference to the newly generated entry (QTreeWidgetItem) |
87 @param filetypeStr file type user string of the entry |
|
88 @type str |
|
89 @param fileCategory category of the file |
|
90 @type str |
|
91 @return reference to the newly generated entry |
|
92 @rtype QTreeWidgetItem |
99 """ |
93 """ |
100 itm = QTreeWidgetItem(self.filetypeAssociationList, [pattern, filetype]) |
94 itm = QTreeWidgetItem(self.filetypeAssociationList, [pattern, filetypeStr]) |
|
95 itm.setData(1, Qt.ItemDataRole.UserRole, fileCategory) |
101 return itm |
96 return itm |
102 |
97 |
103 def on_filetypeAssociationList_currentItemChanged(self, itm, prevItm): |
98 def on_filetypeAssociationList_currentItemChanged(self, itm, prevItm): |
104 """ |
99 """ |
105 Private slot to handle the currentItemChanged signal of the |
100 Private slot to handle the currentItemChanged signal of the |
112 self.filePatternEdit.clear() |
107 self.filePatternEdit.clear() |
113 self.filetypeCombo.setCurrentIndex(0) |
108 self.filetypeCombo.setCurrentIndex(0) |
114 self.deleteAssociationButton.setEnabled(False) |
109 self.deleteAssociationButton.setEnabled(False) |
115 else: |
110 else: |
116 self.filePatternEdit.setText(itm.text(0)) |
111 self.filePatternEdit.setText(itm.text(0)) |
117 self.filetypeCombo.setCurrentIndex(self.filetypeCombo.findText(itm.text(1))) |
112 self.filetypeCombo.setCurrentText(itm.text(1)) |
118 self.deleteAssociationButton.setEnabled(True) |
113 self.deleteAssociationButton.setEnabled(True) |
119 |
114 |
120 @pyqtSlot() |
115 @pyqtSlot() |
121 def on_addAssociationButton_clicked(self): |
116 def on_addAssociationButton_clicked(self): |
122 """ |
117 """ |
123 Private slot to add the association displayed to the list. |
118 Private slot to add the association displayed to the list. |
124 """ |
119 """ |
125 pattern = self.filePatternEdit.text() |
120 pattern = self.filePatternEdit.text() |
126 filetype = self.filetypeCombo.currentText() |
121 filetype = self.filetypeCombo.currentText() |
|
122 fileCategory = self.filetypeCombo.currentData() |
127 if pattern: |
123 if pattern: |
128 items = self.filetypeAssociationList.findItems( |
124 items = self.filetypeAssociationList.findItems( |
129 pattern, Qt.MatchFlag.MatchExactly, 0 |
125 pattern, Qt.MatchFlag.MatchExactly, 0 |
130 ) |
126 ) |
131 for itm in items: |
127 for itm in items: |
132 itm = self.filetypeAssociationList.takeTopLevelItem( |
128 itm = self.filetypeAssociationList.takeTopLevelItem( |
133 self.filetypeAssociationList.indexOfTopLevelItem(itm) |
129 self.filetypeAssociationList.indexOfTopLevelItem(itm) |
134 ) |
130 ) |
135 del itm |
131 del itm |
136 itm = self.__createItem(pattern, filetype) |
132 itm = self.__createItem(pattern, filetype, fileCategory) |
137 self.__resort() |
133 self.__resort() |
138 self.__reformat() |
134 self.__reformat() |
139 self.filePatternEdit.clear() |
135 self.filePatternEdit.clear() |
140 self.filetypeCombo.setCurrentIndex(0) |
136 self.filetypeCombo.setCurrentIndex(0) |
141 self.filetypeAssociationList.setCurrentItem(itm) |
137 self.filetypeAssociationList.setCurrentItem(itm) |
158 |
154 |
159 def on_filePatternEdit_textChanged(self, txt): |
155 def on_filePatternEdit_textChanged(self, txt): |
160 """ |
156 """ |
161 Private slot to handle the textChanged signal of the pattern lineedit. |
157 Private slot to handle the textChanged signal of the pattern lineedit. |
162 |
158 |
163 @param txt text of the lineedit (string) |
159 @param txt text of the line edit (string) |
164 """ |
160 """ |
165 if not txt: |
161 if not txt: |
166 self.addAssociationButton.setEnabled(False) |
|
167 self.deleteAssociationButton.setEnabled(False) |
162 self.deleteAssociationButton.setEnabled(False) |
168 else: |
163 else: |
169 self.addAssociationButton.setEnabled(True) |
|
170 if len(self.filetypeAssociationList.selectedItems()) == 0: |
164 if len(self.filetypeAssociationList.selectedItems()) == 0: |
171 self.deleteAssociationButton.setEnabled(False) |
165 self.deleteAssociationButton.setEnabled(False) |
172 else: |
166 else: |
173 self.deleteAssociationButton.setEnabled( |
167 self.deleteAssociationButton.setEnabled( |
174 self.filetypeAssociationList.selectedItems()[0].text(0) == txt |
168 self.filetypeAssociationList.selectedItems()[0].text(0) == txt |
175 ) |
169 ) |
|
170 self.__updateAddButton() |
|
171 |
|
172 @pyqtSlot(int) |
|
173 def on_filetypeCombo_currentIndexChanged(self, index): |
|
174 """ |
|
175 Private slot handling the selection of a file type. |
|
176 |
|
177 @param index index of the selected entry |
|
178 @type int |
|
179 """ |
|
180 self.__updateAddButton() |
|
181 |
|
182 def __updateAddButton(self): |
|
183 """ |
|
184 Private method to update the enabled state of the 'add' button. |
|
185 """ |
|
186 self.addAssociationButton.setEnabled( |
|
187 bool(self.filePatternEdit.text()) and bool(self.filetypeCombo.currentText()) |
|
188 ) |
176 |
189 |
177 def transferData(self): |
190 def transferData(self): |
178 """ |
191 """ |
179 Public slot to transfer the associations into the projects data |
192 Public slot to transfer the associations into the projects data |
180 structure. |
193 structure. |
181 """ |
194 """ |
182 self.project.setProjectData({}, dataKey="FILETYPES") |
195 fileTypes = {} |
183 for index in range(self.filetypeAssociationList.topLevelItemCount()): |
196 for index in range(self.filetypeAssociationList.topLevelItemCount()): |
184 itm = self.filetypeAssociationList.topLevelItem(index) |
197 itm = self.filetypeAssociationList.topLevelItem(index) |
185 pattern = itm.text(0) |
198 fileTypes[itm.text(0)] = itm.data(1, Qt.ItemDataRole.UserRole) |
186 index = self.filetypeStrings.index(itm.text(1)) |
199 self.__project.setProjectData(fileTypes, dataKey="FILETYPES") |
187 fileTypes = self.project.getProjectData(dataKey="FILETYPES") |
|
188 fileTypes[pattern] = self.filetypes[index] |
|
189 self.project.setProjectData(fileTypes, dataKey="FILETYPES") |
|