diff --git a/src/mainwindow.py b/src/mainwindow.py index b6c0c56..b6a2743 100644 --- a/src/mainwindow.py +++ b/src/mainwindow.py @@ -93,9 +93,8 @@ class MainWindow(QMainWindow): def _action_highlighter(self): manage = RAction( - _("&Highlighter"), - action=lambda: HighlightingDialog(self.settings).exec(), - shortcut='Ctrl+H' + _("&Old Highlighter"), + action=lambda: HighlightingDialog(self.settings).exec() ) return manage diff --git a/src/plugins/logfileplugin.py b/src/plugins/logfileplugin.py index 192b54b..8fbbcf7 100644 --- a/src/plugins/logfileplugin.py +++ b/src/plugins/logfileplugin.py @@ -4,6 +4,8 @@ from typing import Optional from PySide6.QtWidgets import QMessageBox from src.pluginregistry import PluginRegistry +from src.plugins.domain.menucontribution import MenuContribution +from src.plugins.domain.raction import RAction from src.plugins.logfile.fulltabwidget import FullTabWidget from src.ui.bigtext.logFileModel import LogFileModel from src.pluginbase import PluginBase @@ -11,6 +13,7 @@ from src.plugins.krowlog.Tab import Tab from src.settings.settings import Settings from src.i18n import _ +from src.ui.bigtext.newhighlightingdialog import NewHighlightingDialog class LogFilePlugin(PluginBase): @@ -21,6 +24,20 @@ class LogFilePlugin(PluginBase): def set_settings(self, settings: Settings): self.settings = settings + def get_menu_contributions(self) -> [MenuContribution]: + return [ + MenuContribution("settings", action=self._action_highlighter(), action_id="new highlighter", + after=""), + ] + + def _action_highlighter(self): + manage = RAction( + _("&Highlighter"), + action=lambda: NewHighlightingDialog(self.settings).exec(), + shortcut='Ctrl+H' + ) + return manage + def create_tab(self, file: str) -> Optional[Tab]: if not os.path.isfile(file): message = QMessageBox(QMessageBox.Icon.Warning, _("File not found"), diff --git a/src/ui/bigtext/highlight_regex.py b/src/ui/bigtext/highlight_regex.py index 7e840a4..7f81af8 100644 --- a/src/ui/bigtext/highlight_regex.py +++ b/src/ui/bigtext/highlight_regex.py @@ -33,6 +33,18 @@ class HighlightRegex(Highlight): self.query = query self.regex = self._get_regex() + def set_hit_background_color(self, color: str): + self.hit_background_color = color + + def set_line_background_color(self, color: str): + self.line_background_color = color + + def set_is_regex(self, is_regex: bool): + self.is_regex = is_regex + + def set_is_ignore_case(self, ignore_case: bool): + self.ignore_case = ignore_case + def compute_highlight(self, line: Line) -> Optional[List[HighlightedRange]]: result = [] diff --git a/src/ui/bigtext/newhighlightingdialog.py b/src/ui/bigtext/newhighlightingdialog.py new file mode 100644 index 0000000..bf5ca6c --- /dev/null +++ b/src/ui/bigtext/newhighlightingdialog.py @@ -0,0 +1,166 @@ +from PySide6 import QtCore +from PySide6.QtGui import QIcon +from PySide6.QtWidgets import QDialog, QGridLayout, QListWidget, QDialogButtonBox, QAbstractItemView, QWidget, \ + QLineEdit, QLabel, QScrollArea, QVBoxLayout, QHBoxLayout, QPushButton, QCheckBox, QSizePolicy, QListWidgetItem + +from src.settings.settings import Settings + +from src.i18n import _ +from src.ui.bigtext.highlight_regex import HighlightRegex +from src.ui.bigtext.highlighting import Highlighting +from src.ui.colorbutton import ColorButton +from src.ui.hbox import HBox +from src.ui.icon import Icon +from src.ui.vbox import VBox + + +class NewHighlightingDialog(QDialog): + def __init__(self, settings: Settings): + super(NewHighlightingDialog, self).__init__() + self.setWindowTitle(_("Manage Highlighting")) + self.setModal(True) + self.setMinimumWidth(700) + self.setMinimumHeight(600) + self._settings = settings + + self.layout = QGridLayout(self) + + row = 0 + btn_new = QPushButton(Icon.fromTheme("list-add"), _("New")) + btn_new.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)) + btn_new.pressed.connect(self._new_highlighter) + + self.btn_delete = QPushButton(Icon.fromTheme("list-remove"), _("Remove")) + self.btn_delete.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)) + self.btn_delete.pressed.connect(self._delete_selected_items) + + self.btn_move_up = QPushButton(Icon.fromTheme("go-up"), _("Up")) + self.btn_move_up.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)) + self.btn_move_up.pressed.connect(self._move_up) + + self.btn_move_down = QPushButton(Icon.fromTheme("go-down"), _("Down")) + self.btn_move_down.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)) + self.btn_move_down.pressed.connect(self._move_down) + + button_bar = HBox(btn_new, self.btn_delete, self.btn_move_up, self.btn_move_down) + button_bar.layout.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeft) + self.layout.addWidget(button_bar, row, 0, 1, 2) + + row = row + 1 + self.list = QListWidget() + self.list.setDragDropMode(QAbstractItemView.InternalMove) + # self.list.setSelectionMode(QListWidget.SelectionMode.MultiSelection) + self.layout.addWidget(self.list, row, 0, 1, 2) + + row = row + 1 + self.buttons = QDialogButtonBox() + self.buttons.setStandardButtons(QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Ok) + self.buttons.accepted.connect(self._save) + self.buttons.rejected.connect(self.close) + self.layout.addWidget(self.buttons, row, 0, 1, 2) + + self._load_existing_hightlighters() + self.list.setCurrentItem(None) + self._selection_changed() + self.list.itemSelectionChanged.connect(self._selection_changed) + + def _selection_changed(self): + if len(self.list.selectedIndexes()) == 0: + self.btn_delete.setDisabled(True) + self.btn_move_up.setDisabled(True) + self.btn_move_down.setDisabled(True) + if len(self.list.selectedIndexes()) >= 1: + self.btn_delete.setEnabled(True) + current_row = self.list.currentRow() + self.btn_move_up.setEnabled(current_row > 0) + self.btn_move_down.setEnabled(current_row + 1 < self.list.count()) + + def _delete_selected_items(self): + for index in self.list.selectedIndexes(): + selected_index = index.row() + self.list.takeItem(selected_index) + + def _move_up(self): + row = self.list.currentRow() + item = self.list.takeItem(row) + self._add_highlight_regex_to_list(item.payload, index=row - 1, select=True) + + def _move_down(self): + row = self.list.currentRow() + item = self.list.takeItem(row) + self._add_highlight_regex_to_list(item.payload, index=row + 1, select=True) + + def _save(self): + highlighters = [] + for index in range(0, self.list.count()): + item: HighlightListItem = self.list.item(index) + highlighters.append(item.payload) + print(f"saving {item.payload.query}") + Highlighting.write_config(self._settings, highlighters) + self.close() + + def _load_existing_hightlighters(self): + highlighters: [HighlightRegex] = Highlighting.read_config(self._settings) + for highlight_regex in highlighters: + self._add_highlight_regex_to_list(highlight_regex) + + def _add_highlight_regex_to_list(self, highlight_regex: HighlightRegex, index=None, select=False): + row = HighlightListItemWidget(highlight_regex) + item = HighlightListItem(payload=highlight_regex) + if index is not None and index >= 0: + self.list.insertItem(index, item) + else: + self.list.addItem(item) + item.setSizeHint(row.minimumSizeHint()) + self.list.setItemWidget(item, row) + if select: + self.list.setCurrentItem(item) + + def _new_highlighter(self): + highlight_regex = HighlightRegex("", ignore_case=True, is_regex=True, hit_background_color="ccb400") + self._add_highlight_regex_to_list(highlight_regex, select=True) + + +class HighlightListItem(QListWidgetItem): + def __init__(self, payload: HighlightRegex): + super(HighlightListItem, self).__init__() + self.payload = payload + + +class HighlightListItemWidget(QWidget): + def __init__(self, highlight_regex: HighlightRegex): + super(HighlightListItemWidget, self).__init__() + self.highlight_regex = highlight_regex + self.layout = QGridLayout(self) + + row = 0 + query = QLineEdit(self) + query.setText(highlight_regex.query) + query.textChanged[str].connect(lambda: highlight_regex.set_query(query.text())) + self.layout.addWidget(query, row, 0, 1, 3) + + row = row + 1 + self.layout.addWidget(QLabel(_("Hit Background:")), row, 0) + hit_background_color = ColorButton(highlight_regex.hit_background_color) + hit_background_color.setToolTip(_("Hit Background")) + hit_background_color.color_drop_down.currentIndexChanged.connect( + lambda: highlight_regex.set_hit_background_color(hit_background_color.color)) + self.layout.addWidget(hit_background_color, row, 1) + + ignore_case = QCheckBox(_("Ignore Case")) + ignore_case.setChecked(highlight_regex.ignore_case) + ignore_case.stateChanged.connect(lambda: highlight_regex.set_is_ignore_case(ignore_case.isChecked())) + self.layout.addWidget(ignore_case, row, 2) + + row = row + 1 + self.layout.addWidget(QLabel(_("Line Background:")), row, 0) + line_background_color = ColorButton(highlight_regex.line_background_color) + line_background_color.setToolTip(_("Line Background")) + line_background_color.color_drop_down.currentIndexChanged.connect( + lambda: highlight_regex.set_line_background_color(line_background_color.color)) + self.layout.addWidget(line_background_color, row, 1) + + is_regex = QCheckBox(_("Regular Expression")) + is_regex.setChecked(highlight_regex.is_regex) + is_regex.stateChanged.connect(lambda: highlight_regex.set_is_regex(is_regex.isChecked())) + self.layout.addWidget(is_regex, row, 2) diff --git a/src/ui/colorbutton.py b/src/ui/colorbutton.py index 8cf8186..58f4a7d 100644 --- a/src/ui/colorbutton.py +++ b/src/ui/colorbutton.py @@ -50,7 +50,7 @@ class ColorButton(QWidget): if color == color_name or color == color_value: self.color_drop_down.setCurrentIndex(self.color_drop_down.count() - 1) - self.btn_color_picker = QPushButton(QIcon.fromTheme("color-picker"), _("custom")) + self.btn_color_picker = QPushButton(QIcon.fromTheme("color-picker"), _("")) self.layout.addWidget(self.btn_color_picker) self.btn_color_picker.pressed.connect(self._update_color) self.btn_color_picker.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)) diff --git a/src/ui/vbox.py b/src/ui/vbox.py index 69c94b1..99fafae 100644 --- a/src/ui/vbox.py +++ b/src/ui/vbox.py @@ -7,3 +7,6 @@ class VBox(QWidget): self.layout = QVBoxLayout(self) for widget in widgets: self.layout.addWidget(widget) + + def addWidget(self, widget: QWidget): + self.layout.addWidget(widget)