src/eric7/Plugins/VcsPlugins/vcsMercurial/HgBookmarksListDialog.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2011 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to show a list of bookmarks.
8 """
9
10 from PyQt6.QtCore import pyqtSlot, Qt, QCoreApplication, QPoint
11 from PyQt6.QtWidgets import (
12 QDialog, QDialogButtonBox, QHeaderView, QTreeWidgetItem, QLineEdit, QMenu,
13 QInputDialog
14 )
15
16 from EricWidgets.EricApplication import ericApp
17 from EricWidgets import EricMessageBox
18
19 from .Ui_HgBookmarksListDialog import Ui_HgBookmarksListDialog
20
21 import UI.PixmapCache
22
23
24 class HgBookmarksListDialog(QDialog, Ui_HgBookmarksListDialog):
25 """
26 Class implementing a dialog to show a list of bookmarks.
27 """
28 def __init__(self, vcs, parent=None):
29 """
30 Constructor
31
32 @param vcs reference to the vcs object
33 @param parent parent widget (QWidget)
34 """
35 super().__init__(parent)
36 self.setupUi(self)
37 self.setWindowFlags(Qt.WindowType.Window)
38
39 self.refreshButton = self.buttonBox.addButton(
40 self.tr("Refresh"), QDialogButtonBox.ButtonRole.ActionRole)
41 self.refreshButton.setToolTip(
42 self.tr("Press to refresh the bookmarks display"))
43 self.refreshButton.setEnabled(False)
44 self.buttonBox.button(
45 QDialogButtonBox.StandardButton.Close).setEnabled(False)
46 self.buttonBox.button(
47 QDialogButtonBox.StandardButton.Cancel).setDefault(True)
48
49 self.vcs = vcs
50 self.__bookmarksList = None
51 self.__hgClient = vcs.getClient()
52 self.__bookmarksDefined = False
53 self.__currentRevision = ""
54
55 self.bookmarksList.headerItem().setText(
56 self.bookmarksList.columnCount(), "")
57 self.bookmarksList.header().setSortIndicator(
58 3, Qt.SortOrder.AscendingOrder)
59
60 self.show()
61 QCoreApplication.processEvents()
62
63 def closeEvent(self, e):
64 """
65 Protected slot implementing a close event handler.
66
67 @param e close event (QCloseEvent)
68 """
69 if self.__hgClient.isExecuting():
70 self.__hgClient.cancel()
71
72 e.accept()
73
74 def start(self, bookmarksList):
75 """
76 Public slot to start the bookmarks command.
77
78 @param bookmarksList reference to string list receiving the bookmarks
79 (list of strings)
80 """
81 self.bookmarksList.clear()
82 self.__bookmarksDefined = False
83
84 self.errorGroup.hide()
85
86 self.intercept = False
87 self.activateWindow()
88
89 self.__bookmarksList = bookmarksList
90 del self.__bookmarksList[:] # clear the list
91
92 args = self.vcs.initCommand("bookmarks")
93
94 self.refreshButton.setEnabled(False)
95
96 out, err = self.__hgClient.runcommand(args)
97 if err:
98 self.__showError(err)
99 if out:
100 for line in out.splitlines():
101 self.__processOutputLine(line)
102 if self.__hgClient.wasCanceled():
103 break
104 self.__finish()
105
106 def __finish(self):
107 """
108 Private slot called when the process finished or the user pressed
109 the button.
110 """
111 self.refreshButton.setEnabled(True)
112
113 self.buttonBox.button(
114 QDialogButtonBox.StandardButton.Close).setEnabled(True)
115 self.buttonBox.button(
116 QDialogButtonBox.StandardButton.Cancel).setEnabled(False)
117 self.buttonBox.button(
118 QDialogButtonBox.StandardButton.Close).setDefault(True)
119 self.buttonBox.button(
120 QDialogButtonBox.StandardButton.Close).setFocus(
121 Qt.FocusReason.OtherFocusReason)
122
123 if self.bookmarksList.topLevelItemCount() == 0:
124 # no bookmarks defined
125 self.__generateItem(
126 self.tr("no bookmarks defined"), "", "", "")
127 self.__bookmarksDefined = False
128 else:
129 self.__bookmarksDefined = True
130
131 self.__resizeColumns()
132 self.__resort()
133
134 # restore current item
135 if self.__currentRevision:
136 items = self.bookmarksList.findItems(
137 self.__currentRevision, Qt.MatchFlag.MatchExactly, 0)
138 if items:
139 self.bookmarksList.setCurrentItem(items[0])
140 self.__currentRevision = ""
141 self.bookmarksList.setFocus(Qt.FocusReason.OtherFocusReason)
142
143 def on_buttonBox_clicked(self, button):
144 """
145 Private slot called by a button of the button box clicked.
146
147 @param button button that was clicked (QAbstractButton)
148 """
149 if button == self.buttonBox.button(
150 QDialogButtonBox.StandardButton.Close
151 ):
152 self.close()
153 elif button == self.buttonBox.button(
154 QDialogButtonBox.StandardButton.Cancel
155 ):
156 if self.__hgClient:
157 self.__hgClient.cancel()
158 else:
159 self.__finish()
160 elif button == self.refreshButton:
161 self.on_refreshButton_clicked()
162
163 def __resort(self):
164 """
165 Private method to resort the tree.
166 """
167 self.bookmarksList.sortItems(
168 self.bookmarksList.sortColumn(),
169 self.bookmarksList.header().sortIndicatorOrder())
170
171 def __resizeColumns(self):
172 """
173 Private method to resize the list columns.
174 """
175 self.bookmarksList.header().resizeSections(
176 QHeaderView.ResizeMode.ResizeToContents)
177 self.bookmarksList.header().setStretchLastSection(True)
178
179 def __generateItem(self, revision, changeset, status, name):
180 """
181 Private method to generate a bookmark item in the bookmarks list.
182
183 @param revision revision of the bookmark (string)
184 @param changeset changeset of the bookmark (string)
185 @param status of the bookmark (string)
186 @param name name of the bookmark (string)
187 """
188 itm = QTreeWidgetItem(self.bookmarksList)
189 if revision[0].isdecimal():
190 # valid bookmark entry
191 itm.setData(0, Qt.ItemDataRole.DisplayRole, int(revision))
192 itm.setData(1, Qt.ItemDataRole.DisplayRole, changeset)
193 itm.setData(2, Qt.ItemDataRole.DisplayRole, status)
194 itm.setData(3, Qt.ItemDataRole.DisplayRole, name)
195 itm.setTextAlignment(0, Qt.AlignmentFlag.AlignRight)
196 itm.setTextAlignment(1, Qt.AlignmentFlag.AlignRight)
197 itm.setTextAlignment(2, Qt.AlignmentFlag.AlignHCenter)
198 else:
199 # error message
200 itm.setData(0, Qt.ItemDataRole.DisplayRole, revision)
201
202 def __processOutputLine(self, line):
203 """
204 Private method to process the lines of output.
205
206 @param line output line to be processed (string)
207 """
208 li = line.split()
209 if li[-1][0] in "1234567890":
210 # last element is a rev:changeset
211 rev, changeset = li[-1].split(":", 1)
212 del li[-1]
213 if li[0] == "*":
214 status = "current"
215 del li[0]
216 else:
217 status = ""
218 name = " ".join(li)
219 self.__generateItem(rev, changeset, status, name)
220 if self.__bookmarksList is not None:
221 self.__bookmarksList.append(name)
222
223 def __showError(self, out):
224 """
225 Private slot to show some error.
226
227 @param out error to be shown (string)
228 """
229 self.errorGroup.show()
230 self.errors.insertPlainText(out)
231 self.errors.ensureCursorVisible()
232
233 @pyqtSlot()
234 def on_refreshButton_clicked(self):
235 """
236 Private slot to refresh the status display.
237 """
238 # save the current items commit ID
239 itm = self.bookmarksList.currentItem()
240 if itm is not None:
241 self.__currentRevision = itm.text(0)
242 else:
243 self.__currentRevision = ""
244
245 self.start(self.__bookmarksList)
246
247 @pyqtSlot(QPoint)
248 def on_bookmarksList_customContextMenuRequested(self, pos):
249 """
250 Private slot to handle the context menu request.
251
252 @param pos position the context menu was requetsed at
253 @type QPoint
254 """
255 itm = self.bookmarksList.itemAt(pos)
256 if itm is not None and self.__bookmarksDefined:
257 menu = QMenu(self.bookmarksList)
258 menu.addAction(
259 UI.PixmapCache.getIcon("vcsSwitch"),
260 self.tr("Switch to"), self.__switchTo)
261 menu.addSeparator()
262 menu.addAction(
263 UI.PixmapCache.getIcon("deleteBookmark"),
264 self.tr("Delete"), self.__deleteBookmark)
265 menu.addAction(
266 UI.PixmapCache.getIcon("renameBookmark"),
267 self.tr("Rename"), self.__renameBookmark)
268 menu.addSeparator()
269 act = menu.addAction(
270 UI.PixmapCache.getIcon("pullBookmark"),
271 self.tr("Pull"), self.__pullBookmark)
272 act.setEnabled(self.vcs.canPull())
273 act = menu.addAction(
274 UI.PixmapCache.getIcon("pushBookmark"),
275 self.tr("Push"), self.__pushBookmark)
276 act.setEnabled(self.vcs.canPush())
277 menu.addSeparator()
278 act = menu.addAction(
279 UI.PixmapCache.getIcon("pushBookmark"),
280 self.tr("Push Current"), self.__pushCurrentBookmark)
281 act.setEnabled(self.vcs.canPush())
282 if self.vcs.version >= (5, 7):
283 act = menu.addAction(
284 UI.PixmapCache.getIcon("pushBookmark"),
285 self.tr("Push All"), self.__pushAllBookmarks)
286 act.setEnabled(self.vcs.canPush())
287
288 menu.popup(self.bookmarksList.mapToGlobal(pos))
289
290 def __switchTo(self):
291 """
292 Private slot to switch the working directory to the selected revision.
293 """
294 itm = self.bookmarksList.currentItem()
295 bookmark = itm.text(3).strip()
296 if bookmark:
297 shouldReopen = self.vcs.vcsUpdate(revision=bookmark)
298 if shouldReopen:
299 res = EricMessageBox.yesNo(
300 None,
301 self.tr("Switch"),
302 self.tr(
303 """The project should be reread. Do this now?"""),
304 yesDefault=True)
305 if res:
306 ericApp().getObject("Project").reopenProject()
307 return
308
309 self.on_refreshButton_clicked()
310
311 def __deleteBookmark(self):
312 """
313 Private slot to delete the selected bookmark.
314 """
315 itm = self.bookmarksList.currentItem()
316 bookmark = itm.text(3).strip()
317 if bookmark:
318 yes = EricMessageBox.yesNo(
319 self,
320 self.tr("Delete Bookmark"),
321 self.tr("""<p>Shall the bookmark <b>{0}</b> really be"""
322 """ deleted?</p>""").format(bookmark))
323 if yes:
324 self.vcs.hgBookmarkDelete(bookmark=bookmark)
325 self.on_refreshButton_clicked()
326
327 def __renameBookmark(self):
328 """
329 Private slot to rename the selected bookmark.
330 """
331 itm = self.bookmarksList.currentItem()
332 bookmark = itm.text(3).strip()
333 if bookmark:
334 newName, ok = QInputDialog.getText(
335 self,
336 self.tr("Rename Bookmark"),
337 self.tr("<p>Enter the new name for bookmark <b>{0}</b>:</p>")
338 .format(bookmark),
339 QLineEdit.EchoMode.Normal)
340 if ok and bool(newName):
341 self.vcs.hgBookmarkRename((bookmark, newName))
342 self.on_refreshButton_clicked()
343
344 def __pullBookmark(self):
345 """
346 Private slot to pull the selected bookmark.
347 """
348 itm = self.bookmarksList.currentItem()
349 bookmark = itm.text(3).strip()
350 if bookmark:
351 self.vcs.hgBookmarkPull(bookmark=bookmark)
352 self.on_refreshButton_clicked()
353
354 def __pushBookmark(self):
355 """
356 Private slot to push the selected bookmark.
357 """
358 itm = self.bookmarksList.currentItem()
359 bookmark = itm.text(3).strip()
360 if bookmark:
361 self.vcs.hgBookmarkPush(bookmark=bookmark)
362 self.on_refreshButton_clicked()
363
364 def __pushCurrentBookmark(self):
365 """
366 Private slot to push the current bookmark.
367 """
368 self.vcs.hgBookmarkPush(current=True)
369 self.on_refreshButton_clicked()
370
371 def __pushAllBookmarks(self):
372 """
373 Private slot to push all bookmarks.
374 """
375 self.vcs.hgBookmarkPush(allBookmarks=True)
376 self.on_refreshButton_clicked()

eric ide

mercurial