Kiln » TortoiseHg » TortoiseHg
Clone URL:  
messageentry.py
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
# messageentry.py - TortoiseHg's commit message editng widget # # Copyright 2011 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 PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.Qsci import QsciScintilla, QsciLexerMakefile from tortoisehg.hgqt.i18n import _ from tortoisehg.hgqt import qtlib, qscilib class MessageEntry(qscilib.Scintilla): def __init__(self, parent, getCheckedFunc=None): super(MessageEntry, self).__init__(parent) self.setEdgeColor(QColor('LightSalmon')) self.setEdgeMode(QsciScintilla.EdgeLine) self.setReadOnly(False) self.setMarginWidth(1, 0) self.setFont(qtlib.getfont('fontcomment').font()) self.setCaretWidth(10) self.setCaretLineBackgroundColor(QColor("#e6fff0")) self.setCaretLineVisible(True) self.setAutoIndent(True) self.setAutoCompletionThreshold(2) self.setAutoCompletionSource(QsciScintilla.AcsAPIs) self.setAutoCompletionFillupsEnabled(True) self.setLexer(QsciLexerMakefile(self)) self.lexer().setFont(qtlib.getfont('fontcomment').font()) self.lexer().setColor(QColor(Qt.red), QsciLexerMakefile.Error) self.setMatchedBraceBackgroundColor(Qt.yellow) self.setIndentationsUseTabs(False) self.setBraceMatching(QsciScintilla.SloppyBraceMatch) #self.setIndentationGuidesBackgroundColor(QColor("#e6e6de")) #self.setFolding(QsciScintilla.BoxedFoldStyle) # http://www.riverbankcomputing.com/pipermail/qscintilla/2009-February/000461.html self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) # default message entry widgets to word wrap, user may override self.setWrapMode(QsciScintilla.WrapWord) self.getChecked = getCheckedFunc self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.menuRequested) def menuRequested(self, point): line = self.lineAt(point) point = self.viewport().mapToGlobal(point) def apply(): line = 0 while True: line = self.reflowBlock(line) if line is None: break; def paste(): files = self.getChecked() self.insert(', '.join(files)) def settings(): from tortoisehg.hgqt.settings import SettingsDialog dlg = SettingsDialog(True, focus='tortoisehg.summarylen') dlg.exec_() menu = self.createStandardContextMenu() menu.addSeparator() if self.getChecked: action = menu.addAction(_('Paste &Filenames')) action.triggered.connect(paste) for name, func in [(_('App&ly Format'), apply), (_('C&onfigure Format'), settings)]: def add(name, func): action = menu.addAction(name) action.triggered.connect(func) add(name, func) return menu.exec_(point) def refresh(self, repo): self.setEdgeColumn(repo.summarylen) self.setIndentationWidth(repo.tabwidth) self.setTabWidth(repo.tabwidth) self.summarylen = repo.summarylen def reflowBlock(self, line): lines = self.text().split('\n', QString.KeepEmptyParts) if line >= len(lines): return None if not len(lines[line]) > 1: return line+1 # find boundaries (empty lines or bounds) b = line while b and len(lines[b-1]) > 1: b = b - 1 e = line while e+1 < len(lines) and len(lines[e+1]) > 1: e = e + 1 group = QStringList([lines[l].simplified() for l in xrange(b, e+1)]) sentence = group.join(' ') parts = sentence.split(' ', QString.SkipEmptyParts) outlines = QStringList() line = QStringList() partslen = 0 for part in parts: if partslen + len(line) + len(part) + 1 > self.summarylen: if line: outlines.append(line.join(' ')) line, partslen = QStringList(), 0 line.append(part) partslen += len(part) if line: outlines.append(line.join(' ')) self.beginUndoAction() self.setSelection(b, 0, e+1, 0) self.removeSelectedText() self.insertAt(outlines.join('\n')+'\n', b, 0) self.endUndoAction() self.setCursorPosition(b, 0) return b + len(outlines) + 1 def moveCursorToEnd(self): lines = self.lines() if lines: lines -= 1 pos = self.lineLength(lines) self.setCursorPosition(lines, pos) self.ensureLineVisible(lines) self.horizontalScrollBar().setSliderPosition(0) def keyPressEvent(self, event): if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_E: line, col = self.getCursorPosition() self.reflowBlock(line) elif event.key() == Qt.Key_Backtab: event.accept() newev = QKeyEvent(event.type(), Qt.Key_Tab, Qt.ShiftModifier) super(MessageEntry, self).keyPressEvent(newev) else: super(MessageEntry, self).keyPressEvent(event)