Extended QUiLoader to support loading custom widgets from UI files
This commit is contained in:
@@ -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
|
||||
|
||||
84
src/Insight/UserInterfaces/InsightWindow/UiLoader.cpp
Normal file
84
src/Insight/UserInterfaces/InsightWindow/UiLoader.cpp
Normal 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);
|
||||
}
|
||||
19
src/Insight/UserInterfaces/InsightWindow/UiLoader.hpp
Normal file
19
src/Insight/UserInterfaces/InsightWindow/UiLoader.hpp
Normal 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;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user