Extended QUiLoader to support loading custom widgets from UI files

This commit is contained in:
Nav
2021-08-22 20:38:44 +01:00
parent 8259b7dc51
commit bbe0051205
3 changed files with 104 additions and 0 deletions

View File

@@ -124,6 +124,7 @@ add_executable(Bloom
# Insight
src/Insight/Insight.cpp
src/Insight/InsightWorker.cpp
src/Insight/UserInterfaces/InsightWindow/UiLoader.cpp
src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp
src/Insight/UserInterfaces/InsightWindow/AboutWindow.cpp
src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp

View File

@@ -0,0 +1,84 @@
#include "UiLoader.hpp"
#include <QtUiTools>
#include "Widgets/RotatableLabel.hpp"
#include "Widgets/SlidingHandleWidget.hpp"
#include "Widgets/SvgWidget.hpp"
#include "Widgets/SvgToolButton.hpp"
#include "Widgets/ExpandingWidget.hpp"
#include "Widgets/ExpandingHeightScrollAreaWidget.hpp"
#include "Widgets/TargetRegistersPane/TargetRegistersPaneWidget.hpp"
#include "src/Logger/Logger.hpp"
using namespace Bloom;
using namespace Bloom::Widgets;
UiLoader::UiLoader(QObject* parent): QUiLoader(parent) {
this->customWidgetConstructorsByWidgetName = decltype(this->customWidgetConstructorsByWidgetName) {
{
"RotatableLabel",
[this](QWidget* parent, const QString& name) {
auto widget = new RotatableLabel("", parent);
widget->setObjectName(name);
widget->setStyleSheet(parent->styleSheet());
return widget;
}
},
{
"SlidingHandleWidget",
[this](QWidget* parent, const QString& name) {
auto widget = new SlidingHandleWidget(parent);
widget->setObjectName(name);
widget->setStyleSheet(parent->styleSheet());
return widget;
}
},
{
"ExpandingWidget",
[this](QWidget* parent, const QString& name) {
auto widget = new ExpandingWidget(parent);
widget->setObjectName(name);
widget->setStyleSheet(parent->styleSheet());
return widget;
}
},
{
"ExpandingHeightScrollAreaWidget",
[this](QWidget* parent, const QString& name) {
auto widget = new ExpandingHeightScrollAreaWidget(parent);
widget->setObjectName(name);
widget->setStyleSheet(parent->styleSheet());
return widget;
}
},
{
"SvgWidget",
[this](QWidget* parent, const QString& name) {
auto widget = new SvgWidget(parent);
widget->setObjectName(name);
widget->setStyleSheet(parent->styleSheet());
return widget;
}
},
{
"SvgToolButton",
[this](QWidget* parent, const QString& name) {
auto widget = new SvgToolButton(parent);
widget->setObjectName(name);
widget->setStyleSheet(parent->styleSheet());
return widget;
}
},
};
}
QWidget* UiLoader::createWidget(const QString& className, QWidget* parent, const QString& name) {
if (this->customWidgetConstructorsByWidgetName.contains(className)) {
// This is a custom widget - call the mapped constructor
return this->customWidgetConstructorsByWidgetName.at(className)(parent, name);
}
return QUiLoader::createWidget(className, parent, name);
}

View File

@@ -0,0 +1,19 @@
#pragma once
#include <QUiLoader>
#include <QSize>
namespace Bloom
{
class UiLoader: public QUiLoader
{
Q_OBJECT
private:
std::map<QString, std::function<QWidget*(QWidget* parent, const QString& name)>> customWidgetConstructorsByWidgetName = {};
public:
explicit UiLoader(QObject* parent);
QWidget* createWidget(const QString& className, QWidget* parent, const QString& name) override;
};
}