selection by mouse
This commit is contained in:
41
bigtext.py
41
bigtext.py
@@ -9,18 +9,49 @@ from logFileModel import LogFileModel
|
|||||||
class BigText(QWidget):
|
class BigText(QWidget):
|
||||||
_byte_offset = 0
|
_byte_offset = 0
|
||||||
_left_offset = 0
|
_left_offset = 0
|
||||||
_selection_start_byte = 1
|
_selection_start_byte = 0
|
||||||
_selection_end_byte = 65
|
_selection_end_byte = 0
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, model: LogFileModel):
|
def __init__(self, model: LogFileModel):
|
||||||
super(BigText, self).__init__()
|
super(BigText, self).__init__()
|
||||||
self.model = model
|
self.model = model
|
||||||
self.font = QFont("monospace", 12)
|
self.font = QFont("monospace", 12)
|
||||||
self.update_font_metrics(QPainter(self))
|
self.update_font_metrics(QPainter(self))
|
||||||
|
self.lines = []
|
||||||
|
|
||||||
def paintEvent(self, event: QPaintEvent) -> None:
|
def paintEvent(self, event: QPaintEvent) -> None:
|
||||||
self.draw()
|
self.draw()
|
||||||
|
|
||||||
|
def mousePressEvent(self, e: QMouseEvent) -> None:
|
||||||
|
if e.buttons() == Qt.MouseButton.LeftButton:
|
||||||
|
self._selection_start_byte = self.to_byte_offset(e)
|
||||||
|
self._selection_end_byte = self._selection_start_byte
|
||||||
|
self.update()
|
||||||
|
|
||||||
|
def mouseMoveEvent(self, e: QMouseEvent):
|
||||||
|
current_byte = self.to_byte_offset(e)
|
||||||
|
|
||||||
|
if self._selection_end_byte != current_byte:
|
||||||
|
self._selection_end_byte = current_byte
|
||||||
|
self.update()
|
||||||
|
print("-> %s,%s" %(self._selection_start_byte, self._selection_end_byte))
|
||||||
|
|
||||||
|
def to_byte_offset(self, e: QMouseEvent) -> int:
|
||||||
|
line_number = int(e.pos().y() / self.char_height)
|
||||||
|
|
||||||
|
if line_number < len(self.lines):
|
||||||
|
line = self.lines[line_number]
|
||||||
|
char_in_line = int(e.pos().x() / self.char_width) + self._left_offset
|
||||||
|
char_in_line = min(char_in_line, line.length())
|
||||||
|
#print("%s in line %s" % (char_in_line, line_number))
|
||||||
|
|
||||||
|
current_byte = line.byte_offset() + char_in_line
|
||||||
|
#print("%s + %s = %s" % (line.byte_offset(), char_in_line, current_byte))
|
||||||
|
else:
|
||||||
|
current_byte = self.model.byte_count()
|
||||||
|
return current_byte
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
painter = QPainter()
|
painter = QPainter()
|
||||||
painter.begin(self)
|
painter.begin(self)
|
||||||
@@ -29,10 +60,10 @@ class BigText(QWidget):
|
|||||||
self.update_font_metrics( painter)
|
self.update_font_metrics( painter)
|
||||||
|
|
||||||
lines_to_show = self.height() / (self.fontMetrics().height())
|
lines_to_show = self.height() / (self.fontMetrics().height())
|
||||||
lines = self.model.data(self._byte_offset, lines_to_show)
|
self.lines = self.model.data(self._byte_offset, lines_to_show)
|
||||||
|
|
||||||
y_line_offset = self.char_height;
|
y_line_offset = self.char_height;
|
||||||
for l in lines:
|
for l in self.lines:
|
||||||
|
|
||||||
if l.intersects(self._selection_start_byte, self._selection_end_byte):
|
if l.intersects(self._selection_start_byte, self._selection_end_byte):
|
||||||
self.draw_selection(painter, l, y_line_offset)
|
self.draw_selection(painter, l, y_line_offset)
|
||||||
@@ -53,7 +84,7 @@ class BigText(QWidget):
|
|||||||
if line.includes_byte(self._selection_end_byte):
|
if line.includes_byte(self._selection_end_byte):
|
||||||
width = (self._selection_end_byte - line.byte_offset() - self._left_offset) * self.char_width - x1
|
width = (self._selection_end_byte - line.byte_offset() - self._left_offset) * self.char_width - x1
|
||||||
else:
|
else:
|
||||||
width = len(line.line().rstrip()) * self.char_width
|
width = len(line.line().rstrip()) * self.char_width -x1
|
||||||
|
|
||||||
y1 = y_line_offset- self.char_height
|
y1 = y_line_offset- self.char_height
|
||||||
height = self.char_height
|
height = self.char_height
|
||||||
|
|||||||
3
line.py
3
line.py
@@ -13,6 +13,9 @@ class Line:
|
|||||||
def line(self):
|
def line(self):
|
||||||
return self._line
|
return self._line
|
||||||
|
|
||||||
|
def length(self):
|
||||||
|
return len(self._line)
|
||||||
|
|
||||||
def includes_byte(self, byte: int) -> bool:
|
def includes_byte(self, byte: int) -> bool:
|
||||||
return self._byte_offset <= byte <= self._byte_end
|
return self._byte_offset <= byte <= self._byte_end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user