|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2005 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter filetype associations for the project. |
|
8 """ |
|
9 |
|
10 import contextlib |
|
11 |
|
12 from PyQt5.QtCore import Qt, pyqtSlot |
|
13 from PyQt5.QtWidgets import QHeaderView, QDialog, QTreeWidgetItem |
|
14 |
|
15 from .Ui_FiletypeAssociationDialog import Ui_FiletypeAssociationDialog |
|
16 |
|
17 |
|
18 class FiletypeAssociationDialog(QDialog, Ui_FiletypeAssociationDialog): |
|
19 """ |
|
20 Class implementing a dialog to enter filetype associations for the project. |
|
21 """ |
|
22 def __init__(self, project, parent=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param project reference to the project object |
|
27 @param parent reference to the parent widget (QWidget) |
|
28 """ |
|
29 super().__init__(parent) |
|
30 self.setupUi(self) |
|
31 |
|
32 self.filetypeAssociationList.headerItem().setText( |
|
33 self.filetypeAssociationList.columnCount(), "") |
|
34 self.filetypeAssociationList.header().setSortIndicator( |
|
35 0, Qt.SortOrder.AscendingOrder) |
|
36 |
|
37 # keep these lists in sync |
|
38 self.filetypes = ["SOURCES", "FORMS", "TRANSLATIONS", "RESOURCES", |
|
39 "INTERFACES", "PROTOCOLS", "OTHERS", "__IGNORE__"] |
|
40 self.filetypeStrings = [self.tr("Sources"), self.tr("Forms"), |
|
41 self.tr("Translations"), |
|
42 self.tr("Resources"), |
|
43 self.tr("Interfaces"), |
|
44 self.tr("Protocols"), |
|
45 self.tr("Others"), |
|
46 self.tr("Ignore")] |
|
47 self.filetypeCombo.addItems(self.filetypeStrings) |
|
48 |
|
49 self.project = project |
|
50 for pattern, filetype in list(self.project.pdata["FILETYPES"].items()): |
|
51 with contextlib.suppress(ValueError): |
|
52 index = self.filetypes.index(filetype) |
|
53 self.__createItem(pattern, self.filetypeStrings[index]) |
|
54 |
|
55 self.__resort() |
|
56 self.__reformat() |
|
57 |
|
58 def __resort(self): |
|
59 """ |
|
60 Private method to resort the tree. |
|
61 """ |
|
62 self.filetypeAssociationList.sortItems( |
|
63 self.filetypeAssociationList.sortColumn(), |
|
64 self.filetypeAssociationList.header().sortIndicatorOrder()) |
|
65 |
|
66 def __reformat(self): |
|
67 """ |
|
68 Private method to reformat the tree. |
|
69 """ |
|
70 self.filetypeAssociationList.header().resizeSections( |
|
71 QHeaderView.ResizeMode.ResizeToContents) |
|
72 self.filetypeAssociationList.header().setStretchLastSection(True) |
|
73 |
|
74 def __createItem(self, pattern, filetype): |
|
75 """ |
|
76 Private slot to create a new entry in the association list. |
|
77 |
|
78 @param pattern pattern of the entry (string) |
|
79 @param filetype file type of the entry (string) |
|
80 @return reference to the newly generated entry (QTreeWidgetItem) |
|
81 """ |
|
82 itm = QTreeWidgetItem( |
|
83 self.filetypeAssociationList, [pattern, filetype]) |
|
84 return itm |
|
85 |
|
86 def on_filetypeAssociationList_currentItemChanged(self, itm, prevItm): |
|
87 """ |
|
88 Private slot to handle the currentItemChanged signal of the |
|
89 association list. |
|
90 |
|
91 @param itm reference to the new current item (QTreeWidgetItem) |
|
92 @param prevItm reference to the previous current item (QTreeWidgetItem) |
|
93 """ |
|
94 if itm is None: |
|
95 self.filePatternEdit.clear() |
|
96 self.filetypeCombo.setCurrentIndex(0) |
|
97 self.deleteAssociationButton.setEnabled(False) |
|
98 else: |
|
99 self.filePatternEdit.setText(itm.text(0)) |
|
100 self.filetypeCombo.setCurrentIndex( |
|
101 self.filetypeCombo.findText(itm.text(1))) |
|
102 self.deleteAssociationButton.setEnabled(True) |
|
103 |
|
104 @pyqtSlot() |
|
105 def on_addAssociationButton_clicked(self): |
|
106 """ |
|
107 Private slot to add the association displayed to the list. |
|
108 """ |
|
109 pattern = self.filePatternEdit.text() |
|
110 filetype = self.filetypeCombo.currentText() |
|
111 if pattern: |
|
112 items = self.filetypeAssociationList.findItems( |
|
113 pattern, Qt.MatchFlags(Qt.MatchFlag.MatchExactly), 0) |
|
114 for itm in items: |
|
115 itm = self.filetypeAssociationList.takeTopLevelItem( |
|
116 self.filetypeAssociationList.indexOfTopLevelItem(itm)) |
|
117 del itm |
|
118 itm = self.__createItem(pattern, filetype) |
|
119 self.__resort() |
|
120 self.__reformat() |
|
121 self.filePatternEdit.clear() |
|
122 self.filetypeCombo.setCurrentIndex(0) |
|
123 self.filetypeAssociationList.setCurrentItem(itm) |
|
124 |
|
125 @pyqtSlot() |
|
126 def on_deleteAssociationButton_clicked(self): |
|
127 """ |
|
128 Private slot to delete the currently selected association of the |
|
129 listbox. |
|
130 """ |
|
131 for itm in self.filetypeAssociationList.selectedItems(): |
|
132 itm = self.filetypeAssociationList.takeTopLevelItem( |
|
133 self.filetypeAssociationList.indexOfTopLevelItem(itm)) |
|
134 del itm |
|
135 |
|
136 self.filetypeAssociationList.clearSelection() |
|
137 self.filePatternEdit.clear() |
|
138 self.filetypeCombo.setCurrentIndex(0) |
|
139 |
|
140 def on_filePatternEdit_textChanged(self, txt): |
|
141 """ |
|
142 Private slot to handle the textChanged signal of the pattern lineedit. |
|
143 |
|
144 @param txt text of the lineedit (string) |
|
145 """ |
|
146 if not txt: |
|
147 self.addAssociationButton.setEnabled(False) |
|
148 self.deleteAssociationButton.setEnabled(False) |
|
149 else: |
|
150 self.addAssociationButton.setEnabled(True) |
|
151 if len(self.filetypeAssociationList.selectedItems()) == 0: |
|
152 self.deleteAssociationButton.setEnabled(False) |
|
153 else: |
|
154 self.deleteAssociationButton.setEnabled( |
|
155 self.filetypeAssociationList.selectedItems()[0] |
|
156 .text(0) == txt) |
|
157 |
|
158 def transferData(self): |
|
159 """ |
|
160 Public slot to transfer the associations into the projects data |
|
161 structure. |
|
162 """ |
|
163 self.project.pdata["FILETYPES"] = {} |
|
164 for index in range(self.filetypeAssociationList.topLevelItemCount()): |
|
165 itm = self.filetypeAssociationList.topLevelItem(index) |
|
166 pattern = itm.text(0) |
|
167 index = self.filetypeStrings.index(itm.text(1)) |
|
168 self.project.pdata["FILETYPES"][pattern] = self.filetypes[index] |