Debian Codegraph @

relational_gui/editor.py

raw symbols SCIP
1 # Relational
2 # Copyright (C) 2016-2025 Salvo "LtWorf" Tomaselli
3 #
4 # Relational is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # author Salvo "LtWorf" Tomaselli <[email protected]>
18 #
19 # This module provides a classes to represent relations and to perform
20 # relational operations on them.
21
22 from PyQt6.QtWidgets import QPlainTextEdit
23 from PyQt6.QtWidgets import QTextEdit
24 from PyQt6.QtCore import Qt
25 from PyQt6.QtGui import QColor
26 from PyQt6.QtGui import QPalette
27 from PyQt6.QtGui import QTextCharFormat
28 from PyQt6.QtGui import QWheelEvent
29
30
31 class Editor(QPlainTextEdit):
32
33 def __init__(self, *args, **kwargs):
34 super(Editor, self).__init__(*args, **kwargs)
35
36 self._cursor_moved()
37 self.cursorPositionChanged.connect(self._cursor_moved)
38
39 def _cursor_moved(self) -> None:
40 selections = []
41
42 # Current line
43 cur_line = QTextEdit.ExtraSelection()
44 bgcolor = QPalette().color(
45 QPalette.ColorGroup.Active,
46 QPalette.ColorRole.Window,
47 ).lighter()
48 cur_line.format.setBackground(bgcolor)
49 cur_line.format.setProperty(
50 QTextCharFormat.Property.FullWidthSelection,
51 True
52 )
53 cur_line.cursor = self.textCursor()
54 cur_line.cursor.clearSelection()
55 selections.append(cur_line)
56
57 # Apply the selections
58 self.setExtraSelections(selections)
59
60 def wheelEvent(self, event: QWheelEvent | None) -> None:
61 if event is not None and event.modifiers().value == Qt.Modifier.CTRL.value:
62 event.accept()
63 self.zoom(1 if event.angleDelta().y()>0 else -1)
64 else:
65 super(Editor, self).wheelEvent(event)
66
67 def zoom(self, incr: int) -> None:
68 font = self.font()
69 point_size = font.pointSize()
70 point_size += incr
71 if point_size < 1:
72 return
73 font.setPointSize(point_size)
74 self.setFont(font)