/tortoisehg/hgqt/revpanel.py

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