Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
generate_ui.py
Go to the documentation of this file.
1#!/usr/bin/python3
2
3# Copyright (C) 2023 The Qt Company Ltd.
4# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
5
6import os
7import subprocess
8
9from argparse import ArgumentParser, RawTextHelpFormatter
10from pathlib import Path
11from tempfile import NamedTemporaryFile
12
13
14DESCRIPTION = """
15Usage: generate_ui.py
16
17Generates the source files ui4.cpp, ui4.h used in the uic tool, the QtUiTools library and
18Qt Widgets Designer from the XML schema used for .ui files.
19
20Requires xalan.
21"""
22
23
24opt_delete_temp_files = True
25
26
28 """Read out the license from a C++ source"""
29 result = ""
30 for line in path.read_text().splitlines():
31 result += line + "\n"
32 if 'SPDX-License-Identifier' in line:
33 break
34 return result
35
36
37def replace_xsl_keys(xsl_source_file, license, ui_header_name=None):
38 """Replace special keys in XSL files and return a handle to temporary file"""
39 xsl = xsl_source_file.read_text()
40 xsl = xsl.replace("@LICENSE@", license)
41 if ui_header_name:
42 xsl = xsl.replace("@HEADER@", ui_header_name)
43
44 result = NamedTemporaryFile(mode='w', suffix='.xsl',
45 dir=Path.cwd(),
46 delete=opt_delete_temp_files)
47 result.write(xsl)
48 return result
49
50
51def run_xslt(source, sheet, target):
52 """Run xalan."""
53 cmd = ['xalan', '-in', os.fspath(source), '-xsl', os.fspath(sheet),
54 '-out', os.fspath(target)]
55 subprocess.check_call(cmd)
56
57
58if __name__ == '__main__':
59 argument_parser = ArgumentParser(description=DESCRIPTION,
60 formatter_class=RawTextHelpFormatter)
61 argument_parser.add_argument('--keep', '-k', action='store_true',
62 help='Keep temporary files')
63 options = argument_parser.parse_args()
64 opt_delete_temp_files = not options.keep
65
66 # Generate uilib header and source.
67 xml_dir = Path(__file__).parent.resolve()
68 ui4_xsd = xml_dir / 'ui4.xsd'
69
70 designer_dir = xml_dir.parent
71 uilib_dir = designer_dir / "src" / "lib" / "uilib"
72 uilib_impl = uilib_dir / 'ui4.cpp'
73 license = read_cpp_license(uilib_impl)
74
75 print("Running XSLT processor for uilib header...\n")
76 header_xsl_source = xml_dir / 'generate_header.xsl'
77 header_xsl = replace_xsl_keys(header_xsl_source, license)
78 run_xslt(ui4_xsd, header_xsl.name, uilib_dir / 'ui4_p.h')
79
80 print("Running XSLT processor for uilib source...\n")
81 impl_xsl_source = xml_dir / 'generate_impl.xsl'
82 impl_xsl = replace_xsl_keys(impl_xsl_source, license, 'ui4_p.h')
83 run_xslt(ui4_xsd, impl_xsl.name, uilib_impl)
84
85 # uic: Header is called 'ui4.h' instead of 'ui4_p.h'
86 uic_dir = designer_dir.parents[2] / "qtbase" / "src" / "tools" / "uic"
87 uic_impl = uic_dir / 'ui4.cpp'
88 license = read_cpp_license(uic_impl)
89 print("Running XSLT processor for uic header...\n")
90 header_xsl = replace_xsl_keys(header_xsl_source, license)
91 run_xslt(ui4_xsd, header_xsl.name, uic_dir / 'ui4.h')
92 print("Running XSLT processor for uic source...\n")
93 impl_xsl = replace_xsl_keys(impl_xsl_source, license, 'ui4.h')
94 run_xslt(ui4_xsd, impl_xsl.name, uic_impl)
95
96 subprocess.call(['git', 'diff'])
replace_xsl_keys(xsl_source_file, license, ui_header_name=None)
run_xslt(source, sheet, target)
read_cpp_license(path)
QDebug print(QDebug debug, QSslError::SslError error)