Mandate providing a parent on layout creation

This commit is contained in:
Raphael Coeffic
2022-06-11 15:38:08 +02:00
committed by Raphael Coeffic
parent 7eb1aa8667
commit b23cb30884
19 changed files with 164 additions and 164 deletions
+8 -5
View File
@@ -51,11 +51,11 @@ const LayoutFactory * getLayoutFactory(const char * name)
// Loads a layout, but does not attach it to any window // Loads a layout, but does not attach it to any window
// //
WidgetsContainer * WidgetsContainer *
loadLayout(const char * name, LayoutPersistentData * persistentData) loadLayout(Window* parent, const char * name, LayoutPersistentData * persistentData)
{ {
const LayoutFactory * factory = getLayoutFactory(name); const LayoutFactory * factory = getLayoutFactory(name);
if (factory) { if (factory) {
return factory->load(persistentData); return factory->load(parent, persistentData);
} }
return nullptr; return nullptr;
} }
@@ -83,7 +83,9 @@ void loadDefaultLayout()
if (screen == nullptr && defaultLayout != nullptr) { if (screen == nullptr && defaultLayout != nullptr) {
strcpy(screenData.LayoutId, defaultLayout->getId()); strcpy(screenData.LayoutId, defaultLayout->getId());
screen = defaultLayout->create(&screenData.layoutData);
auto viewMain = ViewMain::instance();
screen = defaultLayout->create(viewMain, &screenData.layoutData);
// //
// TODO: // TODO:
// -> attach a few default widgets // -> attach a few default widgets
@@ -108,7 +110,7 @@ void loadCustomScreens()
while (i < MAX_CUSTOM_SCREENS) { while (i < MAX_CUSTOM_SCREENS) {
auto& screen = customScreens[i]; auto& screen = customScreens[i];
screen = loadLayout(g_model.screenData[i].LayoutId, screen = loadLayout(viewMain, g_model.screenData[i].LayoutId,
&g_model.screenData[i].layoutData); &g_model.screenData[i].layoutData);
if (!screen) { if (!screen) {
@@ -148,7 +150,8 @@ createCustomScreen(const LayoutFactory* factory, unsigned customScreenIndex)
delete customScreens[customScreenIndex]; delete customScreens[customScreenIndex];
} }
auto screen = factory->create(&g_model.screenData[customScreenIndex].layoutData); auto screen = factory->create(
ViewMain::instance(), &g_model.screenData[customScreenIndex].layoutData);
customScreens[customScreenIndex] = screen; customScreens[customScreenIndex] = screen;
if (screen) { if (screen) {
+3 -3
View File
@@ -55,10 +55,10 @@ class LayoutFactory
virtual const ZoneOption* getOptions() const = 0; virtual const ZoneOption* getOptions() const = 0;
virtual WidgetsContainer* create( virtual WidgetsContainer* create(Window* parent,
LayoutPersistentData* persistentData) const = 0; LayoutPersistentData* persistentData) const = 0;
virtual WidgetsContainer* load( virtual WidgetsContainer* load(Window* parent,
LayoutPersistentData* persistentData) const = 0; LayoutPersistentData* persistentData) const = 0;
virtual void initPersistentData(LayoutPersistentData* persistentData, virtual void initPersistentData(LayoutPersistentData* persistentData,
@@ -69,7 +69,7 @@ class LayoutFactory
const char* name; const char* name;
}; };
WidgetsContainer * loadLayout(const char * name, LayoutPersistentData * persistentData); WidgetsContainer * loadLayout(Window* parent, const char * name, LayoutPersistentData * persistentData);
// intented for new models // intented for new models
void loadDefaultLayout(); void loadDefaultLayout();
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1P2[] = {
class Layout1P2: public Layout class Layout1P2: public Layout
{ {
public: public:
Layout1P2(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout1P2(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1P3[] = {
class Layout1P3: public Layout class Layout1P3: public Layout
{ {
public: public:
Layout1P3(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout1P3(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1x1[] = {
class Layout1x1: public Layout class Layout1x1: public Layout
{ {
public: public:
Layout1x1(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout1x1(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1x2[] = {
class Layout1x2: public Layout class Layout1x2: public Layout
{ {
public: public:
Layout1x2(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout1x2(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1x3[] = {
class Layout1x3: public Layout class Layout1x3: public Layout
{ {
public: public:
Layout1x3(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout1x3(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1x4[] = {
class Layout1x4: public Layout class Layout1x4: public Layout
{ {
public: public:
Layout1x4(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout1x4(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_2P1[] = {
class Layout2P1: public Layout class Layout2P1: public Layout
{ {
public: public:
Layout2P1(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout2P1(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_2P3[] = {
class Layout2P3: public Layout class Layout2P3: public Layout
{ {
public: public:
Layout2P3(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout2P3(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_2x1[] = {
class Layout2x1: public Layout class Layout2x1: public Layout
{ {
public: public:
Layout2x1(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout2x1(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_2x2[] = {
class Layout2x2: public Layout class Layout2x2: public Layout
{ {
public: public:
Layout2x2(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout2x2(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_2x3[] = {
class Layout2x3: public Layout class Layout2x3: public Layout
{ {
public: public:
Layout2x3(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout2x3(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -46,8 +46,8 @@ class Layout2x4: public Layout
OPTION_PANEL2_COLOR OPTION_PANEL2_COLOR
}; };
Layout2x4(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout2x4(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_4P2[] = {
class Layout4P2: public Layout class Layout4P2: public Layout
{ {
public: public:
Layout4P2(const LayoutFactory * factory, Layout::PersistentData * persistentData): Layout4P2(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData) Layout(parent, factory, persistentData)
{ {
} }
@@ -25,8 +25,8 @@
#include "view_main_decoration.h" #include "view_main_decoration.h"
#include "view_main.h" #include "view_main.h"
Layout::Layout(const LayoutFactory * factory, PersistentData * persistentData): Layout::Layout(Window* parent, const LayoutFactory * factory, PersistentData * persistentData):
LayoutBase({0, 0, LCD_W, LCD_H}, persistentData), LayoutBase(parent, {0, 0, LCD_W, LCD_H}, persistentData),
factory(factory), factory(factory),
decoration(new ViewMainDecoration(this, getRect())) decoration(new ViewMainDecoration(this, getRect()))
{ {
@@ -49,7 +49,7 @@ class Layout: public LayoutBase
public: public:
Layout(const LayoutFactory * factory, PersistentData * persistentData); Layout(Window* parent, const LayoutFactory * factory, PersistentData * persistentData);
#if defined(DEBUG_WINDOWS) #if defined(DEBUG_WINDOWS)
std::string getName() const override std::string getName() const override
@@ -132,20 +132,20 @@ class BaseLayoutFactory: public LayoutFactory
return options; return options;
} }
Layout * create(Layout::PersistentData * persistentData) const override Layout * create(Window* parent, Layout::PersistentData * persistentData) const override
{ {
initPersistentData(persistentData, true); initPersistentData(persistentData, true);
Layout * layout = new T(this, persistentData); Layout * layout = new T(parent, this, persistentData);
if (layout) { if (layout) {
layout->create(); layout->create();
} }
return layout; return layout;
} }
Layout * load(Layout::PersistentData * persistentData) const override Layout * load(Window* parent, Layout::PersistentData * persistentData) const override
{ {
initPersistentData(persistentData, false); initPersistentData(persistentData, false);
Layout * layout = new T(this, persistentData); Layout * layout = new T(parent, this, persistentData);
if (layout) { if (layout) {
layout->load(); layout->load();
} }
@@ -26,9 +26,9 @@ const char * const STR_MONTHS[] = TR_MONTHS;
constexpr uint32_t TOPBAR_REFRESH = 1000 / 10; // 10 Hz constexpr uint32_t TOPBAR_REFRESH = 1000 / 10; // 10 Hz
TopbarImpl::TopbarImpl(Window * parent) : TopbarImpl::TopbarImpl(Window * parent) :
TopbarImplBase({0, 0, LCD_W, MENU_HEADER_HEIGHT}, &g_model.topbarData) TopbarImplBase(parent, {0, 0, LCD_W, MENU_HEADER_HEIGHT}, &g_model.topbarData)
{ {
attach(parent); //attach(parent);
} }
unsigned int TopbarImpl::getZonesCount() const unsigned int TopbarImpl::getZonesCount() const
@@ -25,149 +25,146 @@
#include "widgets_container.h" #include "widgets_container.h"
#include "draw_functions.h" #include "draw_functions.h"
template<int N, int O> template <int N, int O>
class WidgetsContainerImpl: public WidgetsContainer class WidgetsContainerImpl : public WidgetsContainer
{ {
public: public:
typedef WidgetsContainerPersistentData<N,O> PersistentData; typedef WidgetsContainerPersistentData<N, O> PersistentData;
WidgetsContainerImpl(const rect_t & rect, PersistentData * persistentData): WidgetsContainerImpl(Window* parent, const rect_t& rect,
WidgetsContainer(nullptr, rect, FORM_FORWARD_FOCUS), PersistentData* persistentData) :
WidgetsContainer(parent, rect, FORM_FORWARD_FOCUS),
persistentData(persistentData) persistentData(persistentData)
{ {
}
Widget* createWidget(unsigned int index,
const WidgetFactory* factory) override
{
if (index >= N) return nullptr;
// remove old one if existing
removeWidget(index);
Widget* widget = nullptr;
if (factory) {
strncpy(persistentData->zones[index].widgetName, factory->getName(),
sizeof(ZonePersistentData::widgetName));
widget = factory->create(this, getZone(index),
&persistentData->zones[index].widgetData);
}
widgets[index] = widget;
if (widget) widget->attach(this);
return widget;
}
void removeWidget(unsigned int index) override
{
if (index >= N) return;
if (widgets[index]) {
removeField(widgets[index]);
widgets[index]->deleteLater();
} }
Widget * createWidget(unsigned int index, const WidgetFactory * factory) override widgets[index] = nullptr;
{ memset(persistentData->zones[index].widgetName, 0,
if (index >= N) sizeof(ZonePersistentData::widgetName));
return nullptr; memset(&persistentData->zones[index].widgetData, 0,
sizeof(Widget::PersistentData));
}
// remove old one if existing Widget* getWidget(unsigned int index) override
removeWidget(index); {
if (index < N) return widgets[index];
Widget* widget = nullptr; return nullptr;
if (factory) { }
strncpy(persistentData->zones[index].widgetName, factory->getName(),
sizeof(ZonePersistentData::widgetName));
widget = factory->create(this, getZone(index),
&persistentData->zones[index].widgetData);
}
widgets[index] = widget;
if (widget) virtual void create() { memset(persistentData, 0, sizeof(PersistentData)); }
widget->attach(this);
return widget; virtual void load()
} {
unsigned int count = getZonesCount();
void removeWidget(unsigned int index) override for (unsigned int i = 0; i < count; i++) {
{ // remove old widget
if (index >= N) if (widgets[i]) {
return; removeField(widgets[i]);
widgets[i]->deleteLater();
if (widgets[index]) { widgets[i] = nullptr;
removeField(widgets[index]);
widgets[index]->deleteLater();
}
widgets[index] = nullptr;
memset(persistentData->zones[index].widgetName, 0,
sizeof(ZonePersistentData::widgetName));
memset(&persistentData->zones[index].widgetData, 0,
sizeof(Widget::PersistentData));
}
Widget * getWidget(unsigned int index) override
{
if (index < N)
return widgets[index];
return nullptr;
}
virtual void create()
{
memset(persistentData, 0, sizeof(PersistentData));
}
virtual void load()
{
unsigned int count = getZonesCount();
for (unsigned int i = 0; i < count; i++) {
// remove old widget
if (widgets[i]) {
removeField(widgets[i]);
widgets[i]->deleteLater();
widgets[i] = nullptr;
}
}
for (unsigned int i = 0; i < count; i++) {
// and load new one if required
if (persistentData->zones[i].widgetName[0]) {
char name[WIDGET_NAME_LEN + 1];
memset(name, 0, sizeof(name));
strncpy(name, persistentData->zones[i].widgetName, WIDGET_NAME_LEN);
widgets[i] = loadWidget(name, this, getZone(i), &persistentData->zones[i].widgetData);
}
} }
} }
inline ZoneOptionValue * getOptionValue(unsigned int index) const for (unsigned int i = 0; i < count; i++) {
{ // and load new one if required
return &persistentData->options[index].value; if (persistentData->zones[i].widgetName[0]) {
} char name[WIDGET_NAME_LEN + 1];
memset(name, 0, sizeof(name));
inline void setOptionValue(unsigned int index, const ZoneOptionValue& value) strncpy(name, persistentData->zones[i].widgetName, WIDGET_NAME_LEN);
{ widgets[i] = loadWidget(name, this, getZone(i),
persistentData->options[index].value = value; &persistentData->zones[i].widgetData);
}
unsigned int getZonesCount() const override = 0;
rect_t getZone(unsigned int index) const override = 0;
virtual void background()
{
for (int i = 0; i < N; i++) {
if (widgets[i]) {
widgets[i]->background();
}
} }
} }
}
void updateZones() override inline ZoneOptionValue* getOptionValue(unsigned int index) const
{ {
for (int i = 0; i < N; i++) { return &persistentData->options[index].value;
if (widgets[i]) { }
auto zone = getZone(i);
widgets[i]->setRect(zone); inline void setOptionValue(unsigned int index, const ZoneOptionValue& value)
widgets[i]->setInnerHeight(zone.h); {
} persistentData->options[index].value = value;
}
unsigned int getZonesCount() const override = 0;
rect_t getZone(unsigned int index) const override = 0;
virtual void background()
{
for (int i = 0; i < N; i++) {
if (widgets[i]) {
widgets[i]->background();
} }
} }
}
void adjustLayout() override void updateZones() override
{ {
for (int i = 0; i < N; i++) {
if (widgets[i]) {
auto zone = getZone(i);
widgets[i]->setRect(zone);
widgets[i]->setInnerHeight(zone.h);
}
} }
}
protected:
PersistentData * persistentData; void adjustLayout() override {}
Widget * widgets[N] = {};
protected:
PersistentData* persistentData;
Widget* widgets[N] = {};
}; };
template<class T> template <class T>
class BaseWidgetFactory: public WidgetFactory class BaseWidgetFactory : public WidgetFactory
{ {
public: public:
BaseWidgetFactory(const char * name, const ZoneOption * options, const char * displayName = nullptr): BaseWidgetFactory(const char* name, const ZoneOption* options,
const char* displayName = nullptr) :
WidgetFactory(name, options, displayName) WidgetFactory(name, options, displayName)
{ {
} }
Widget * create(FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData, bool init = true) const override Widget* create(FormGroup* parent, const rect_t& rect,
{ Widget::PersistentData* persistentData,
initPersistentData(persistentData, init); bool init = true) const override
return new T(this, parent, rect, persistentData); {
} initPersistentData(persistentData, init);
return new T(this, parent, rect, persistentData);
}
}; };