Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.9, 1.9.1, and 1.9.2

settings, qtlib: move generic editor functionality to qtlib

If you don't like the properties lexer, make it an argument

Changeset 6afe5bf217f4

Parent 581dccdce09f

by Steve Borho

Changes to 2 files · Browse files at 6afe5bf217f4 Showing diff from parent 581dccdce09f Diff from another changeset...

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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 # qtlib.py - Qt utility code  #  # 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 or any later version.    import atexit  import shutil  import tempfile    from PyQt4 import QtCore, QtGui +from PyQt4 import Qsci  from mercurial import extensions    from tortoisehg.util import hglib  from tortoisehg.hgqt.i18n import _  from tortoisehg.hgqt import icon as geticon  from hgext.color import _styles   +qsci = Qsci.QsciScintilla +  tmproot = None  def gettempdir():   global tmproot   def cleanup():   try: shutil.rmtree(tmproot)   except: pass   if not tmproot:   tmproot = tempfile.mkdtemp(prefix='thg.')   atexit.register(cleanup)   return tmproot    # _styles maps from ui labels to effects  # _effects maps an effect to font style properties. We define a limited  # set of _effects, since we convert color effect names to font style  # effect programatically.    _effects = {   'bold': 'font-weight: bold',   'italic': 'font-style: italic',   'underline': 'text-decoration: underline',  }    thgstylesheet = '* { white-space: pre; font-family: monospace; font-size: 9pt; }'    def configstyles(ui):   # extensions may provide more labels and default effects   for name, ext in extensions.extensions():   _styles.update(getattr(ext, 'colortable', {}))     # tortoisehg defines a few labels and default effects   _styles.update({'ui.error':'red bold', 'control':'black bold'})     # allow the user to override   for status, cfgeffects in ui.configitems('color'):   if '.' not in status:   continue   cfgeffects = ui.configlist('color', status)   _styles[status] = ' '.join(cfgeffects)     for status, cfgeffects in ui.configitems('thg-color'):   if '.' not in status:   continue   cfgeffects = ui.configlist('thg-color', status)   _styles[status] = ' '.join(cfgeffects)    # See http://doc.trolltech.com/4.2/richtext-html-subset.html  # and http://www.w3.org/TR/SVG/types.html#ColorKeywords    def geteffect(labels):   'map labels like "log.date" to Qt font styles'   effects = []   # Multiple labels may be requested   for l in labels.split():   if not l:   continue   # Each label may request multiple effects   es = _styles.get(l, '')   for e in es.split():   if e in _effects:   effects.append(_effects[e])   elif e in QtGui.QColor.colorNames():   # Accept any valid QColor   effects.append('color: ' + e)   elif e.endswith('_background'):   e = e[:-11]   if e in QtGui.QColor.colorNames():   effects.append('bgcolor: ' + e)   return ';'.join(effects)    NAME_MAP = {   'fg': 'color', 'bg': 'background-color', 'family': 'font-family',   'size': 'font-size', 'weight': 'font-weight', 'space': 'white-space',   'style': 'font-style', 'decoration': 'text-decoration',  }    def markup(msg, **styles):   style = {'white-space': 'pre'}   for name, value in styles.items():   if not value:   continue   if NAME_MAP.has_key(name):   name = NAME_MAP[name]   style[name] = value   style = ';'.join(['%s: %s' % t for t in style.items()])   msg = hglib.tounicode(msg)   msg = QtCore.Qt.escape(msg)   msg = msg.replace('\n', '<br />')   return '<span style="%s">%s</span>' % (style, msg)    def CommonMsgBox(icon, title, main, text='', buttons=QtGui.QMessageBox.Close,   parent=None):   msg = QtGui.QMessageBox(parent)   msg.setIcon(icon)   msg.setWindowTitle(title)   msg.setStandardButtons(buttons)   msg.setText('<b>%s</b>' % main)   info = ''   for line in text.split('\n'):   info += '<nobr>%s</nobr><br />' % line   msg.setInformativeText(info)   return msg.exec_()    def InfoMsgBox(*args, **kargs):   return CommonMsgBox(QtGui.QMessageBox.Information, *args, **kargs)    def WarningMsgBox(*args, **kargs):   return CommonMsgBox(QtGui.QMessageBox.Warning, *args, **kargs)    def ErrorMsgBox(*args, **kargs):   return CommonMsgBox(QtGui.QMessageBox.Critical, *args, **kargs)    class CustomPrompt(QtGui.QMessageBox):   def __init__(self, title, message, parent, choices, default=None,   esc=None, files=None):   QtGui.QMessageBox.__init__(self, parent)     self.setWindowTitle(hglib.toutf(title))   self.setText(hglib.toutf(message))   if files:   msg = ''   for i, file in enumerate(files):   msg += ' %s\n' % file   if i == 9:   msg += ' ...\n'   break   self.setDetailedText(hglib.toutf(msg))   self.hotkeys = {}   for i, s in enumerate(choices):   btn = self.addButton(s, QtGui.QMessageBox.AcceptRole)   try:   char = s[s.index('&')+1].lower()   self.hotkeys[char] = btn   if default == i:   self.setDefaultButton(btn)   if esc == i:   self.setEscapeButton(btn)   except ValueError:   pass     def run(self):   return self.exec_()     def keyPressEvent(self, event):   for k, btn in self.hotkeys.iteritems():   if event.text() == k:   btn.emit(QtCore.SIGNAL('clicked()'))   super(CustomPrompt, self).keyPressEvent(event)    def setup_font_substitutions():   QtGui.QFont.insertSubstitutions('monospace', ['monaco', 'courier new'])    class PMButton(QtGui.QPushButton):   """Toggle button with plus/minus icon images"""     def __init__(self, expanded=True, parent=None):   QtGui.QPushButton.__init__(self, parent)     size = QtCore.QSize(11, 11)   self.setIconSize(size)   self.setMaximumSize(size)   self.setFlat(True)     self.plus = geticon('plus')   self.minus = geticon('minus')   icon = expanded and self.minus or self.plus   self.setIcon(icon)     def clicked():   icon = self.is_expanded() and self.plus or self.minus   self.setIcon(icon)   self.clicked.connect(clicked)     def set_expanded(self, state=True):   icon = state and self.minus or self.plus   self.setIcon(icon)     def set_collapsed(self, state=True):   icon = state and self.plus or self.minus   self.setIcon(icon)     def is_expanded(self):   return self.icon().serialNumber() == self.minus.serialNumber()     def is_collapsed(self):   return not self.is_expanded() + +def fileEditor(filename): + 'Open a simple modal file editing dialog' + dialog = QtGui.QDialog() + vbox = QtGui.QVBoxLayout() + dialog.setLayout(vbox) + editor = qsci() + editor.setBraceMatching(qsci.SloppyBraceMatch) + vbox.addWidget(editor) + BB = QtGui.QDialogButtonBox + bb = QtGui.QDialogButtonBox(BB.Save|BB.Cancel) + dialog.connect(bb, QtCore.SIGNAL('accepted()'), + dialog, QtCore.SLOT('accept()')) + dialog.connect(bb, QtCore.SIGNAL('rejected()'), + dialog, QtCore.SLOT('reject()')) + vbox.addWidget(bb) + lexer = Qsci.QsciLexerProperties() + editor.setLexer(lexer) + s = QtCore.QSettings() + ret = QtGui.QDialog.Rejected + try: + contents = open(filename, 'rb').read() + dialog.setWindowTitle(filename) + geomname = 'editor-geom' + editor.setText(contents) + editor.setModified(False) + dialog.restoreGeometry(s.value(geomname).toByteArray()) + ret = dialog.exec_() + if ret == QtGui.QDialog.Accepted: + f = util.atomictempfile(filename, 'w', createmode=None) + f.write(editor.text()) + f.rename() + s.setValue(geomname, dialog.saveGeometry()) + except EnvironmentError, e: + qtlib.WarningMsgBox(_('Unable to read/write config file'), + hglib.tounicode(e), parent=dialog) + return ret
 
