Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 2.1, 2.1.1, and 2.1.2

annotate: merge annotate.py into fileview.py, use manifest dialog

Changeset d7b8b9516a9b

Parent ade2ff756fd1

by Steve Borho

Changes to 6 files · Browse files at d7b8b9516a9b Showing diff from parent ade2ff756fd1 Diff from another changeset...

Change 1 of 1 Show Entire File tortoisehg/​hgqt/​annotate.py Stacked
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
@@ -1,444 +0,0 @@
-# annotate.py - File annotation widget -# -# Copyright 2010 Steve Borho <steve@borho.org> -# -# This software may be used and distributed according to the terms of the -# GNU General Public License version 2, incorporated herein by reference. - -import os - -from mercurial import ui, error, util - -from tortoisehg.hgqt import visdiff, qtlib, qscilib, thgrepo, lexers -from tortoisehg.util import paths, hglib, colormap, thread2 -from tortoisehg.hgqt.i18n import _ -from tortoisehg.hgqt.grep import SearchWidget - -from PyQt4.QtCore import * -from PyQt4.QtGui import * -from PyQt4.Qsci import QsciScintilla, QsciStyle - -# Technical Debt -# Pass search parameters to grep -# forward/backward history buttons -# menu options for viewing appropriate changesets - -class AnnotateView(qscilib.Scintilla): - revisionHint = pyqtSignal(QString) - - searchRequested = pyqtSignal(QString) - """Emitted (pattern) when user request to search content""" - - editSelected = pyqtSignal(unicode, object, int) - """Emitted (path, rev, line) when user requests to open editor""" - - grepRequested = pyqtSignal(QString, dict) - """Emitted (pattern, **opts) when user request to search changelog""" - - sourceChanged = pyqtSignal(unicode, object) - """Emitted (path, rev) when the content source changed""" - - def __init__(self, repo, parent=None, **opts): - super(AnnotateView, self).__init__(parent) - self.setReadOnly(True) - self.setMarginLineNumbers(1, True) - self.setMarginType(2, QsciScintilla.TextMarginRightJustified) - self.setMouseTracking(True) - self.setContextMenuPolicy(Qt.CustomContextMenu) - self.customContextMenuRequested.connect(self.menuRequest) - - self.repo = repo - self.repo.configChanged.connect(self.configChanged) - self.configChanged() - self._rev = None - self.annfile = None - self._annotation_enabled = bool(opts.get('annotationEnabled', False)) - - self._links = [] # by line - self._revmarkers = {} # by rev - self._lastrev = None - - self._thread = _AnnotateThread(self) - self._thread.finished.connect(self.fillModel) - - def configChanged(self): - self.setIndentationWidth(self.repo.tabwidth) - self.setTabWidth(self.repo.tabwidth) - - def keyPressEvent(self, event): - if event.key() == Qt.Key_Escape: - self._thread.abort() - return - return super(AnnotateView, self).keyPressEvent(event) - - def mouseMoveEvent(self, event): - self._emitRevisionHintAtLine(self.lineAt(event.pos())) - super(AnnotateView, self).mouseMoveEvent(event) - - def _emitRevisionHintAtLine(self, line): - if line < 0: - return - try: - fctx = self._links[line][0] - if fctx.rev() != self._lastrev: - s = hglib.get_revision_desc(fctx, - hglib.fromunicode(self.annfile)) - self.revisionHint.emit(s) - self._lastrev = fctx.rev() - except IndexError: - pass - - @pyqtSlot(QPoint) - def menuRequest(self, point): - menu = self.createStandardContextMenu() - line = self.lineAt(point) - point = self.mapToGlobal(point) - if line < 0 or not self.isAnnotationEnabled(): - return menu.exec_(point) - - fctx, line = self._links[line] - data = [hglib.tounicode(fctx.path()), fctx.rev(), line] - - if self.hasSelectedText(): - selection = self.selectedText() - def sreq(**opts): - return lambda: self.grepRequested.emit(selection, opts) - def sann(): - self.searchRequested.emit(selection) - menu.addSeparator() - for name, func in [(_('Search in original revision'), - sreq(rev=fctx.rev())), - (_('Search in working revision'), - sreq(rev='.')), - (_('Search in current annotation'), sann), - (_('Search in history'), sreq(all=True))]: - def add(name, func): - action = menu.addAction(name) - action.triggered.connect(func) - add(name, func) - - def annorig(): - self.setSource(*data) - def editorig(): - self.editSelected.emit(*data) - menu.addSeparator() - for name, func in [(_('Annotate originating revision'), annorig), - (_('View originating revision'), editorig)]: - def add(name, func): - action = menu.addAction(name) - action.triggered.connect(func) - add(name, func) - for pfctx in fctx.parents(): - pdata = [hglib.tounicode(pfctx.path()), pfctx.changectx().rev(), - line] - def annparent(data): - self.setSource(*data) - def editparent(data): - self.editSelected.emit(*data) - for name, func in [(_('Annotate parent revision %d') % pdata[1], - annparent), - (_('View parent revision %d') % pdata[1], - editparent)]: - def add(name, func): - action = menu.addAction(name) - action.data = pdata - action.run = lambda: func(action.data) - action.triggered.connect(action.run) - add(name, func) - menu.exec_(point) - - @property - def rev(self): - """Returns the current revision number""" - return self._rev - - @pyqtSlot(unicode, object, int) - def setSource(self, wfile, rev, line=None): - """Change the content to the specified file at rev [unicode] - - line is counted from 1. - """ - if self.annfile == wfile and self.rev == rev: - if line: - self.setCursorPosition(int(line) - 1, 0) - return - - try: - ctx = self.repo[rev] - fctx = ctx[hglib.fromunicode(wfile)] - except error.LookupError: - qtlib.ErrorMsgBox(_('Unable to annotate'), - _('%s is not found in revision %d') % (wfile, ctx.rev())) - return - - try: - if rev is None: - size = fctx.size() - else: - size = fctx._filelog.rawsize(fctx.filerev()) - except (EnvironmentError, error.LookupError), e: - self.setText(_('File or diffs not displayed: ') + \ - hglib.tounicode(str(e))) - self.error = p + hglib.tounicode(str(e)) - return - - if size > ctx._repo.maxdiff: - self.setText(_('File or diffs not displayed: ') + \ - _('File is larger than the specified max size.\n')) - else: - self._rev = ctx.rev() - self.clear() - self.annfile = wfile - if util.binary(fctx.data()): - self.setText(_('File is binary.\n')) - else: - self.setText(hglib.tounicode(fctx.data())) - if line: - self.setCursorPosition(int(line) - 1, 0) - self._updatelexer(fctx) - self._updatemarginwidth() - self.sourceChanged.emit(wfile, self._rev) - self._updateannotation() - - def _updateannotation(self): - if not self.isAnnotationEnabled() or not self.annfile: - return - ctx = self.repo[self._rev] - fctx = ctx[hglib.fromunicode(self.annfile)] - if util.binary(fctx.data()): - return - self._thread.abort() - self._thread.start(fctx) - - @pyqtSlot() - def fillModel(self): - self._thread.wait() - if self._thread.data is None: - return - - self._links = list(self._thread.data) - - self._updaterevmargin() - self._updatemarkers() - self._updatemarginwidth() - - def clear(self): - super(AnnotateView, self).clear() - self.clearMarginText() - self.markerDeleteAll() - self.annfile = None - - @pyqtSlot(bool) - def setAnnotationEnabled(self, enabled): - """Enable / disable annotation""" - enabled = bool(enabled) - if enabled == self.isAnnotationEnabled(): - return - self._annotation_enabled = enabled - self._updateannotation() - self._updatemarginwidth() - self.setMouseTracking(enabled) - if not self.isAnnotationEnabled(): - self.annfile = None - self.markerDeleteAll() - - def isAnnotationEnabled(self): - """True if annotation enabled and available""" - if self.rev is None: - return False # annotate working copy is not supported - return self._annotation_enabled - - def _updatelexer(self, fctx): - """Update the lexer according to the given file""" - lex = lexers.get_lexer(fctx.path(), hglib.tounicode(fctx.data()), self) - self.setLexer(lex) - if lex is None: - self.setFont(qtlib.getfont('fontlog').font()) - - def _updaterevmargin(self): - """Update the content of margin area showing revisions""" - s = self._margin_style - # Workaround to set style of the current sci widget. - # QsciStyle sends style data only to the first sci widget. - # See qscintilla2/Qt4/qscistyle.cpp - self.SendScintilla(QsciScintilla.SCI_STYLESETBACK, - s.style(), s.paper()) - self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, - s.style(), s.font().family().toAscii().data()) - self.SendScintilla(QsciScintilla.SCI_STYLESETSIZE, - s.style(), s.font().pointSize()) - for i, (fctx, _origline) in enumerate(self._links): - self.setMarginText(i, str(fctx.rev()), s) - - def _updatemarkers(self): - """Update markers which colorizes each line""" - self._redefinemarkers() - for i, (fctx, _origline) in enumerate(self._links): - m = self._revmarkers.get(fctx.rev()) - if m is not None: - self.markerAdd(i, m) - - def _redefinemarkers(self): - """Redefine line markers according to the current revs""" - curdate = self.repo[self._rev].date()[0] - - # make sure to colorize at least 1 year - mindate = curdate - 365 * 24 * 60 * 60 - - self._revmarkers.clear() - filectxs = iter(fctx for fctx, _origline in self._links) - palette = colormap.makeannotatepalette(filectxs, curdate, - maxcolors=32, maxhues=8, - maxsaturations=16, - mindate=mindate) - for i, (color, fctxs) in enumerate(palette.iteritems()): - self.markerDefine(QsciScintilla.Background, i) - self.setMarkerBackgroundColor(QColor(color), i) - for fctx in fctxs: - self._revmarkers[fctx.rev()] = i - - @util.propertycache - def _margin_style(self): - """Style for margin area""" - s = QsciStyle() - s.setPaper(QApplication.palette().color(QPalette.Window)) - s.setFont(self.font()) - return s - - @pyqtSlot() - def _updatemarginwidth(self): - self.setMarginsFont(self.font()) - def lentext(s): - return 'M' * (len(str(s)) + 2) # 2 for margin - self.setMarginWidth(1, lentext(self.lines())) - if self.isAnnotationEnabled() and self._links: - maxrev = max(fctx.rev() for fctx, _origline in self._links) - self.setMarginWidth(2, lentext(maxrev)) - else: - self.setMarginWidth(2, 0) - -class _AnnotateThread(QThread): - 'Background thread for annotating a file at a revision' - def __init__(self, parent=None): - super(_AnnotateThread, self).__init__(parent) - self._threadid = None - - @pyqtSlot(object) - def start(self, fctx): - self._fctx = fctx - super(_AnnotateThread, self).start() - self.data = None - - @pyqtSlot() - def abort(self): - if self._threadid is None: - return - try: - thread2._async_raise(self._threadid, KeyboardInterrupt) - self.wait() - except ValueError: - pass - - def run(self): - assert self.currentThread() != qApp.thread() - self._threadid = self.currentThreadId() - try: - data = [] - for (fctx, line), _text in self._fctx.annotate(True, True): - data.append((fctx, line)) - self.data = data - except KeyboardInterrupt: - pass - finally: - self._threadid = None - del self._fctx - -class AnnotateDialog(QMainWindow): - def __init__(self, *pats, **opts): - super(AnnotateDialog,self).__init__(opts.get('parent'), Qt.Window) - - root = opts.get('root') or paths.find_root() - repo = thgrepo.repository(ui.ui(), path=root) - # TODO: handle repo not found - - av = AnnotateView(repo, self, annotationEnabled=True) - self.setCentralWidget(av) - self.av = av - - status = QStatusBar() - self.setStatusBar(status) - av.revisionHint.connect(status.showMessage) - av.editSelected.connect(self.editSelected) - av.grepRequested.connect(self._openSearchWidget) - - self._searchbar = qscilib.SearchToolBar() - self.addToolBar(self._searchbar) - self._searchbar.setPattern(hglib.tounicode(opts.get('pattern', ''))) - self._searchbar.searchRequested.connect(self.av.find) - self._searchbar.conditionChanged.connect(self.av.highlightText) - av.searchRequested.connect(self._searchbar.search) - QShortcut(QKeySequence.Find, self, - lambda: self._searchbar.setFocus(Qt.OtherFocusReason)) - - self.av.sourceChanged.connect( - lambda *args: self.setWindowTitle(_('Annotate %s@%d') % args)) - - self.searchwidget = opts.get('searchwidget') - - self.opts = opts - line = opts.get('line') - if line and isinstance(line, str): - line = int(line) - - self.repo = repo - - self.restoreSettings() - - # run heavy operation after the dialog visible - path = hglib.tounicode(pats[0]) - rev = opts.get('rev') or '.' - QTimer.singleShot(0, lambda: av.setSource(path, rev, line)) - - def closeEvent(self, event): - self.storeSettings() - super(AnnotateDialog, self).closeEvent(event) - - def editSelected(self, wfile, rev, line): - pattern = hglib.fromunicode(self._searchbar._le.text()) or None - wfile = hglib.fromunicode(wfile) - repo = self.repo - try: - ctx = repo[rev] - fctx = ctx[wfile] - except Exception, e: - self.statusBar().showMessage(hglib.tounicode(str(e))) - - base, _ = visdiff.snapshot(repo, [wfile], repo[rev]) - files = [os.path.join(base, wfile)] - qtlib.editfiles(repo, files, line, pattern, self) - - @pyqtSlot(unicode, dict) - def _openSearchWidget(self, pattern, opts): - opts = dict((str(k), str(v)) for k, v in opts.iteritems()) - if self.searchwidget is None: - self.searchwidget = SearchWidget([pattern], repo=self.repo, - **opts) - self.searchwidget.show() - else: - self.searchwidget.setSearch(pattern, **opts) - self.searchwidget.show() - self.searchwidget.raise_() - - def storeSettings(self): - s = QSettings() - s.setValue('annotate/geom', self.saveGeometry()) - self.av.saveSettings(s, 'annotate/av') - - def restoreSettings(self): - s = QSettings() - self.restoreGeometry(s.value('annotate/geom').toByteArray()) - self.av.loadSettings(s, 'annotate/av') - -def run(ui, *pats, **opts): - pats = hglib.canonpaths(pats) - return AnnotateDialog(*pats, **opts)
 
