PageRenderTime 57ms CodeModel.GetById 36ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tortoisehg/hgqt/branchop.py

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