|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2005 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter filetype associations for the project. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
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(FiletypeAssociationDialog, self).__init__(parent) |
|
30 self.setupUi(self) |
|
31 |
|
32 self.filetypeAssociationList.headerItem().setText( |
|
33 self.filetypeAssociationList.columnCount(), "") |
|
34 self.filetypeAssociationList.header().setSortIndicator( |
|
35 0, Qt.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 try: |
|
52 index = self.filetypes.index(filetype) |
|
53 self.__createItem(pattern, self.filetypeStrings[index]) |
|
54 except ValueError: |
|
55 pass # silently discard entries of unknown type |
|
56 |
|
57 self.__resort() |
|
58 self.__reformat() |
|
59 |
|
60 def __resort(self): |
|
61 """ |
|
62 Private method to resort the tree. |
|
63 """ |
|
64 self.filetypeAssociationList.sortItems( |
|
65 self.filetypeAssociationList.sortColumn(), |
|
66 self.filetypeAssociationList.header().sortIndicatorOrder()) |
|
67 |
|
68 def __reformat(self): |
|
69 """ |
|
70 Private method to reformat the tree. |
|
71 """ |
|
72 self.filetypeAssociationList.header().resizeSections( |
|
73 QHeaderView.ResizeToContents) |
|
74 self.filetypeAssociationList.header().setStretchLastSection(True) |
|
75 |
|
76 def __createItem(self, pattern, filetype): |
|
77 """ |
|
78 Private slot to create a new entry in the association list. |
|
79 |
|
80 @param pattern pattern of the entry (string) |
|
81 @param filetype file type of the entry (string) |
|
82 @return reference to the newly generated entry (QTreeWidgetItem) |
|
83 """ |
|
84 itm = QTreeWidgetItem( |
|
85 self.filetypeAssociationList, [pattern, filetype]) |
|
86 return itm |
|
87 |
|
88 def on_filetypeAssociationList_currentItemChanged(self, itm, prevItm): |
|
89 """ |
|
90 Private slot to handle the currentItemChanged signal of the |
|
91 association list. |
|
92 |
|
93 @param itm reference to the new current item (QTreeWidgetItem) |
|
94 @param prevItm reference to the previous current item (QTreeWidgetItem) |
|
95 """ |
|
96 if itm is None: |
|
97 self.filePatternEdit.clear() |
|
98 self.filetypeCombo.setCurrentIndex(0) |
|
99 self.deleteAssociationButton.setEnabled(False) |
|
100 else: |
|
101 self.filePatternEdit.setText(itm.text(0)) |
|
102 self.filetypeCombo.setCurrentIndex( |
|
103 self.filetypeCombo.findText(itm.text(1))) |
|
104 self.deleteAssociationButton.setEnabled(True) |
|
105 |
|
106 @pyqtSlot() |
|
107 def on_addAssociationButton_clicked(self): |
|
108 """ |
|
109 Private slot to add the association displayed to the list. |
|
110 """ |
|
111 pattern = self.filePatternEdit.text() |
|
112 filetype = self.filetypeCombo.currentText() |
|
113 if pattern: |
|
114 items = self.filetypeAssociationList.findItems( |
|
115 pattern, Qt.MatchFlags(Qt.MatchExactly), 0) |
|
116 for itm in items: |
|
117 itm = self.filetypeAssociationList.takeTopLevelItem( |
|
118 self.filetypeAssociationList.indexOfTopLevelItem(itm)) |
|
119 del itm |
|
120 itm = self.__createItem(pattern, filetype) |
|
121 self.__resort() |
|
122 self.__reformat() |
|
123 self.filePatternEdit.clear() |
|
124 self.filetypeCombo.setCurrentIndex(0) |
|
125 self.filetypeAssociationList.setCurrentItem(itm) |
|
126 |
|
127 @pyqtSlot() |
|
128 def on_deleteAssociationButton_clicked(self): |
|
129 """ |
|
130 Private slot to delete the currently selected association of the |
|
131 listbox. |
|
132 """ |
|
133 for itm in self.filetypeAssociationList.selectedItems(): |
|
134 itm = self.filetypeAssociationList.takeTopLevelItem( |
|
135 self.filetypeAssociationList.indexOfTopLevelItem(itm)) |
|
136 del itm |
|
137 |
|
138 self.filetypeAssociationList.clearSelection() |
|
139 self.filePatternEdit.clear() |
|
140 self.filetypeCombo.setCurrentIndex(0) |
|
141 |
|
142 def on_filePatternEdit_textChanged(self, txt): |
|
143 """ |
|
144 Private slot to handle the textChanged signal of the pattern lineedit. |
|
145 |
|
146 @param txt text of the lineedit (string) |
|
147 """ |
|
148 if not txt: |
|
149 self.addAssociationButton.setEnabled(False) |
|
150 self.deleteAssociationButton.setEnabled(False) |
|
151 else: |
|
152 self.addAssociationButton.setEnabled(True) |
|
153 if len(self.filetypeAssociationList.selectedItems()) == 0: |
|
154 self.deleteAssociationButton.setEnabled(False) |
|
155 else: |
|
156 self.deleteAssociationButton.setEnabled( |
|
157 self.filetypeAssociationList.selectedItems()[0] |
|
158 .text(0) == txt) |
|
159 |
|
160 def transferData(self): |
|
161 """ |
|
162 Public slot to transfer the associations into the projects data |
|
163 structure. |
|
164 """ |
|
165 self.project.pdata["FILETYPES"] = {} |
|
166 for index in range(self.filetypeAssociationList.topLevelItemCount()): |
|
167 itm = self.filetypeAssociationList.topLevelItem(index) |
|
168 pattern = itm.text(0) |
|
169 index = self.filetypeStrings.index(itm.text(1)) |
|
170 self.project.pdata["FILETYPES"][pattern] = self.filetypes[index] |