change font size
This commit is contained in:
49
bigtext.py
49
bigtext.py
@@ -20,18 +20,17 @@ import re
|
|||||||
from settings import Settings
|
from settings import Settings
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class BigText(QWidget):
|
class BigText(QWidget):
|
||||||
def __init__(self, model: LogFileModel):
|
def __init__(self, model: LogFileModel, settings: Settings):
|
||||||
super(BigText, self).__init__()
|
super(BigText, self).__init__()
|
||||||
|
|
||||||
self.grid = QGridLayout()
|
self.grid = QGridLayout()
|
||||||
self.grid.setContentsMargins(0,0,0,0)
|
self.grid.setContentsMargins(0, 0, 0, 0)
|
||||||
self.grid.setHorizontalSpacing(0)
|
self.grid.setHorizontalSpacing(0)
|
||||||
self.grid.setVerticalSpacing(0)
|
self.grid.setVerticalSpacing(0)
|
||||||
self.setLayout(self.grid)
|
self.setLayout(self.grid)
|
||||||
|
|
||||||
big_text = InnerBigText(model, self)
|
big_text = InnerBigText(self, model, settings)
|
||||||
big_text.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding))
|
big_text.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding))
|
||||||
|
|
||||||
self.h_scroll_bar = QScrollBar(Qt.Orientation.Horizontal)
|
self.h_scroll_bar = QScrollBar(Qt.Orientation.Horizontal)
|
||||||
@@ -43,7 +42,7 @@ class BigText(QWidget):
|
|||||||
self.v_scroll_bar.setPageStep(1)
|
self.v_scroll_bar.setPageStep(1)
|
||||||
self.v_scroll_bar.valueChanged.connect(big_text.v_scroll_event)
|
self.v_scroll_bar.valueChanged.connect(big_text.v_scroll_event)
|
||||||
|
|
||||||
self.grid.addWidget(big_text,0,0)
|
self.grid.addWidget(big_text, 0, 0)
|
||||||
self.grid.addWidget(self.h_scroll_bar, 1, 0)
|
self.grid.addWidget(self.h_scroll_bar, 1, 0)
|
||||||
self.grid.addWidget(self.v_scroll_bar, 0, 1)
|
self.grid.addWidget(self.v_scroll_bar, 0, 1)
|
||||||
|
|
||||||
@@ -56,14 +55,14 @@ class InnerBigText(QWidget):
|
|||||||
|
|
||||||
highlights: [Highlight] = []
|
highlights: [Highlight] = []
|
||||||
|
|
||||||
def __init__(self, model: LogFileModel, parent: BigText):
|
def __init__(self, parent: BigText, model: LogFileModel, settings: Settings):
|
||||||
super(InnerBigText, self).__init__()
|
super(InnerBigText, self).__init__()
|
||||||
self.model = model
|
self.model = model
|
||||||
|
self.settings = settings
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
|
self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
|
||||||
self.setFocusPolicy(Qt.FocusPolicy.WheelFocus)
|
self.setFocusPolicy(Qt.FocusPolicy.WheelFocus)
|
||||||
|
|
||||||
self.font = QFont("monospace", 12)
|
|
||||||
self.update_font_metrics(QPainter(self))
|
self.update_font_metrics(QPainter(self))
|
||||||
self.lines = []
|
self.lines = []
|
||||||
self.selection_highlight = HighlightSelection()
|
self.selection_highlight = HighlightSelection()
|
||||||
@@ -75,7 +74,6 @@ class InnerBigText(QWidget):
|
|||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def keyPressEvent(self, e: QKeyEvent) -> None:
|
def keyPressEvent(self, e: QKeyEvent) -> None:
|
||||||
|
|
||||||
print("%s + %s" % (e.keyCombination().keyboardModifiers(), e.key()))
|
print("%s + %s" % (e.keyCombination().keyboardModifiers(), e.key()))
|
||||||
@@ -94,7 +92,7 @@ class InnerBigText(QWidget):
|
|||||||
|
|
||||||
def wheelEvent(self, event: QWheelEvent):
|
def wheelEvent(self, event: QWheelEvent):
|
||||||
direction = 1 if event.angleDelta().y() < 0 else -1
|
direction = 1 if event.angleDelta().y() < 0 else -1
|
||||||
#print("wheel event fired :) %s" % (direction))
|
# print("wheel event fired :) %s" % (direction))
|
||||||
self.scroll_by_lines(direction * 3)
|
self.scroll_by_lines(direction * 3)
|
||||||
|
|
||||||
def scroll_by_lines(self, scroll_lines: int):
|
def scroll_by_lines(self, scroll_lines: int):
|
||||||
@@ -120,19 +118,18 @@ class InnerBigText(QWidget):
|
|||||||
column_in_line = self.x_pos_to_column(e.pos().x())
|
column_in_line = self.x_pos_to_column(e.pos().x())
|
||||||
if line_number == 0:
|
if line_number == 0:
|
||||||
self.scroll_by_lines(-1)
|
self.scroll_by_lines(-1)
|
||||||
if line_number+1 >= int(self.lines_shown()):
|
if line_number + 1 >= int(self.lines_shown()):
|
||||||
self.scroll_by_lines(1)
|
self.scroll_by_lines(1)
|
||||||
if column_in_line <= 1:
|
if column_in_line <= 1:
|
||||||
self._left_offset = max(0, self._left_offset-2)
|
self._left_offset = max(0, self._left_offset - 2)
|
||||||
self.update()
|
self.update()
|
||||||
if column_in_line+1 >= self.columns_shown():
|
if column_in_line + 1 >= self.columns_shown():
|
||||||
self._left_offset = self._left_offset+2
|
self._left_offset = self._left_offset + 2
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
|
||||||
def h_scroll_event(self, left_offset: int):
|
def h_scroll_event(self, left_offset: int):
|
||||||
self._left_offset = left_offset
|
self._left_offset = left_offset
|
||||||
#print("left_offset: %d" % left_offset)
|
# print("left_offset: %d" % left_offset)
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def v_scroll_event(self, byte_offset: int):
|
def v_scroll_event(self, byte_offset: int):
|
||||||
@@ -141,10 +138,10 @@ class InnerBigText(QWidget):
|
|||||||
|
|
||||||
def update_longest_line(self, length: int):
|
def update_longest_line(self, length: int):
|
||||||
width_in_chars = self.width() / self.char_width
|
width_in_chars = self.width() / self.char_width
|
||||||
#print("width_in_chars: %d" % width_in_chars)
|
# print("width_in_chars: %d" % width_in_chars)
|
||||||
if self.longest_line < length:
|
if self.longest_line < length:
|
||||||
self.longest_line = length
|
self.longest_line = length
|
||||||
maximum = max(0, length - width_in_chars+1)
|
maximum = max(0, length - width_in_chars + 1)
|
||||||
self.parent.h_scroll_bar.setMaximum(maximum)
|
self.parent.h_scroll_bar.setMaximum(maximum)
|
||||||
|
|
||||||
def y_pos_to_line(self, y: int) -> int:
|
def y_pos_to_line(self, y: int) -> int:
|
||||||
@@ -159,7 +156,6 @@ class InnerBigText(QWidget):
|
|||||||
def columns_shown(self) -> float:
|
def columns_shown(self) -> float:
|
||||||
return self.width() / float(self.char_width)
|
return self.width() / float(self.char_width)
|
||||||
|
|
||||||
|
|
||||||
def to_byte_offset(self, e: QMouseEvent) -> int:
|
def to_byte_offset(self, e: QMouseEvent) -> int:
|
||||||
line_number = self.y_pos_to_line(e.pos().y())
|
line_number = self.y_pos_to_line(e.pos().y())
|
||||||
|
|
||||||
@@ -185,21 +181,20 @@ class InnerBigText(QWidget):
|
|||||||
|
|
||||||
def paintEvent(self, event: QPaintEvent) -> None:
|
def paintEvent(self, event: QPaintEvent) -> None:
|
||||||
painter = QPainter(self)
|
painter = QPainter(self)
|
||||||
painter.setFont(self.font)
|
painter.setFont(self.settings.font())
|
||||||
painter.setPen(QColor(0, 0, 0))
|
painter.setPen(QColor(0, 0, 0))
|
||||||
self.update_font_metrics(painter)
|
self.update_font_metrics(painter)
|
||||||
|
|
||||||
lines_to_show = self.lines_shown()
|
lines_to_show = self.lines_shown()
|
||||||
#print("%s / %s = %s" %(self.height(), float(self.char_height), lines_to_show))
|
# print("%s / %s = %s" %(self.height(), float(self.char_height), lines_to_show))
|
||||||
|
|
||||||
self.lines = self.model.data(self._byte_offset, self.scroll_lines, lines_to_show)
|
self.lines = self.model.data(self._byte_offset, self.scroll_lines, lines_to_show)
|
||||||
#print("lines_to_show: %d returned: %d" % (lines_to_show, len(self.lines)))
|
# print("lines_to_show: %d returned: %d" % (lines_to_show, len(self.lines)))
|
||||||
self.scroll_lines=0
|
self.scroll_lines = 0
|
||||||
self._byte_offset = self.lines[0].byte_offset() if len(self.lines) > 0 else 0
|
self._byte_offset = self.lines[0].byte_offset() if len(self.lines) > 0 else 0
|
||||||
# document length == maximum + pageStep + aFewBytesSoThatTheLastLineIsShown
|
# document length == maximum + pageStep + aFewBytesSoThatTheLastLineIsShown
|
||||||
self.parent.v_scroll_bar.setMaximum(self.model.byte_count() - 1)
|
self.parent.v_scroll_bar.setMaximum(self.model.byte_count() - 1)
|
||||||
|
|
||||||
|
|
||||||
for l in self.lines:
|
for l in self.lines:
|
||||||
self.update_longest_line(len(l.line()))
|
self.update_longest_line(len(l.line()))
|
||||||
|
|
||||||
@@ -217,7 +212,7 @@ class InnerBigText(QWidget):
|
|||||||
self.draw_highlight(highlight, painter, y_line_offset)
|
self.draw_highlight(highlight, painter, y_line_offset)
|
||||||
y_line_offset = y_line_offset + self.char_height
|
y_line_offset = y_line_offset + self.char_height
|
||||||
|
|
||||||
left_offset = -1*self._left_offset * self.char_width
|
left_offset = -1 * self._left_offset * self.char_width
|
||||||
y_line_offset = self.char_height;
|
y_line_offset = self.char_height;
|
||||||
for l in self.lines:
|
for l in self.lines:
|
||||||
painter.drawText(left_offset, y_line_offset, l.line())
|
painter.drawText(left_offset, y_line_offset, l.line())
|
||||||
@@ -226,7 +221,7 @@ class InnerBigText(QWidget):
|
|||||||
painter.end()
|
painter.end()
|
||||||
|
|
||||||
def draw_highlight(self, highlight: HighlightedRange, painter: QPainter, y_line_offset: int):
|
def draw_highlight(self, highlight: HighlightedRange, painter: QPainter, y_line_offset: int):
|
||||||
left_offset = -1*self._left_offset * self.char_width
|
left_offset = -1 * self._left_offset * self.char_width
|
||||||
x1 = highlight.get_start() * self.char_width
|
x1 = highlight.get_start() * self.char_width
|
||||||
width = highlight.get_width() * self.char_width
|
width = highlight.get_width() * self.char_width
|
||||||
|
|
||||||
@@ -238,7 +233,7 @@ class InnerBigText(QWidget):
|
|||||||
rect = QRect(left_offset, y1, full_width, height)
|
rect = QRect(left_offset, y1, full_width, height)
|
||||||
self.highlight_background(painter, rect, highlight.get_brush_full_line())
|
self.highlight_background(painter, rect, highlight.get_brush_full_line())
|
||||||
|
|
||||||
rect = QRect(left_offset+x1, y1, width, height)
|
rect = QRect(left_offset + x1, y1, width, height)
|
||||||
self.highlight_background(painter, rect, highlight.get_brush())
|
self.highlight_background(painter, rect, highlight.get_brush())
|
||||||
|
|
||||||
def highlight_background(self, painter: QPainter, rect: QRect, brush: QBrush):
|
def highlight_background(self, painter: QPainter, rect: QRect, brush: QBrush):
|
||||||
@@ -256,4 +251,4 @@ class InnerBigText(QWidget):
|
|||||||
self.char_width = fm.averageCharWidth() # all chars have same width for monospace font
|
self.char_width = fm.averageCharWidth() # all chars have same width for monospace font
|
||||||
text = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
text = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
||||||
self.char_width = fm.horizontalAdvance(text) / float(len(text))
|
self.char_width = fm.horizontalAdvance(text) / float(len(text))
|
||||||
#print("font width=%s height=%s" % (self.char_width, self.char_height))
|
# print("font width=%s height=%s" % (self.char_width, self.char_height))
|
||||||
|
|||||||
38
main.py
38
main.py
@@ -7,6 +7,7 @@ import sys
|
|||||||
|
|
||||||
from bigtext import BigText
|
from bigtext import BigText
|
||||||
from logFileModel import LogFileModel
|
from logFileModel import LogFileModel
|
||||||
|
from settings import Settings
|
||||||
|
|
||||||
MAX_LINE_LENGTH = 4096
|
MAX_LINE_LENGTH = 4096
|
||||||
|
|
||||||
@@ -15,28 +16,53 @@ class MainWindow(QMainWindow):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(MainWindow, self).__init__(*args, **kwargs)
|
super(MainWindow, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
self.settings = Settings()
|
||||||
self.setWindowTitle("RavenLog")
|
self.setWindowTitle("RavenLog")
|
||||||
self.setGeometry(0, 0, 640, 480)
|
self.setGeometry(0, 0, 640, 480)
|
||||||
self.setDockNestingEnabled(True)
|
self.setDockNestingEnabled(True)
|
||||||
|
|
||||||
self.setCentralWidget(self.create_tabs())
|
self.tabs = self.create_tabs()
|
||||||
self.addToolBar(QToolBar("main toolbar"))
|
self.setCentralWidget(self.tabs)
|
||||||
|
self.main_tool_bar = self.create_main_tool_bar()
|
||||||
|
self.addToolBar(self.main_tool_bar)
|
||||||
self.setStatusBar(QStatusBar(self))
|
self.setStatusBar(QStatusBar(self))
|
||||||
self.setMenuBar(self.create_menu_bar())
|
self.setMenuBar(self.create_menu_bar())
|
||||||
|
|
||||||
@staticmethod
|
def create_main_tool_bar(self):
|
||||||
def create_tabs() -> QTabWidget:
|
result = QToolBar("main toolbar")
|
||||||
|
follow_file = QCheckBox("Follow File")
|
||||||
|
follow_file.toggled.connect(self.on_follow_file_changed)
|
||||||
|
result.addWidget(follow_file)
|
||||||
|
|
||||||
|
result.addWidget(QLabel("Font Size:"))
|
||||||
|
font_size = QComboBox(result)
|
||||||
|
for s in range(3,30):
|
||||||
|
font_size.addItem(str(s))
|
||||||
|
font_size.setCurrentText(str(self.settings.get_font_size()))
|
||||||
|
font_size.currentTextChanged.connect(self.update_font_size)
|
||||||
|
result.addWidget(font_size)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def update_font_size(self, font_size):
|
||||||
|
self.settings.font_size(int(font_size))
|
||||||
|
self.tabs.update()
|
||||||
|
|
||||||
|
def on_follow_file_changed(self, e):
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
def create_tabs(self) -> QTabWidget:
|
||||||
tabs = QTabWidget()
|
tabs = QTabWidget()
|
||||||
tabs.setTabsClosable(True)
|
tabs.setTabsClosable(True)
|
||||||
|
|
||||||
|
|
||||||
#model = LogFileModel("/home/andi/ws/performanceDb/data/production/logs_2018-09-06_2018-09-06.csv")
|
#model = LogFileModel("/home/andi/ws/performanceDb/data/production/logs_2018-09-06_2018-09-06.csv")
|
||||||
model = LogFileModel("/home/andi/ws/ravenlog/example.log")
|
model = LogFileModel("/home/andi/ws/ravenlog/example.log")
|
||||||
big_text = BigText(model)
|
big_text = BigText(model, self.settings)
|
||||||
tabs.addTab(big_text, "small")
|
tabs.addTab(big_text, "small")
|
||||||
|
|
||||||
model = LogFileModel("/home/andi/ws/performanceDb/data/production/logs_2018-09-06_2018-09-06.csv")
|
model = LogFileModel("/home/andi/ws/performanceDb/data/production/logs_2018-09-06_2018-09-06.csv")
|
||||||
big_text = BigText(model)
|
big_text = BigText(model, self.settings)
|
||||||
tabs.addTab(big_text, "big")
|
tabs.addTab(big_text, "big")
|
||||||
|
|
||||||
return tabs
|
return tabs
|
||||||
|
|||||||
15
settings.py
15
settings.py
@@ -1,6 +1,21 @@
|
|||||||
|
from PyQt6.QtGui import QFont
|
||||||
|
|
||||||
|
|
||||||
class Settings():
|
class Settings():
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.font_size(12)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def max_line_length():
|
def max_line_length():
|
||||||
return 4096
|
return 4096
|
||||||
|
|
||||||
|
def font(self) -> QFont:
|
||||||
|
return self._font
|
||||||
|
|
||||||
|
def get_font_size(self) -> int:
|
||||||
|
return self._font_size
|
||||||
|
|
||||||
|
def font_size(self, font_size: int):
|
||||||
|
self._font_size = font_size
|
||||||
|
self._font = QFont("monospace", font_size)
|
||||||
Reference in New Issue
Block a user