From b23cb30884a7c1d2b7826fc18a7269bb5ef66dc3 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Tue, 25 Jan 2022 08:13:12 +0100 Subject: [PATCH] Mandate providing a parent on layout creation --- radio/src/gui/colorlcd/layout.cpp | 13 +- radio/src/gui/colorlcd/layout.h | 6 +- radio/src/gui/colorlcd/layouts/layout1+2.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout1+3.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout1x1.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout1x2.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout1x3.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout1x4.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout2+1.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout2+3.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout2x1.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout2x2.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout2x3.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout2x4.cpp | 4 +- radio/src/gui/colorlcd/layouts/layout4+2.cpp | 4 +- .../colorlcd/layouts/layout_factory_impl.cpp | 4 +- .../colorlcd/layouts/layout_factory_impl.h | 10 +- .../src/gui/colorlcd/layouts/topbar_impl.cpp | 4 +- .../colorlcd/widgets/widgets_container_impl.h | 239 +++++++++--------- 19 files changed, 164 insertions(+), 164 deletions(-) diff --git a/radio/src/gui/colorlcd/layout.cpp b/radio/src/gui/colorlcd/layout.cpp index 972f7e9ce..f4c2f8f83 100644 --- a/radio/src/gui/colorlcd/layout.cpp +++ b/radio/src/gui/colorlcd/layout.cpp @@ -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) { diff --git a/radio/src/gui/colorlcd/layout.h b/radio/src/gui/colorlcd/layout.h index 2418db9a7..eddd62e5c 100644 --- a/radio/src/gui/colorlcd/layout.h +++ b/radio/src/gui/colorlcd/layout.h @@ -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(); diff --git a/radio/src/gui/colorlcd/layouts/layout1+2.cpp b/radio/src/gui/colorlcd/layouts/layout1+2.cpp index df968e6fd..6691f701b 100644 --- a/radio/src/gui/colorlcd/layouts/layout1+2.cpp +++ b/radio/src/gui/colorlcd/layouts/layout1+2.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout1+3.cpp b/radio/src/gui/colorlcd/layouts/layout1+3.cpp index 6793deba0..668765e6a 100644 --- a/radio/src/gui/colorlcd/layouts/layout1+3.cpp +++ b/radio/src/gui/colorlcd/layouts/layout1+3.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout1x1.cpp b/radio/src/gui/colorlcd/layouts/layout1x1.cpp index 2fd47996a..8e2da48d4 100644 --- a/radio/src/gui/colorlcd/layouts/layout1x1.cpp +++ b/radio/src/gui/colorlcd/layouts/layout1x1.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout1x2.cpp b/radio/src/gui/colorlcd/layouts/layout1x2.cpp index 9c90bf05c..88e1dfccf 100644 --- a/radio/src/gui/colorlcd/layouts/layout1x2.cpp +++ b/radio/src/gui/colorlcd/layouts/layout1x2.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout1x3.cpp b/radio/src/gui/colorlcd/layouts/layout1x3.cpp index 140dcb4b4..12fd83406 100644 --- a/radio/src/gui/colorlcd/layouts/layout1x3.cpp +++ b/radio/src/gui/colorlcd/layouts/layout1x3.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout1x4.cpp b/radio/src/gui/colorlcd/layouts/layout1x4.cpp index acf4e097e..69793e7a9 100644 --- a/radio/src/gui/colorlcd/layouts/layout1x4.cpp +++ b/radio/src/gui/colorlcd/layouts/layout1x4.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout2+1.cpp b/radio/src/gui/colorlcd/layouts/layout2+1.cpp index ed12b0153..cf0ada650 100644 --- a/radio/src/gui/colorlcd/layouts/layout2+1.cpp +++ b/radio/src/gui/colorlcd/layouts/layout2+1.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout2+3.cpp b/radio/src/gui/colorlcd/layouts/layout2+3.cpp index 606a227b2..516aeafce 100644 --- a/radio/src/gui/colorlcd/layouts/layout2+3.cpp +++ b/radio/src/gui/colorlcd/layouts/layout2+3.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout2x1.cpp b/radio/src/gui/colorlcd/layouts/layout2x1.cpp index eb66cb579..c7d8984cf 100644 --- a/radio/src/gui/colorlcd/layouts/layout2x1.cpp +++ b/radio/src/gui/colorlcd/layouts/layout2x1.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout2x2.cpp b/radio/src/gui/colorlcd/layouts/layout2x2.cpp index 955e6a3fa..bb421c05f 100644 --- a/radio/src/gui/colorlcd/layouts/layout2x2.cpp +++ b/radio/src/gui/colorlcd/layouts/layout2x2.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout2x3.cpp b/radio/src/gui/colorlcd/layouts/layout2x3.cpp index d31f65cd6..b5cf88d9d 100644 --- a/radio/src/gui/colorlcd/layouts/layout2x3.cpp +++ b/radio/src/gui/colorlcd/layouts/layout2x3.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout2x4.cpp b/radio/src/gui/colorlcd/layouts/layout2x4.cpp index dd253ae03..420fe8016 100644 --- a/radio/src/gui/colorlcd/layouts/layout2x4.cpp +++ b/radio/src/gui/colorlcd/layouts/layout2x4.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout4+2.cpp b/radio/src/gui/colorlcd/layouts/layout4+2.cpp index df6de4e10..cc7599204 100644 --- a/radio/src/gui/colorlcd/layouts/layout4+2.cpp +++ b/radio/src/gui/colorlcd/layouts/layout4+2.cpp @@ -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) { } diff --git a/radio/src/gui/colorlcd/layouts/layout_factory_impl.cpp b/radio/src/gui/colorlcd/layouts/layout_factory_impl.cpp index b6a5ae6cb..41f9fd0c8 100644 --- a/radio/src/gui/colorlcd/layouts/layout_factory_impl.cpp +++ b/radio/src/gui/colorlcd/layouts/layout_factory_impl.cpp @@ -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())) { diff --git a/radio/src/gui/colorlcd/layouts/layout_factory_impl.h b/radio/src/gui/colorlcd/layouts/layout_factory_impl.h index 7297db763..bfde29232 100644 --- a/radio/src/gui/colorlcd/layouts/layout_factory_impl.h +++ b/radio/src/gui/colorlcd/layouts/layout_factory_impl.h @@ -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(); } diff --git a/radio/src/gui/colorlcd/layouts/topbar_impl.cpp b/radio/src/gui/colorlcd/layouts/topbar_impl.cpp index a1fa1c43d..11df502e0 100644 --- a/radio/src/gui/colorlcd/layouts/topbar_impl.cpp +++ b/radio/src/gui/colorlcd/layouts/topbar_impl.cpp @@ -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 diff --git a/radio/src/gui/colorlcd/widgets/widgets_container_impl.h b/radio/src/gui/colorlcd/widgets/widgets_container_impl.h index fb4a6bde2..2535f5e2e 100644 --- a/radio/src/gui/colorlcd/widgets/widgets_container_impl.h +++ b/radio/src/gui/colorlcd/widgets/widgets_container_impl.h @@ -25,149 +25,146 @@ #include "widgets_container.h" #include "draw_functions.h" -template -class WidgetsContainerImpl: public WidgetsContainer +template +class WidgetsContainerImpl : public WidgetsContainer { - public: - typedef WidgetsContainerPersistentData PersistentData; + public: + typedef WidgetsContainerPersistentData 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 BaseWidgetFactory: public WidgetFactory +template +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); + } };