Skip to content
Draft
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
100 changes: 52 additions & 48 deletions addon/globalPlugins/webAccess/gui/rule/criteriaEditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ def getSummary_selector_unit(data) -> Sequence[str]:

def getSummary(context, data, indent="", condensed=False) -> str:
parts = []

parts.extend(getSummary_selector_full(
data.get("selector", {}), condensed=condensed
))

# Properties
subParts = []
props = Properties(context, data.get("properties", {}), iterOnlyFirstMap=True)
Expand Down Expand Up @@ -361,7 +361,7 @@ def convertToSingleNode(data):


class CriteriaEditorPanel(RuleAwarePanelBase):

def getData(self):
# Should always be initialized, as the Rule Editor populates it with at least
# the index of this Alternative Criteria Set ("criteriaIndex").
Expand All @@ -371,11 +371,11 @@ def getData(self):
class GeneralPanel(CriteriaEditorPanel):
# Translators: The label for a Criteria editor category.
title = _("General")

def __init__(self, parent):
self.hideable: Mapping[Sequence[wx.Window]] = {}
super().__init__(parent)

def makeSettings(self, settingsSizer):
scale = self.scale
gbSizer = wx.GridBagSizer()
Expand Down Expand Up @@ -463,7 +463,7 @@ def makeSettings(self, settingsSizer):
item.Show(False)

gbSizer.AddGrowableCol(2)

def initData(self, context):
super().initData(context)
self.sequenceOrderChoice.Clear()
Expand All @@ -475,9 +475,13 @@ def initData(self, context):
for item in self.hideable["order"]:
item.Show(False)
else:
for item in self.hideable["order"]:
item.Show(True)
for index in range(nbAlternatives):
self.sequenceOrderChoice.Append(str(index + 1))
index = data.get("criteriaIndex", nbAlternatives + 1)
index = data.get("criteriaIndex", nbAlternatives - 1)
if index < 0 or index >= nbAlternatives:
index = nbAlternatives - 1
self.sequenceOrderChoice.SetSelection(index)
if self.getRuleType() == ruleTypes.ZONE:
key = "convert.single" if isDualNode(data) else "convert.dual"
Expand Down Expand Up @@ -519,11 +523,11 @@ def onSave(self):

class CriteriaPanel(CriteriaEditorPanel):
"""Criteria Panel of the Alternative Criteria Set Editor

To accomodate with the Rule Creation Wizard which reuses this panel split on two different pages,
several method are split in two: "<method>_context" and "<method>_others".
"""

# Translators: The label for a Criteria editor category.
title = _("Criteria")

Expand Down Expand Up @@ -769,7 +773,7 @@ def initData(self, context):
super().initData(context)
self.initData_context(context)
self.initData_others(context)

def initData_context(self, context):
data = self.getData()
self.contextPageTitleCombo.Set([context["pageTitle"]])
Expand All @@ -790,7 +794,7 @@ def initData_context(self, context):
self.contextPageTitleCombo.Value = data.get("contextPageTitle", "")
self.contextPageTypeCombo.Value = data.get("contextPageType", "")
self.contextParentCombo.Value = data.get("contextParent", "")

def initData_others(self, context):
data = self.getData()
mgr = context["webModule"].ruleManager
Expand All @@ -804,7 +808,7 @@ def initData_others(self, context):
textChoices = [t]
if node.previousTextNode is not None:
textChoices.append("<" + node.previousTextNode.text)

roleChoices = []
tagChoices = []
idChoices = []
Expand All @@ -822,7 +826,7 @@ def initData_others(self, context):
srcChoices.append(node.src or "")
urlChoices.append(node.url or "")
node = node.parent

self.textCombo.Set(textChoices)
self.roleCombo.Set(roleChoices)
self.tagCombo.Set(tagChoices)
Expand All @@ -831,7 +835,7 @@ def initData_others(self, context):
self.statesCombo.Set(statesChoices)
self.srcCombo.Set(srcChoices)
self.urlCombo.Set(urlChoices)

self.textCombo.Value = data.get("text", "")
value = data.get("role", "")
if isinstance(value, InvalidValue):
Expand All @@ -858,13 +862,13 @@ def initData_others(self, context):
def updateData(self):
self.updateData_context()
self.updateData_others()

def updateData_context(self):
data = self.getData()
updateOrDrop(data, "contextPageTitle", self.contextPageTitleCombo.Value)
updateOrDrop(data, "contextPageType", self.contextPageTypeCombo.Value)
updateOrDrop(data, "contextParent", self.contextParentCombo.Value)

def updateData_others(self):
data = self.getData()
updateOrDrop(data, "text", self.textCombo.Value)
Expand Down Expand Up @@ -942,27 +946,27 @@ def onContextMacroChoice(self, evt):
def onTestCriteria(self, evt):
self.updateData()
testCriteria(self.context)

def onPanelActivated(self):
self.refreshContextMacroChoices()
super().onPanelActivated()

def onTestCriteria(self, evt):
self.updateData()
testCriteria(self.context)

def isValid(self):
self.updateData()
self.updateData()
return self.isValid_context() and self.isValid_others()

def isValid_context(self):
# TODO: Check the syntax of expressions
return True

def isValid_others(self):
# TODO: Check the syntax of expressions
data = self.getData()

if not data:
gui.messageBox(
# Translators: An error message on the Criteria Editor
Expand All @@ -973,7 +977,7 @@ def isValid_others(self):
)
self.SetFocus()
return False

roleLblExpr = self.roleCombo.Value
if roleLblExpr.strip():
if not EXPR.match(roleLblExpr):
Expand Down Expand Up @@ -1055,10 +1059,10 @@ def isValid_others(self):
class StartCriteriaPanel(CriteriaPanel):
# Translators: The label for a Criteria editor category.
title = _("Start Criteria")

