Project/FiletypeAssociationDialog.py

changeset 0
de9c2efb9d02
child 12
1d8dd9706f46
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2005 - 2009 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 sys
11 import os
12
13 from PyQt4.QtCore import *
14 from PyQt4.QtGui import *
15
16 from Ui_FiletypeAssociationDialog import Ui_FiletypeAssociationDialog
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 QDialog.__init__(self, parent)
30 self.setupUi(self)
31
32 self.filetypeAssociationList.headerItem().setText(
33 self.filetypeAssociationList.columnCount(), "")
34 self.filetypeAssociationList.header().setSortIndicator(0, Qt.AscendingOrder)
35
36 # keep these lists in sync
37 self.filetypes = ["SOURCES", "FORMS", "TRANSLATIONS", "RESOURCES", "INTERFACES",
38 "OTHERS", "__IGNORE__"]
39 self.filetypeStrings = [self.trUtf8("Sources"), self.trUtf8("Forms"),
40 self.trUtf8("Translations"), self.trUtf8("Resources"),
41 self.trUtf8("Interfaces"), self.trUtf8("Others"),
42 self.trUtf8("Ignore")]
43 self.filetypeCombo.addItems(self.filetypeStrings)
44
45 self.project = project
46 for pattern, filetype in self.project.pdata["FILETYPES"].items():
47 try:
48 index = self.filetypes.index(filetype)
49 itm = self.__createItem(pattern, self.filetypeStrings[index])
50 except ValueError:
51 pass # silently discard entries of unknown type
52
53 self.__resort()
54 self.__reformat()
55
56 def __resort(self):
57 """
58 Private method to resort the tree.
59 """
60 self.filetypeAssociationList.sortItems(self.filetypeAssociationList.sortColumn(),
61 self.filetypeAssociationList.header().sortIndicatorOrder())
62
63 def __reformat(self):
64 """
65 Private method to reformat the tree.
66 """
67 self.filetypeAssociationList.header().resizeSections(QHeaderView.ResizeToContents)
68 self.filetypeAssociationList.header().setStretchLastSection(True)
69
70 def __createItem(self, pattern, filetype):
71 """
72 Private slot to create a new entry in the association list.
73
74 @param pattern pattern of the entry (string)
75 @param filetype file type of the entry (string)
76 @return reference to the newly generated entry (QTreeWidgetItem)
77 """
78 itm = QTreeWidgetItem(self.filetypeAssociationList, [pattern, filetype])
79 return itm
80
81 def on_filetypeAssociationList_currentItemChanged(self, itm, prevItm):
82 """
83 Private slot to handle the currentItemChanged signal of the association list.
84
85 @param itm reference to the new current item (QTreeWidgetItem)
86 @param prevItm reference to the previous current item (QTreeWidgetItem)
87 """
88 if itm is None:
89 self.filePatternEdit.clear()
90 self.filetypeCombo.setCurrentIndex(0)
91 self.deleteAssociationButton.setEnabled(False)
92 else:
93 self.filePatternEdit.setText(itm.text(0))
94 self.filetypeCombo.setCurrentIndex(self.filetypeCombo.findText(itm.text(1)))
95 self.deleteAssociationButton.setEnabled(True)
96
97 @pyqtSlot()
98 def on_addAssociationButton_clicked(self):
99 """
100 Private slot to add the association displayed to the list.
101 """
102 pattern = self.filePatternEdit.text()
103 filetype = self.filetypeCombo.currentText()
104 if pattern:
105 items = self.filetypeAssociationList.findItems(\
106 pattern, Qt.MatchFlags(Qt.MatchExactly), 0)
107 for itm in items:
108 itm = self.filetypeAssociationList.takeTopLevelItem(\
109 self.filetypeAssociationList.indexOfTopLevelItem(itm))
110 del itm
111 itm = self.__createItem(pattern, filetype)
112 self.__resort()
113 self.__reformat()
114 self.filePatternEdit.clear()
115 self.filetypeCombo.setCurrentIndex(0)
116 self.filetypeAssociationList.setCurrentItem(itm)
117
118 @pyqtSlot()
119 def on_deleteAssociationButton_clicked(self):
120 """
121 Private slot to delete the currently selected association of the listbox.
122 """
123 for itm in self.filetypeAssociationList.selectedItems():
124 itm = self.filetypeAssociationList.takeTopLevelItem(\
125 self.filetypeAssociationList.indexOfTopLevelItem(itm))
126 del itm
127
128 self.filetypeAssociationList.clearSelection()
129 self.filePatternEdit.clear()
130 self.filetypeCombo.setCurrentIndex(0)
131
132 def on_filePatternEdit_textChanged(self, txt):
133 """
134 Private slot to handle the textChanged signal of the pattern lineedit.
135
136 @param txt text of the lineedit (string)
137 """
138 if not txt:
139 self.addAssociationButton.setEnabled(False)
140 self.deleteAssociationButton.setEnabled(False)
141 else:
142 self.addAssociationButton.setEnabled(True)
143 if len(self.filetypeAssociationList.selectedItems()) == 0:
144 self.deleteAssociationButton.setEnabled(False)
145 else:
146 self.deleteAssociationButton.setEnabled(\
147 self.filetypeAssociationList.selectedItems()[0].text(0) == txt)
148
149 def transferData(self):
150 """
151 Public slot to transfer the associations into the projects data structure.
152 """
153 self.project.pdata["FILETYPES"] = {}
154 for index in range(self.filetypeAssociationList.topLevelItemCount()):
155 itm = self.filetypeAssociationList.topLevelItem(index)
156 pattern = itm.text(0)
157 index = self.filetypeStrings.index(itm.text(1))
158 self.project.pdata["FILETYPES"][pattern] = self.filetypes[index]

eric ide

mercurial