aboutsummaryrefslogtreecommitdiff
path: root/confui/ui_tab_automations.cpp
blob: 7dd150e034155f944743b7bcbd8f47dcfcb33e19 (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
71
72
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>

#include "ui_tab_automations.h"

using std::pair;

class CDAddAutomationWidget : public QWidget {
private:
	CDAutomationsTabWidget *_parent;

public:
	void new_automation() {
		g_cd_mesh_connector->create_link();
		_parent->update();
	}

	CDAddAutomationWidget(QWidget *parent) : QWidget(parent) {
		_parent = (CDAutomationsTabWidget *)parent;

		QHBoxLayout *main_layout = new QHBoxLayout;
		QPushButton *button_add	 = new QPushButton("Add automation");

		main_layout->addStretch();
		main_layout->addWidget(button_add);
		main_layout->addStretch();

		connect(button_add, &QPushButton::clicked, this, &CDAddAutomationWidget::new_automation);

		setLayout(main_layout);
	}

	~CDAddAutomationWidget() {}
};

CDAutomationsTabWidget::~CDAutomationsTabWidget() {}
CDAutomationsTabWidget::CDAutomationsTabWidget(CDMainWindow *main_window) : QWidget(main_window) {
	this->mainwindow	  = main_window;
	main_layout			  = new CDScrollContainerLayout(this);
	automations			  = new QVBoxLayout;
	new_automation_button = new CDAddAutomationWidget(this);

	QWidget *automations_widget = new QWidget(this);
	automations_widget->setLayout(automations);

	main_layout->addWidget(automations_widget);
	main_layout->addWidget(new_automation_button);

	update();
	setLayout(main_layout);
}

void CDAutomationsTabWidget::update() {
	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);
			automation_widgets[link.first]->update();
			automations->addWidget(automation_widgets[link.first]);
		} else if (link.second != nullptr) {
			automation_widgets[link.first]->update(); // update existing widget
		} else if (automation_widgets[link.first] != nullptr) {
			automations->removeWidget(automation_widgets[link.first]); // remove removed automation
			delete automation_widgets[link.first];					   // free automation widget
			automation_widgets[link.first] = nullptr;
		}
	}
}