summaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/uiloader.cpp
blob: cc02b165d1fade452f6c7af95d7cd9084483846c (plain) (blame)
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
#include "uiloader.h"
#include "common/unused.h"
#include "uiloaderpropertyhandler.h"
#include "uiscriptingcombo.h"
#include "uiscriptingedit.h"
#include "uicustomicon.h"
#include "uiurlbutton.h"
#include "sqlview.h"
#include "common/configradiobutton.h"
#include "common/configcombobox.h"
#include "common/fileedit.h"
#include "common/colorbutton.h"
#include <QComboBox>
#include <QDebug>
#include <QMetaProperty>
#include <QXmlSimpleReader>

#define REGISTER_WIDGET(Class) \
    registerWidgetClass(#Class, [](QWidget* parent, const QString& name) -> QWidget*\
    {\
        Class* w = new Class(parent);\
        w->setObjectName(name);\
        return w;\
    })

UiLoader::UiLoader(QObject *parent) :
    QUiLoader(parent)
{
    registerPropertyHandler(new UiScriptingCombo());
    registerPropertyHandler(new UiScriptingEdit());
    registerPropertyHandler(new UiCustomIcon());
    registerPropertyHandler(new UiUrlButton());

    REGISTER_WIDGET(ConfigRadioButton);
    REGISTER_WIDGET(ConfigComboBox);
    REGISTER_WIDGET(FileEdit);
    REGISTER_WIDGET(ColorButton);
    REGISTER_WIDGET(SqlView);
}

QWidget* UiLoader::createWidget(const QString& className, QWidget* parent, const QString& name)
{
    QWidget* w = nullptr;
    if (registeredClasses.contains(className))
        w = registeredClasses[className](parent, name);
    else
        w = QUiLoader::createWidget(className, parent, name);

    return w;
}

void UiLoader::registerWidgetClass(const QString& className, FactoryFunction factoryFunction)
{
    registeredClasses[className] = factoryFunction;
}

void UiLoader::handlePropertiesRecursively(QWidget* widget)
{
    if (widget->dynamicPropertyNames().size() > 0)
        handleProperties(widget);

    for (QWidget* w : widget->findChildren<QWidget*>())
        handleProperties(w);
}

void UiLoader::handleProperties(QWidget* widget)
{
    QVariant propValue;
    for (UiLoaderPropertyHandler* handler : propertyHandlers)
    {
        propValue = widget->property(handler->getPropertyName());
        if (propValue.isValid())
            handler->handle(widget, propValue);
    }
}

QWidget* UiLoader::load(const QString& path)
{
    QFile file(path);
    if (!file.open(QIODevice::ReadOnly))
    {
        qCritical() << "FormManager was unable to open ui file:" << path;
        return nullptr;
    }

    QWidget* w = QUiLoader::load(&file, nullptr);
    handlePropertiesRecursively(w);
    return w;
}

void UiLoader::registerPropertyHandler(UiLoaderPropertyHandler* handler)
{
    propertyHandlers << handler;
}