| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
al. |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
from PyQt5 import QtCore, QtGui, QtWidgets |
| 22 |
from breezy.branch import Branch |
| 23 |
from breezy import ( |
| 24 |
errors, |
| 25 |
) |
| 26 |
from breezy.plugins.qbrz.lib.i18n import gettext |
| 27 |
from breezy.plugins.qbrz.lib.subprocess import SubProcessDialog |
| 28 |
from breezy.plugins.qbrz.lib.ui_tag import Ui_TagForm |
| 29 |
from breezy.plugins.qbrz.lib.util import url_for_display |
| 30 |
|
| 31 |
|
| 32 |
class TagWindow(SubProcessDialog): |
| 33 |
|
| 34 |
|
| 35 |
IX_CREATE = 0 |
| 36 |
IX_MOVE = 1 |
| 37 |
IX_DELETE = 2 |
| 38 |
|
| 39 |
def __init__(self, branch, action=None, tag_name=None, revision=None, |
| 40 |
parent=None, ui_mode=False): |
| 41 |
"""Create tag edit window. |
| 42 |
@param action: default action (create, move, delete) |
| 43 |
""" |
| 44 |
super(TagWindow, self).__init__(name="tag", ui_mode=ui_mode, |
| 45 |
dialog=True, parent=parent) |
| 46 |
|
| 47 |
self.ui = Ui_TagForm() |
| 48 |
self.ui.setupUi(self) |
| 49 |
|
| 50 |
self.restoreSize("tag", (340, 220)) |
| 51 |
|
| 52 |
for w in self.make_default_layout_widgets(): |
| 53 |
self.layout().addWidget(w) |
| 54 |
self.process_widget.hide_progress() |
| 55 |
self.ui.cb_tag.setFocus() |
| 56 |
|
| 57 |
self.set_branch(branch) |
| 58 |
self.setup_initial_values(action, tag_name, revision) |
| 59 |
|
| 60 |
|
| 61 |
self.ui.cb_action.currentIndexChanged[int].connect(self.on_action_changed) |
| 62 |
self.ui.cb_tag.currentIndexChanged[int].connect(self.on_tag_changed) |
| 63 |
self.ui.cb_tag.lineEdit().editingFinished.connect(self.on_tag_changed) |
| 64 |
self.ui.branch_browse.clicked.connect(self.on_browse) |
| 65 |
self.ui.branch_location.editingFinished.connect(self.on_editing_branch) |
| 66 |
|
| 67 |
def set_branch(self, branch): |
| 68 |
self.branch = branch |
| 69 |
self.tags = branch.tags.get_tag_dict() |
| 70 |
self.revno_map = None |
| 71 |
|
| 72 |
self.ui.branch_location.setText(url_for_display(branch.base)) |
| 73 |
self.ui.cb_tag.clear() |
| 74 |
self.ui.cb_tag.addItems(sorted(list(self.tags.keys()), key=str.lower)) |
| 75 |
self.ui.cb_tag.setEditText("") |
| 76 |
self.ui.cb_tag.setCurrentIndex(-1) |
| 77 |
|
| 78 |
def setup_initial_values(self, action=None, tag_name=None, revision=None): |
| 79 |
action_index = {'create': self.IX_CREATE, 'move': self.IX_MOVE, |
| 80 |
'delete': self.IX_DELETE}.get(action) |
| 81 |
if action_index is not None: |
| 82 |
self.ui.cb_action.setCurrentIndex(action_index) |
| 83 |
self.on_action_changed(action_index) |
| 84 |
if tag_name: |
| 85 |
if tag_name not in self.tags: |
| 86 |
self.ui.cb_tag.setCurrentIndex(-1) |
| 87 |
self.ui.cb_tag.setEditText(tag_name) |
| 88 |
self.on_tag_changed() |
| 89 |
if revision and action_index != self.IX_DELETE: |
| 90 |
self.ui.rev_edit.setText(revision[0].user_spec) |
| 91 |
|
| 92 |
def on_action_changed(self, index): |
| 93 |
self.ui.cb_tag.setEditText("") |
| 94 |
self.ui.cb_tag.setCurrentIndex(-1) |
| 95 |
self.ui.rev_edit.setReadOnly(index == self.IX_DELETE) |
| 96 |
self.ui.rev_edit.setText('') |
| 97 |
|
| 98 |
def on_tag_changed(self, index=None): |
| 99 |
if self.ui.cb_action.currentIndex() == self.IX_DELETE: |
| 100 |
tag = str(self.ui.cb_tag.currentText()) |
| 101 |
revid = self.tags.get(tag) |
| 102 |
rev_str = '' |
| 103 |
tooltip = '' |
| 104 |
if revid: |
| 105 |
|
| 106 |
if self.revno_map is None: |
| 107 |
|
| 108 |
|
| 109 |
self.revno_map = self.branch.get_revision_id_to_revno_map() |
| 110 |
rt = self.revno_map.get(revid) |
| 111 |
if rt: |
| 112 |
rev_str = '.'.join(map(str, rt)) |
| 113 |
else: |
| 114 |
rev_str = 'revid:'+revid |
| 115 |
tooltip = rev_str |
| 116 |
self.ui.rev_edit.setText(rev_str) |
| 117 |
self.ui.rev_edit.setToolTip(tooltip) |
| 118 |
|
| 119 |
def validate(self): |
| 120 |
action = self.ui.cb_action.currentIndex() |
| 121 |
title = self.ui.cb_action.currentText() |
| 122 |
tag = str(self.ui.cb_tag.lineEdit().text()) |
| 123 |
has_tag = tag in self.tags |
| 124 |
rev = str(self.ui.rev_edit.text()) |
| 125 |
if not tag: |
| 126 |
self.operation_blocked(gettext('You should specify tag name')) |
| 127 |
return False |
| 128 |
if action == self.IX_CREATE and has_tag: |
| 129 |
if self.ask_confirmation(gettext( |
| 130 |
'Tag "%s" already exists.\n' |
| 131 |
'Do you want to move existing tag?' |
| 132 |
) % tag): |
| 133 |
|
| 134 |
action = self.IX_MOVE |
| 135 |
title = self.ui.cb_action.itemText(action) |
| 136 |
else: |
| 137 |
return False |
| 138 |
if action == self.IX_MOVE and not has_tag: |
| 139 |
if self.ask_confirmation(gettext( |
| 140 |
'Tag "%s" does not exists yet.\n' |
| 141 |
'Do you want to create new tag?' |
| 142 |
) % tag): |
| 143 |
|
| 144 |
action = self.IX_CREATE |
| 145 |
title = self.ui.cb_action.itemText(action) |
| 146 |
else: |
| 147 |
return False |
| 148 |
if action == self.IX_DELETE and not has_tag: |
| 149 |
self.operation_blocked(gettext('Tag "%s" does not exists') % tag) |
| 150 |
return False |
| 151 |
|
| 152 |
args = ['tag'] |
| 153 |
args.append('--directory') |
| 154 |
args.append(str(self.ui.branch_location.text())) |
| 155 |
if action != self.IX_CREATE: |
| 156 |
args.append({self.IX_MOVE: '--force', |
| 157 |
self.IX_DELETE: '--delete' |
| 158 |
}[action]) |
| 159 |
if action != self.IX_DELETE: |
| 160 |
args.append('--revision') |
| 161 |
args.append(rev or '-1') |
| 162 |
args.append(tag) |
| 163 |
self.args = args |
| 164 |
|
| 165 |
return True |
| 166 |
|
| 167 |
def on_browse(self): |
| 168 |
|
| 169 |
directory = QtWidgets.QFileDialog.getExistingDirectory(self, |
| 170 |
gettext('Select branch location'), |
| 171 |
self.ui.branch_location.text()) |
| 172 |
self._try_to_open_branch(directory) |
| 173 |
|
| 174 |
def on_editing_branch(self): |
| 175 |
self._try_to_open_branch(self.ui.branch_location.text()) |
| 176 |
|
| 177 |
def _try_to_open_branch(self, location): |
| 178 |
if location: |
| 179 |
location = str(location) |
| 180 |
try: |
| 181 |
branch = Branch.open_containing(location)[0] |
| 182 |
except errors.NotBranchError: |
| 183 |
QtWidgets.QMessageBox.critical(self, |
| 184 |
gettext('Error'), |
| 185 |
gettext('Not a branch:\n%s') % location, |
| 186 |
gettext('&Close')) |
| 187 |
return |
| 188 |
self.set_branch(branch) |
| 189 |
|
| 190 |
@staticmethod |
| 191 |
def action_from_options(force=None, delete=None): |
| 192 |
action = 'create' |
| 193 |
if force: |
| 194 |
action = 'move' |
| 195 |
if delete: |
| 196 |
action = 'delete' |
| 197 |
return action |
| 198 |
|
| 199 |
|
| 200 |
class CallBackTagWindow(TagWindow): |
| 201 |
|
| 202 |
def __init__(self, branch, closing_function, action=None, tag_name=None, revision=None, |
| 203 |
parent=None, ui_mode=False): |
| 204 |
super(CallBackTagWindow, self).__init__(branch, action=action, |
| 205 |
tag_name=tag_name, revision=revision, |
| 206 |
parent=parent, ui_mode=ui_mode) |
| 207 |
self.closing_function = closing_function |
| 208 |
|
| 209 |
def closeEvent(self, event): |
| 210 |
super(CallBackTagWindow, self).closeEvent(event); |
| 211 |
if self.closing_function: |
| 212 |
self.closing_function() |