add context menu

This commit is contained in:
2021-10-31 19:01:26 +01:00
parent 9fb8a45ef7
commit f1b0bf4981

View File

@@ -17,6 +17,7 @@ from highlight import Highlight
from highlight_regex import HighlightRegex from highlight_regex import HighlightRegex
from highlight_selection import HighlightSelection from highlight_selection import HighlightSelection
from highlighted_range import HighlightedRange from highlighted_range import HighlightedRange
from highlightingdialog import HighlightingDialog
from line import Line from line import Line
from logFileModel import LogFileModel from logFileModel import LogFileModel
import re import re
@@ -110,6 +111,8 @@ class InnerBigText(QWidget):
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.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.customContextMenuRequested.connect(self._open_menu)
self.update_font_metrics(QPainter(self)) self.update_font_metrics(QPainter(self))
self.lines = [] self.lines = []
@@ -128,9 +131,9 @@ class InnerBigText(QWidget):
self.scroll_by_lines(-3) self.scroll_by_lines(-3)
if e.key() == 16777237: # page down if e.key() == 16777237: # page down
self.scroll_by_lines(3) self.scroll_by_lines(3)
if e.modifiers() == Qt.KeyboardModifier.ControlModifier and e.key() == 67: # ctrl + c elif e.modifiers() == Qt.KeyboardModifier.ControlModifier and e.key() == 67: # ctrl + c
self.copy_selection() self.copy_selection()
if e.modifiers() == Qt.KeyboardModifier.ControlModifier and e.key() == 65: # ctrl + a elif e.modifiers() == Qt.KeyboardModifier.ControlModifier and e.key() == 65: # ctrl + a
self.selection_highlight.start_byte = 0 self.selection_highlight.start_byte = 0
self.selection_highlight.end_byte = self.model.byte_count() self.selection_highlight.end_byte = self.model.byte_count()
self.update() self.update()
@@ -148,6 +151,27 @@ class InnerBigText(QWidget):
# 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 _open_menu(self, position):
menu = QMenu(self)
copy_clipboard = QAction(self.tr("Copy to Clipboard"), self, triggered=self.copy_selection)
copy_clipboard.setShortcut("CTRL+C")
copy_clipboard.setDisabled(not self._has_selection())
menu.addAction(copy_clipboard)
copy_to_file = QAction(self.tr("Copy to File"), self, triggered=self._copy_selection_to_file)
copy_to_file.setDisabled(not self._has_selection())
menu.addAction(copy_to_file)
manage_highlighting = QAction(
self.tr("Highlighting"),
self,
triggered=lambda: HighlightingDialog(self.model.settings).exec())
manage_highlighting.setShortcut("CTRL+M")
menu.addAction(manage_highlighting)
menu.exec(self.mapToGlobal(position))
def scroll_by_lines(self, scroll_lines: int): def scroll_by_lines(self, scroll_lines: int):
self.scroll_lines = scroll_lines self.scroll_lines = scroll_lines
self.update() self.update()
@@ -161,6 +185,10 @@ class InnerBigText(QWidget):
self.update() self.update()
def mouseMoveEvent(self, e: QMouseEvent): def mouseMoveEvent(self, e: QMouseEvent):
if e.buttons() != Qt.MouseButton.LeftButton:
return
current_byte = self.to_byte_offset(e) current_byte = self.to_byte_offset(e)
if self.selection_highlight.end_byte != current_byte: if self.selection_highlight.end_byte != current_byte:
@@ -224,8 +252,11 @@ class InnerBigText(QWidget):
current_byte = self.model.byte_count() current_byte = self.model.byte_count()
return current_byte return current_byte
def _has_selection(self):
return self.selection_highlight.start_byte != self.selection_highlight.end_byte
def copy_selection(self): def copy_selection(self):
if self.selection_highlight.start_byte != self.selection_highlight.end_byte: if self._has_selection():
start = min(self.selection_highlight.start_byte, self.selection_highlight.end_byte) start = min(self.selection_highlight.start_byte, self.selection_highlight.end_byte)
end = max(self.selection_highlight.start_byte, self.selection_highlight.end_byte) end = max(self.selection_highlight.start_byte, self.selection_highlight.end_byte)
bytes_human_readable = humanbytes(end - start) bytes_human_readable = humanbytes(end - start)
@@ -242,14 +273,7 @@ class InnerBigText(QWidget):
you_sure.setDefaultButton(QMessageBox.StandardButton.Cancel) you_sure.setDefaultButton(QMessageBox.StandardButton.Cancel)
result = you_sure.exec() result = you_sure.exec()
if result == 1: # second custom button has the number 1 if result == 1: # second custom button has the number 1
dialog = QFileDialog(self) self._copy_selection_to_file()
(selected_file, _filter) = dialog.getSaveFileName(
caption=self.tr("Save File"),
directory=os.path.dirname(self.model.get_file())
)
if selected_file:
self.model.write_range(start, end, selected_file)
return
if result == QMessageBox.StandardButton.Cancel.value: if result == QMessageBox.StandardButton.Cancel.value:
# abort # abort
@@ -259,6 +283,18 @@ class InnerBigText(QWidget):
cb = QApplication.clipboard() cb = QApplication.clipboard()
cb.setText(selected_text) cb.setText(selected_text)
def _copy_selection_to_file(self):
if self._has_selection():
start = min(self.selection_highlight.start_byte, self.selection_highlight.end_byte)
end = max(self.selection_highlight.start_byte, self.selection_highlight.end_byte)
dialog = QFileDialog(self)
(selected_file, _filter) = dialog.getSaveFileName(
caption=self.tr("Save File"),
directory=os.path.dirname(self.model.get_file())
)
if selected_file:
self.model.write_range(start, end, selected_file)
def paintEvent(self, event: QPaintEvent) -> None: def paintEvent(self, event: QPaintEvent) -> None:
# print("paintEvent") # print("paintEvent")
painter = QPainter(self) painter = QPainter(self)