/tortoisehg/hgqt/branchop.py

https://bitbucket.org/tortoisehg/hgtk/ · Python · 106 lines · 83 code · 11 blank · 12 comment · 17 complexity · 4ede06a8858bf059a9aca006cd80d6fc MD5 · raw file

  1. # branchop.py - branch operations dialog for TortoiseHg commit tool
  2. #
  3. # Copyright 2010 Steve Borho <steve@borho.org>
  4. #
  5. # This software may be used and distributed according to the terms of the
  6. # GNU General Public License version 2, incorporated herein by reference.
  7. import os
  8. from PyQt4.QtCore import *
  9. from PyQt4.QtGui import *
  10. from tortoisehg.hgqt.i18n import _
  11. from tortoisehg.util import hglib
  12. from tortoisehg.hgqt import qtlib
  13. class BranchOpDialog(QDialog):
  14. 'Dialog for manipulating wctx.branch()'
  15. def __init__(self, repo, oldbranchop, parent=None):
  16. QDialog.__init__(self, parent)
  17. self.setWindowTitle('%s - branch operation' % repo.displayname)
  18. layout = QVBoxLayout()
  19. self.setLayout(layout)
  20. wctx = repo[None]
  21. if len(wctx.parents()) == 2:
  22. lbl = QLabel('<b>'+_('Select branch of merge commit')+'</b>')
  23. layout.addWidget(lbl)
  24. branchCombo = QComboBox()
  25. for p in wctx.parents():
  26. branchCombo.addItem(hglib.tounicode(p.branch()))
  27. layout.addWidget(branchCombo)
  28. else:
  29. text = '<b>'+_('Changes take effect on next commit')+'</b>'
  30. lbl = QLabel(text)
  31. layout.addWidget(lbl)
  32. grid = QGridLayout()
  33. nochange = QRadioButton(_('No branch changes'))
  34. newbranch = QRadioButton(_('Open a new named branch'))
  35. closebranch = QRadioButton(_('Close current named branch'))
  36. branchCombo = QComboBox()
  37. branchCombo.setEditable(True)
  38. for name in hglib.getlivebranch(repo):
  39. if name == wctx.branch():
  40. continue
  41. branchCombo.addItem(hglib.tounicode(name))
  42. branchCombo.activated.connect(self.accept)
  43. grid.addWidget(nochange, 0, 0)
  44. grid.addWidget(newbranch, 1, 0)
  45. grid.addWidget(branchCombo, 1, 1)
  46. grid.addWidget(closebranch, 2, 0)
  47. layout.addLayout(grid)
  48. newbranch.toggled.connect(branchCombo.setEnabled)
  49. branchCombo.setEnabled(False)
  50. if oldbranchop is None:
  51. nochange.setChecked(True)
  52. elif oldbranchop == False:
  53. closebranch.setChecked(True)
  54. else:
  55. assert type(oldbranchop) == QString
  56. bc = branchCombo
  57. names = [bc.itemText(i) for i in xrange(bc.count())]
  58. if oldbranchop in names:
  59. bc.setCurrentIndex(names.index(oldbranchop))
  60. else:
  61. bc.addItem(oldbranchop)
  62. bc.setCurrentIndex(len(names))
  63. newbranch.setChecked(True)
  64. self.closebranch = closebranch
  65. BB = QDialogButtonBox
  66. bb = QDialogButtonBox(BB.Ok|BB.Cancel)
  67. bb.accepted.connect(self.accept)
  68. bb.rejected.connect(self.reject)
  69. bb.button(BB.Ok).setAutoDefault(True)
  70. layout.addWidget(bb)
  71. self.bb = bb
  72. self.branchCombo = branchCombo
  73. def keyPressEvent(self, event):
  74. # todo - is this necessary for a derivation of QDialog?
  75. if event.key() in (Qt.Key_Return, Qt.Key_Enter):
  76. if event.modifiers() == Qt.ControlModifier:
  77. self.accept() # Ctrl+Enter
  78. return
  79. elif event.key() == Qt.Key_Escape:
  80. self.reject()
  81. return
  82. return super(QDialog, self).keyPressEvent(event)
  83. def accept(self):
  84. '''Branch operation is one of:
  85. None - leave wctx branch name untouched
  86. False - close current branch
  87. QString - open new named branch
  88. '''
  89. if self.branchCombo.isEnabled():
  90. self.branchop = self.branchCombo.currentText()
  91. elif self.closebranch.isChecked():
  92. self.branchop = False
  93. else:
  94. self.branchop = None
  95. QDialog.accept(self)