Files
krowlog/src/ui/bigtext/highlight_selection.py
Andreas Huber a640b35c87 rename ravenlog to krowlog
There is a database named RavenDB.
KrowLog starts with a K, which is a) distinctive and b) has an association to KDE.
2022-02-12 10:48:38 +01:00

57 lines
2.1 KiB
Python

from typing import Optional, List
from src.ui.bigtext.highlight import Highlight
from src.ui.bigtext.highlighted_range import HighlightedRange
from src.ui.bigtext.line import Line
from PySide6.QtCore import Qt
from PySide6.QtGui import QBrush, QColor
from src.settings.settings import Settings
class HighlightSelection(Highlight):
start_byte = 0
end_byte = 0
def set_start(self, start_byte):
self.start_byte = start_byte
def set_end_byte(self, end_byte):
self.end_byte = end_byte
def compute_highlight(self, line: Line) -> Optional[List[HighlightedRange]]:
begin = min(self.start_byte, self.end_byte)
end = max(self.start_byte, self.end_byte)
if line.intersects(begin, end):
if line.includes_byte(begin):
start_byte_in_line = begin - line.byte_offset()
else:
start_byte_in_line = 0
start_char = line.byte_index_to_char_index(start_byte_in_line)
if line.includes_byte(end):
length_in_bytes = end - line.byte_offset() - start_byte_in_line
end_char = line.byte_index_to_char_index(start_byte_in_line + length_in_bytes)
else:
# renders the highlighting to the end of the line
# this is how selections usually behave
length_in_bytes = Settings.max_line_length() - start_byte_in_line
# note: this mixes chars and bytes, but that should not matter, because
# it just means that we render the highlight into the invisible range on the right
end_char = start_char + length_in_bytes
start_column = line.char_to_column(start_char)
end_column = line.char_to_column(end_char)
if end_column >= 0:
length_in_columns = end_column - start_column
else:
length_in_columns = 4096
return [HighlightedRange(start_column, length_in_columns, brush=QBrush(QColor(156, 215, 255, 192)),
pen=Qt.PenStyle.NoPen)]
else:
return None