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(720) self.setMinimumHeight(600) self._settings = settings self.layout = QGridLayout(self) row = 0 btn_new = QPushButton(Icon.fromTheme("list-add", "icons/myicons/list-add.svg"), _("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", "icons/myicons/list-remove.svg"), _("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", "icons/myicons/go-up.svg"), _("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", "icons/myicons/go-down.svg"), _("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", line_background_color="fff080") 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) self.layout.setColumnStretch(4, 1) row = 0 self.active = QCheckBox("") self.active.setChecked(highlight_regex.is_active()) self.active.stateChanged.connect(self._change_active_state) self.layout.addWidget(self.active, row, 0, 3, 1, alignment=QtCore.Qt.AlignmentFlag.AlignVCenter) query = QLineEdit(self) query.setText(highlight_regex.query) query.textChanged[str].connect(lambda: highlight_regex.set_query(query.text())) query.setEnabled(highlight_regex.is_active()) self.layout.addWidget(query, row, 1, 1, 4) row = row + 1 hit_background_label = QLabel(_("Hit Background:")) hit_background_label.setEnabled(highlight_regex.is_active()) self.layout.addWidget(hit_background_label, row, 1) hit_background_color = ColorButton(highlight_regex.hit_background_color) hit_background_color.setToolTip(_("Hit Background")) hit_background_color.setEnabled(highlight_regex.is_active()) 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, 2) 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())) ignore_case.setEnabled(highlight_regex.is_active()) self.layout.addWidget(ignore_case, row, 3) row = row + 1 line_background_label = QLabel(_("Line Background:")) line_background_label.setEnabled(highlight_regex.is_active()) self.layout.addWidget(line_background_label, row, 1) 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)) line_background_color.setEnabled(highlight_regex.is_active()) self.layout.addWidget(line_background_color, row, 2) 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())) is_regex.setEnabled(highlight_regex.is_active()) self.layout.addWidget(is_regex, row, 3) def _change_active_state(self): active = self.active.isChecked() self.highlight_regex.set_active(active) for child in self.children(): if child is not self.active: child.setEnabled(active)