1
2
 
3
4
5
6
7
 
8
9
10
11
12
13
14
15
16
17
18
 
 
19
20
21
22
23
 
 
 
24
25
 
26
27
28
 
36
37
38
39
 
40
41
42
 
94
95
96
97
 
98
99
100
 
547
548
549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
 
 
 
 
3
4
 
 
 
 
 
 
 
 
 
 
5
6
7
8
9
10
 
11
12
13
14
 
15
16
17
18
 
26
27
28
 
29
30
31
32
 
84
85
86
 
87
88
89
90
 
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
@@ -1,28 +1,18 @@
-# Copyright (c) 2009-2010 LOGILAB S.A. (Paris, FRANCE). -# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# fileview.py - File diff, content, and annotation display widget  # -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Copyright 2010 Steve Borho <steve@borho.org>  # -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, write to the Free Software Foundation, Inc., -# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -""" -Qt4 high level widgets for hg repo changelogs and filelogs -""" +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2, incorporated herein by reference.    import os  import difflib   -from tortoisehg.util import hglib, patchctx +from mercurial import error, util + +from tortoisehg.util import hglib, patchctx, colormap, thread2  from tortoisehg.hgqt.i18n import _ -from tortoisehg.hgqt import annotate, qscilib, qtlib, blockmatcher, lexers +from tortoisehg.hgqt import qscilib, qtlib, blockmatcher, lexers  from tortoisehg.hgqt import visdiff, filedata    from PyQt4.QtCore import * @@ -36,7 +26,7 @@
 AnnMode = 3    class HgFileView(QFrame): - """file diff and content viewer""" + "file diff, content, and annotation viewer"     linkActivated = pyqtSignal(QString)   fileDisplayed = pyqtSignal(QString, QString) @@ -94,7 +84,7 @@
  l.addLayout(hbox)     self.blk = blockmatcher.BlockList(self) - self.sci = annotate.AnnotateView(repo, self) + self.sci = AnnotateView(repo, self)   hbox.addWidget(self.blk)   hbox.addWidget(self.sci, 1)   @@ -547,3 +537,336 @@
    def nDiffs(self):   return len(self._diffs) + + +class AnnotateView(qscilib.Scintilla): + 'QScintilla widget capable of displaying annotations' + + revisionHint = pyqtSignal(QString) + + searchRequested = pyqtSignal(QString) + """Emitted (pattern) when user request to search content""" + + editSelected = pyqtSignal(unicode, object, int) + """Emitted (path, rev, line) when user requests to open editor""" + + grepRequested = pyqtSignal(QString, dict) + """Emitted (pattern, **opts) when user request to search changelog""" + + sourceChanged = pyqtSignal(unicode, object) + """Emitted (path, rev) when the content source changed""" + + def __init__(self, repo, parent=None, **opts): + super(AnnotateView, self).__init__(parent) + self.setReadOnly(True) + self.setMarginLineNumbers(1, True) + self.setMarginType(2, qsci.TextMarginRightJustified) + self.setMouseTracking(True) + self.setContextMenuPolicy(Qt.CustomContextMenu) + self.customContextMenuRequested.connect(self.menuRequest) + + self.repo = repo + self.repo.configChanged.connect(self.configChanged) + self.configChanged() + self._rev = None + self.annfile = None + self._annotation_enabled = bool(opts.get('annotationEnabled', False)) + + self._links = [] # by line + self._revmarkers = {} # by rev + self._lastrev = None + + self._thread = AnnotateThread(self) + self._thread.finished.connect(self.fillModel) + + def configChanged(self): + self.setIndentationWidth(self.repo.tabwidth) + self.setTabWidth(self.repo.tabwidth) + + def keyPressEvent(self, event): + if event.key() == Qt.Key_Escape: + self._thread.abort() + return + return super(AnnotateView, self).keyPressEvent(event) + + def mouseMoveEvent(self, event): + self._emitRevisionHintAtLine(self.lineAt(event.pos())) + super(AnnotateView, self).mouseMoveEvent(event) + + def _emitRevisionHintAtLine(self, line): + if line < 0: + return + try: + fctx = self._links[line][0] + if fctx.rev() != self._lastrev: + s = hglib.get_revision_desc(fctx, + hglib.fromunicode(self.annfile)) + self.revisionHint.emit(s) + self._lastrev = fctx.rev() + except IndexError: + pass + + @pyqtSlot(QPoint) + def menuRequest(self, point): + menu = self.createStandardContextMenu() + line = self.lineAt(point) + point = self.mapToGlobal(point) + if line < 0 or not self.isAnnotationEnabled(): + return menu.exec_(point) + + fctx, line = self._links[line] + data = [hglib.tounicode(fctx.path()), fctx.rev(), line] + + if self.hasSelectedText(): + selection = self.selectedText() + def sreq(**opts): + return lambda: self.grepRequested.emit(selection, opts) + def sann(): + self.searchRequested.emit(selection) + menu.addSeparator() + for name, func in [(_('Search in original revision'), + sreq(rev=fctx.rev())), + (_('Search in working revision'), + sreq(rev='.')), + (_('Search in current annotation'), sann), + (_('Search in history'), sreq(all=True))]: + def add(name, func): + action = menu.addAction(name) + action.triggered.connect(func) + add(name, func) + + def annorig(): + self.setSource(*data) + def editorig(): + self.editSelected.emit(*data) + menu.addSeparator() + for name, func in [(_('Annotate originating revision'), annorig), + (_('View originating revision'), editorig)]: + def add(name, func): + action = menu.addAction(name) + action.triggered.connect(func) + add(name, func) + for pfctx in fctx.parents(): + pdata = [hglib.tounicode(pfctx.path()), pfctx.changectx().rev(), + line] + def annparent(data): + self.setSource(*data) + def editparent(data): + self.editSelected.emit(*data) + for name, func in [(_('Annotate parent revision %d') % pdata[1], + annparent), + (_('View parent revision %d') % pdata[1], + editparent)]: + def add(name, func): + action = menu.addAction(name) + action.data = pdata + action.run = lambda: func(action.data) + action.triggered.connect(action.run) + add(name, func) + menu.exec_(point) + + @property + def rev(self): + """Returns the current revision number""" + return self._rev + + @pyqtSlot(unicode, object, int) + def setSource(self, wfile, rev, line=None): + """Change the content to the specified file at rev [unicode] + + line is counted from 1. + """ + if self.annfile == wfile and self.rev == rev: + if line: + self.setCursorPosition(int(line) - 1, 0) + return + + try: + ctx = self.repo[rev] + fctx = ctx[hglib.fromunicode(wfile)] + except error.LookupError: + qtlib.ErrorMsgBox(_('Unable to annotate'), + _('%s is not found in revision %d') % (wfile, ctx.rev())) + return + + try: + if rev is None: + size = fctx.size() + else: + size = fctx._filelog.rawsize(fctx.filerev()) + except (EnvironmentError, error.LookupError), e: + self.setText(_('File or diffs not displayed: ') + \ + hglib.tounicode(str(e))) + self.error = p + hglib.tounicode(str(e)) + return + + if size > ctx._repo.maxdiff: + self.setText(_('File or diffs not displayed: ') + \ + _('File is larger than the specified max size.\n')) + else: + self._rev = ctx.rev() + self.clear() + self.annfile = wfile + if util.binary(fctx.data()): + self.setText(_('File is binary.\n')) + else: + self.setText(hglib.tounicode(fctx.data())) + if line: + self.setCursorPosition(int(line) - 1, 0) + self._updatelexer(fctx) + self._updatemarginwidth() + self.sourceChanged.emit(wfile, self._rev) + self._updateannotation() + + def _updateannotation(self): + if not self.isAnnotationEnabled() or not self.annfile: + return + ctx = self.repo[self._rev] + fctx = ctx[hglib.fromunicode(self.annfile)] + if util.binary(fctx.data()): + return + self._thread.abort() + self._thread.start(fctx) + + @pyqtSlot() + def fillModel(self): + self._thread.wait() + if self._thread.data is None: + return + + self._links = list(self._thread.data) + + self._updaterevmargin() + self._updatemarkers() + self._updatemarginwidth() + + def clear(self): + super(AnnotateView, self).clear() + self.clearMarginText() + self.markerDeleteAll() + self.annfile = None + + @pyqtSlot(bool) + def setAnnotationEnabled(self, enabled): + """Enable / disable annotation""" + enabled = bool(enabled) + if enabled == self.isAnnotationEnabled(): + return + self._annotation_enabled = enabled + self._updateannotation() + self._updatemarginwidth() + self.setMouseTracking(enabled) + if not self.isAnnotationEnabled(): + self.annfile = None + self.markerDeleteAll() + + def isAnnotationEnabled(self): + """True if annotation enabled and available""" + if self.rev is None: + return False # annotate working copy is not supported + return self._annotation_enabled + + def _updatelexer(self, fctx): + """Update the lexer according to the given file""" + lex = lexers.get_lexer(fctx.path(), hglib.tounicode(fctx.data()), self) + self.setLexer(lex) + if lex is None: + self.setFont(qtlib.getfont('fontlog').font()) + + def _updaterevmargin(self): + """Update the content of margin area showing revisions""" + s = self._margin_style + # Workaround to set style of the current sci widget. + # QsciStyle sends style data only to the first sci widget. + # See qscintilla2/Qt4/qscistyle.cpp + self.SendScintilla(qsci.SCI_STYLESETBACK, + s.style(), s.paper()) + self.SendScintilla(qsci.SCI_STYLESETFONT, + s.style(), s.font().family().toAscii().data()) + self.SendScintilla(qsci.SCI_STYLESETSIZE, + s.style(), s.font().pointSize()) + for i, (fctx, _origline) in enumerate(self._links): + self.setMarginText(i, str(fctx.rev()), s) + + def _updatemarkers(self): + """Update markers which colorizes each line""" + self._redefinemarkers() + for i, (fctx, _origline) in enumerate(self._links): + m = self._revmarkers.get(fctx.rev()) + if m is not None: + self.markerAdd(i, m) + + def _redefinemarkers(self): + """Redefine line markers according to the current revs""" + curdate = self.repo[self._rev].date()[0] + + # make sure to colorize at least 1 year + mindate = curdate - 365 * 24 * 60 * 60 + + self._revmarkers.clear() + filectxs = iter(fctx for fctx, _origline in self._links) + palette = colormap.makeannotatepalette(filectxs, curdate, + maxcolors=32, maxhues=8, + maxsaturations=16, + mindate=mindate) + for i, (color, fctxs) in enumerate(palette.iteritems()): + self.markerDefine(qsci.Background, i) + self.setMarkerBackgroundColor(QColor(color), i) + for fctx in fctxs: + self._revmarkers[fctx.rev()] = i + + @util.propertycache + def _margin_style(self): + """Style for margin area""" + s = Qsci.QsciStyle() + s.setPaper(QApplication.palette().color(QPalette.Window)) + s.setFont(self.font()) + return s + + @pyqtSlot() + def _updatemarginwidth(self): + self.setMarginsFont(self.font()) + def lentext(s): + return 'M' * (len(str(s)) + 2) # 2 for margin + self.setMarginWidth(1, lentext(self.lines())) + if self.isAnnotationEnabled() and self._links: + maxrev = max(fctx.rev() for fctx, _origline in self._links) + self.setMarginWidth(2, lentext(maxrev)) + else: + self.setMarginWidth(2, 0) + +class AnnotateThread(QThread): + 'Background thread for annotating a file at a revision' + def __init__(self, parent=None): + super(AnnotateThread, self).__init__(parent) + self._threadid = None + + @pyqtSlot(object) + def start(self, fctx): + self._fctx = fctx + super(AnnotateThread, self).start() + self.data = None + + @pyqtSlot() + def abort(self): + if self._threadid is None: + return + try: + thread2._async_raise(self._threadid, KeyboardInterrupt) + self.wait() + except ValueError: + pass + + def run(self): + assert self.currentThread() != qApp.thread() + self._threadid = self.currentThreadId() + try: + data = [] + for (fctx, line), _text in self._fctx.annotate(True, True): + data.append((fctx, line)) + self.data = data + except KeyboardInterrupt: + pass + finally: + self._threadid = None + del self._fctx
 
