diff --git a/bigtext.py b/bigtext.py index abb45e8..3e86ea9 100644 --- a/bigtext.py +++ b/bigtext.py @@ -17,6 +17,7 @@ from highlight import Highlight from highlight_regex import HighlightRegex from highlight_selection import HighlightSelection from highlighted_range import HighlightedRange +from highlightingdialog import HighlightingDialog from line import Line from logFileModel import LogFileModel import re @@ -110,6 +111,8 @@ class InnerBigText(QWidget): self.parent = parent self.setFocusPolicy(Qt.FocusPolicy.StrongFocus) self.setFocusPolicy(Qt.FocusPolicy.WheelFocus) + self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) + self.customContextMenuRequested.connect(self._open_menu) self.update_font_metrics(QPainter(self)) self.lines = [] @@ -128,9 +131,9 @@ class InnerBigText(QWidget): self.scroll_by_lines(-3) if e.key() == 16777237: # page down 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() - 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.end_byte = self.model.byte_count() self.update() @@ -148,6 +151,27 @@ class InnerBigText(QWidget): # print("wheel event fired :) %s" % (direction)) 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): self.scroll_lines = scroll_lines self.update() @@ -161,6 +185,10 @@ class InnerBigText(QWidget): self.update() def mouseMoveEvent(self, e: QMouseEvent): + + if e.buttons() != Qt.MouseButton.LeftButton: + return + current_byte = self.to_byte_offset(e) if self.selection_highlight.end_byte != current_byte: @@ -224,8 +252,11 @@ class InnerBigText(QWidget): current_byte = self.model.byte_count() return current_byte + def _has_selection(self): + return self.selection_highlight.start_byte != self.selection_highlight.end_byte + 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) end = max(self.selection_highlight.start_byte, self.selection_highlight.end_byte) bytes_human_readable = humanbytes(end - start) @@ -242,14 +273,7 @@ class InnerBigText(QWidget): you_sure.setDefaultButton(QMessageBox.StandardButton.Cancel) result = you_sure.exec() if result == 1: # second custom button has the number 1 - 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) - return + self._copy_selection_to_file() if result == QMessageBox.StandardButton.Cancel.value: # abort @@ -259,6 +283,18 @@ class InnerBigText(QWidget): cb = QApplication.clipboard() 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: # print("paintEvent") painter = QPainter(self)