|
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 define guards for patches. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot, Qt, QCoreApplication |
|
11 from PyQt6.QtWidgets import ( |
|
12 QDialog, QDialogButtonBox, QAbstractButton, QListWidgetItem |
|
13 ) |
|
14 |
|
15 from EricWidgets import EricMessageBox |
|
16 |
|
17 from .Ui_HgQueuesDefineGuardsDialog import Ui_HgQueuesDefineGuardsDialog |
|
18 |
|
19 import UI.PixmapCache |
|
20 |
|
21 |
|
22 class HgQueuesDefineGuardsDialog(QDialog, Ui_HgQueuesDefineGuardsDialog): |
|
23 """ |
|
24 Class implementing a dialog to define guards for patches. |
|
25 """ |
|
26 def __init__(self, vcs, extension, patchesList, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param vcs reference to the vcs object |
|
31 @param extension reference to the extension module (Queues) |
|
32 @param patchesList list of patches (list of strings) |
|
33 @param parent reference to the parent widget (QWidget) |
|
34 """ |
|
35 super().__init__(parent) |
|
36 self.setupUi(self) |
|
37 self.setWindowFlags(Qt.WindowType.Window) |
|
38 |
|
39 self.vcs = vcs |
|
40 self.extension = extension |
|
41 self.__hgClient = vcs.getClient() |
|
42 |
|
43 self.__patches = patchesList[:] |
|
44 self.patchSelector.addItems([""] + self.__patches) |
|
45 |
|
46 self.plusButton.setIcon(UI.PixmapCache.getIcon("plus")) |
|
47 self.minusButton.setIcon(UI.PixmapCache.getIcon("minus")) |
|
48 |
|
49 self.__dirtyList = False |
|
50 self.__currentPatch = "" |
|
51 |
|
52 self.show() |
|
53 QCoreApplication.processEvents() |
|
54 |
|
55 def closeEvent(self, e): |
|
56 """ |
|
57 Protected slot implementing a close event handler. |
|
58 |
|
59 @param e close event (QCloseEvent) |
|
60 """ |
|
61 if self.__hgClient.isExecuting(): |
|
62 self.__hgClient.cancel() |
|
63 |
|
64 if self.__dirtyList: |
|
65 res = EricMessageBox.question( |
|
66 self, |
|
67 self.tr("Unsaved Changes"), |
|
68 self.tr("""The guards list has been changed.""" |
|
69 """ Shall the changes be applied?"""), |
|
70 EricMessageBox.Apply | EricMessageBox.Discard, |
|
71 EricMessageBox.Apply) |
|
72 if res == EricMessageBox.Apply: |
|
73 self.__applyGuards() |
|
74 else: |
|
75 self.__dirtyList = False |
|
76 |
|
77 e.accept() |
|
78 |
|
79 def start(self): |
|
80 """ |
|
81 Public slot to start the list command. |
|
82 """ |
|
83 self.on_patchSelector_activated(0) |
|
84 |
|
85 @pyqtSlot(int) |
|
86 def on_patchSelector_activated(self, index): |
|
87 """ |
|
88 Private slot to get the list of guards defined for the given patch |
|
89 name. |
|
90 |
|
91 @param index index of the selected entry |
|
92 @type int |
|
93 """ |
|
94 patch = self.patchSelector.itemText(index) |
|
95 if self.__dirtyList: |
|
96 res = EricMessageBox.question( |
|
97 self, |
|
98 self.tr("Unsaved Changes"), |
|
99 self.tr("""The guards list has been changed.""" |
|
100 """ Shall the changes be applied?"""), |
|
101 EricMessageBox.Apply | EricMessageBox.Discard, |
|
102 EricMessageBox.Apply) |
|
103 if res == EricMessageBox.Apply: |
|
104 self.__applyGuards() |
|
105 else: |
|
106 self.__dirtyList = False |
|
107 |
|
108 self.guardsList.clear() |
|
109 self.patchNameLabel.setText("") |
|
110 |
|
111 self.guardCombo.clear() |
|
112 guardsList = self.extension.getGuardsList() |
|
113 self.guardCombo.addItems(guardsList) |
|
114 self.guardCombo.setEditText("") |
|
115 |
|
116 args = self.vcs.initCommand("qguard") |
|
117 if patch: |
|
118 args.append(patch) |
|
119 |
|
120 output = self.__hgClient.runcommand(args)[0] |
|
121 |
|
122 if output: |
|
123 patchName, guards = output.split(":", 1) |
|
124 self.patchNameLabel.setText(patchName) |
|
125 guardsList = guards.strip().split() |
|
126 for guard in guardsList: |
|
127 if guard.startswith("+"): |
|
128 icon = UI.PixmapCache.getIcon("plus") |
|
129 guard = guard[1:] |
|
130 sign = "+" |
|
131 elif guard.startswith("-"): |
|
132 icon = UI.PixmapCache.getIcon("minus") |
|
133 guard = guard[1:] |
|
134 sign = "-" |
|
135 else: |
|
136 continue |
|
137 itm = QListWidgetItem(icon, guard, self.guardsList) |
|
138 itm.setData(Qt.ItemDataRole.UserRole, sign) |
|
139 |
|
140 self.on_guardsList_itemSelectionChanged() |
|
141 |
|
142 @pyqtSlot() |
|
143 def on_guardsList_itemSelectionChanged(self): |
|
144 """ |
|
145 Private slot to handle changes of the selection of guards. |
|
146 """ |
|
147 self.removeButton.setEnabled( |
|
148 len(self.guardsList.selectedItems()) > 0) |
|
149 |
|
150 def __getGuard(self, guard): |
|
151 """ |
|
152 Private method to get a reference to a named guard. |
|
153 |
|
154 @param guard name of the guard (string) |
|
155 @return reference to the guard item (QListWidgetItem) |
|
156 """ |
|
157 items = self.guardsList.findItems( |
|
158 guard, Qt.MatchFlag.MatchCaseSensitive) |
|
159 if items: |
|
160 return items[0] |
|
161 else: |
|
162 return None |
|
163 |
|
164 @pyqtSlot(str) |
|
165 def on_guardCombo_editTextChanged(self, txt): |
|
166 """ |
|
167 Private slot to handle changes of the text of the guard combo. |
|
168 |
|
169 @param txt contents of the guard combo line edit (string) |
|
170 """ |
|
171 self.addButton.setEnabled(txt != "") |
|
172 |
|
173 @pyqtSlot() |
|
174 def on_addButton_clicked(self): |
|
175 """ |
|
176 Private slot to add a guard definition to the list or change it. |
|
177 """ |
|
178 guard = self.guardCombo.currentText() |
|
179 if self.plusButton.isChecked(): |
|
180 sign = "+" |
|
181 icon = UI.PixmapCache.getIcon("plus") |
|
182 else: |
|
183 sign = "-" |
|
184 icon = UI.PixmapCache.getIcon("minus") |
|
185 |
|
186 guardItem = self.__getGuard(guard) |
|
187 if guardItem: |
|
188 # guard already exists, remove it first |
|
189 row = self.guardsList.row(guardItem) |
|
190 itm = self.guardsList.takeItem(row) |
|
191 del itm |
|
192 |
|
193 itm = QListWidgetItem(icon, guard, self.guardsList) |
|
194 itm.setData(Qt.ItemDataRole.UserRole, sign) |
|
195 self.guardsList.sortItems() |
|
196 |
|
197 self.__dirtyList = True |
|
198 |
|
199 @pyqtSlot() |
|
200 def on_removeButton_clicked(self): |
|
201 """ |
|
202 Private slot to remove guard definitions from the list. |
|
203 """ |
|
204 res = EricMessageBox.yesNo( |
|
205 self, |
|
206 self.tr("Remove Guards"), |
|
207 self.tr( |
|
208 """Do you really want to remove the selected guards?""")) |
|
209 if res: |
|
210 for guardItem in self.guardsList.selectedItems(): |
|
211 row = self.guardsList.row(guardItem) |
|
212 itm = self.guardsList.takeItem(row) # __IGNORE_WARNING__ |
|
213 del itm |
|
214 |
|
215 self.__dirtyList = True |
|
216 |
|
217 @pyqtSlot(QAbstractButton) |
|
218 def on_buttonBox_clicked(self, button): |
|
219 """ |
|
220 Private slot called by a button of the button box clicked. |
|
221 |
|
222 @param button button that was clicked (QAbstractButton) |
|
223 """ |
|
224 if button == self.buttonBox.button( |
|
225 QDialogButtonBox.StandardButton.Apply |
|
226 ): |
|
227 self.__applyGuards() |
|
228 elif button == self.buttonBox.button( |
|
229 QDialogButtonBox.StandardButton.Close |
|
230 ): |
|
231 self.close() |
|
232 |
|
233 @pyqtSlot() |
|
234 def __applyGuards(self): |
|
235 """ |
|
236 Private slot to apply the defined guards to the current patch. |
|
237 """ |
|
238 if self.__dirtyList: |
|
239 guardsList = [] |
|
240 for row in range(self.guardsList.count()): |
|
241 itm = self.guardsList.item(row) |
|
242 guard = itm.data(Qt.ItemDataRole.UserRole) + itm.text() |
|
243 guardsList.append(guard) |
|
244 |
|
245 args = self.vcs.initCommand("qguard") |
|
246 args.append(self.patchNameLabel.text()) |
|
247 if guardsList: |
|
248 args.append("--") |
|
249 args.extend(guardsList) |
|
250 else: |
|
251 args.append("--none") |
|
252 |
|
253 error = self.__hgClient.runcommand(args)[1] |
|
254 |
|
255 if error: |
|
256 EricMessageBox.warning( |
|
257 self, |
|
258 self.tr("Apply Guard Definitions"), |
|
259 self.tr("""<p>The defined guards could not be""" |
|
260 """ applied.</p><p>Reason: {0}</p>""") |
|
261 .format(error)) |
|
262 else: |
|
263 self.__dirtyList = False |
|
264 index = self.patchSelector.findText(self.patchNameLabel.text()) |
|
265 if index == -1: |
|
266 index = 0 |
|
267 self.on_patchSelector_activated(index) |