diff options
| author | 2014-12-06 17:33:25 -0500 | |
|---|---|---|
| committer | 2014-12-06 17:33:25 -0500 | |
| commit | 7167ce41b61d2ba2cdb526777a4233eb84a3b66a (patch) | |
| tree | a35c14143716e1f2c98f808c81f89426045a946f /SQLiteStudio3/guiSQLiteStudio/uiloader.cpp | |
Imported Upstream version 2.99.6upstream/2.99.6
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/uiloader.cpp')
| -rw-r--r-- | SQLiteStudio3/guiSQLiteStudio/uiloader.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/uiloader.cpp b/SQLiteStudio3/guiSQLiteStudio/uiloader.cpp new file mode 100644 index 0000000..cc02b16 --- /dev/null +++ b/SQLiteStudio3/guiSQLiteStudio/uiloader.cpp @@ -0,0 +1,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; +} + |
