File: generate-ApplicationPalette-class.py

package info (click to toggle)
mumble 1.5.735-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 90,008 kB
  • sloc: cpp: 556,921; ansic: 81,662; python: 3,606; sh: 659; makefile: 506; asm: 371; cs: 306; sql: 228; javascript: 143; perl: 80; xml: 13
file content (140 lines) | stat: -rwxr-xr-x 4,579 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2014-2023 The Mumble Developers. All rights reserved.
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file at the root of the
# Mumble source tree or at <https://www.mumble.info/LICENSE>.

# Generates the Mumble palette generation class from
# template_source and writes it into target after
# template expansion.

import argparse
import os

color_group = ["Active", "Disabled", "Inactive"]
color_role = [ "WindowText", "Button", "Light", "Midlight", "Dark", "Mid",
              "Text", "BrightText", "ButtonText", "Base", "Window", "Shadow",
              "Highlight", "HighlightedText",
              "Link", "LinkVisited",
              "AlternateBase",
              "ToolTipBase", "ToolTipText"]

property_template = """		Q_PROPERTY(QBrush %(prop)s READ get_%(prop)s WRITE set_%(prop)s)
"""

gettersetter_template = """
		QBrush get_%(prop)s() {
			if (!m_%(prop)s)
				return QBrush();

			return *m_%(prop)s;
		}

		void set_%(prop)s(const QBrush& brush) {
			m_%(prop)s = brush;
		}
"""

role_multigettersetter_template = """
	QBrush get_%(role)s() {
		if (%(comparators)s) {
			return %(representative)s;
		}
		return QBrush();
	}

	void set_%(role)s(const QBrush& brush) {
%(setters)s
	}
"""

role_multisetter_template="""		m_%(prop)s = brush;
"""


paletteupdate_template ="""
			if (m_%(prop)s) {
				newPalette.setBrush(QPalette::%(group)s, QPalette::%(role)s, *m_%(prop)s);
			}
"""

variable_template = """		boost::optional<QBrush> m_%(prop)s;
"""

reset_template = """			m_%(prop)s = boost::none;
"""

def rolename(role):
    return role.lower()

def groupname(group):
    return group.lower()

def propname(role, group):
    return rolename(role) + "_" + groupname(group)


def add_role_property(variables, role):
    """
    Add a property that sets all color groups to the same QBrush using
    the setters of the single group properties.
    """
    variables["properties"] += property_template % {"prop": rolename(role)}
    # Build a comparator that checks whether all properties
    # are equal and can be represented as one QBrush.
    all_groups_equal = " && ".join(
        ['property("%s") == property("%s")' % (propname(role, color_group[0]), propname(role, color_group[i])) for i in
         range(1, len(color_group))])
    role_representative = 'qvariant_cast<QBrush>(property("%s"))' % propname(role, color_group[0])
    role_multisetters = "".join([role_multisetter_template % {"prop": propname(role, group)} for group in color_group])
    variables["getterssetters"] += role_multigettersetter_template % {"role": rolename(role),
                                                                      "comparators": all_groups_equal,
                                                                      "representative": role_representative,
                                                                      "setters": role_multisetters}

def add_role_group_property(variables, role, group):
    """
    Add separate group properties.
    """
    vars = {"prop" : propname(role, group),
            "group" : group,
            "role" : role}

    variables["properties"] += property_template % vars
    variables["getterssetters"] += gettersetter_template % vars
    variables["paletteupdates"] += paletteupdate_template % vars
    variables["variables"] += variable_template % vars
    variables["propertyresets"] += reset_template % vars


if __name__ == "__main__":
    parser = argparse.ArgumentParser("Turns the ApplicationPalette template into an actually usable source file")
    parser.add_argument("--template", help="Path to the template file", metavar="PATH", required=True)
    parser.add_argument("--output", help="Path to the file to which to write the generated source file", metavar="PATH", default=None)

    args = parser.parse_args()

    template = open(args.template, "r").read()

    variables = {"warning": "// Auto-generated from %s . Do not edit manually." % os.path.basename(args.template),
                 "properties": "",
                 "propertyresets": "",
                 "getterssetters": "",
                 "paletteupdates": "",
                 "variables": ""}

    for role in color_role:
        add_role_property(variables, role)

        for group in color_group:
            add_role_group_property(variables, role, group)


    generatedSource = template % variables

    if args.output:
        open(args.output, "w").write(generatedSource)
    else:
        print(generatedSource)