1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
|
import operator
from qtpy import QtCore
from qtpy import QtWidgets
from qtpy.QtCore import Qt
from qtpy.QtCore import Signal
from ..git import STDOUT
from ..i18n import N_
from ..qtutils import get
from .. import cmds
from .. import core
from .. import gitcmds
from .. import icons
from .. import qtutils
from .. import utils
from . import completion
from . import defs
from . import standard
from . import text
def editor(context, run=True):
"""Launch a RemoteEditor instance"""
view = RemoteEditor(context, parent=qtutils.active_window())
if run:
view.show()
view.exec_()
return view
class RemoteEditor(standard.Dialog):
"""Edit remotes associated with the current repository"""
def __init__(self, context, parent=None):
standard.Dialog.__init__(self, parent)
self.setWindowTitle(N_('Edit Remotes'))
if parent is not None:
self.setWindowModality(Qt.WindowModal)
self.context = context
self.current_name = ''
self.current_url = ''
self.remote_list = []
self.remotes = QtWidgets.QListWidget()
self.remotes.setToolTip(N_('Remote git repositories - double-click to rename'))
self.editor = RemoteWidget(context, self)
self.save_button = qtutils.create_button(
text=N_('Save'), icon=icons.save(), default=True
)
self.reset_button = qtutils.create_button(
text=N_('Reset'), icon=icons.discard()
)
tooltip = N_(
'Add and remove remote repositories using the \n'
'Add(+) and Delete(-) buttons on the left-hand side.\n'
'\n'
'Remotes can be renamed by selecting one from the list\n'
'and pressing "enter", or by double-clicking.'
)
hint = N_('Edit remotes by selecting them from the list')
self.default_hint = hint
self.info = text.VimHintedPlainTextEdit(context, hint, parent=self)
self.info.setToolTip(tooltip)
text_width, text_height = qtutils.text_size(self.info.font(), 'M')
width = text_width * 42
height = text_height * 13
self.info.setMinimumWidth(width)
self.info.setMinimumHeight(height)
self.info_thread = RemoteInfoThread(context, self)
icon = icons.add()
tooltip = N_('Add new remote git repository')
self.add_button = qtutils.create_toolbutton(icon=icon, tooltip=tooltip)
self.refresh_button = qtutils.create_toolbutton(
icon=icons.sync(), tooltip=N_('Refresh')
)
self.delete_button = qtutils.create_toolbutton(
icon=icons.remove(), tooltip=N_('Delete remote')
)
self.close_button = qtutils.close_button()
self._edit_button_layout = qtutils.vbox(
defs.no_margin, defs.spacing, self.save_button, self.reset_button
)
self._edit_layout = qtutils.hbox(
defs.no_margin, defs.spacing, self.editor, self._edit_button_layout
)
self._display_layout = qtutils.vbox(
defs.no_margin, defs.spacing, self._edit_layout, self.info
)
self._display_widget = QtWidgets.QWidget(self)
self._display_widget.setLayout(self._display_layout)
self._left_buttons_layout = qtutils.hbox(
defs.no_margin,
defs.button_spacing,
self.add_button,
self.refresh_button,
self.delete_button,
qtutils.STRETCH,
)
self._left_layout = qtutils.vbox(
defs.no_margin, defs.spacing, self._left_buttons_layout, self.remotes
)
self._left_widget = QtWidgets.QWidget(self)
self._left_widget.setLayout(self._left_layout)
self._top_layout = qtutils.splitter(
Qt.Horizontal, self._left_widget, self._display_widget
)
width = self._top_layout.width()
self._top_layout.setSizes([width // 4, width * 3 // 4])
self._button_layout = qtutils.hbox(
defs.margin,
defs.spacing,
qtutils.STRETCH,
self.close_button,
)
self._layout = qtutils.vbox(
defs.margin, defs.spacing, self._top_layout, self._button_layout
)
self.setLayout(self._layout)
qtutils.connect_button(self.add_button, self.add)
qtutils.connect_button(self.delete_button, self.delete)
qtutils.connect_button(self.refresh_button, self.refresh)
qtutils.connect_button(self.close_button, self.accept)
qtutils.connect_button(self.save_button, self.save)
qtutils.connect_button(self.reset_button, self.reset)
qtutils.add_close_action(self)
thread = self.info_thread
thread.result.connect(self.set_info, type=Qt.QueuedConnection)
self.editor.remote_name.returnPressed.connect(self.save)
self.editor.remote_url.returnPressed.connect(self.save)
self.editor.valid.connect(self.editor_validated)
self.remotes.itemChanged.connect(self.remote_item_renamed)
self.remotes.itemSelectionChanged.connect(self.selection_changed)
self.disable_editor()
self.init_state(None, self.resize_widget, parent)
self.remotes.setFocus(Qt.OtherFocusReason)
self.refresh()
def reset(self):
"""Reset the editor back to the last-saved configuration"""
focus = self.focusWidget()
if self.current_name:
self.activate_remote(self.current_name, gather_info=False)
restore_focus(focus)
@property
def changed(self):
"""Has the editor changed any values?"""
url = self.editor.url
name = self.editor.name
return url != self.current_url or name != self.current_name
def save(self):
"""Save edited settings to Git's configuration"""
if not self.changed:
return
context = self.context
name = self.editor.name
url = self.editor.url
old_url = self.current_url
old_name = self.current_name
name_changed = name and name != old_name
url_changed = url and url != old_url
focus = self.focusWidget()
name_ok = False
url_ok = False
# Run the corresponding commands
if name_changed and url_changed:
name_ok, url_ok = cmds.do(cmds.RemoteEdit, context, old_name, name, url)
elif name_changed:
result = cmds.do(cmds.RemoteRename, context, old_name, name)
name_ok = result[0]
elif url_changed:
result = cmds.do(cmds.RemoteSetURL, context, name, url)
url_ok = result[0]
# Update state if the URL change succeeded
gather = False
if url_changed and url_ok:
self.current_url = url
gather = True
# A name change requires a refresh
if name_changed and name_ok:
self.current_name = name
self.refresh(select=False)
remotes = utils.Sequence(self.remote_list)
idx = remotes.index(name)
self.select_remote(idx)
gather = False # already done by select_remote()
if name_changed or url_changed:
valid = self.editor.validate()
self.editor_validated(valid)
restore_focus(focus)
if gather:
self.gather_info()
def editor_validated(self, valid):
"""Update widgets when fields are edited and validated"""
changed = self.changed
self.reset_button.setEnabled(changed)
self.save_button.setEnabled(changed and valid)
def disable_editor(self):
"""Disable the editor"""
self.save_button.setEnabled(False)
self.reset_button.setEnabled(False)
self.editor.setEnabled(False)
self.editor.name = ''
self.editor.url = ''
self.info.hint.set_value(self.default_hint)
self.info.set_value('')
def resize_widget(self, parent):
"""Set the initial size of the widget"""
width, height = qtutils.default_size(parent, 720, 445)
self.resize(width, height)
def set_info(self, info):
"""Update the info display widget with remote details"""
self.info.hint.set_value(self.default_hint)
self.info.set_value(info)
def select_remote(self, index):
"""Select a remote by index"""
if index >= 0:
item = self.remotes.item(index)
if item:
item.setSelected(True)
def refresh(self, select=True):
"""Refresh the list of remotes to match the Git configuration"""
git = self.context.git
remotes = git.remote()[STDOUT].splitlines()
# Ignore notifications from self.remotes while mutating.
with qtutils.BlockSignals(self.remotes):
self.remotes.clear()
self.remotes.addItems(remotes)
self.remote_list = remotes
for idx in range(len(remotes)):
item = self.remotes.item(idx)
item.setFlags(item.flags() | Qt.ItemIsEditable)
if select:
if not self.current_name and remotes:
# Nothing is selected; select the first item
self.select_remote(0)
elif self.current_name and remotes:
# Reselect the previously selected item
remote_seq = utils.Sequence(remotes)
idx = remote_seq.index(self.current_name)
if idx >= 0:
item = self.remotes.item(idx)
if item:
item.setSelected(True)
def add(self):
"""Add a new remote"""
ok, name = add_remote(self.context, self)
if ok:
self.refresh(select=False)
try:
idx = self.remote_list.index(name)
except ValueError:
return
self.select_remote(idx)
def delete(self):
"""Delete the current remote"""
remote = qtutils.selected_item(self.remotes, self.remote_list)
if not remote:
return
cmds.do(cmds.RemoteRemove, self.context, remote)
self.update_editor(name='', url='', enable=False)
self.refresh(select=True)
def remote_item_renamed(self, item):
"""Update the editor when the remote name is edited from the remotes list"""
idx = self.remotes.row(item)
if idx < 0:
return
if idx >= len(self.remote_list):
return
old_name = self.remote_list[idx]
new_name = item.text()
if new_name == old_name:
return
if not new_name:
item.setText(old_name)
return
context = self.context
ok, status, _, _ = cmds.do(cmds.RemoteRename, context, old_name, new_name)
if ok and status == 0:
self.remote_list[idx] = new_name
self.activate_remote(new_name)
else:
item.setText(old_name)
def selection_changed(self):
"""Edit remotes when the remote list selection is changed"""
remote = qtutils.selected_item(self.remotes, self.remote_list)
if not remote:
self.disable_editor()
return
self.activate_remote(remote)
def activate_remote(self, name, gather_info=True):
"""Activate the specified remote"""
url = gitcmds.remote_url(self.context, name)
self.update_editor(name=name, url=url)
if gather_info:
self.gather_info()
def update_editor(self, name=None, url=None, enable=True):
"""Update the editor and enable it for editing"""
# These fields must be updated in this exact order otherwise
# the editor will be seen as edited, which causes the Reset button
# to re-enable itself via the valid() -> editor_validated() signal chain.
if name is not None:
self.current_name = name
self.editor.name = name
if url is not None:
self.current_url = url
self.editor.url = url
self.editor.setEnabled(enable)
def gather_info(self):
"""Display details about the remote"""
name = self.current_name
self.info.hint.set_value(N_('Gathering info for "%s"...') % name)
self.info.set_value('')
self.info_thread.remote = name
self.info_thread.start()
def add_remote(context, parent, name='', url='', readonly_url=False):
"""Bring up the "Add Remote" dialog"""
widget = AddRemoteDialog(context, parent, readonly_url=readonly_url)
if name:
widget.name = name
if url:
widget.url = url
if widget.run():
cmds.do(cmds.RemoteAdd, context, widget.name, widget.url)
result = True
else:
result = False
return (result, widget.name)
def restore_focus(focus):
if focus:
focus.setFocus(Qt.OtherFocusReason)
if hasattr(focus, 'selectAll'):
focus.selectAll()
class RemoteInfoThread(QtCore.QThread):
"""Gathers information about a remote for display in the info widget"""
result = Signal(object)
def __init__(self, context, parent):
QtCore.QThread.__init__(self, parent)
self.context = context
self.remote = None
def run(self):
"""Run the thread to gather information"""
remote = self.remote
if remote is None:
return
git = self.context.git
_, out, err = git.remote('show', '-n', remote, _readonly=True)
self.result.emit(out + err)
class AddRemoteDialog(QtWidgets.QDialog):
"""A simple dialog for adding remotes"""
def __init__(self, context, parent, readonly_url=False):
super().__init__(parent)
self.context = context
if parent:
self.setWindowModality(Qt.WindowModal)
self.context = context
self.widget = RemoteWidget(context, self, readonly_url=readonly_url)
self.add_button = qtutils.create_button(
text=N_('Add Remote'), icon=icons.ok(), enabled=False
)
self.close_button = qtutils.close_button()
self._button_layout = qtutils.hbox(
defs.no_margin,
defs.button_spacing,
qtutils.STRETCH,
self.close_button,
self.add_button,
)
self._layout = qtutils.vbox(
defs.margin, defs.spacing, self.widget, self._button_layout
)
self.setLayout(self._layout)
self.widget.valid.connect(self.add_button.setEnabled)
qtutils.connect_button(self.add_button, self.accept)
qtutils.connect_button(self.close_button, self.reject)
def set_name(self, value):
self.widget.name = value
def set_url(self, value):
self.widget.url = value
name = property(operator.attrgetter('widget.name'), set_name)
url = property(operator.attrgetter('widget.url'), set_url)
def run(self):
self.show()
self.raise_()
return self.exec_() == QtWidgets.QDialog.Accepted
def lineedit(context, hint):
"""Create a HintedLineEdit with a preset minimum width"""
widget = text.HintedLineEdit(context, hint)
width = qtutils.text_width(widget.font(), 'M')
widget.setMinimumWidth(width * 32)
return widget
class RemoteWidget(QtWidgets.QWidget):
name = property(
lambda self: get(self.remote_name),
lambda self, value: self.remote_name.set_value(value),
)
url = property(
lambda self: get(self.remote_url),
lambda self, value: self.remote_url.set_value(value),
)
valid = Signal(bool)
def __init__(self, context, parent, readonly_url=False):
super().__init__(parent)
self.setWindowModality(Qt.WindowModal)
self.context = context
self.setWindowTitle(N_('Add remote'))
self.remote_name = lineedit(context, N_('Name for the new remote'))
self.remote_url = lineedit(context, 'git://git.example.com/repo.git')
self.open_button = qtutils.create_button(
text=N_('Browse...'), icon=icons.folder(), tooltip=N_('Select repository')
)
self.url_layout = qtutils.hbox(
defs.no_margin, defs.spacing, self.remote_url, self.open_button
)
validate_remote = completion.RemoteValidator(self.remote_name)
self.remote_name.setValidator(validate_remote)
self._form = qtutils.form(
defs.margin,
defs.spacing,
(N_('Name'), self.remote_name),
(N_('URL'), self.url_layout),
)
self._layout = qtutils.vbox(defs.margin, defs.spacing, self._form)
self.setLayout(self._layout)
self.remote_name.textChanged.connect(self.validate)
self.remote_url.textChanged.connect(self.validate)
qtutils.connect_button(self.open_button, self.open_repo)
if readonly_url:
self.remote_url.setReadOnly(True)
self.open_button.setEnabled(False)
def validate(self, _text_or_index=''):
"""Validate the current inputs"""
name = self.name
url = self.url
self.valid.emit(bool(name) and bool(url))
def open_repo(self):
"""Set the URL from a repository on disk"""
git = self.context.git
repo = qtutils.opendir_dialog(N_('Open Git Repository'), core.getcwd())
if repo and git.is_git_repository(repo):
self.url = repo
|