VCS/ProjectBrowserHelper.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 the base class of the VCS project browser helper.
8 """
9
10 import os
11
12 from PyQt4.QtCore import QObject
13 from PyQt4.QtGui import QDialog
14
15 from E4Gui.E4Application import e4App
16
17 from UI.DeleteFilesConfirmationDialog import DeleteFilesConfirmationDialog
18
19 from Project.ProjectBrowserModel import ProjectBrowserSimpleDirectoryItem, \
20 ProjectBrowserFileItem, ProjectBrowserDirectoryItem
21
22 from RepositoryInfoDialog import VcsRepositoryInfoDialog
23
24 import Preferences
25
26 class VcsProjectBrowserHelper(QObject):
27 """
28 Class implementing the base class of the VCS project browser helper.
29 """
30 def __init__(self, vcsObject, browserObject, projectObject, isTranslationsBrowser,
31 parent = None, name = None):
32 """
33 Constructor
34
35 @param vcsObject reference to the vcs object
36 @param browserObject reference to the project browser object
37 @param projectObject reference to the project object
38 @param isTranslationsBrowser flag indicating, the helper is requested for the
39 translations browser (this needs some special treatment)
40 @param parent parent widget (QWidget)
41 @param name name of this object (string)
42 """
43 QObject.__init__(self, parent)
44 if name:
45 self.setObjectName(name)
46
47 self.vcs = vcsObject
48 self.browser = browserObject
49 self.isTranslationsBrowser = isTranslationsBrowser
50 self.project = projectObject
51
52 def addVCSMenus(self, mainMenu, multiMenu, backMenu, dirMenu, dirMultiMenu):
53 """
54 Public method to add the VCS entries to the various project browser menus.
55
56 @param mainMenu reference to the main menu (QPopupMenu)
57 @param multiMenu reference to the multiple selection menu (QPopupMenu)
58 @param backMenu reference to the background menu (QPopupMenu)
59 @param dirMenu reference to the directory menu (QPopupMenu)
60 @param dirMultiMenu reference to the multiple selection directory
61 menu (QPopupMenu)
62 """
63 self._addVCSMenu(mainMenu)
64 self._addVCSMenuMulti(multiMenu)
65 self._addVCSMenuBack(backMenu)
66 self._addVCSMenuDir(dirMenu)
67 self._addVCSMenuDirMulti(dirMultiMenu)
68
69 def showContextMenu(self, menu, standardItems):
70 """
71 Slot called before the context menu is shown.
72
73 It enables/disables the VCS menu entries depending on the overall
74 VCS status and the file status.
75
76 @param menu reference to the menu to be shown
77 @param standardItems array of standard items that need activation/deactivation
78 depending on the overall VCS status
79 """
80 raise RuntimeError('Not implemented')
81
82 def showContextMenuMulti(self, menu, standardItems):
83 """
84 Slot called before the context menu (multiple selections) is shown.
85
86 It enables/disables the VCS menu entries depending on the overall
87 VCS status and the files status.
88
89 @param menu reference to the menu to be shown
90 @param standardItems array of standard items that need activation/deactivation
91 depending on the overall VCS status
92 """
93 raise RuntimeError('Not implemented')
94
95 def showContextMenuDir(self, menu, standardItems):
96 """
97 Slot called before the context menu is shown.
98
99 It enables/disables the VCS menu entries depending on the overall
100 VCS status and the directory status.
101
102 @param menu reference to the menu to be shown
103 @param standardItems array of standard items that need activation/deactivation
104 depending on the overall VCS status
105 """
106 raise RuntimeError('Not implemented')
107
108 def showContextMenuDirMulti(self, menu, standardItems):
109 """
110 Slot called before the context menu is shown.
111
112 It enables/disables the VCS menu entries depending on the overall
113 VCS status and the directory status.
114
115 @param menu reference to the menu to be shown
116 @param standardItems array of standard items that need activation/deactivation
117 depending on the overall VCS status
118 """
119 raise RuntimeError('Not implemented')
120
121 ############################################################################
122 ## General menu handling methods below
123 ############################################################################
124
125 def _VCSUpdate(self):
126 """
127 Protected slot called by the context menu to update a file from the
128 VCS repository.
129 """
130 if self.isTranslationsBrowser:
131 names = [itm.dirName() \
132 for itm in self.browser.getSelectedItems(\
133 [ProjectBrowserSimpleDirectoryItem])]
134 if not names:
135 names = [itm.fileName() \
136 for itm in self.browser.getSelectedItems([ProjectBrowserFileItem])]
137 else:
138 names = []
139 for itm in self.browser.getSelectedItems():
140 try:
141 name = itm.fileName()
142 except AttributeError:
143 name = itm.dirName()
144 names.append(name)
145 self.vcs.vcsUpdate(names)
146
147 def _VCSCommit(self):
148 """
149 Protected slot called by the context menu to commit the changes to the
150 VCS repository.
151 """
152 if self.isTranslationsBrowser:
153 names = [itm.dirName() \
154 for itm in self.browser.getSelectedItems(\
155 [ProjectBrowserSimpleDirectoryItem])]
156 if not names:
157 names = [itm.fileName() \
158 for itm in self.browser.getSelectedItems([ProjectBrowserFileItem])]
159 else:
160 names = []
161 for itm in self.browser.getSelectedItems():
162 try:
163 name = itm.fileName()
164 except AttributeError:
165 name = itm.dirName()
166 names.append(name)
167 if Preferences.getVCS("AutoSaveFiles"):
168 vm = e4App().getObject("ViewManager")
169 for name in names:
170 vm.saveEditor(name)
171 self.vcs.vcsCommit(names, '')
172
173 def _VCSAdd(self):
174 """
175 Protected slot called by the context menu to add the selected file to the
176 VCS repository.
177 """
178 if self.isTranslationsBrowser:
179 items = self.browser.getSelectedItems([ProjectBrowserSimpleDirectoryItem])
180 if items:
181 names = [itm.dirName() for itm in items]
182 qnames = []
183 else:
184 items = self.browser.getSelectedItems([ProjectBrowserFileItem])
185 names = []
186 qnames = []
187 for itm in items:
188 name = itm.fileName()
189 if name.endswith('.qm'):
190 qnames.append(name)
191 else:
192 names.append(name)
193 else:
194 names = []
195 for itm in self.browser.getSelectedItems():
196 try:
197 name = itm.fileName()
198 except AttributeError:
199 name = itm.dirName()
200 names.append(name)
201 qnames = []
202
203 if not len(names + qnames):
204 return
205
206 if len(names + qnames) == 1:
207 if names:
208 self.vcs.vcsAdd(names[0], os.path.isdir(names[0]))
209 else:
210 if self.vcs.canDetectBinaries:
211 self.vcs.vcsAdd(qnames)
212 else:
213 self.vcs.vcsAddBinary(qnames)
214 else:
215 if self.vcs.canDetectBinaries:
216 self.vcs.vcsAdd(names + qnames)
217 else:
218 self.vcs.vcsAdd(names)
219 if len(qnames):
220 self.vcs.vcsAddBinary(qnames)
221 for fn in names + qnames:
222 self._updateVCSStatus(fn)
223
224 def _VCSAddTree(self):
225 """
226 Protected slot called by the context menu.
227
228 It is used to add the selected
229 directory tree to the VCS repository.
230 """
231 items = self.browser.getSelectedItems()
232 names = []
233 for itm in self.browser.getSelectedItems():
234 try:
235 name = itm.fileName()
236 except AttributeError:
237 name = itm.dirName()
238 names.append(name)
239 self.vcs.vcsAddTree(names)
240 for fn in names:
241 self._updateVCSStatus(fn)
242
243 def _VCSRemove(self):
244 """
245 Protected slot called by the context menu to remove the selected file from the
246 VCS repository.
247 """
248 if self.isTranslationsBrowser:
249 items = self.browser.getSelectedItems([ProjectBrowserSimpleDirectoryItem])
250 if items:
251 return # not supported
252
253 isRemoveDirs = False
254 items = self.browser.getSelectedItems([ProjectBrowserFileItem])
255 names = [itm.fileName() for itm in items]
256
257 dlg = DeleteFilesConfirmationDialog(self.parent(),
258 self.trUtf8("Remove from repository (and disk)"),
259 self.trUtf8("Do you really want to remove these translation files from"
260 " the repository (and disk)?"),
261 names)
262 else:
263 items = self.browser.getSelectedItems()
264 isRemoveDirs = len(items) == \
265 self.browser.getSelectedItemsCount(\
266 [ProjectBrowserSimpleDirectoryItem, ProjectBrowserDirectoryItem])
267 if isRemoveDirs:
268 names = [itm.dirName() for itm in items]
269 else:
270 names = [itm.fileName() for itm in items]
271 files = [
272 name.replace(self.browser.project.ppath+os.sep, '') \
273 for name in names]
274
275 dlg = DeleteFilesConfirmationDialog(self.parent(),
276 self.trUtf8("Remove from repository (and disk)"),
277 self.trUtf8("Do you really want to remove these files/directories"
278 " from the repository (and disk)?"),
279 files)
280
281 if dlg.exec_() == QDialog.Accepted:
282 status = self.vcs.vcsRemove(names)
283 if status:
284 if isRemoveDirs:
285 self.browser._removeDir() # remove directories from Project
286 else:
287 self.browser._removeFile() # remove file(s) from project
288
289 def _VCSLog(self):
290 """
291 Protected slot called by the context menu to show the VCS log of a file/directory.
292 """
293 itm = self.browser.currentItem()
294 try:
295 fn = itm.fileName()
296 except AttributeError:
297 fn = itm.dirName()
298 self.vcs.vcsLog(fn)
299
300 def _VCSDiff(self):
301 """
302 Protected slot called by the context menu to show the difference of a
303 file/directory to the repository.
304 """
305 names = []
306 for itm in self.browser.getSelectedItems():
307 try:
308 name = itm.fileName()
309 except AttributeError:
310 name = itm.dirName()
311 names.append(name)
312 self.vcs.vcsDiff(names)
313
314 def _VCSStatus(self):
315 """
316 Protected slot called by the context menu to show the status of a file.
317 """
318 if self.isTranslationsBrowser:
319 items = self.browser.getSelectedItems([ProjectBrowserSimpleDirectoryItem])
320 if items:
321 names = [itm.dirName() for itm in items]
322 else:
323 items = self.browser.getSelectedItems([ProjectBrowserFileItem])
324 names = [itm.fileName() for itm in items]
325 else:
326 names = []
327 for itm in self.browser.getSelectedItems():
328 try:
329 name = itm.fileName()
330 except AttributeError:
331 name = itm.dirName()
332 names.append(name)
333 self.vcs.vcsStatus(names)
334
335 def _VCSRevert(self):
336 """
337 Protected slot called by the context menu to revert changes made to a file.
338 """
339 names = []
340 for itm in self.browser.getSelectedItems():
341 try:
342 name = itm.fileName()
343 except AttributeError:
344 name = itm.dirName()
345 names.append(name)
346 self.vcs.vcsRevert(names)
347
348 def _VCSMerge(self):
349 """
350 Protected slot called by the context menu to merge changes into to a file.
351 """
352 itm = self.browser.currentItem()
353 try:
354 name = itm.fileName()
355 except AttributeError:
356 name = itm.dirName()
357 self.vcs.vcsMerge(name)
358
359 def _VCSInfoDisplay(self):
360 """
361 Protected slot called to show some vcs information.
362 """
363 info = self.vcs.vcsRepositoryInfos(self.project.ppath)
364 dlg = VcsRepositoryInfoDialog(None, info)
365 dlg.exec_()
366
367 def _updateVCSStatus(self, name):
368 """
369 Protected method to update the VCS status of an item.
370
371 @param name filename or directoryname of the item to be updated (string)
372 """
373 self.project.getModel().updateVCSStatus(name)

eric ide

mercurial