diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-12-09 17:02:38 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-12-09 17:02:38 +0100 |
commit | 9c3224b0474675195b53b4d58b27a854f405c3f4 (patch) | |
tree | 85cbbaeb1ec9167fee0b9e72bcad5ec3946f336e | |
parent | 2eadbbd85105ad1199c43c72f551ddd69538e994 (diff) |
automation widget updates automation
-rw-r--r-- | confui/confui.pro | 2 | ||||
-rw-r--r-- | confui/mesh_connector.cpp | 38 | ||||
-rw-r--r-- | confui/mesh_connector.h | 5 | ||||
-rw-r--r-- | confui/ui_automation.cpp | 39 | ||||
-rw-r--r-- | confui/ui_automation.h | 4 | ||||
-rw-r--r-- | confui/ui_node.cpp | 5 | ||||
-rw-r--r-- | confui/ui_node.h | 3 | ||||
-rw-r--r-- | confui/ui_tab_automations.cpp | 4 | ||||
-rw-r--r-- | confui/ui_tab_node_overview.cpp | 2 |
9 files changed, 71 insertions, 31 deletions
diff --git a/confui/confui.pro b/confui/confui.pro index 6d36db2..2c4df61 100644 --- a/confui/confui.pro +++ b/confui/confui.pro @@ -21,4 +21,4 @@ HEADERS += \ CONFIG += c++17 CONFIG += force_debug_info - +QMAKE_CXXFLAGS += -Wno-missing-field-initializers diff --git a/confui/mesh_connector.cpp b/confui/mesh_connector.cpp index 56069bd..2994363 100644 --- a/confui/mesh_connector.cpp +++ b/confui/mesh_connector.cpp @@ -21,7 +21,6 @@ cd_uid_t CDMeshConnector::get_new_node_id() { CDMeshConnector::CDMeshConnector() { cd_uid_t berta = create_node({ - .id = 0, // automatically assigned (satisfy compiler) .address = { 0x00, 0xff, 0x21, 0x69, 0xf2, 0x31 }, .name_len = 5, .name = "berta", @@ -30,7 +29,6 @@ CDMeshConnector::CDMeshConnector() { }); cd_uid_t gerrit = create_node({ - .id = 0, // automatically assigned (satisfy compiler) .address = { 0x0e, 0xf9, 0x46, 0x4d, 0xe8, 0x02 }, .name_len = 6, .name = "gerrit", @@ -38,10 +36,20 @@ CDMeshConnector::CDMeshConnector() { .provisioned = false, }); - create_link(berta, berta, CD_AUTOMATION_TYPE_TOGGLE); - create_link(berta, berta, CD_AUTOMATION_TYPE_TOGGLE); - create_link(gerrit, berta, CD_AUTOMATION_TYPE_TURN_OFF); - create_link(gerrit, gerrit, CD_AUTOMATION_TYPE_TURN_ON); + // garbage code below (only for dummy links) + cd_link_t temp; + temp = create_link(berta, berta, CD_AUTOMATION_TYPE_TOGGLE); + _links[temp]->valid = true; + update_link(temp, _links[temp]); + temp = create_link(berta, berta, CD_AUTOMATION_TYPE_TOGGLE); + _links[temp]->valid = true; + update_link(temp, _links[temp]); + temp = create_link(gerrit, berta, CD_AUTOMATION_TYPE_TURN_OFF); + _links[temp]->valid = true; + update_link(temp, _links[temp]); + temp = create_link(gerrit, gerrit, CD_AUTOMATION_TYPE_TURN_ON); + _links[temp]->valid = true; + update_link(temp, _links[temp]); return; } @@ -91,8 +99,13 @@ map<cd_uid_t, cd_s_node*> CDMeshConnector::get_nodes(bool provisioned) { return nodes; } -map<cd_link_t, cd_s_automation*> CDMeshConnector::get_links() { - return _links; +map<cd_link_t, cd_s_automation*> CDMeshConnector::get_links(bool valid) { + map<cd_link_t, cd_s_automation*> links; + for (pair<cd_link_t, cd_s_automation*> link : _links) { + if (valid && (link.second->valid == false)) continue; + links[link.first] = link.second; + } + return links; } void CDMeshConnector::update_link(cd_link_t link, cd_s_automation* automation) { @@ -108,6 +121,7 @@ cd_link_t CDMeshConnector::create_link(cd_uid_t button, cd_uid_t light, enum cd_ automation->type = type; automation->button = _nodes[button]; automation->light = _nodes[light]; + automation->valid = false; printf("(new) "); update_link(id, automation); @@ -156,3 +170,11 @@ string CDMeshConnector::cd_mac_to_string(cd_mac_addr_t mac) { return ret; } +cd_s_automation* CDMeshConnector::get_link(cd_link_t id) { + return _links[id]; +} + +cd_s_node* CDMeshConnector::get_node(cd_uid_t id) { + return _nodes[id]; +} + diff --git a/confui/mesh_connector.h b/confui/mesh_connector.h index 7ead59a..659b9fc 100644 --- a/confui/mesh_connector.h +++ b/confui/mesh_connector.h @@ -35,6 +35,7 @@ typedef struct { cd_e_automation_type type; cd_s_node* button; cd_s_node* light; + bool valid; } cd_s_automation; class CDMeshConnector { @@ -60,8 +61,10 @@ public: virtual void refresh_config_sync(); // data fetching functions - virtual map<cd_link_t, cd_s_automation*> get_links(); + virtual map<cd_link_t, cd_s_automation*> get_links(bool valid = true); virtual map<cd_uid_t, cd_s_node*> get_nodes(bool provisioned = false); + virtual cd_s_automation* get_link(cd_link_t id); + virtual cd_s_node* get_node(cd_uid_t id); // network modification functions virtual cd_link_t create_link(cd_uid_t button, cd_uid_t light, enum cd_e_automation_type action); diff --git a/confui/ui_automation.cpp b/confui/ui_automation.cpp index 1c5c1f0..cecb88e 100644 --- a/confui/ui_automation.cpp +++ b/confui/ui_automation.cpp @@ -1,3 +1,4 @@ +#include "ui_tab_automations.h" #include "ui_automation.h" #include "mesh_connector.h" @@ -5,12 +6,13 @@ using std::pair; CDAutomationWidget::~CDAutomationWidget() { } CDAutomationWidget::CDAutomationWidget(QWidget* parent) : QWidget(parent) { + _parent = (CDAutomationsTabWidget*) parent; main_layout = new QHBoxLayout; dropdown_button = new QComboBox; dropdown_action = new QComboBox; dropdown_light = new QComboBox; - button_remove = new QPushButton; + button_remove = new QPushButton("Delete"); main_layout->addWidget(dropdown_button); main_layout->addWidget(dropdown_action); @@ -20,18 +22,20 @@ CDAutomationWidget::CDAutomationWidget(QWidget* parent) : QWidget(parent) { connect(button_remove, &QPushButton::clicked, this, &CDAutomationWidget::remove); + connect(dropdown_button, qOverload<int>(&QComboBox::activated), this, &CDAutomationWidget::apply); + connect(dropdown_action, qOverload<int>(&QComboBox::activated), this, &CDAutomationWidget::apply); + connect(dropdown_light, qOverload<int>(&QComboBox::activated), this, &CDAutomationWidget::apply); + update(); setLayout(main_layout); } -void CDAutomationWidget::set_automation(cd_link_t link, cd_s_automation* automation) { +void CDAutomationWidget::set_automation(cd_link_t link) { _id = link; - _automation = automation; + _automation = g_cd_mesh_connector->get_link(link); } void CDAutomationWidget::update() { - button_remove->setText("Delete"); - map<cd_uid_t, cd_s_node*> nodes = g_cd_mesh_connector->get_nodes(false); dropdown_button->clear(); @@ -43,34 +47,41 @@ void CDAutomationWidget::update() { label.append(QString::fromStdString(CDMeshConnector::cd_mac_to_string(node.second->address))); label.append(")"); - QString userData = QString::fromLocal8Bit((char*) node.second->address, 6); - dropdown_button->addItem(label, userData); - dropdown_light->addItem(label, userData); + dropdown_button->addItem(label, node.second->id); + dropdown_light->addItem(label, node.second->id); } - if (_automation == nullptr) return; - - dropdown_button->setCurrentIndex(dropdown_button->findData(QString::fromLocal8Bit((char*) _automation->button->address, 6))); - dropdown_action->clear(); dropdown_action->addItem("Toggles", CD_AUTOMATION_TYPE_TOGGLE); dropdown_action->addItem("Switches on", CD_AUTOMATION_TYPE_TURN_ON); dropdown_action->addItem("Switches off", CD_AUTOMATION_TYPE_TURN_OFF); - dropdown_action->setCurrentIndex(dropdown_action->findData(_automation->type)); - dropdown_light->setCurrentIndex(dropdown_light->findData(QString::fromLocal8Bit((char*) _automation->light->address, 6))); + if (_automation == nullptr) return; + + dropdown_button->setCurrentIndex(_automation->button == nullptr ? -1 : dropdown_button->findData(_automation->button->id)); + dropdown_action->setCurrentIndex(dropdown_action->findData(_automation->type)); + dropdown_light->setCurrentIndex(_automation->light == nullptr ? -1 : dropdown_light->findData(_automation->light->id)); } bool CDAutomationWidget::conf_valid() { + if (dropdown_button->currentIndex() == -1) return false; + if (dropdown_action->currentIndex() == -1) return false; + if (dropdown_light->currentIndex() == -1) return false; return true; } void CDAutomationWidget::apply() { if (!conf_valid()) return; + _automation->button = g_cd_mesh_connector->get_node(dropdown_button->findData(dropdown_button->currentIndex())); + _automation->type = (enum cd_e_automation_type) dropdown_action->findData(dropdown_action->currentIndex()); + _automation->light = g_cd_mesh_connector->get_node(dropdown_light->findData(dropdown_light->currentIndex())); + + _automation->valid = true; g_cd_mesh_connector->update_link(_id, _automation); } void CDAutomationWidget::remove() { g_cd_mesh_connector->remove_link(_id); + if (_parent != nullptr) _parent->update(); } diff --git a/confui/ui_automation.h b/confui/ui_automation.h index 7ea4cbb..e4118dc 100644 --- a/confui/ui_automation.h +++ b/confui/ui_automation.h @@ -8,12 +8,14 @@ #include "mesh_connector.h" +class CDAutomationsTabWidget; class CDAutomationWidget : public QWidget { Q_OBJECT private: cd_s_automation* _automation = nullptr; cd_link_t _id; + CDAutomationsTabWidget* _parent = nullptr; QHBoxLayout* main_layout; QComboBox* dropdown_button; @@ -27,7 +29,7 @@ public: /** @brief redraw ui with new values */ virtual void update(); /** @brief register automation that this widget controls */ - virtual void set_automation(cd_link_t link, cd_s_automation* automation); + virtual void set_automation(cd_link_t link); /** @brief test if all fields are valid / entered */ virtual bool conf_valid(); /** @brief apply new automation rule */ diff --git a/confui/ui_node.cpp b/confui/ui_node.cpp index 41ffad6..3bb3bc7 100644 --- a/confui/ui_node.cpp +++ b/confui/ui_node.cpp @@ -21,8 +21,9 @@ CDNodeWidget::CDNodeWidget(QWidget *parent) : QWidget(parent) { setLayout(main_layout); } -void CDNodeWidget::set_node(cd_s_node* node) { - _node = node; +void CDNodeWidget::set_node(cd_uid_t id) { + _id = id; + _node = g_cd_mesh_connector->get_node(id); } void CDNodeWidget::update() { diff --git a/confui/ui_node.h b/confui/ui_node.h index 9e12b60..615921b 100644 --- a/confui/ui_node.h +++ b/confui/ui_node.h @@ -14,6 +14,7 @@ class CDNodeWidget : public QWidget { private: cd_s_node* _node = nullptr; + cd_uid_t _id; QHBoxLayout* main_layout; QLabel* label_node_name; @@ -25,7 +26,7 @@ public: CDNodeWidget(QWidget *parent = nullptr); virtual ~CDNodeWidget(); virtual void update(); - virtual void set_node(cd_s_node* node); + virtual void set_node(cd_uid_t id); virtual void toggle_provision(); virtual void update_led(bool on); }; diff --git a/confui/ui_tab_automations.cpp b/confui/ui_tab_automations.cpp index dae00ba..53349dd 100644 --- a/confui/ui_tab_automations.cpp +++ b/confui/ui_tab_automations.cpp @@ -42,12 +42,12 @@ CDAutomationsTabWidget::CDAutomationsTabWidget(CDMainWindow* main_window) : QWid } void CDAutomationsTabWidget::update() { - map<cd_link_t, cd_s_automation*> links = this->mainwindow->mesh_connector->get_links(); + map<cd_link_t, cd_s_automation*> links = this->mainwindow->mesh_connector->get_links(false); for (pair<cd_link_t, cd_s_automation*> link : links) { if (automation_widgets.count(link.first) == 0) { automation_widgets[link.first] = new CDAutomationWidget(this); // create new automation - automation_widgets[link.first]->set_automation(link.first, link.second); + automation_widgets[link.first]->set_automation(link.first); automation_widgets[link.first]->update(); automations->addWidget(automation_widgets[link.first]); } else if (link.second != nullptr) { diff --git a/confui/ui_tab_node_overview.cpp b/confui/ui_tab_node_overview.cpp index 752bade..7b6c53a 100644 --- a/confui/ui_tab_node_overview.cpp +++ b/confui/ui_tab_node_overview.cpp @@ -22,7 +22,7 @@ void CDNodeOverviewTabWidget::update() { for (pair<cd_uid_t, cd_s_node*> node : nodes) { if (node_widgets.count(node.first) == 0) { node_widgets[node.first] = new CDNodeWidget(this); // create new node widget - node_widgets[node.first]->set_node(node.second); + node_widgets[node.first]->set_node(node.first); node_widgets[node.first]->update(); main_layout->addWidget(node_widgets[node.first]); } else if (node.second != nullptr) { |