add new highlight dialog
This commit is contained in:
@@ -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 = []
|
||||
|
||||
|
||||
166
src/ui/bigtext/newhighlightingdialog.py
Normal file
166
src/ui/bigtext/newhighlightingdialog.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user