|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2008 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the multi project browser. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 from E4Gui.E4Application import e4App |
|
14 |
|
15 from AddProjectDialog import AddProjectDialog |
|
16 |
|
17 import UI.PixmapCache |
|
18 |
|
19 class MultiProjectBrowser(QListWidget): |
|
20 """ |
|
21 Class implementing the multi project browser. |
|
22 """ |
|
23 def __init__(self, multiProject, parent = None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param project reference to the multi project object |
|
28 @param parent parent widget (QWidget) |
|
29 """ |
|
30 QListWidget.__init__(self, parent) |
|
31 self.multiProject = multiProject |
|
32 |
|
33 self.setWindowIcon(UI.PixmapCache.getIcon("eric.png")) |
|
34 self.setAlternatingRowColors(True) |
|
35 |
|
36 self.connect(self.multiProject, SIGNAL("newMultiProject"), |
|
37 self.__newMultiProject) |
|
38 self.connect(self.multiProject, SIGNAL("multiProjectOpened"), |
|
39 self.__multiProjectOpened) |
|
40 self.connect(self.multiProject, SIGNAL("multiProjectClosed"), |
|
41 self.__multiProjectClosed) |
|
42 self.connect(self.multiProject, SIGNAL("projectDataChanged"), |
|
43 self.__projectDataChanged) |
|
44 self.connect(self.multiProject, SIGNAL("projectAdded"), |
|
45 self.__projectAdded) |
|
46 self.connect(self.multiProject, SIGNAL("projectRemoved"), |
|
47 self.__projectRemoved) |
|
48 self.connect(self.multiProject, SIGNAL("projectOpened"), |
|
49 self.__projectOpened) |
|
50 |
|
51 self.__createPopupMenu() |
|
52 self.setContextMenuPolicy(Qt.CustomContextMenu) |
|
53 self.connect(self, SIGNAL("customContextMenuRequested(const QPoint &)"), |
|
54 self.__contextMenuRequested) |
|
55 self.connect(self, SIGNAL("itemActivated(QListWidgetItem*)"), self.__openItem) |
|
56 |
|
57 ############################################################################ |
|
58 ## Slot handling methods below |
|
59 ############################################################################ |
|
60 |
|
61 def __newMultiProject(self): |
|
62 """ |
|
63 Private slot to handle the creation of a new multi project. |
|
64 """ |
|
65 self.clear() |
|
66 |
|
67 def __multiProjectOpened(self): |
|
68 """ |
|
69 Private slot to handle the opening of a multi project. |
|
70 """ |
|
71 for project in self.multiProject.getProjects(): |
|
72 self.__addProject(project) |
|
73 |
|
74 self.sortItems() |
|
75 |
|
76 def __multiProjectClosed(self): |
|
77 """ |
|
78 Private slot to handle the closing of a multi project. |
|
79 """ |
|
80 self.clear() |
|
81 |
|
82 def __projectAdded(self, project): |
|
83 """ |
|
84 Private slot to handle the addition of a project to the multi project. |
|
85 |
|
86 @param project reference to the project data dictionary |
|
87 """ |
|
88 self.__addProject(project) |
|
89 self.sortItems() |
|
90 |
|
91 def __projectRemoved(self, project): |
|
92 """ |
|
93 Private slot to handle the removal of a project from the multi project. |
|
94 |
|
95 @param project reference to the project data dictionary |
|
96 """ |
|
97 row = self.__findProjectItem(project) |
|
98 if row > -1: |
|
99 itm = self.takeItem(row) |
|
100 del itm |
|
101 |
|
102 def __projectDataChanged(self, project): |
|
103 """ |
|
104 Private slot to handle the change of a project of the multi project. |
|
105 |
|
106 @param project reference to the project data dictionary |
|
107 """ |
|
108 row = self.__findProjectItem(project) |
|
109 if row > -1: |
|
110 self.__setItemData(self.item(row), project) |
|
111 |
|
112 self.sortItems() |
|
113 |
|
114 def __projectOpened(self, projectfile): |
|
115 """ |
|
116 Private slot to handle the opening of a project. |
|
117 """ |
|
118 project = { |
|
119 'name' : "", |
|
120 'file' : projectfile, |
|
121 'master' : False, |
|
122 'description' : "", |
|
123 } |
|
124 row = self.__findProjectItem(project) |
|
125 if row > -1: |
|
126 self.item(row).setSelected(True) |
|
127 |
|
128 def __contextMenuRequested(self, coord): |
|
129 """ |
|
130 Private slot to show the context menu. |
|
131 |
|
132 @param coord the position of the mouse pointer (QPoint) |
|
133 """ |
|
134 itm = self.itemAt(coord) |
|
135 if itm is None: |
|
136 self.__backMenu.popup(self.mapToGlobal(coord)) |
|
137 else: |
|
138 self.__menu.popup(self.mapToGlobal(coord)) |
|
139 |
|
140 def __openItem(self, itm = None): |
|
141 """ |
|
142 Private slot to open a project. |
|
143 |
|
144 @param itm reference to the project item to be opened (QListWidgetItem) |
|
145 """ |
|
146 if itm is None: |
|
147 itm = self.currentItem() |
|
148 if itm is None: |
|
149 return |
|
150 |
|
151 filename = itm.data(Qt.UserRole).toString() |
|
152 if filename: |
|
153 self.multiProject.openProject(filename) |
|
154 |
|
155 ############################################################################ |
|
156 ## Private methods below |
|
157 ############################################################################ |
|
158 |
|
159 def __addProject(self, project): |
|
160 """ |
|
161 Private method to add a project to the list. |
|
162 |
|
163 @param project reference to the project data dictionary |
|
164 """ |
|
165 itm = QListWidgetItem(self) |
|
166 self.__setItemData(itm, project) |
|
167 |
|
168 def __setItemData(self, itm, project): |
|
169 """ |
|
170 Private method to set the data of a project item. |
|
171 |
|
172 @param itm reference to the item to be set (QListWidgetItem) |
|
173 @param project reference to the project data dictionary |
|
174 """ |
|
175 itm.setText(project['name']) |
|
176 if project['master']: |
|
177 itm.setIcon(UI.PixmapCache.getIcon("masterProject.png")) |
|
178 else: |
|
179 itm.setIcon(UI.PixmapCache.getIcon("empty.png")) |
|
180 itm.setToolTip(project['file']) |
|
181 itm.setData(Qt.UserRole, QVariant(project['file'])) |
|
182 |
|
183 def __findProjectItem(self, project): |
|
184 """ |
|
185 Private method to search a specific project item. |
|
186 |
|
187 @param project reference to the project data dictionary |
|
188 @return row number of the project, -1 if not found (integer) |
|
189 """ |
|
190 row = 0 |
|
191 while row < self.count(): |
|
192 itm = self.item(row) |
|
193 data = itm.data(Qt.UserRole).toString() |
|
194 if data == project['file']: |
|
195 return row |
|
196 row += 1 |
|
197 |
|
198 return -1 |
|
199 |
|
200 def __removeProject(self): |
|
201 """ |
|
202 Private method to handle the Remove context menu entry. |
|
203 """ |
|
204 itm = self.currentItem() |
|
205 if itm is not None: |
|
206 filename = itm.data(Qt.UserRole).toString() |
|
207 if filename: |
|
208 self.multiProject.removeProject(filename) |
|
209 |
|
210 def __showProjectProperties(self): |
|
211 """ |
|
212 Private method to show the data of a project entry. |
|
213 """ |
|
214 itm = self.currentItem() |
|
215 if itm is not None: |
|
216 filename = itm.data(Qt.UserRole).toString() |
|
217 if filename: |
|
218 project = self.multiProject.getProject(filename) |
|
219 if project is not None: |
|
220 dlg = AddProjectDialog(self, project = project) |
|
221 if dlg.exec_() == QDialog.Accepted: |
|
222 name, filename, isMaster, description = dlg.getData() |
|
223 project = { |
|
224 'name' : name, |
|
225 'file' : filename, |
|
226 'master' : isMaster, |
|
227 'description' : description, |
|
228 } |
|
229 self.multiProject.changeProjectProperties(project) |
|
230 |
|
231 def __createPopupMenu(self): |
|
232 """ |
|
233 Private method to create the popup menu. |
|
234 """ |
|
235 self.__menu = QMenu(self) |
|
236 self.__menu.addAction(self.trUtf8("Open"), self.__openItem) |
|
237 self.__menu.addAction(self.trUtf8("Remove"), self.__removeProject) |
|
238 self.__menu.addAction(self.trUtf8("Properties"), self.__showProjectProperties) |
|
239 self.__menu.addSeparator() |
|
240 self.__menu.addAction(self.trUtf8("Configure..."), self.__configure) |
|
241 |
|
242 self.__backMenu = QMenu(self) |
|
243 self.__backMenu.addAction(self.trUtf8("Configure..."), self.__configure) |
|
244 |
|
245 def __configure(self): |
|
246 """ |
|
247 Private method to open the configuration dialog. |
|
248 """ |
|
249 e4App().getObject("UserInterface").showPreferences("multiProjectPage") |