CANdevStudio
Development tool for CAN bus simulation
Loading...
Searching...
No Matches
pluginloader.h
Go to the documentation of this file.
1#ifndef PLUGINLOADER_H_QWADUNZ7
2#define PLUGINLOADER_H_QWADUNZ7
3
4#include <nodes/DataModelRegistry>
5
6#include "plugin_type.h"
7
8template <class T, class Tuple> struct section_ndx;
9
10template <class T, class... Types> struct section_ndx<T, std::tuple<T, Types...>> {
11 static const std::size_t value = 0;
12};
13
14template <class T, class U, class... Types> struct section_ndx<T, std::tuple<U, Types...>> {
15 static const std::size_t value = 1 + section_ndx<T, std::tuple<Types...>>::value;
16};
17
18template <typename T> void registerModel(QtNodes::DataModelRegistry& registry)
19{
20 registry.registerModel<typename T::Model>();
21}
22
23template <typename W, typename Z, typename... Args> void registerModel(QtNodes::DataModelRegistry& registry)
24{
25 registerModel<W>(registry);
26 registerModel<Z, Args...>(registry);
27}
28
29template <typename Array, typename... Args> struct SectionLoader;
30
31template <typename Array, typename... Args> struct SectionLoader<Array, std::tuple<Args...>> {
32 SectionLoader(Array& widgets, Ui::ProjectConfigPrivate& ui)
33 : _widgets(widgets)
34 {
35 initSection<Args...>(ui);
36 }
37
38private:
39 template <typename W, typename Z, typename... Secs> void initSection(Ui::ProjectConfigPrivate& ui)
40 {
41 initSection<W>(ui);
42 initSection<Z, Secs...>(ui);
43 }
44
45 template <typename T> void initSection(Ui::ProjectConfigPrivate& ui)
46 {
47 constexpr size_t ndx = section_ndx<T, std::tuple<Args...>>::value;
48
49 static_assert(ndx < std::tuple_size<std::remove_reference_t<decltype(_widgets)>>::value, "Index out of bounds");
50
51 auto pb = new QCheckBox(T::sectionName(), ui.frame);
52 pb->setLayoutDirection(Qt::RightToLeft);
53 pb->setStyleSheet(QString("spacing: ") + QString::number(T::spacing()) + "px;");
54 pb->setProperty("type", "sectionHeader");
55 pb->setChecked(true);
56 ui.verticalLayout->addWidget(pb);
57
58 auto wdg = new QWidget(ui.frame);
59 wdg->setProperty("type", "sectionWidget");
60 auto wdgLayout = new QVBoxLayout(wdg);
61 wdgLayout->setSpacing(10);
62 wdgLayout->setContentsMargins(0, 0, 0, 0);
63 ui.verticalLayout->addWidget(wdg);
64 _widgets[ndx] = wdg;
65
66 QObject::connect(pb, &QPushButton::toggled, wdg, &QWidget::setVisible);
67 }
68
69 Array& _widgets;
70};
71
72template <typename S, typename... Args> struct PluginLoader {
73 PluginLoader(QtNodes::DataModelRegistry& registry)
74 {
75 registerModel<Args...>(registry);
76 }
77
78 void initSections(Ui::ProjectConfigPrivate& ui)
79 {
80 SectionLoader<decltype(_widgets), S> sections(_widgets, ui);
81
82 auto spacer = new QSpacerItem(17, 410, QSizePolicy::Minimum, QSizePolicy::Expanding);
83 ui.verticalLayout->addItem(spacer);
84 }
85
86 void addWidgets(const QColor& bg)
87 {
88 addWidget<Args...>(bg);
89 }
90
92 {
93 for (auto wdg : _widgets) {
94 QLayoutItem* item;
95 while ((item = wdg->layout()->takeAt(0)) != nullptr) {
96 delete item->widget();
97 delete item;
98 }
99 }
100 };
101
102private:
103 template <typename T> void addWidget(const QColor& bg)
104 {
106
107 static_assert(ndx < std::tuple_size<decltype(_widgets)>::value, "Index out of bounds");
108
109 if (_widgets[ndx]) {
110 _widgets[ndx]->layout()->addWidget(new IconLabel(T::name, T::PluginType::sectionColor(), bg));
111 } else {
112 cds_error("Sections not initialized");
113 }
114 }
115
116 template <typename W, typename Z, typename... Plugs> void addWidget(const QColor& bg)
117 {
118 addWidget<W>(bg);
119 addWidget<Z, Plugs...>(bg);
120 }
121
122 std::array<QWidget*, std::tuple_size<S>::value> _widgets {};
123};
124
125#endif /* end of include guard: PLUGINLOADER_H_QWADUNZ7 */
Definition iconlabel.h:9
#define cds_error(fmt,...)
Definition log.h:21
void registerModel(QtNodes::DataModelRegistry &registry)
Definition pluginloader.h:18
Definition pluginloader.h:72
void initSections(Ui::ProjectConfigPrivate &ui)
Definition pluginloader.h:78
void addWidgets(const QColor &bg)
Definition pluginloader.h:86
void clearSections()
Definition pluginloader.h:91
PluginLoader(QtNodes::DataModelRegistry &registry)
Definition pluginloader.h:73
SectionLoader(Array &widgets, Ui::ProjectConfigPrivate &ui)
Definition pluginloader.h:32
Definition pluginloader.h:29
Definition pluginloader.h:8