From bb61368c7cdd6c86ee3830455f136ec836bb00dd Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 16 Aug 2022 15:37:47 +0200 Subject: snippets_translate: Handle connect statements Replace pairs of instance/pointer to member functions (PMF) by the Python dot notation and connect statements afterwards. Task-number: PYSIDE-1721 Change-Id: I29f01d47026e3a7ab2407cf8c5b112533d5fb4dc Reviewed-by: Christian Tismer (cherry picked from commit ed4474cb3be12c1a9566c6cd7299db39e7b9a756) Reviewed-by: Qt Cherry-pick Bot --- tools/snippets_translate/converter.py | 6 +++- tools/snippets_translate/handlers.py | 46 ++++++++++++++++++++++++ tools/snippets_translate/tests/test_converter.py | 4 +-- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/tools/snippets_translate/converter.py b/tools/snippets_translate/converter.py index 01df94505..77250a857 100644 --- a/tools/snippets_translate/converter.py +++ b/tools/snippets_translate/converter.py @@ -46,7 +46,7 @@ from handlers import (handle_array_declarations, handle_casts, handle_class, handle_inc_dec, handle_include, handle_keywords, handle_methods_return_type, handle_negate, handle_type_var_declaration, handle_useless_qt_classes, - handle_void_functions) + handle_void_functions, handle_qt_connects) from parse_utils import dstrip, get_indent, remove_ref @@ -76,6 +76,10 @@ def snippet_translate(x): x = x.replace("//", "#", 1) return x + qt_connects = handle_qt_connects(x) + if qt_connects: + return qt_connects + # Handle "->" if "->" in x: x = x.replace("->", ".") diff --git a/tools/snippets_translate/handlers.py b/tools/snippets_translate/handlers.py index 40ba71841..72c70eef8 100644 --- a/tools/snippets_translate/handlers.py +++ b/tools/snippets_translate/handlers.py @@ -545,3 +545,49 @@ def handle_useless_qt_classes(x): if content: x = x.replace(content.group(0), content.group(1)) return x + + +# The code below handles pairs of instance/pointer to member functions (PMF) +# which appear in Qt in connect statements like: +# "connect(fontButton, &QAbstractButton::clicked, this, &Dialog::setFont)". +# In a first pass, these pairs are replaced by: +# "connect(fontButton.clicked, self.setFont)" to be able to handle statements +# spanning lines. A 2nd pass then checks for the presence of a connect +# statement and replaces it by: +# "fontButton.clicked.connect(self.setFont)". +# To be called right after checking for comments. + + +INSTANCE_PMF_RE = re.compile(r"&?(\w+),\s*&\w+::(\w+)") + + +CONNECT_RE = re.compile(r"^(\s*)(QObject::)?connect\((\w+\.\w+),\s*") + + +def handle_qt_connects(line): + if not INSTANCE_PMF_RE.search(line): + return None + # 1st pass, "fontButton, &QAbstractButton::clicked" -> "fontButton.clicked" + last_pos = 0 + result = "" + for match in INSTANCE_PMF_RE.finditer(line): + instance = match.group(1) + if instance == "this": + instance = "self" + member_fun = match.group(2) + next_pos = match.start() + result += line[last_pos:next_pos] + last_pos = match.end() + result += f"{instance}.{member_fun}" + result += line[last_pos:] + + # 2nd pass, reorder connect. + connect_match = CONNECT_RE.match(result) + if not connect_match: + return result + + space = connect_match.group(1) + signal_ = connect_match.group(3) + connect_stmt = f"{space}{signal_}.connect(" + connect_stmt += result[connect_match.end():] + return connect_stmt diff --git a/tools/snippets_translate/tests/test_converter.py b/tools/snippets_translate/tests/test_converter.py index b2d6468d8..2c127a7ba 100644 --- a/tools/snippets_translate/tests/test_converter.py +++ b/tools/snippets_translate/tests/test_converter.py @@ -137,7 +137,7 @@ def test_double_colon(): # multiline statement connect statement # eg: connect(reply, &QNetworkReply::errorOccurred, # this, &MyClass::slotError); - assert st("this, &MyClass::slotError);") == "self, MyClass.slotError)" + assert st("this, &MyClass::slotError);") == "self.slotError)" def test_cout_endl(): @@ -411,7 +411,7 @@ def test_special_cases(): ) assert ( st("QObject::connect(&window1, &Window::messageSent,") - == "QObject.connect(window1, Window.messageSent," + == "window1.messageSent.connect(" ) assert st("double num;") == "num = float()" -- cgit v1.2.3