File: editor.py

package info (click to toggle)
relational 3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: python: 2,982; makefile: 101
file content (74 lines) | stat: -rw-r--r-- 2,507 bytes parent folder | download
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
# Relational
# Copyright (C) 2016-2025  Salvo "LtWorf" Tomaselli
#
# Relational 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
#
# This module provides a classes to represent relations and to perform
# relational operations on them.

from PyQt6.QtWidgets import QPlainTextEdit
from PyQt6.QtWidgets import QTextEdit
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QColor
from PyQt6.QtGui import QPalette
from PyQt6.QtGui import QTextCharFormat
from PyQt6.QtGui import QWheelEvent


class Editor(QPlainTextEdit):

    def __init__(self, *args, **kwargs):
        super(Editor, self).__init__(*args, **kwargs)

        self._cursor_moved()
        self.cursorPositionChanged.connect(self._cursor_moved)

    def _cursor_moved(self) -> None:
        selections = []

        # Current line
        cur_line = QTextEdit.ExtraSelection()
        bgcolor = QPalette().color(
            QPalette.ColorGroup.Active,
            QPalette.ColorRole.Window,
        ).lighter()
        cur_line.format.setBackground(bgcolor)
        cur_line.format.setProperty(
            QTextCharFormat.Property.FullWidthSelection,
            True
        )
        cur_line.cursor = self.textCursor()
        cur_line.cursor.clearSelection()
        selections.append(cur_line)

        # Apply the selections
        self.setExtraSelections(selections)

    def wheelEvent(self, event: QWheelEvent | None) -> None:
        if event is not None and event.modifiers().value == Qt.Modifier.CTRL.value:
            event.accept()
            self.zoom(1 if event.angleDelta().y()>0 else -1)
        else:
            super(Editor, self).wheelEvent(event)

    def zoom(self, incr: int) -> None:
        font = self.font()
        point_size = font.pointSize()
        point_size += incr
        if point_size < 1:
            return
        font.setPointSize(point_size)
        self.setFont(font)