repofilter.py 3.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
# repofilter.py - TortoiseHg toolbar for filtering changesets
#
# Copyright (C) 2007-2010 Logilab. All rights reserved.
# Copyright (C) 2010 Yuya Nishihara <yuya@tcha.org>
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.

from PyQt4.QtCore import *
from PyQt4.QtGui import *

Yuya Nishihara's avatar
Yuya Nishihara committed
12
13
from tortoisehg.hgqt.i18n import _

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class RepoFilterBar(QToolBar):
    """Toolbar for RepoWidget to filter changesets"""

    branchChanged = pyqtSignal(unicode, bool)
    """Emitted (branch, allparents) when branch selection changed"""

    def __init__(self, repo, parent=None):
        super(RepoFilterBar, self).__init__(parent)
        self.layout().setContentsMargins(0, 0, 0, 0)
        self._repo = repo

        self.addWidget(QLineEdit(text='### placeholder for revsets ###',
                                 enabled=False))

        self._initbranchfilter()
        self.refresh()

    def _initbranchfilter(self):
32
        self._branchLabel = QToolButton(
33
34
            text=_('Branch'), popupMode=QToolButton.InstantPopup,
            statusTip=_('Display graph the named branch only'))
35
36
        self._branchMenu = QMenu(self._branchLabel)
        self._cbranchAction = self._branchMenu.addAction(
37
            _('Display closed branches'), self.refresh)
38
39
        self._cbranchAction.setCheckable(True)
        self._allparAction = self._branchMenu.addAction(
40
            _('Include all ancestors'), self._emitBranchChanged)
41
42
        self._allparAction.setCheckable(True)
        self._branchLabel.setMenu(self._branchMenu)
43

44
45
        self._branchCombo = QComboBox()
        self._branchCombo.currentIndexChanged.connect(self._emitBranchChanged)
46

47
48
        self.addWidget(self._branchLabel)
        self.addWidget(self._branchCombo)
49
50
51
52
53

    def _updatebranchfilter(self):
        """Update the list of branches"""
        curbranch = self.branch()

54
55
56
57
58
59
60
61
62
63
64
        def iterbranches(all=False):
            allbranches = self._repo.branchtags()
            if all:
                return sorted(allbranches.keys())

            openbrnodes = []
            for br in allbranches.iterkeys():
                openbrnodes.extend(self._repo.branchheads(br, closed=False))
            return sorted(br for br, n in allbranches.iteritems()
                          if n in openbrnodes)

65
66
67
68
69
        branches = list(iterbranches(all=self._cbranchAction.isChecked()))
        self._branchCombo.clear()
        self._branchCombo.addItems([''] + branches)
        self._branchLabel.setEnabled(len(branches) > 1)
        self._branchCombo.setEnabled(len(branches) > 1)
70
71
72
73
74
75

        self.setBranch(curbranch)

    @pyqtSlot(unicode)
    def setBranch(self, branch):
        """Change the current branch by name [unicode]"""
76
        self._branchCombo.setCurrentIndex(self._branchCombo.findText(branch))
77
78
79

    def branch(self):
        """Return the current branch name [unicode]"""
80
        return unicode(self._branchCombo.currentText())
81
82
83
84

    @pyqtSlot()
    def _emitBranchChanged(self):
        self.branchChanged.emit(self.branch(),
85
                                self._allparAction.isChecked())
86
87
88
89

    @pyqtSlot()
    def refresh(self):
        self._updatebranchfilter()