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
//
WidgetsContainer *
loadLayout(const char * name, LayoutPersistentData * persistentData)
loadLayout(Window* parent, const char * name, LayoutPersistentData * persistentData)
{
const LayoutFactory * factory = getLayoutFactory(name);
if (factory) {
return factory->load(persistentData);
return factory->load(parent, persistentData);
}
return nullptr;
}
@@ -83,7 +83,9 @@ void loadDefaultLayout()
if (screen == nullptr && defaultLayout != nullptr) {
strcpy(screenData.LayoutId, defaultLayout->getId());
screen = defaultLayout->create(&screenData.layoutData);
auto viewMain = ViewMain::instance();
screen = defaultLayout->create(viewMain, &screenData.layoutData);
//
// TODO:
// -> attach a few default widgets
@@ -108,7 +110,7 @@ void loadCustomScreens()
while (i < MAX_CUSTOM_SCREENS) {
auto& screen = customScreens[i];
screen = loadLayout(g_model.screenData[i].LayoutId,
screen = loadLayout(viewMain, g_model.screenData[i].LayoutId,
&g_model.screenData[i].layoutData);
if (!screen) {
@@ -148,7 +150,8 @@ createCustomScreen(const LayoutFactory* factory, unsigned 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;
if (screen) {
+3 -3
View File
@@ -55,10 +55,10 @@ class LayoutFactory
virtual const ZoneOption* getOptions() const = 0;
virtual WidgetsContainer* create(
virtual WidgetsContainer* create(Window* parent,
LayoutPersistentData* persistentData) const = 0;
virtual WidgetsContainer* load(
virtual WidgetsContainer* load(Window* parent,
LayoutPersistentData* persistentData) const = 0;
virtual void initPersistentData(LayoutPersistentData* persistentData,
@@ -69,7 +69,7 @@ class LayoutFactory
const char* name;
};
WidgetsContainer * loadLayout(const char * name, LayoutPersistentData * persistentData);
WidgetsContainer * loadLayout(Window* parent, const char * name, LayoutPersistentData * persistentData);
// intented for new models
void loadDefaultLayout();
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1P2[] = {
class Layout1P2: public Layout
{
public:
Layout1P2(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout1P2(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1P3[] = {
class Layout1P3: public Layout
{
public:
Layout1P3(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout1P3(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1x1[] = {
class Layout1x1: public Layout
{
public:
Layout1x1(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout1x1(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1x2[] = {
class Layout1x2: public Layout
{
public:
Layout1x2(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout1x2(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1x3[] = {
class Layout1x3: public Layout
{
public:
Layout1x3(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout1x3(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_1x4[] = {
class Layout1x4: public Layout
{
public:
Layout1x4(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout1x4(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_2P1[] = {
class Layout2P1: public Layout
{
public:
Layout2P1(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout2P1(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_2P3[] = {
class Layout2P3: public Layout
{
public:
Layout2P3(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout2P3(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_2x1[] = {
class Layout2x1: public Layout
{
public:
Layout2x1(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout2x1(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_2x2[] = {
class Layout2x2: public Layout
{
public:
Layout2x2(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout2x2(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_2x3[] = {
class Layout2x3: public Layout
{
public:
Layout2x3(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout2x3(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -46,8 +46,8 @@ class Layout2x4: public Layout
OPTION_PANEL2_COLOR
};
Layout2x4(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout2x4(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
+2 -2
View File
@@ -34,8 +34,8 @@ const ZoneOption OPTIONS_LAYOUT_4P2[] = {
class Layout4P2: public Layout
{
public:
Layout4P2(const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(factory, persistentData)
Layout4P2(Window* parent, const LayoutFactory * factory, Layout::PersistentData * persistentData):
Layout(parent, factory, persistentData)
{
}
@@ -25,8 +25,8 @@
#include "view_main_decoration.h"
#include "view_main.h"
Layout::Layout(const LayoutFactory * factory, PersistentData * persistentData):
LayoutBase({0, 0, LCD_W, LCD_H}, persistentData),
Layout::Layout(Window* parent, const LayoutFactory * factory, PersistentData * persistentData):
LayoutBase(parent, {0, 0, LCD_W, LCD_H}, persistentData),
factory(factory),
decoration(new ViewMainDecoration(this, getRect()))
{
@@ -49,7 +49,7 @@ class Layout: public LayoutBase
public:
Layout(const LayoutFactory * factory, PersistentData * persistentData);
Layout(Window* parent, const LayoutFactory * factory, PersistentData * persistentData);
#if defined(DEBUG_WINDOWS)
std::string getName() const override
@@ -132,20 +132,20 @@ class BaseLayoutFactory: public LayoutFactory
return options;
}
Layout * create(Layout::PersistentData * persistentData) const override
Layout * create(Window* parent, Layout::PersistentData * persistentData) const override
{
initPersistentData(persistentData, true);
Layout * layout = new T(this, persistentData);
Layout * layout = new T(parent, this, persistentData);
if (layout) {
layout->create();
}
return layout;
}
Layout * load(Layout::PersistentData * persistentData) const override
Layout * load(Window* parent, Layout::PersistentData * persistentData) const override
{
initPersistentData(persistentData, false);
Layout * layout = new T(this, persistentData);
Layout * layout = new T(parent, this, persistentData);
if (layout) {
layout->load();
}
@@ -26,9 +26,9 @@ const char * const STR_MONTHS[] = TR_MONTHS;
constexpr uint32_t TOPBAR_REFRESH = 1000 / 10; // 10 Hz
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
@@ -25,149 +25,146 @@
#include "widgets_container.h"
#include "draw_functions.h"
template<int N, int O>
class WidgetsContainerImpl: public WidgetsContainer
template <int N, int O>
class WidgetsContainerImpl : public WidgetsContainer
{
public:
typedef WidgetsContainerPersistentData<N,O> PersistentData;
public:
typedef WidgetsContainerPersistentData<N, O> PersistentData;
WidgetsContainerImpl(const rect_t & rect, PersistentData * persistentData):
WidgetsContainer(nullptr, rect, FORM_FORWARD_FOCUS),
WidgetsContainerImpl(Window* parent, const rect_t& rect,
PersistentData* persistentData) :
WidgetsContainer(parent, rect, FORM_FORWARD_FOCUS),
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
{
if (index >= N)
return nullptr;
widgets[index] = nullptr;
memset(persistentData->zones[index].widgetName, 0,
sizeof(ZonePersistentData::widgetName));
memset(&persistentData->zones[index].widgetData, 0,
sizeof(Widget::PersistentData));
}
// remove old one if existing
removeWidget(index);
Widget* getWidget(unsigned int index) override
{
if (index < N) return widgets[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;
return nullptr;
}
if (widget)
widget->attach(this);
virtual void create() { memset(persistentData, 0, sizeof(PersistentData)); }
return widget;
}
void removeWidget(unsigned int index) override
{
if (index >= N)
return;
if (widgets[index]) {
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);
}
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;
}
}
inline ZoneOptionValue * getOptionValue(unsigned int index) const
{
return &persistentData->options[index].value;
}
inline void setOptionValue(unsigned int index, const ZoneOptionValue& value)
{
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();
}
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);
}
}
}
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);
}
inline ZoneOptionValue* getOptionValue(unsigned int index) const
{
return &persistentData->options[index].value;
}
inline void setOptionValue(unsigned int index, const ZoneOptionValue& value)
{
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;
Widget * widgets[N] = {};
}
void adjustLayout() override {}
protected:
PersistentData* persistentData;
Widget* widgets[N] = {};
};
template<class T>
class BaseWidgetFactory: public WidgetFactory
template <class T>
class BaseWidgetFactory : public WidgetFactory
{
public:
BaseWidgetFactory(const char * name, const ZoneOption * options, const char * displayName = nullptr):
public:
BaseWidgetFactory(const char* name, const ZoneOption* options,
const char* displayName = nullptr) :
WidgetFactory(name, options, displayName)
{
}
{
}
Widget * create(FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData, bool init = true) const override
{
initPersistentData(persistentData, init);
return new T(this, parent, rect, persistentData);
}
Widget* create(FormGroup* parent, const rect_t& rect,
Widget::PersistentData* persistentData,
bool init = true) const override
{
initPersistentData(persistentData, init);
return new T(this, parent, rect, persistentData);
}
};