625
626
627
628
 
 
629
630
631
 
639
640
641
 
 
 
 
 
 
642
643
644
645
646
647
648
649
650
 
 
 
 
 
651
652
653
 
625
626
627
 
628
629
630
631
632
 
640
641
642
643
644
645
646
647
648
649
650
651
 
 
 
 
 
 
652
653
654
655
656
657
658
659
@@ -625,7 +625,8 @@
  self.selectedRows = saved     def onAnnotateFile(self): - from tortoisehg.hgqt import annotate + from tortoisehg.hgqt.manifestdialog import run + from tortoisehg.hgqt.run import qtrun   repo, ui, pattern = self.repo, self.repo.ui, self.pattern   seen = set()   for rev, path, line in self.selectedRows: @@ -639,15 +640,20 @@
  root = paths.find_root(abs)   if root and abs.startswith(root):   path = abs[len(root)+1:] + if rev is None: + rev = repo['.'].rev() + srepo = thgrepo.repository(None, root) + opts = {'repo': srepo, 'canonpath' : path, 'rev' : rev, + 'line': line, 'pattern': pattern} + qtrun(run, ui, **opts)   else:   continue   else: - root = repo.root - dlg = annotate.AnnotateDialog(path, rev=rev, line=line, - pattern=pattern, parent=self, - searchwidget=self.parent(), - root=root) - dlg.show() + if rev is None: + rev = repo['.'].rev() + opts = {'repo': repo, 'canonpath' : path, 'rev' : rev, + 'line': line, 'pattern': pattern} + qtrun(run, ui, **opts)     def onViewChangeset(self):   for rev, path, line in self.selectedRows:
 
