| 1 |
# response_id.py |
| 2 |
# |
| 3 |
# Copyright 2022 Gleb Smirnov <[email protected]> |
| 4 |
# |
| 5 |
# This file is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU Lesser General Public License as |
| 7 |
# published by the Free Software Foundation; either version 3 of the |
| 8 |
# License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This file is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# Lesser General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public |
| 16 |
# License along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
# SPDX-License-Identifier: LGPL-3.0-or-later |
| 19 |
|
| 20 |
|
| 21 |
import typing as T |
| 22 |
|
| 23 |
from .common import * |
| 24 |
|
| 25 |
|
| 26 |
class ExtResponse(AstNode): |
| 27 |
"""Response ID of action widget.""" |
| 28 |
|
| 29 |
ALLOWED_PARENTS: T.List[T.Tuple[str, str]] = [("Gtk", "Dialog"), ("Gtk", "InfoBar")] |
| 30 |
|
| 31 |
grammar = [ |
| 32 |
Keyword("action"), |
| 33 |
Keyword("response"), |
| 34 |
"=", |
| 35 |
AnyOf( |
| 36 |
UseIdent("response_id"), |
| 37 |
[ |
| 38 |
Optional(UseExact("sign", "-")), |
| 39 |
UseNumber("response_id"), |
| 40 |
], |
| 41 |
), |
| 42 |
Optional([Keyword("default"), UseLiteral("is_default", True)]), |
| 43 |
] |
| 44 |
|
| 45 |
@validate() |
| 46 |
def parent_has_action_widgets(self) -> None: |
| 47 |
"""Chech that parent widget has allowed type.""" |
| 48 |
from .gobject_object import Object |
| 49 |
|
| 50 |
container_type = self.parent_by_type(Object).gir_class |
| 51 |
if container_type is None: |
| 52 |
return |
| 53 |
|
| 54 |
gir = self.root.gir |
| 55 |
|
| 56 |
for namespace, name in ExtResponse.ALLOWED_PARENTS: |
| 57 |
parent_type = gir.get_type(name, namespace) |
| 58 |
if container_type.assignable_to(parent_type): |
| 59 |
break |
| 60 |
else: |
| 61 |
raise CompileError( |
| 62 |
f"{container_type.full_name} doesn't have action widgets" |
| 63 |
) |
| 64 |
|
| 65 |
@validate() |
| 66 |
def widget_have_id(self) -> None: |
| 67 |
"""Check that action widget have ID.""" |
| 68 |
from .gtkbuilder_child import Child |
| 69 |
|
| 70 |
object = self.parent_by_type(Child).object |
| 71 |
if object.id is None: |
| 72 |
raise CompileError(f"Action widget must have ID") |
| 73 |
|
| 74 |
@validate("response_id") |
| 75 |
def correct_response_type(self) -> None: |
| 76 |
"""Validate response type. |
| 77 |
|
| 78 |
Response type might be GtkResponseType member |
| 79 |
or positive number. |
| 80 |
""" |
| 81 |
gir = self.root.gir |
| 82 |
response = self.tokens["response_id"] |
| 83 |
|
| 84 |
if self.tokens["sign"] == "-": |
| 85 |
raise CompileError("Numeric response type can't be negative") |
| 86 |
|
| 87 |
if isinstance(response, float): |
| 88 |
raise CompileError( |
| 89 |
"Response type must be GtkResponseType member or integer," " not float" |
| 90 |
) |
| 91 |
elif not isinstance(response, int): |
| 92 |
responses = gir.get_type("ResponseType", "Gtk").members.keys() |
| 93 |
if response not in responses: |
| 94 |
raise CompileError(f'Response type "{response}" doesn\'t exist') |
| 95 |
|
| 96 |
@validate("default") |
| 97 |
def no_multiple_default(self) -> None: |
| 98 |
"""Only one action widget in dialog can be default.""" |
| 99 |
from .gobject_object import Object |
| 100 |
|
| 101 |
if not self.is_default: |
| 102 |
return |
| 103 |
|
| 104 |
action_widgets = self.parent_by_type(Object).action_widgets |
| 105 |
for widget in action_widgets: |
| 106 |
if widget == self: |
| 107 |
break |
| 108 |
if widget.tokens["is_default"]: |
| 109 |
raise CompileError("Default response is already set") |
| 110 |
|
| 111 |
@property |
| 112 |
def response_id(self) -> str: |
| 113 |
return self.tokens["response_id"] |
| 114 |
|
| 115 |
@property |
| 116 |
def is_default(self) -> bool: |
| 117 |
return self.tokens["is_default"] or False |
| 118 |
|
| 119 |
@property |
| 120 |
def widget_id(self) -> str: |
| 121 |
"""Get action widget ID.""" |
| 122 |
from .gtkbuilder_child import Child |
| 123 |
|
| 124 |
object = self.parent_by_type(Child).object |
| 125 |
return object.id |
| 126 |
|
| 127 |
@docs() |
| 128 |
def ref_docs(self): |
| 129 |
return get_docs_section("Syntax ExtResponse") |
| 130 |
|
| 131 |
@docs("response_id") |
| 132 |
def response_id_docs(self): |
| 133 |
if enum := self.root.gir.get_type("ResponseType", "Gtk"): |
| 134 |
if member := enum.members.get(self.response_id, None): |
| 135 |
return member.doc |
| 136 |
|
| 137 |
|
| 138 |
def decompile_response_type(parent_element, child_element): |
| 139 |
obj_id = None |
| 140 |
for obj in child_element.children: |
| 141 |
if obj.tag == "object": |
| 142 |
obj_id = obj["id"] |
| 143 |
break |
| 144 |
|
| 145 |
if obj_id is None: |
| 146 |
return None |
| 147 |
|
| 148 |
for child in parent_element.children: |
| 149 |
if child.tag == "action-widgets": |
| 150 |
for action_widget in child.children: |
| 151 |
if action_widget.cdata == obj_id: |
| 152 |
response_id = action_widget["response"] |
| 153 |
is_default = ( |
| 154 |
" default" if decompile.truthy(action_widget["default"]) else "" |
| 155 |
) |
| 156 |
return f"[action response={response_id}{is_default}]" |
| 157 |
|
| 158 |
return None |
| 159 |
|
| 160 |
|
| 161 |
@decompiler("action-widgets", skip_children=True) |
| 162 |
def decompile_action_widgets(ctx, gir): |
| 163 |
# This is handled in the <child> decompiler and decompile_response_type above |
| 164 |
pass |