Skip to content

Commit

Permalink
Merge pull request #344 from zxcalc/refresh-rules-context-menu
Browse files Browse the repository at this point in the history
Move "refresh rules" in a context menu
  • Loading branch information
RazinShaikh authored Jul 31, 2024
2 parents fd3d710 + 49f4b82 commit dc012fb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
46 changes: 5 additions & 41 deletions zxlive/proof_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,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 @@ -24,8 +23,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 @@ -44,9 +42,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 @@ -90,37 +87,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 @@ -438,11 +410,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))

0 comments on commit dc012fb

Please sign in to comment.