summaryrefslogtreecommitdiffstats
path: root/src/designer/data/generate_ui.py
blob: 05472c7f5baf5613ad2e3d7b92bc51aee35455ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python3

# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import os
import subprocess

from argparse import ArgumentParser, RawTextHelpFormatter
from pathlib import Path
from tempfile import NamedTemporaryFile


DESCRIPTION = """
Usage: generate_ui.py

Generates the source files ui4.cpp, ui4.h used in the uic tool, the QtUiTools library and
Qt Widgets Designer from the XML schema used for .ui files.

Requires xalan.
"""


opt_delete_temp_files = True


def read_cpp_license(path):
    """Read out the license from a C++ source"""
    result = ""
    for line in path.read_text().splitlines():
        result += line + "\n"
        if 'SPDX-License-Identifier' in line:
            break
    return result


def replace_xsl_keys(xsl_source_file, license, ui_header_name=None):
    """Replace special keys in XSL files and return a handle to temporary file"""
    xsl = xsl_source_file.read_text()
    xsl = xsl.replace("@LICENSE@", license)
    if ui_header_name:
        xsl = xsl.replace("@HEADER@", ui_header_name)

    result = NamedTemporaryFile(mode='w', suffix='.xsl',
                                dir=Path.cwd(),
                                delete=opt_delete_temp_files)
    result.write(xsl)
    return result


def run_xslt(source, sheet, target):
    """Run xalan."""
    cmd = ['xalan', '-in', os.fspath(source), '-xsl', os.fspath(sheet),
           '-out', os.fspath(target)]
    subprocess.check_call(cmd)


if __name__ == '__main__':
    argument_parser = ArgumentParser(description=DESCRIPTION,
                                     formatter_class=RawTextHelpFormatter)
    argument_parser.add_argument('--keep', '-k', action='store_true',
                                 help='Keep temporary files')
    options = argument_parser.parse_args()
    opt_delete_temp_files = not options.keep

    # Generate uilib header and source.
    xml_dir = Path(__file__).parent.resolve()
    ui4_xsd = xml_dir / 'ui4.xsd'

    designer_dir = xml_dir.parent
    uilib_dir = designer_dir / "src" / "lib" / "uilib"
    uilib_impl = uilib_dir / 'ui4.cpp'
    license = read_cpp_license(uilib_impl)

    print("Running XSLT processor for uilib header...\n")
    header_xsl_source = xml_dir / 'generate_header.xsl'
    header_xsl = replace_xsl_keys(header_xsl_source, license)
    run_xslt(ui4_xsd, header_xsl.name, uilib_dir / 'ui4_p.h')

    print("Running XSLT processor for uilib source...\n")
    impl_xsl_source = xml_dir / 'generate_impl.xsl'
    impl_xsl = replace_xsl_keys(impl_xsl_source, license, 'ui4_p.h')
    run_xslt(ui4_xsd, impl_xsl.name, uilib_impl)

    # uic: Header is called 'ui4.h' instead of 'ui4_p.h'
    uic_dir = designer_dir.parents[2] / "qtbase" / "src" / "tools" / "uic"
    uic_impl = uic_dir / 'ui4.cpp'
    license = read_cpp_license(uic_impl)
    print("Running XSLT processor for uic header...\n")
    header_xsl = replace_xsl_keys(header_xsl_source, license)
    run_xslt(ui4_xsd, header_xsl.name, uic_dir / 'ui4.h')
    print("Running XSLT processor for uic source...\n")
    impl_xsl = replace_xsl_keys(impl_xsl_source, license, 'ui4.h')
    run_xslt(ui4_xsd, impl_xsl.name, uic_impl)

    subprocess.call(['git', 'diff'])