404
405
406
407
 
 
 
 
 
 
408
409
 
 
 
 
410
411
412
 
404
405
406
 
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
@@ -404,9 +404,18 @@
  # set initial state after dialog visible   def init():   try: - path = hglib.canonpaths(pats)[0] + if pats: + path = hglib.canonpaths(pats)[0] + elif 'canonpath' in opts: + path = opts['canonpath'] + else: + return   line = opts.get('line') and int(opts['line']) or None   dlg.setSource(path, opts.get('rev'), line) + if opts.get('pattern'): + dlg.setSearchPattern(opts['pattern']) + if dlg._manifest_widget._fileview.actionAnnMode.isEnabled(): + dlg._manifest_widget._fileview.actionAnnMode.trigger()   except IndexError:   pass   dlg.setSearchPattern(hglib.tounicode(opts.get('pattern')) or '')
 
703
704
705
706
 
707
708
709
 
703
704
705
 
706
707
708
709
@@ -703,7 +703,7 @@
   def annotate(ui, *pats, **opts):   """annotate dialog""" - from tortoisehg.hgqt.annotate import run + from tortoisehg.hgqt.manifestdialog import run   if len(pats) != 1:   ui.warn(_('annotate requires a single filename\n'))   return
 
248
249
250
251
 
252
253
254
 
 
255
256
257
 
248
249
250
 
251
252
 
 
253
254
255
256
257
@@ -248,10 +248,10 @@
  return False    def annotate(parent, ui, repo, files): - from tortoisehg.hgqt.annotate import run + from tortoisehg.hgqt.manifestdialog import run   from tortoisehg.hgqt.run import qtrun - opts = {'root': repo.root} - qtrun(run, repo.ui, *files, **opts) + opts = {'repo': repo, 'canonpath' : files[0], 'rev' : repo['.'].rev()} + qtrun(run, repo.ui, **opts)   return False    def forget(parent, ui, repo, files):