summaryrefslogtreecommitdiff
path: root/scripts/rekonf.py
blob: 48706c3d76aecf4f0a10196cdef7a6902d305c34 (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
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
141
142
143
144
145
146
147
#!/usr/bin/env python3
# ============================================================
#     The rekonq project
# ============================================================
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
# ============================================================
""" Generate SettingsWidgets from KDE kcfg files """


import argparse
import sys
from xml.etree import ElementTree


def write_int_entry(entry):
    '''Add a QSpinBox connected to an Int entry'''
    obj = entry.attrib['key']
    default = entry.find('{*}default').text
    print(f'  auto* { obj } = new QSpinBox(this);')
    print(f'  { obj }->setValue(s->value("{ obj }", { default }).toInt());')
    print(f'  formLayout->addRow(tr("{ entry.attrib["name"] }"), { obj });')


def write_bool_entry(entry):
    '''Add a QCheckBox connected to a Bool entry'''
    obj = entry.attrib['key']
    default = entry.find('{*}default').text
    print(f'  auto* { obj } = new QCheckBox(tr("{ entry.attrib["name"] }"), this);')
    print(f'  { obj }->setChecked(s->value("{ obj }", { default }).toBool());')
    print(f'  formLayout->addRow(QString(), { obj });')


def write_string_entry(entry):
    '''Add a QLineEdit connected to a String entry'''
    obj = entry.attrib['key']
    default = entry.find('{*}default').text
    print(f'  auto* {obj} = new QLineEdit(this);')
    print(f'  {obj}->setText(s->value("{ obj }", "{ default }").toString());')
    print(f'  formLayout->addRow(tr("{ entry.attrib["name"] }"), { obj });')


def write_font_entry(entry):
    '''Add a QFontComboBox connected to a Font entry'''
    obj = entry.attrib['key']
    default = entry.find('{*}default').text
    print(f'  auto* { obj } = new QFontComboBox(this);')
    print(f'  { obj }->setCurrentFont(s->value("{ obj }", "{ default }").toString());')
    print(f'  formLayout->addRow(tr("{ entry.attrib["name"] }"), { obj });')


def write_shortcut_entry(entry):
    '''Add a QKeySequenceEdit connected to a Shortcut entry'''
    obj = entry.attrib['key']
    default = entry.find('{*}default').text
    print(f'  auto* { entry.attrib["key"] } = new QKeySequenceEdit(this);')
    print(f'  { obj }->setKeySequence(s->value("{ obj }", "{ default }").toString());')
    print(f'  formLayout->addRow(tr("{ entry.attrib["name"] }"), { obj });')


def generate_group_widget(root, group):
    '''Generate a class based on the group name'''
    class_group = group.attrib["name"].replace(' ', '')
    class_name = group.attrib["name"].replace(' ', '') + 'SettingsWidget'

    # includes
    print('// Includes')
    print('#include "settingswidgets.hpp"')
    print('#include "helpers.hpp"')
    print('#include <QFormLayout>')
    print('#include <QLineEdit>')
    print('#include <QSpinBox>')
    print('#include <QFontComboBox>')
    print('#include <QKeySequenceEdit>')
    print('#include <QCheckBox>')
    print('// kcfg Includes')
    for include in root.findall('{http://www.kde.org/standards/kcfg/1.0}include'):
        print(f'#include <{ include.text }>')
    print('')

    print(f'{ class_name }::{ class_name }(RekonqSettings *s, QWidget *parent) : SettingsWidget(s, parent) {{')
    print(f'  s->beginGroup("{ class_group }");')
    print('  auto *formLayout = new QFormLayout;')
    print('  setLayout(formLayout);')
    print('')

    print('  // Entries')
    for entry in group.findall('{http://www.kde.org/standards/kcfg/1.0}entry'):
        if entry.attrib.get("hidden") == "true":
            print(f'  // hidden entry { entry.attrib.get("name") }')
        elif entry.attrib['type'] == 'Int':
            write_int_entry(entry)
        elif entry.attrib['type'] == 'Bool':
            write_bool_entry(entry)
        elif entry.attrib['type'] == 'String':
            write_string_entry(entry)
        elif entry.attrib['type'] == 'Font':
            write_font_entry(entry)
        elif entry.attrib['type'] == 'Shortcut':
            write_shortcut_entry(entry)
        else:
            print(f'#error entry with unknown type { entry.attrib["type"] }')
        print('')

    print('  s->endGroup();')
    print('}\n')

    print(f'void { class_name }::save() {{ }}')
    print(f'void { class_name }::reset() {{ }}')


def generate_group_ini(root, group):
    group_name = group.attrib["name"].replace(' ', '')
    print(f'[{ group_name }]')
    for entry in group.findall('{http://www.kde.org/standards/kcfg/1.0}entry'):
        if entry.find('{*}default').get('code') == 'true':
            continue
        entry_key = entry.attrib['key']
        entry_val = entry.find('{*}default').text
        print(f'{ entry_key }={ entry_val }')
    print('')


def main():
    parser = argparse.ArgumentParser(description='Generate SettingsWidgets')
    parser.add_argument('file', type=str, help='kcfg file')
    parser.add_argument('--group', type=str, required=True, help='Group')
    parser.add_argument('--output', type=str, default=None, help='Redirect output to file')
    args = parser.parse_args()

    with open(args.output, 'w', encoding="utf-8") if args.output else sys.stdout as sys.stdout:
        tree = ElementTree.parse(args.file)
        root = tree.getroot()

        if args.group == 'all':
            for group in root.findall('{http://www.kde.org/standards/kcfg/1.0}group'):
                generate_group_ini(root, group)
        else:
            for group in root.findall('{http://www.kde.org/standards/kcfg/1.0}group'):
                if group.attrib["name"] == args.group:
                    print('// This is an automatically generated file')
                    generate_group_widget(root, group)
                    break


if __name__ == '__main__':
    main()