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
@@ -31,16 +31,17 @@ 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 Widget* createWidget(unsigned int index,
const WidgetFactory* factory) override
{ {
if (index >= N) if (index >= N) return nullptr;
return nullptr;
// remove old one if existing // remove old one if existing
removeWidget(index); removeWidget(index);
@@ -54,16 +55,14 @@ class WidgetsContainerImpl: public WidgetsContainer
} }
widgets[index] = widget; widgets[index] = widget;
if (widget) if (widget) widget->attach(this);
widget->attach(this);
return widget; return widget;
} }
void removeWidget(unsigned int index) override void removeWidget(unsigned int index) override
{ {
if (index >= N) if (index >= N) return;
return;
if (widgets[index]) { if (widgets[index]) {
removeField(widgets[index]); removeField(widgets[index]);
@@ -79,16 +78,12 @@ class WidgetsContainerImpl: public WidgetsContainer
Widget* getWidget(unsigned int index) override Widget* getWidget(unsigned int index) override
{ {
if (index < N) if (index < N) return widgets[index];
return widgets[index];
return nullptr; return nullptr;
} }
virtual void create() virtual void create() { memset(persistentData, 0, sizeof(PersistentData)); }
{
memset(persistentData, 0, sizeof(PersistentData));
}
virtual void load() virtual void load()
{ {
@@ -108,7 +103,8 @@ class WidgetsContainerImpl: public WidgetsContainer
char name[WIDGET_NAME_LEN + 1]; char name[WIDGET_NAME_LEN + 1];
memset(name, 0, sizeof(name)); memset(name, 0, sizeof(name));
strncpy(name, persistentData->zones[i].widgetName, WIDGET_NAME_LEN); strncpy(name, persistentData->zones[i].widgetName, WIDGET_NAME_LEN);
widgets[i] = loadWidget(name, this, getZone(i), &persistentData->zones[i].widgetData); widgets[i] = loadWidget(name, this, getZone(i),
&persistentData->zones[i].widgetData);
} }
} }
} }
@@ -147,9 +143,7 @@ class WidgetsContainerImpl: public WidgetsContainer
} }
} }
void adjustLayout() override void adjustLayout() override {}
{
}
protected: protected:
PersistentData* persistentData; PersistentData* persistentData;
@@ -160,12 +154,15 @@ 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,
bool init = true) const override
{ {
initPersistentData(persistentData, init); initPersistentData(persistentData, init);
return new T(this, parent, rect, persistentData); return new T(this, parent, rect, persistentData);