#!/usr/bin/env python3 # ============================================================ # The rekonq project # ============================================================ # SPDX-License-Identifier: GPL-3.0-only # Copyright (C) 2022 aqua # ============================================================ """ 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''' print(f' // { entry.attrib["name"] }') print(f' auto* { entry.attrib["key"] } = new QSpinBox(this);') print(f' { entry.attrib["key"] }->setValue({ entry.find("{*}default").text });') print(f' formLayout->addRow(tr("{ entry.attrib["name"] }"), { entry.attrib["key"] });') def write_bool_entry(entry): '''Add a QCheckBox connected to a Bool entry''' print(f' // { entry.attrib["name"] }') print(f' auto* { entry.attrib["key"] } = new QCheckBox(tr("{ entry.attrib["name"] }"), this);') print(f' { entry.attrib["key"] }->setChecked({ entry.find("{*}default").text });') print(f' formLayout->addRow(QString(), { entry.attrib["key"] });') def write_string_entry(entry): '''Add a QLineEdit connected to a String entry''' print(f' // { entry.attrib["name"] }') print(f' auto* { entry.attrib["key"] } = new QLineEdit(this);') print(f' { entry.attrib["key"] }->setText("{ entry.find("{*}default").text }");') print(f' formLayout->addRow(tr("{ entry.attrib["name"] }"), { entry.attrib["key"] });') def write_font_entry(entry): '''Add a QFontComboBox connected to a Font entry''' print(f' // { entry.attrib["name"] }') print(f' auto* { entry.attrib["key"] } = new QFontComboBox(this);') print(f' { entry.attrib["key"] }->setCurrentFont({ entry.find("{*}default").text });') print(f' formLayout->addRow(tr("{ entry.attrib["name"] }"), { entry.attrib["key"] });') def write_shortcut_entry(entry): '''Add a QKeySequenceEdit connected to a Shortcut entry''' print(f' // { entry.attrib["name"] }') print(f' auto* { entry.attrib["key"] } = new QKeySequenceEdit({{ "{entry.find("{*}default").text}" }}, this);') print(f' formLayout->addRow(tr("{ entry.attrib["name"] }"), { entry.attrib["key"] });') def generate_group_widget(root, group): '''Generate a class based on the group name''' class_name = group.attrib["name"].replace(' ', '') + 'SettingsWidget' # includes print('// Includes') print('#include "settingswidgets.hpp"') print('#include "helpers.hpp"') print('#include ') print('#include ') print('#include ') print('#include ') print('#include ') print('#include ') 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 }(QWidget *parent) : SettingsWidget(parent) {{') 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('}\n') print(f'void { class_name }::save() {{ }}') print(f'void { class_name }::reset() {{ }}') if __name__ == '__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() # groups 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)