PageRenderTime 34ms CodeModel.GetById 13ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tortoisehg/hgqt/revpanel.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 168 lines | 147 code | 11 blank | 10 comment | 18 complexity | c4788a57c6418192c1c19d1d2ba583ba MD5 | raw file
  1# -*- coding: iso-8859-1 -*-
  2#!/usr/bin/env python
  3# repowidget.py - TortoiseHg repository widget
  4#
  5# Copyright (C) 2007-2010 Logilab. All rights reserved.
  6# Copyright (C) 2010 Adrian Buehlmann <adrian@cadifra.com>
  7#
  8# This software may be used and distributed according to the terms
  9# of the GNU General Public License, incorporated herein by reference.
 10
 11
 12from mercurial import error
 13
 14from tortoisehg.util import hglib
 15from tortoisehg.hgqt.i18n import _
 16from tortoisehg.hgqt import csinfo, qtlib
 17
 18from PyQt4.QtCore import *
 19
 20def label_func(widget, item):
 21    if item == 'cset':
 22        return _('Changeset:')
 23    elif item == 'parents':
 24        return _('Parent:')
 25    elif item == 'children':
 26        return _('Child:')
 27    elif item == 'patch':
 28        return _('Patch:')
 29    raise csinfo.UnknownItem(item)
 30
 31def revid_markup(revid, **kargs):
 32    opts = dict(family='monospace', size='9pt')
 33    opts.update(kargs)
 34    return qtlib.markup(revid, **opts)
 35
 36def data_func(widget, item, ctx):
 37    def summary_line(desc):
 38        desc = desc.replace('\0', '').split('\n')[0]
 39        return hglib.tounicode(desc)[:80]
 40    def revline_data(ctx, hl=False, branch=None):
 41        if isinstance(ctx, basestring):
 42            return ctx
 43        desc = ctx.description()
 44        return (str(ctx.rev()), str(ctx), summary_line(desc), hl, branch)
 45    if item == 'cset':
 46        return revline_data(ctx)
 47    elif item == 'branch':
 48        value = hglib.tounicode(ctx.branch())
 49        return value != 'default' and value or None
 50    elif item == 'parents':
 51        # TODO: need to put 'diff to other' checkbox
 52        #pindex = self.diff_other_parent() and 1 or 0
 53        pindex = 0 # always show diff with first parent
 54        pctxs = ctx.parents()
 55        parents = []
 56        for pctx in pctxs:
 57            highlight = len(pctxs) == 2 and pctx == pctxs[pindex]
 58            branch = None
 59            if hasattr(pctx, 'branch') and pctx.branch() != ctx.branch():
 60                branch = pctx.branch()
 61            parents.append(revline_data(pctx, highlight, branch))
 62        return parents
 63    elif item == 'children':
 64        children = []
 65        for cctx in ctx.children():
 66            branch = None
 67            if hasattr(cctx, 'branch') and cctx.branch() != ctx.branch():
 68                branch = cctx.branch()
 69            children.append(revline_data(cctx, branch=branch))
 70        return children
 71    elif item in ('transplant', 'p4', 'svn'):
 72        ts = widget.get_data(item, usepreset=True)
 73        if not ts:
 74            return None
 75        try:
 76            tctx = ctx._repo[ts]
 77            return revline_data(tctx)
 78        except (error.LookupError, error.RepoLookupError, error.RepoError):
 79            return ts
 80    elif item == 'patch':
 81        if hasattr(ctx, '_patchname'):
 82            desc = ctx.description()
 83            return (ctx._patchname, str(ctx), summary_line(desc))
 84        return None
 85    elif item == 'ishead':
 86        if ctx.rev() is None:
 87            ctx = ctx.p1()
 88        childbranches = [cctx.branch() for cctx in ctx.children()]
 89        return ctx.branch() not in childbranches
 90    raise csinfo.UnknownItem(item)
 91
 92def markup_func(widget, item, value):
 93    def link_markup(revnum, revid, enable=True):
 94        mrevid = revid_markup('%s (%s)' % (revnum, revid))
 95        if not enable:
 96            return mrevid
 97        link = 'cset:%s' % revid
 98        return '<a href="%s">%s</a>' % (link, mrevid)
 99    def revline_markup(revnum, revid, summary, highlight=None,
100                       branch=None, link=True):
101        def branch_markup(branch):
102            opts = dict(fg='black', bg='#aaffaa')
103            return qtlib.markup(' %s ' % branch, **opts)
104        summary = qtlib.markup(summary)
105        if branch:
106            branch = branch_markup(branch)
107        if revid:
108            rev = link_markup(revnum, revid, link)
109            if branch:
110                return '%s %s %s' % (rev, branch, summary)
111            return '%s %s' % (rev, summary)
112        else:
113            revnum = qtlib.markup(revnum)
114            if branch:
115                return '%s - %s %s' % (revnum, branch, summary)
116            return '%s - %s' % (revnum, summary)
117    if item in ('cset', 'transplant', 'patch', 'p4', 'svn'):
118        link = item != 'cset'
119        if isinstance(value, basestring):
120            return revid_markup(value)
121        return revline_markup(link=link, *value)
122    elif item in ('parents', 'children'):
123        csets = []
124        for cset in value:
125            if isinstance(cset, basestring):
126                csets.append(revid_markup(cset))
127            else:
128                csets.append(revline_markup(*cset))
129        return csets
130    raise csinfo.UnknownItem(item)
131
132def RevPanelWidget(repo):
133    '''creates a rev panel widget and returns it'''
134    custom = csinfo.custom(data=data_func, label=label_func,
135                           markup=markup_func)
136    style = csinfo.panelstyle(contents=('cset', 'branch', 'user',
137                   'dateage', 'parents', 'children', 'tags', 'transplant',
138                   'p4', 'svn'), selectable=True, expandable=True)
139    return csinfo.create(repo, style=style, custom=custom)
140
141
142def nomarkup(widget, item, value):
143    def revline_markup(revnum, revid, summary, highlight=None, branch=None):
144        summary = qtlib.markup(summary)
145        if revid:
146            rev = revid_markup('%s (%s)' % (revnum, revid))
147            return '%s %s' % (rev, summary)
148        else:
149            revnum = qtlib.markup(revnum)
150            return '%s - %s' % (revnum, summary)
151    csets = []
152    if item == 'ishead':
153        if value is False:
154            text = _('Not a head revision!')
155            return qtlib.markup(text, fg='red', weight='bold')
156        raise csinfo.UnknownItem(item)
157    for cset in value:
158        if isinstance(cset, basestring):
159            csets.append(revid_markup(cset))
160        else:
161            csets.append(revline_markup(*cset))
162    return csets
163
164def ParentWidget(repo):
165    'creates a parent rev widget and returns it'
166    custom = csinfo.custom(data=data_func, label=label_func, markup=nomarkup)
167    style = csinfo.panelstyle(contents=('parents','ishead'))
168    return csinfo.create(repo, style=style, custom=custom)