1
# -*- coding: utf-8 -*-
3
# Copyright (c) 2004 - 2007 Detlev Offenbach <detlev@die-offenbachs.de>
7
Module implementing a compatability interface class to QextScintilla.
12
from qt import Qt, QKeyEvent, QEvent, qApp, SIGNAL
13
from qtext import QextScintilla, \
14
QSCINTILLA_VERSION as QextQSCINTILLA_VERSION, QSCINTILLA_VERSION_STR
17
#####################################################################################
18
# enums below have been added to QScintilla starting with version after 1.6
19
#####################################################################################
21
if "WrapFlagNone" not in QextScintilla.__dict__:
22
QextScintilla.WrapFlagNone = 0
23
QextScintilla.WrapFlagByText = 1
24
QextScintilla.WrapFlagByBorder = 2
26
if "AcsAll" not in QextScintilla.__dict__:
27
QextScintilla.AcsAll = QextScintilla.AcsDocument
28
# identical to AcsDocument for older QScintilla versions
30
#####################################################################################
32
def QSCINTILLA_VERSION():
34
Module function to return the QScintilla version.
36
If the installed QScintilla is a snapshot version, then assume it is
37
of the latest release and return a version number of 0x99999.
39
@return QScintilla version (integer)
41
if QSCINTILLA_VERSION_STR.startswith('snapshot') or \
42
QSCINTILLA_VERSION_STR.startswith('1-snapshot'):
45
return QextQSCINTILLA_VERSION
47
#####################################################################################
49
class QextScintillaCompat(QextScintilla):
51
Class implementing a compatability interface to QextScintilla.
53
This class implements all the functions, that were added to
54
QextScintilla incrementally. This class ensures compatibility
55
to older versions of QextScintilla.
57
def __init__(self, parent=None, name=None, flags=0):
61
@param parent parent widget (QWidget)
62
@param name name of this instance (string or QString)
63
@param flags window flags
65
QextScintilla.__init__(self, parent, name, flags)
68
# compatibility code for QScintilla < 1.7
69
if QSCINTILLA_VERSION < 0x010700:
70
self.autoCompleteFromAll = self.autoCompleteFromDocument
72
# workaround for a bug in version <= 1.5.1
74
self.connect(self, SIGNAL('modificationChanged(bool)'),
75
self._handleModificationChanged)
77
# workaround for an initialization bug in version <= 1.4
80
# workaround for a bug in version <= 1.5.1
81
def _handleModificationChanged(self, m):
83
Private slot to handle the modificationChanged signal.
85
It emits the signal modificationStatusChanged with parameters
88
@param m modification status
94
Public method to retrieve the modification status.
96
@return flag indicating the modification status (boolean)
100
def setModified(self, m):
102
Public method to set the modification status.
104
@param m flag indicating the new modification status
107
QextScintilla.setModified(self, m)
109
# workaround for a bug in setLexer() in version <= 1.4
110
def setLexer(self, lex = None):
112
Public method to set the lexer.
114
@param lex the lexer to be set or None to reset it.
116
QextScintilla.setLexer(self, lex)
120
def clearStyles(self):
122
Public method to set the styles according the selected Qt style.
124
colorGroup = qApp.palette().active()
125
self.SendScintilla(QextScintilla.SCI_STYLESETFORE,
126
QextScintilla.STYLE_DEFAULT, colorGroup.text())
127
self.SendScintilla(QextScintilla.SCI_STYLESETBACK,
128
QextScintilla.STYLE_DEFAULT, colorGroup.base())
129
self.SendScintilla(QextScintilla.SCI_STYLECLEARALL)
130
self.SendScintilla(QextScintilla.SCI_CLEARDOCUMENTSTYLE)
132
self.setMarginsForegroundColor(colorGroup.foreground())
133
self.setMarginsBackgroundColor(colorGroup.background())
134
self.setFoldMarginColors(colorGroup.mid(),
137
def monospacedStyles(self, font):
139
Public method to set the current style to be monospaced.
141
@param font font to be used (QFont)
144
rangeLow = range(self.STYLE_DEFAULT)
145
except AttributeError:
148
rangeHigh = range(self.STYLE_LASTPREDEFINED + 1,
150
except AttributeError:
151
rangeHigh = range(40, 128)
153
f = font.family().latin1()
154
ps = font.pointSize()
155
for style in rangeLow + rangeHigh:
156
self.SendScintilla(QextScintilla.SCI_STYLESETFONT, style, f)
157
self.SendScintilla(QextScintilla.SCI_STYLESETSIZE, style, ps)
159
def lineAt(self, pos):
161
Public method to calculate the line at a position.
163
This variant is able to calculate the line for positions in the
164
margins and for empty lines.
166
@param pos position to calculate the line for (QPoint)
167
@return linenumber at position or -1, if there is no line at pos
168
(integer, zero based)
170
scipos = self.SendScintilla(QextScintilla.SCI_POSITIONFROMPOINT, pos.x(), pos.y())
171
line = self.SendScintilla(QextScintilla.SCI_LINEFROMPOSITION, scipos)
172
if line >= self.lines():
176
#####################################################################################
177
# methods below are missing from QScintilla
178
#####################################################################################
180
def zoomIn(self, zoom = 1):
182
Public method used to increase the zoom factor.
184
@param zoom zoom factor increment
187
QextScintilla.zoomIn(self, zoom)
189
def zoomOut(self, zoom = 1):
191
Public method used to decrease the zoom factor.
193
@param zoom zoom factor decrement
196
QextScintilla.zoomOut(self, zoom)
198
def zoomTo(self, zoom):
200
Public method used to zoom to a specific zoom factor.
202
@param zoom zoom factor
205
QextScintilla.zoomTo(self, zoom)
209
Public method used to retrieve the current zoom factor.
211
@return zoom factor (int)
215
def editorCommand(self, cmd):
217
Public method to perform a simple editor command.
219
@param cmd the scintilla command to be performed
221
self.SendScintilla(cmd)
223
def scrollVertical(self, lines):
225
Public method to scroll the text area.
227
@param lines number of lines to scroll (negative scrolls up,
228
positive scrolls down) (integer)
230
self.SendScintilla(QextScintilla.SCI_LINESCROLL, 0, lines)
232
def moveCursorToEOL(self):
234
Public method to move the cursor to the end of line.
236
self.SendScintilla(QextScintilla.SCI_LINEEND)
238
def moveCursorLeft(self):
240
Public method to move the cursor left.
242
self.SendScintilla(QextScintilla.SCI_CHARLEFT)
244
def moveCursorRight(self):
246
Public method to move the cursor right.
248
self.SendScintilla(QextScintilla.SCI_CHARRIGHT)
250
def moveCursorWordLeft(self):
252
Public method to move the cursor left one word.
254
self.SendScintilla(QextScintilla.SCI_WORDLEFT)
256
def moveCursorWordRight(self):
258
Public method to move the cursor right one word.
260
self.SendScintilla(QextScintilla.SCI_WORDRIGHT)
262
def deleteBack(self):
264
Public method to delete the character to the left of the cursor.
266
self.SendScintilla(QextScintilla.SCI_DELETEBACK)
270
Public method to delete the character to the right of the cursor.
272
self.SendScintilla(QextScintilla.SCI_CLEAR)
274
def deleteWordLeft(self):
276
Public method to delete the word to the left of the cursor.
278
self.SendScintilla(QextScintilla.SCI_DELWORDLEFT)
280
def deleteWordRight(self):
282
Public method to delete the word to the right of the cursor.
284
self.SendScintilla(QextScintilla.SCI_DELWORDRIGHT)
286
def deleteLineLeft(self):
288
Public method to delete the line to the left of the cursor.
290
self.SendScintilla(QextScintilla.SCI_DELLINELEFT)
292
def deleteLineRight(self):
294
Public method to delete the line to the right of the cursor.
296
self.SendScintilla(QextScintilla.SCI_DELLINERIGHT)
298
def extendSelectionLeft(self):
300
Public method to extend the selection one character to the left.
302
self.SendScintilla(QextScintilla.SCI_CHARLEFTEXTEND)
304
def extendSelectionRight(self):
306
Public method to extend the selection one character to the right.
308
self.SendScintilla(QextScintilla.SCI_CHARRIGHTEXTEND)
310
def extendSelectionWordLeft(self):
312
Public method to extend the selection one word to the left.
314
self.SendScintilla(QextScintilla.SCI_WORDLEFTEXTEND)
316
def extendSelectionWordRight(self):
318
Public method to extend the selection one word to the right.
320
self.SendScintilla(QextScintilla.SCI_WORDRIGHTEXTEND)
322
def extendSelectionToBOL(self):
324
Public method to extend the selection to the beginning of the line.
326
self.SendScintilla(QextScintilla.SCI_VCHOMEEXTEND)
328
def extendSelectionToEOL(self):
330
Public method to extend the selection to the end of the line.
332
self.SendScintilla(QextScintilla.SCI_LINEENDEXTEND)
334
#####################################################################################
335
# methods below have been added to QScintilla starting with version after 1.6
336
#####################################################################################
338
if "setAutoCompletionFillupsEnabled" not in QextScintilla.__dict__:
339
def setAutoCompletionFillupsEnabled(self, on):
341
Public method to enable the use of autocompletion fillup characters.
343
@param on flag indicating the usage of autocompletion fillup
347
self.setAutoCompletionFillups("(")
349
self.setAutoCompletionFillups("")
351
if "isCalltipActive" not in QextScintilla.__dict__:
352
def isCallTipActive(self):
354
Public method to check, if a call tip is active.
356
@return flag indicating call tip active state (boolean)
358
return self.SendScintilla(QextScintilla.SCI_CALLTIPACTIVE)
360
if "isListActive" not in QextScintilla.__dict__:
361
def isListActive(self):
363
Public method to check, if autocompletion or user-defined list is active.
365
@return flag indicating autocompletion active state (boolean)
367
return self.SendScintilla(QextScintilla.SCI_AUTOCACTIVE)
369
if "cancelList" not in QextScintilla.__dict__:
370
def cancelList(self):
372
Public method to cancel autocompletion list or user-defined list.
374
self.SendScintilla(QextScintilla.SCI_AUTOCCANCEL)
376
if "showUserList" not in QextScintilla.__dict__:
377
def showUserList(self, id, userlist):
379
Public method to show a user list.
381
@param id id of the list to be shown (integer)
382
@param userlist list of items to be shown (QStringList)
387
liststring = str(userlist.join(' '))
388
self.SendScintilla(QextScintilla.SCI_USERLISTSHOW, id, liststring)
390
if "firstVisibleLine" not in QextScintilla.__dict__:
391
def firstVisibleLine(self):
393
Public method to get the line number of the first visible line.
395
@return number of the first visible line (integer)
397
return self.SendScintilla(QextScintilla.SCI_GETFIRSTVISIBLELINE)
399
if "textHeight" not in QextScintilla.__dict__:
400
def textHeight(self, linenr):
402
Public method to get the text height of a line.
404
@param linenr number of the line (integer)
405
@return text height (integer)
407
return self.SendScintilla(QextScintilla.SCI_TEXTHEIGHT, linenr)
409
if "setWrapVisualFlags" not in QextScintilla.__dict__:
410
def setWrapVisualFlags(self, eflag, sflag = QextScintilla.WrapFlagNone,
413
Public method to set the visual flags for wrapped lines.
415
@param eflag determines if and where the flag at the end of a line
417
@param sflag determines if and where the flag at the start of a line
419
@param sindent is the number of characters a wrapped line is indented by
421
flags = QextScintilla.SC_WRAPVISUALFLAG_NONE
422
loc = QextScintilla.SC_WRAPVISUALFLAGLOC_DEFAULT
424
if eflag == QextScintilla.WrapFlagByText:
425
flags |= QextScintilla.SC_WRAPVISUALFLAG_END
426
loc |= QextScintilla.SC_WRAPVISUALFLAGLOC_END_BY_TEXT
427
elif eflag == QextScintilla.WrapFlagByBorder:
428
flags |= QextScintilla.SC_WRAPVISUALFLAG_END
430
if sflag == QextScintilla.WrapFlagByText:
431
flags |= QextScintilla.SC_WRAPVISUALFLAG_START
432
loc |= QextScintilla.SC_WRAPVISUALFLAGLOC_START_BY_TEXT
433
elif sflag == QextScintilla.WrapFlagByBorder:
434
flags |= QextScintilla.SC_WRAPVISUALFLAG_START
436
self.SendScintilla(QextScintilla.SCI_SETWRAPVISUALFLAGS, flags)
437
self.SendScintilla(QextScintilla.SCI_SETWRAPVISUALFLAGSLOCATION, loc)
438
self.SendScintilla(QextScintilla.SCI_SETWRAPSTARTINDENT, sindent)
440
def setLayoutCache(self, cacheMode):
442
Public method to set the layout cache mode.
444
@param cacheMode layout cache mode to be set
446
self.SendScintilla(QextScintilla.SCI_SETLAYOUTCACHE, cacheMode)
448
def setLayoutCache(self, cacheMode):
450
Public method to set the layout cache mode.
452
@param cacheMode layout cache mode to be set
454
# do nothing, it is handled by QextScintilla
457
#####################################################################################
458
# methods below have been added to QScintilla starting with version after 1.3
459
#####################################################################################
461
if "setEdgeMode" not in QextScintilla.__dict__:
462
def setEdgeMode(self, edgeMode):
464
Public method to set the edge mode.
466
@param edgeMode mode to be set
468
self.SendScintilla(QextScintilla.SCI_SETEDGEMODE, edgeMode)
470
if "setEdgeColumn" not in QextScintilla.__dict__:
471
def setEdgeColumn(self, column):
473
Public method to set the edge column.
475
@param column column number to be set for the edge mode (integer)
477
self.SendScintilla(QextScintilla.SCI_SETEDGECOLUMN, column)
479
if "setEdgeColor" not in QextScintilla.__dict__:
480
def setEdgeColor(self, colour):
482
Public method to set the color of the edge mode highlight.
484
@param colour colour to be set (QColor)
486
self.SendScintilla(QextScintilla.SCI_SETEDGECOLOUR, colour)
488
if "setWrapMode" not in QextScintilla.__dict__:
489
def setWrapMode(self, mode):
491
Public slot to set the wrap mode of the text display.
493
@param mode wrap mode (QextScintilla.WrapNone or
494
QextScintilla.WrapWord)
496
self.SendScintilla(QextScintilla.SCI_SETWRAPMODE, mode)
498
if "resetSelectionForegroundColor" not in QextScintilla.__dict__:
499
def resetSelectionForegroundColor(self):
501
Public method to reset the foreground color of the selection.
503
self.SendScintilla(QextScintilla.SCI_SETSELFORE, 0)
505
# compatibility method to correct a bug under win32
506
if sys.platform == "win32":
507
def keyPressEvent(self, event):
509
Workaround for QScintilla <= 1.3 bug w/ keyboard layouts
510
having a AltGr key in win32:
511
AltGr is reported as state == Qt.ControlButton | Qt.AltButton,
512
but QScintilla expects (Unix-style) Qt.MetaButton. Just
513
translate states for infected QScintilla versions.
515
Reimplemented from QextScintilla.keyPressEvent
517
@param event reference to the key press event (QKeyEvent)
518
@author Torsten Marek, 2004-08-22
520
if event.state() == Qt.AltButton | Qt.ControlButton:
521
newevent = QKeyEvent(QEvent.KeyPress,
527
QextScintilla.keyPressEvent(self, event)
529
#####################################################################################
530
# methods below have been added to QScintilla starting with version after 1.2
531
#####################################################################################
533
if "setSelectionBackgroundColor" not in QextScintilla.__dict__:
534
def setSelectionBackgroundColor(self, colour):
536
Public method to set the background color of the selection.
538
@param colour colour to be set (QColor)
540
self.SendScintilla(QextScintilla.SCI_SETSELBACK, 1, colour)
542
if "setSelectionForegroundColor" not in QextScintilla.__dict__:
543
def setSelectionForegroundColor(self, colour):
545
Public method to set the foreground color of the selection.
547
@param colour colour to be set (QColor)
549
self.SendScintilla(QextScintilla.SCI_SETSELFORE, 1, colour)
551
if "setCaretForegroundColor" not in QextScintilla.__dict__:
552
def setCaretForegroundColor(self, colour):
554
Public method to set the foreground color of the caret.
556
@param colour colour to be set (QColor)
558
self.SendScintilla(QextScintilla.SCI_SETCARETFORE, colour)
560
if "setCaretLineBackgroundColor" not in QextScintilla.__dict__:
561
def setCaretLineBackgroundColor(self, colour):
563
Public method to set the background color of the caret line.
565
@param colour colour to be set (QColor)
567
self.SendScintilla(QextScintilla.SCI_SETCARETLINEBACK, colour)
569
if "setCaretLineVisible" not in QextScintilla.__dict__:
570
def setCaretLineVisible(self, enable):
572
Public method to enable the highlighting of the caret line.
574
@param enable flag indicating the highlighting state to be set (boolean)
576
self.SendScintilla(QextScintilla.SCI_SETCARETLINEVISIBLE, enable)
578
if "setCaretWidth" not in QextScintilla.__dict__:
579
def setCaretWidth(self, width):
581
Public method to enable the highlighting of the caret line.
583
@param width width of the caret (integer - 0, 1, 2 or 3)
589
self.SendScintilla(QextScintilla.SCI_SETCARETWIDTH, width)
591
if "recolor" not in QextScintilla.__dict__:
592
def recolor(self, start = 0, end = -1):
594
Public method to recolor the given region.
596
@param start linenumber where to start recolourisation (integer)
597
@param end linenumber where to end recolourisation (integer)
599
self.SendScintilla(QextScintilla.SCI_COLOURISE, start, end)
601
#####################################################################################
602
# interface methods to the standard keyboard command set
603
#####################################################################################
605
def clearAlternateKeys(self):
607
Protected method to clear the alternate key commands.
609
if "clearAlternateKeys" not in self.standardCommands().__dict__:
610
# clear each alternate key for QScintilla versions <= 1.3
611
for cmd in self.standardCommands().commands():
612
cmd.setAlternateKey(0)
614
# call into the QextScintillaCommandSet
615
self.standardCommands().clearAlternateKeys()