|
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 incoming or outgoing bookmarks. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import Qt, QCoreApplication |
|
11 from PyQt6.QtWidgets import ( |
|
12 QDialog, QDialogButtonBox, QHeaderView, QTreeWidgetItem |
|
13 ) |
|
14 |
|
15 from .Ui_HgBookmarksInOutDialog import Ui_HgBookmarksInOutDialog |
|
16 |
|
17 |
|
18 class HgBookmarksInOutDialog(QDialog, Ui_HgBookmarksInOutDialog): |
|
19 """ |
|
20 Class implementing a dialog to show a list of incoming or outgoing |
|
21 bookmarks. |
|
22 """ |
|
23 INCOMING = 0 |
|
24 OUTGOING = 1 |
|
25 |
|
26 def __init__(self, vcs, mode, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param vcs reference to the vcs object |
|
31 @param mode mode of the dialog (HgBookmarksInOutDialog.INCOMING, |
|
32 HgBookmarksInOutDialog.OUTGOING) |
|
33 @param parent reference to the parent widget (QWidget) |
|
34 @exception ValueError raised to indicate an invalid dialog mode |
|
35 """ |
|
36 super().__init__(parent) |
|
37 self.setupUi(self) |
|
38 self.setWindowFlags(Qt.WindowType.Window) |
|
39 |
|
40 self.buttonBox.button( |
|
41 QDialogButtonBox.StandardButton.Close).setEnabled(False) |
|
42 self.buttonBox.button( |
|
43 QDialogButtonBox.StandardButton.Cancel).setDefault(True) |
|
44 |
|
45 if mode not in [self.INCOMING, self.OUTGOING]: |
|
46 raise ValueError("Bad value for mode") |
|
47 if mode == self.INCOMING: |
|
48 self.setWindowTitle(self.tr("Mercurial Incoming Bookmarks")) |
|
49 elif mode == self.OUTGOING: |
|
50 self.setWindowTitle(self.tr("Mercurial Outgoing Bookmarks")) |
|
51 |
|
52 self.vcs = vcs |
|
53 self.mode = mode |
|
54 self.__hgClient = vcs.getClient() |
|
55 |
|
56 self.bookmarksList.headerItem().setText( |
|
57 self.bookmarksList.columnCount(), "") |
|
58 self.bookmarksList.header().setSortIndicator( |
|
59 3, Qt.SortOrder.AscendingOrder) |
|
60 |
|
61 self.show() |
|
62 QCoreApplication.processEvents() |
|
63 |
|
64 def closeEvent(self, e): |
|
65 """ |
|
66 Protected slot implementing a close event handler. |
|
67 |
|
68 @param e close event (QCloseEvent) |
|
69 """ |
|
70 if self.__hgClient.isExecuting(): |
|
71 self.__hgClient.cancel() |
|
72 |
|
73 e.accept() |
|
74 |
|
75 def start(self): |
|
76 """ |
|
77 Public slot to start the bookmarks command. |
|
78 |
|
79 @exception ValueError raised to indicate an invalid dialog mode |
|
80 """ |
|
81 self.errorGroup.hide() |
|
82 |
|
83 self.intercept = False |
|
84 self.activateWindow() |
|
85 |
|
86 if self.mode not in (self.INCOMING, self.OUTGOING): |
|
87 raise ValueError("Bad value for mode") |
|
88 |
|
89 args = ( |
|
90 self.vcs.initCommand("incoming") |
|
91 if self.mode == self.INCOMING else |
|
92 self.vcs.initCommand("outgoing") |
|
93 ) |
|
94 |
|
95 args.append('--bookmarks') |
|
96 |
|
97 out, err = self.__hgClient.runcommand(args) |
|
98 if err: |
|
99 self.__showError(err) |
|
100 if out: |
|
101 for line in out.splitlines(): |
|
102 self.__processOutputLine(line) |
|
103 if self.__hgClient.wasCanceled(): |
|
104 break |
|
105 self.__finish() |
|
106 |
|
107 def __finish(self): |
|
108 """ |
|
109 Private slot called when the process finished or the user pressed |
|
110 the button. |
|
111 """ |
|
112 self.buttonBox.button( |
|
113 QDialogButtonBox.StandardButton.Close).setEnabled(True) |
|
114 self.buttonBox.button( |
|
115 QDialogButtonBox.StandardButton.Cancel).setEnabled(False) |
|
116 self.buttonBox.button( |
|
117 QDialogButtonBox.StandardButton.Close).setDefault(True) |
|
118 self.buttonBox.button( |
|
119 QDialogButtonBox.StandardButton.Close).setFocus( |
|
120 Qt.FocusReason.OtherFocusReason) |
|
121 |
|
122 if self.bookmarksList.topLevelItemCount() == 0: |
|
123 # no bookmarks defined |
|
124 self.__generateItem(self.tr("no bookmarks found"), "") |
|
125 self.__resizeColumns() |
|
126 self.__resort() |
|
127 |
|
128 def on_buttonBox_clicked(self, button): |
|
129 """ |
|
130 Private slot called by a button of the button box clicked. |
|
131 |
|
132 @param button button that was clicked (QAbstractButton) |
|
133 """ |
|
134 if button == self.buttonBox.button( |
|
135 QDialogButtonBox.StandardButton.Close |
|
136 ): |
|
137 self.close() |
|
138 elif button == self.buttonBox.button( |
|
139 QDialogButtonBox.StandardButton.Cancel |
|
140 ): |
|
141 if self.__hgClient: |
|
142 self.__hgClient.cancel() |
|
143 else: |
|
144 self.__finish() |
|
145 |
|
146 def __resort(self): |
|
147 """ |
|
148 Private method to resort the tree. |
|
149 """ |
|
150 self.bookmarksList.sortItems( |
|
151 self.bookmarksList.sortColumn(), |
|
152 self.bookmarksList.header().sortIndicatorOrder()) |
|
153 |
|
154 def __resizeColumns(self): |
|
155 """ |
|
156 Private method to resize the list columns. |
|
157 """ |
|
158 self.bookmarksList.header().resizeSections( |
|
159 QHeaderView.ResizeMode.ResizeToContents) |
|
160 self.bookmarksList.header().setStretchLastSection(True) |
|
161 |
|
162 def __generateItem(self, changeset, name): |
|
163 """ |
|
164 Private method to generate a bookmark item in the bookmarks list. |
|
165 |
|
166 @param changeset changeset of the bookmark (string) |
|
167 @param name name of the bookmark (string) |
|
168 """ |
|
169 QTreeWidgetItem(self.bookmarksList, [ |
|
170 name, |
|
171 changeset]) |
|
172 |
|
173 def __processOutputLine(self, line): |
|
174 """ |
|
175 Private method to process the lines of output. |
|
176 |
|
177 @param line output line to be processed (string) |
|
178 """ |
|
179 if line.startswith(" "): |
|
180 li = line.strip().split() |
|
181 changeset = li[-1] |
|
182 del li[-1] |
|
183 name = " ".join(li) |
|
184 self.__generateItem(changeset, name) |
|
185 |
|
186 def __showError(self, out): |
|
187 """ |
|
188 Private slot to show some error. |
|
189 |
|
190 @param out error to be shown (string) |
|
191 """ |
|
192 self.errorGroup.show() |
|
193 self.errors.insertPlainText(out) |
|
194 self.errors.ensureCursorVisible() |