Changeset View
Changeset View
Standalone View
Standalone View
plugins/extensions/pykrita/plugin/plugins/scripter/pythoneditor.py
- This file was added.
1 | # -*- coding: utf-8 -*- | ||||
---|---|---|---|---|---|
2 | | ||||
3 | from PyQt5.QtCore import * | ||||
4 | from PyQt5.QtWidgets import * | ||||
5 | from scripter import linenumberarea | ||||
6 | from PyQt5.QtGui import * | ||||
7 | | ||||
8 | class CodeEditor(QPlainTextEdit): | ||||
9 | | ||||
10 | def __init__(self, parent=None): | ||||
11 | super(CodeEditor, self).__init__(parent) | ||||
12 | | ||||
13 | self.setLineWrapMode(self.NoWrap) | ||||
14 | | ||||
15 | self.lineNumberArea = linenumberarea.LineNumberArea(self) | ||||
16 | | ||||
17 | self.blockCountChanged.connect(self.updateLineNumberAreaWidth) | ||||
18 | self.updateRequest.connect(self.updateLineNumberArea) | ||||
19 | self.cursorPositionChanged.connect(self.highlightCurrentLine) | ||||
20 | | ||||
21 | self.updateLineNumberAreaWidth() | ||||
22 | self.highlightCurrentLine() | ||||
23 | | ||||
24 | def lineNumberAreaWidth(self): | ||||
25 | """The lineNumberAreaWidth is the quatity of decimal places in blockCount""" | ||||
26 | digits = 1 | ||||
27 | max_ = max(1, self.blockCount()) | ||||
28 | while (max_ >= 10): | ||||
29 | max_ /= 10 | ||||
30 | digits += 1 | ||||
31 | | ||||
32 | space = 3 + self.fontMetrics().width('9') * digits | ||||
33 | | ||||
34 | return space | ||||
35 | | ||||
36 | def resizeEvent(self, event): | ||||
37 | super(CodeEditor, self).resizeEvent(event) | ||||
38 | | ||||
39 | qRect = self.contentsRect(); | ||||
40 | self.lineNumberArea.setGeometry(QRect(qRect.left(), qRect.top(), self.lineNumberAreaWidth(), qRect.height())); | ||||
41 | | ||||
42 | def updateLineNumberAreaWidth(self): | ||||
43 | self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0) | ||||
44 | | ||||
45 | def updateLineNumberArea(self, rect, dy): | ||||
46 | """ This slot is invoked when the editors viewport has been scrolled """ | ||||
47 | | ||||
48 | if dy: | ||||
49 | self.lineNumberArea.scroll(0, dy) | ||||
50 | else: | ||||
51 | self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height()) | ||||
52 | | ||||
53 | if rect.contains(self.viewport().rect()): | ||||
54 | self.updateLineNumberAreaWidth() | ||||
55 | | ||||
56 | def lineNumberAreaPaintEvent(self, event): | ||||
57 | """This method draws the current lineNumberArea for while""" | ||||
58 | painter = QPainter(self.lineNumberArea) | ||||
59 | painter.fillRect(event.rect(), Qt.lightGray) | ||||
60 | | ||||
61 | block = self.firstVisibleBlock() | ||||
62 | blockNumber = block.blockNumber() | ||||
63 | top = int(self.blockBoundingGeometry(block).translated(self.contentOffset()).top()) | ||||
64 | bottom = top + int(self.blockBoundingRect(block).height()) | ||||
65 | | ||||
66 | while block.isValid() and top <= event.rect().bottom(): | ||||
67 | if block.isVisible() and bottom >= event.rect().top(): | ||||
68 | number = str(blockNumber + 1) | ||||
69 | painter.setPen(Qt.black) | ||||
70 | painter.drawText(0, top, self.lineNumberArea.width(), self.fontMetrics().height(), | ||||
71 | Qt.AlignRight, number) | ||||
72 | | ||||
73 | block = block.next() | ||||
74 | top = bottom | ||||
75 | bottom = top + int(self.blockBoundingRect(block).height()) | ||||
76 | blockNumber += 1 | ||||
77 | | ||||
78 | def highlightCurrentLine(self): | ||||
79 | """Highlight current line under cursor""" | ||||
80 | currentSelection = QTextEdit.ExtraSelection() | ||||
81 | | ||||
82 | lineColor = QColor(Qt.gray).lighter(180) | ||||
83 | currentSelection.format.setBackground(lineColor) | ||||
84 | currentSelection.format.setProperty(QTextFormat.FullWidthSelection, True) | ||||
85 | currentSelection.cursor = self.textCursor() | ||||
86 | currentSelection.cursor.clearSelection() | ||||
87 | self.setExtraSelections([currentSelection]) |