aboutsummaryrefslogtreecommitdiff
path: root/confui/ui_automation.cpp
blob: 72e2446dca87cbe3ff946db88ee323bc1bfa94a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "ui_automation.h"
#include "mesh_connector.h"

using std::pair;

CDAutomationWidget::~CDAutomationWidget() { }
CDAutomationWidget::CDAutomationWidget(QWidget* parent) : QWidget(parent) {
	main_layout = new QHBoxLayout;

	dropdown_button = new QComboBox;
	dropdown_action = new QComboBox;
	dropdown_light = new QComboBox;
	button_remove = new QPushButton;

	main_layout->addWidget(dropdown_button);
	main_layout->addWidget(dropdown_action);
	main_layout->addWidget(dropdown_light);
	main_layout->addStretch();
	main_layout->addWidget(button_remove);

	update();
	setLayout(main_layout);
}

void CDAutomationWidget::set_automation(cd_link_t link, cd_s_automation* automation) {
	_id = link;
	_automation = automation;
}

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();
	dropdown_light->clear();
	for (pair<cd_uid_t, cd_s_node*> node : nodes) {
		QString label = "";
		label.append(QString::fromLocal8Bit(node.second->name, node.second->name_len));
		label.append(" (");
		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);
	}

	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)));
}

bool CDAutomationWidget::conf_valid() {
	return true;
}

void CDAutomationWidget::apply() {
	if (!conf_valid()) return;

	g_cd_mesh_connector->update_link(_id, _automation);
}