forked from wonder-sk/qgis-first-aid-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsourceview.py
94 lines (72 loc) · 3.08 KB
/
sourceview.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
#-----------------------------------------------------------
# Copyright (C) 2015 Martin Dobias
#-----------------------------------------------------------
# Licensed under the terms of GNU GPL 2
#
# 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.
#---------------------------------------------------------------------
from builtins import range
import sys
from qgis.PyQt.Qsci import QsciCommand
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtWidgets import QApplication
from qgis.PyQt.Qsci import QsciScintilla, QsciLexerPython
fontName = 'Courier'
fontSize = 10
caretBackground = QColor("#e4e4ff")
# based on code from
# http://eli.thegreenplace.net/2011/04/01/sample-using-qscintilla-with-pyqt
class SourceView(QsciScintilla):
def __init__(self, parent=None):
QsciScintilla.__init__(self, parent)
# Set the default font
font = QFont()
font.setFamily(fontName)
font.setFixedPitch(True)
font.setPointSize(fontSize)
fontmetrics = QFontMetrics(font)
# Margin 0 is used for line numbers
self.setMarginsFont(font)
self.setMarginWidth(1, fontmetrics.width("00000"))
self.setMarginLineNumbers(1, True)
#self.setMarginsBackgroundColor(QColor("#cccccc"))
# Brace matching: enable for a brace immediately before or after the current position
self.setBraceMatching(QsciScintilla.SloppyBraceMatch)
# Current line visible with special background color
self.setCaretLineVisible(True)
#self.setCaretLineBackgroundColor(caretBackground)
# Set Python lexer
self.setLexer(QsciLexerPython())
# override style settings to the same font and size
# (python lexer has styles 0 ... 15)
for i in range(16):
self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, i, fontName.encode("ascii"))
self.SendScintilla(QsciScintilla.SCI_STYLESETSIZE, i, fontSize)
# make the source read-only
self.SendScintilla(QsciScintilla.SCI_SETREADONLY, True)
def openFile(self, filename):
self.setText(open(filename).read())
def jumpToLine(self, line_number):
self.setCursorPosition(line_number-1,0)
# prevent issues with initially invisible cursor / caret line
self.setFocus()
self.standardCommands().find(QsciCommand.VerticalCentreCaret).execute()
#def resizeEvent(self, event):
#QsciScintilla.resizeEvent(self, event)
#print "RESIZE"
def showEvent(self, event):
QsciScintilla.showEvent(self, event)
#self.jumpToLine(0)
# prevent issues with incorrect initial scroll position
self.standardCommands().find(QsciCommand.VerticalCentreCaret).execute()
if __name__ == "__main__":
app = QApplication(sys.argv)
editor = SourceView()
editor.openFile(sys.argv[0])
editor.jumpToLine(35)
editor.show()
app.exec_()