16
17
18
19
20
21
22
23
24
25
26
27
 
535
536
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
 
16
17
18
 
19
20
21
 
 
22
23
24
 
532
533
534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
535
536
537
538
539
@@ -16,12 +16,9 @@
 from tortoisehg.util import hglib, settings, paths  from tortoisehg.hgqt import qtlib   -from PyQt4 import Qsci  from PyQt4.QtCore import *  from PyQt4.QtGui import *   -qsci = Qsci.QsciScintilla -  # Technical Debt  # stacked widget or pages need to be scrollable  # add extensions page after THG 1.1 is released @@ -535,37 +532,8 @@
  self.applyChanges()   elif ret == 2:   return - - dialog = QDialog() - vbox = QVBoxLayout() - dialog.setLayout(vbox) - editor = qsci() - editor.setBraceMatching(qsci.SloppyBraceMatch) - vbox.addWidget(editor) - BB = QDialogButtonBox - bb = QDialogButtonBox(BB.Save|BB.Cancel) - self.connect(bb, SIGNAL("accepted()"), dialog, SLOT('accept()')) - self.connect(bb, SIGNAL("rejected()"), dialog, SLOT('reject()')) - vbox.addWidget(bb) - lexer = Qsci.QsciLexerProperties() - editor.setLexer(lexer) - s = self.settings - try: - contents = open(self.fn, 'rb').read() - dialog.setWindowTitle(self.fn) - geomname = 'settings/editor-geom' - editor.setText(contents) - editor.setModified(False) - dialog.restoreGeometry(s.value(geomname).toByteArray()) - if dialog.exec_() == QDialog.Accepted: - f = util.atomictempfile(self.fn, 'w', createmode=None) - f.write(editor.text()) - f.rename() - self.refresh() - s.setValue(geomname, dialog.saveGeometry()) - except EnvironmentError, e: - qtlib.WarningMsgBox(_('Unable to read/write config file'), - hglib.tounicode(e), parent=self) + if qtlib.fileEditor(self.fn) == QDialog.Accepted: + self.refresh()     def refresh(self, *args):   # determine target config file