src/eric7/Project/FiletypeAssociationDialog.py

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

eric ide

mercurial