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 lines = path.read_text().splitlines()
31 for i in range(len(lines)):
32 result += lines[i] + "\n"
33 if 'Qt-Security' in lines[i]:
34 break
35 if 'SPDX-License-Identifier' in lines[i] and not 'Qt-Security' in lines[i+1]:
36 break
37 return result
38
39
40def replace_xsl_keys(xsl_source_file, license, ui_header_name=None):
41 """Replace special keys in XSL files and return a handle to temporary file"""
42 xsl = xsl_source_file.read_text()
43 xsl = xsl.replace("@LICENSE@", license)
44 if ui_header_name:
45 xsl = xsl.replace("@HEADER@", ui_header_name)
46
47 result = NamedTemporaryFile(mode='w', suffix='.xsl',
48 dir=Path.cwd(),
49 delete=opt_delete_temp_files)
50 result.write(xsl)
51 return result
52
53
54def run_xslt(source, sheet, target):
55 """Run xalan."""
56 cmd = ['xalan', '-in', os.fspath(source), '-xsl', os.fspath(sheet),
57 '-out', os.fspath(target)]
58 subprocess.check_call(cmd)
59
60
61if __name__ == '__main__':
62 argument_parser = ArgumentParser(description=DESCRIPTION,
63 formatter_class=RawTextHelpFormatter)
64 argument_parser.add_argument('--keep', '-k', action='store_true',
65 help='Keep temporary files')
66 options = argument_parser.parse_args()
67 opt_delete_temp_files = not options.keep
68
69 # Generate uilib header and source.
70 xml_dir = Path(__file__).parent.resolve()
71 ui4_xsd = xml_dir / 'ui4.xsd'
72
73 designer_dir = xml_dir.parent
74 uilib_dir = designer_dir / "src" / "lib" / "uilib"
75 uilib_impl = uilib_dir / 'ui4.cpp'
76 license = read_cpp_license(uilib_impl)
77
78 print("Running XSLT processor for uilib header...\n")
79 header_xsl_source = xml_dir / 'generate_header.xsl'
80 header_xsl = replace_xsl_keys(header_xsl_source, license)
81 run_xslt(ui4_xsd, header_xsl.name, uilib_dir / 'ui4_p.h')
82
83 print("Running XSLT processor for uilib source...\n")
84 impl_xsl_source = xml_dir / 'generate_impl.xsl'
85 impl_xsl = replace_xsl_keys(impl_xsl_source, license, 'ui4_p.h')
86 run_xslt(ui4_xsd, impl_xsl.name, uilib_impl)
87
88 # uic: Header is called 'ui4.h' instead of 'ui4_p.h'
89 uic_dir = designer_dir.parents[2] / "qtbase" / "src" / "tools" / "uic"
90 uic_impl = uic_dir / 'ui4.cpp'
91 license = read_cpp_license(uic_impl)
92 print("Running XSLT processor for uic header...\n")
93 header_xsl = replace_xsl_keys(header_xsl_source, license)
94 run_xslt(ui4_xsd, header_xsl.name, uic_dir / 'ui4.h')
95 print("Running XSLT processor for uic source...\n")
96 impl_xsl = replace_xsl_keys(impl_xsl_source, license, 'ui4.h')
97 run_xslt(ui4_xsd, impl_xsl.name, uic_impl)
98
99 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)