def getData(self):
return super().getData()["start"]

def onTestCriteria(self, evt):
self.updateData()
testCriteria(self.context, restrictDualNodeTo=="start")
Expand All @@ -1067,10 +1071,10 @@ def onTestCriteria(self, evt):
class EndCriteriaPanel(CriteriaPanel):
# Translators: The label for a Criteria editor category.
title = _("End Criteria")

def getData(self):
return super().getData()["end"]

def onTestCriteria(self, evt):
self.updateData()
testCriteria(self.context, restrictDualNodeTo="end")
Expand All @@ -1083,20 +1087,20 @@ class GesturesPanel(GesturesPanelBase, CriteriaEditorPanel):
class PropertyOverrideSelectMenu(wx.Menu):
"""Menu to select a property to override on the CriteriaPropertiesPanel
"""

def __init__(self, menuIdProps: Mapping[int, Property]):
super().__init__(title=_("Select a property to override"))
for menuId, prop in menuIdProps.items():
self.Append(menuId, prop.displayName)


class PropertiesPanel(PropertiesPanelBase, CriteriaEditorPanel):

def makeSettings(self, settingsSizer):
super().makeSettings(settingsSizer)
scale = self.scale
gbSizer = self.gbSizer

# Translators: The label for a list on the Criteria Editor dialog
self.listLabel.Label = _("Properties specific to this criteria set")
listCtrl = self.listCtrl
Expand Down Expand Up @@ -1125,7 +1129,7 @@ def makeSettings(self, settingsSizer):
item.Enable(False)
item.Bind(wx.EVT_BUTTON, self.onDelPropBtn)
gbSizer.Add(item, pos=(row, col))

# Called by PropertiesPanelBase.initData
def initData_properties(self):
context = self.context
Expand All @@ -1135,7 +1139,7 @@ def initData_properties(self):
context["data"]["rule"].setdefault("properties", {}),
iterOnlyFirstMap=True,
)

def listCtrl_insert(self, index: int, prop: Property) -> None:
super().listCtrl_insert(index, prop)
self.listCtrl.SetStringItem(index, 2, prop.displayDefault)
Expand Down Expand Up @@ -1179,14 +1183,14 @@ def onAddPropBtn(self, evt):
self.editor.SetFocus()
else:
self.listCtrl.SetFocus()

@guarded
def onDelPropBtn(self, evt):
self.prop.reset()
self.listCtrl_update_all()
if not self.props:
self.addPropBtn.SetFocus()

def prop_reset(self):
# Using Property.reset would actually remove the overridden property from the list.
# Manually reset to its default instead.
Expand All @@ -1205,7 +1209,7 @@ class CriteriaEditorDialog(ContextualMultiCategorySettingsDialog):
title = _("WebAccess Criteria Set editor")
categoryClasses = [GeneralPanel, CriteriaPanel, GesturesPanel, PropertiesPanel]
INITIAL_SIZE = (900, 580)

def __init__(
self,
parent,
Expand All @@ -1225,12 +1229,12 @@ def __init__(
catList.append(GesturesPanel)
catList.append(PropertiesPanel)
super().__init__(parent, *args, **kwargs)

def getData(self):
# Should always be initialized, as the Rule Editor populates it with at least
# the index of this Alternative Criteria Set ("criteriaIndex").
return self.context["data"]["criteria"]

def initData(self, context):
super().initData(context)
reload = context.pop("CriteriaEditorFocusOnReload", None)
Expand All @@ -1245,10 +1249,10 @@ def initData(self, context):
catList.SetFocus()
else:
self.currentCategory.SetFocus()

def makeSettings(self, settingsSizer):
super().makeSettings(settingsSizer)

def onCharHook(self, evt):
# Bound by MultiCategorySettingsDialog.makeSettings
keycode = evt.GetKeyCode()
Expand Down Expand Up @@ -1276,11 +1280,11 @@ def onCharHook(self, evt):
self.switchToFullEditor()
return
super().onCharHook(evt)

def onConvertToDualNode(self, evt):
convertToDualNode(self.getData())
self.EndModal(wx.ID_CONVERT)

def onConvertToSingleNode(self, evt):
if gui.messageBox(
_(
Expand All @@ -1296,7 +1300,7 @@ def onConvertToSingleNode(self, evt):
return
convertToSingleNode(self.getData())
self.EndModal(wx.ID_CONVERT)

def switchToFullEditor(self):
if not self.simpleMode:
wx.Bell()
Expand All @@ -1308,19 +1312,19 @@ def switchToFullEditor(self):
"catList.focus": self.catListCtrl.HasFocus(),
}
self.EndModal(wx.ID_MORE)

def _validateAllPanels(self):
# Ensure all panels are loaded, hence validated
for catId in range(len(self.categoryClasses)):
self._getCategoryPanel(catId)
super()._validateAllPanels()

def _saveAllPanels(self):
super()._saveAllPanels()

critData = self.getData()
ruleData = self.context["data"]["rule"]

if set(critData.get("selector", {}).keys()) == {"start", "end"} and (
any(
key == "mutation" and value
Expand All @@ -1343,7 +1347,7 @@ def _saveAllPanels(self):
style=wx.ICON_WARNING | wx.YES_NO | wx.CANCEL | wx.NO_DEFAULT
) != wx.YES:
raise ValidationError() # Cancels closing of the dialog

if createMissingSubModule(self.context, self.getData(), self) is False:
raise ValidationError() # Cancels closing of the dialog

Expand Down