diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-08-16 15:37:47 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-08-17 11:27:05 +0200 |
| commit | ed4474cb3be12c1a9566c6cd7299db39e7b9a756 (patch) | |
| tree | 424e2338898953f0259299cf305d27549e432868 /tools/snippets_translate/handlers.py | |
| parent | bd3e8afdb06c8614164e4ed1f195a8618c1d586d (diff) | |
snippets_translate: Handle connect statements
Replace pairs of instance/pointer to member functions (PMF) by
the Python dot notation and connect statements afterwards.
Pick-to: 6.3 6.2
Task-number: PYSIDE-1721
Change-Id: I29f01d47026e3a7ab2407cf8c5b112533d5fb4dc
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'tools/snippets_translate/handlers.py')
| -rw-r--r-- | tools/snippets_translate/handlers.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/tools/snippets_translate/handlers.py b/tools/snippets_translate/handlers.py index b749f04b8..94d4ebe88 100644 --- a/tools/snippets_translate/handlers.py +++ b/tools/snippets_translate/handlers.py @@ -509,3 +509,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 |
