src/eric7/VCS/ProjectBrowserHelper.py

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

eric ide

mercurial