Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move "refresh rules" in a context menu #344

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 5 additions & 41 deletions zxlive/proof_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

import pyzx
from PySide6.QtCore import QPointF, QSize
from PySide6.QtGui import QAction, QFontInfo, QIcon, QVector2D
from PySide6.QtWidgets import (QAbstractItemView, QInputDialog, QToolButton,
QTreeView)
from PySide6.QtGui import QAction, QIcon, QVector2D
from PySide6.QtWidgets import QInputDialog, QToolButton
from pyzx import VertexType, basicrules
from pyzx.graph.jsonparser import string_to_phase
from pyzx.utils import (EdgeType, FractionLike, get_w_partner, get_z_box_label,
Expand All @@ -23,8 +22,7 @@
from .graphscene import GraphScene
from .graphview import GraphTool, ProofGraphView, WandTrace
from .proof import ProofModel, ProofStepView
from .rewrite_action import RewriteActionTreeModel
from .rewrite_data import action_groups, refresh_custom_rules
from .rewrite_action import RewriteActionTreeView
from .settings import display_setting
from .sfx import SFXEnum
from .vitem import SCALE, W_INPUT_OFFSET, DragState, VItem
Expand All @@ -43,9 +41,8 @@ def __init__(self, graph: GraphT, *actions: QAction) -> None:
self.splitter.addWidget(self.graph_view)
self.graph_view.set_graph(graph)

self.rewrites_panel = QTreeView(self)
self.rewrites_panel = RewriteActionTreeView(self)
self.splitter.insertWidget(0, self.rewrites_panel)
self.init_rewrites_bar()

self.graph_view.wand_trace_finished.connect(self._wand_trace_finished)
self.graph_scene.vertex_dragged.connect(self._vertex_dragged)
Expand Down Expand Up @@ -89,37 +86,12 @@ def _toolbar_sections(self) -> Iterator[ToolbarSection]:
self.identity_choice[1].setText("X")
self.identity_choice[1].setCheckable(True)

self.refresh_rules = QToolButton(self)
self.refresh_rules.setText("Refresh rules")
self.refresh_rules.clicked.connect(self._refresh_rewrites_model)

yield ToolbarSection(*self.identity_choice, exclusive=True)
yield ToolbarSection(*self.actions())
yield ToolbarSection(self.refresh_rules)

def init_rewrites_bar(self) -> None:
self.reset_rewrite_panel_style()
self._refresh_rewrites_model()

def reset_rewrite_panel_style(self) -> None:
self.rewrites_panel.setUniformRowHeights(True)
self.rewrites_panel.setSelectionMode(QAbstractItemView.SelectionMode.NoSelection)
self.rewrites_panel.setStyleSheet(
f'''
QTreeView::Item:hover {{
background-color: #e2f4ff;
}}
QTreeView::Item{{
height:{display_setting.font.pointSizeF() * 2.5}px;
}}
QTreeView::Item:!enabled {{
color: #c0c0c0;
}}
''')

def update_font(self) -> None:
self.rewrites_panel.setFont(display_setting.font)
self.reset_rewrite_panel_style()
self.rewrites_panel.reset_rewrite_panel_style()
super().update_font()

def parse_selection(self) -> tuple[list[VT], list[ET]]:
Expand Down Expand Up @@ -430,11 +402,3 @@ def _vert_double_clicked(self, v: VT) -> None:
basicrules.color_change(new_g, v)
cmd = AddRewriteStep(self.graph_view, new_g, self.step_view, "color change")
self.undo_stack.push(cmd)

def _refresh_rewrites_model(self) -> None:
refresh_custom_rules()
model = RewriteActionTreeModel.from_dict(action_groups, self)
self.rewrites_panel.setModel(model)
self.rewrites_panel.expand(model.index(0,0))
self.rewrites_panel.clicked.connect(model.do_rewrite)
self.graph_scene.selection_changed_custom.connect(lambda: model.executor.submit(model.update_on_selection))
43 changes: 42 additions & 1 deletion zxlive/rewrite_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
from PySide6.QtCore import (Qt, QAbstractItemModel, QModelIndex, QPersistentModelIndex,
Signal, QObject, QMetaObject, QIODevice, QBuffer, QPoint, QPointF, QLineF)
from PySide6.QtGui import QPixmap, QColor, QPen
from PySide6.QtWidgets import QAbstractItemView, QMenu, QTreeView


from .animations import make_animation
from .commands import AddRewriteStep
from .common import ET, GraphT, VT, get_data
from .dialogs import show_error_msg
from .rewrite_data import is_rewrite_data, RewriteData, MatchType, MATCHES_VERTICES
from .rewrite_data import is_rewrite_data, RewriteData, MatchType, MATCHES_VERTICES, refresh_custom_rules, action_groups
from .settings import display_setting
from .graphscene import GraphScene
from .graphview import GraphView
Expand Down Expand Up @@ -305,3 +306,43 @@ def update_on_selection(self) -> None:
g = self.proof_panel.graph_scene.g
self.root_item.update_on_selection(g, selection, edges)
QMetaObject.invokeMethod(self.emitter, "finished", Qt.ConnectionType.QueuedConnection) # type: ignore

class RewriteActionTreeView(QTreeView):
def __init__(self, parent: 'ProofPanel'):
super().__init__(parent)
self.proof_panel = parent
self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_context_menu)
self.reset_rewrite_panel_style()
self.refresh_rewrites_model()

def reset_rewrite_panel_style(self) -> None:
self.setUniformRowHeights(True)
self.setSelectionMode(QAbstractItemView.SelectionMode.NoSelection)
self.setStyleSheet(
f'''
QTreeView::Item:hover {{
background-color: #e2f4ff;
}}
QTreeView::Item{{
height:{display_setting.font.pointSizeF() * 2.5}px;
}}
QTreeView::Item:!enabled {{
color: #c0c0c0;
}}
''')

def show_context_menu(self, position: QPoint) -> None:
context_menu = QMenu(self)
refresh_rules = context_menu.addAction("Refresh rules")
action = context_menu.exec_(self.mapToGlobal(position))
if action == refresh_rules:
self.refresh_rewrites_model()

def refresh_rewrites_model(self) -> None:
refresh_custom_rules()
model = RewriteActionTreeModel.from_dict(action_groups, self.proof_panel)
self.setModel(model)
self.expand(model.index(0,0))
self.clicked.connect(model.do_rewrite)
self.proof_panel.graph_scene.selection_changed_custom.connect(lambda: model.executor.submit(model.update_on_selection))
Loading