-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiled_designer.cpp
240 lines (217 loc) · 8 KB
/
profiled_designer.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <QtWidgets>
#include "profiled_designer.h"
#include "ui_profiled_designer.h"
profiled_designer::profiled_designer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::profiled_designer),
selected_led_id(-1)
{
ui->setupUi(this);
//Set Scene for LED Design scene
scene = new design_scene(this);
ui->graphicsView->setScene(scene);
scene->setBackgroundBrush(QBrush(Qt::black,Qt::SolidPattern));
createActions();
createMenus();
// Add the vertical lines first, paint them red
for (int x=0; x<=1000; x+=20) {
scene->addLine(x,0,x,1000, QPen(Qt::white));
}
// Now add the horizontal lines, paint them green
for (int y=0; y<=1000; y+=20) {
scene->addLine(0,y,1000,y, QPen(Qt::white));
}
//Setup 10ms Timer
timer = new QTimer(this);
//Connect Signals and slots
QObject::connect(scene,SIGNAL(led_selected(qint8)), this, SLOT(led_selected_handler(qint8)));
QObject::connect(ui->color_select, SIGNAL(clicked(bool)), this, SLOT(color_select_handler()));
QObject::connect(ui->start_color_select, SIGNAL(clicked(bool)), this, SLOT(start_color_select_handler()));
QObject::connect(ui->end_color_select, SIGNAL(clicked(bool)), this, SLOT(end_color_select_handler()));
QObject::connect(ui->add_pattern, SIGNAL(clicked(bool)), this, SLOT(add_pattern_handler()));
QObject::connect(ui->play_button, SIGNAL(clicked(bool)), this, SLOT(play_button_handler()));
QObject::connect(ui->pause_button, SIGNAL(clicked(bool)), this, SLOT(pause_button_handler()));
QObject::connect(ui->create_bin, SIGNAL(clicked(bool)), this, SLOT(create_bin_handler()));
QObject::connect(ui->remove_pattern, SIGNAL(clicked(bool)), this, SLOT(remove_pattern_handler()));
QObject::connect(timer, SIGNAL(timeout()), scene, SLOT(loop_player()));
QObject::connect(ui->delete_led, SIGNAL(clicked(bool)), this, SLOT(delete_led_handler()));
}
void profiled_designer::play_button_handler()
{
scene->set_loop_time(ui->loop_duration->value());
timer->start(10);
}
void profiled_designer::pause_button_handler()
{
timer->stop();
}
void profiled_designer::create_bin_handler()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("LED Designer Binary Files"), tr(".ledbin"));
scene->save_patterns_to_file(fileName);
}
void profiled_designer::remove_pattern_handler()
{
if(selected_led_id == -1) {
return;
}
qint16 remove_idx = ui->pattern_list->currentIndex().row();
if(remove_idx >= 0) {
scene->remove_led_pattern(selected_led_id, remove_idx);
}
ui->pattern_list->removeItemWidget(ui->pattern_list->currentItem());
delete ui->pattern_list->currentItem();
}
void profiled_designer::delete_led_handler()
{
if(selected_led_id == -1) {
return;
}
scene->delete_led(selected_led_id);
selected_led_id -= 1;
if(selected_led_id == -1) {
ui->led_id->setText("");
}
}
void profiled_designer::led_selected_handler(qint8 led_id)
{
selected_led_id = led_id;
ui->led_id->setText(QString::number(led_id));
//update and populate pattern list
update_params(true);
}
void profiled_designer::update_params(bool update_list)
{
qint16 consumed_time = 0;
qint16 global_total_time = ui->loop_duration->value();
QList<pattern> pattern_list = scene->get_led_pattern_list(selected_led_id);
//empty pattern list
if(update_list) {
ui->pattern_list->clear();
}
for(int i = 0; i < pattern_list.length(); i++) {
if(update_list) {
ui->pattern_list->addItem(pattern_list[i].toString());
}
consumed_time += pattern_list[i].offset + pattern_list[i].total_time;
}
//reset colors and values
ui->total_time->setMaximum(global_total_time*100 - consumed_time);
if(global_total_time*100 - consumed_time <= 0) {
ui->offset_time->setMaximum(0);
} else {
ui->offset_time->setMaximum(global_total_time*100 - consumed_time - 1);
}
}
void profiled_designer::color_select_handler()
{
if(selected_led_id == -1 || !ui->solid_color->isChecked()) {
return;
}
QColorDialog *color_dialog = new QColorDialog(Qt::white,this);
QObject::connect(color_dialog, SIGNAL(currentColorChanged(QColor)), this, SLOT(color_dialog_handler(QColor)));
QObject::connect(color_dialog, SIGNAL(colorSelected(QColor)), this, SLOT(color_dialog_handler(QColor)));
color_dialog->show();
}
void profiled_designer::color_dialog_handler(QColor color)
{
scene->set_led_color(selected_led_id, color);
QPalette palette;
QColor neg_color(255 - color.red(), 255 - color.green(), 255 - color.blue());
palette.setColor(QPalette::Base,color);
palette.setColor(QPalette::Text,neg_color);
ui->solid_color_text->setPalette(palette);
ui->solid_color_text->setText(color.name().toUpper());
curr_pattern.start_color = color;
curr_pattern.end_color = color;
}
void profiled_designer::start_color_select_handler()
{
if(selected_led_id == -1 || !ui->pattern->isChecked()) {
return;
}
curr_pattern.start_color = QColorDialog::getColor(Qt::white, this);
scene->set_led_color(selected_led_id, curr_pattern.start_color);
QPalette palette;
QColor neg_color(255 - curr_pattern.start_color.red(),
255 - curr_pattern.start_color.green(),
255 - curr_pattern.start_color.blue());
palette.setColor(QPalette::Base,curr_pattern.start_color);
palette.setColor(QPalette::Text,neg_color);
ui->start_color->setPalette(palette);
ui->start_color->setText(curr_pattern.start_color.name().toUpper());
}
void profiled_designer::end_color_select_handler()
{
if(selected_led_id == -1 || !ui->pattern->isChecked()) {
return;
}
curr_pattern.end_color = QColorDialog::getColor(Qt::white, this);
QPalette palette;
QColor neg_color(255 - curr_pattern.end_color.red(),
255 - curr_pattern.end_color.green(),
255 - curr_pattern.end_color.blue());
palette.setColor(QPalette::Base,curr_pattern.end_color);
palette.setColor(QPalette::Text,neg_color);
ui->end_color->setPalette(palette);
ui->end_color->setText(curr_pattern.end_color.name().toUpper());
}
void profiled_designer::add_pattern_handler()
{
if(selected_led_id == -1) {
return;
}
if(ui->pattern->isChecked()) {
curr_pattern.is_solid = false;
} else {
curr_pattern.is_solid = true;
}
curr_pattern.offset = ui->offset_time->value();
curr_pattern.mid = ui->pattern_mid->value();
curr_pattern.total_time = ui->total_time->value();
scene->push_led_pattern(selected_led_id ,curr_pattern);
ui->pattern_list->addItem(curr_pattern.toString());
//update and populate pattern list
update_params(false);
}
#ifndef QT_NO_CONTEXTMENU
void profiled_designer::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.exec(event->globalPos());
}
#endif // QT_NO_CONTEXTMENU
void profiled_designer::open()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("LED Designer Project Files"), QDir::homePath(), tr("LED Project (*.ledproj)"));
scene->load_ledproj(fileName);
}
void profiled_designer::save()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("LED Designer Project Files"), tr(".ledproj"));
scene->save_ledproj(fileName);
}
void profiled_designer::createActions()
{
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, &QAction::triggered, this, &profiled_designer::open);
saveAct = new QAction(tr("&Save"), this);
saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip(tr("Save the document to disk"));
connect(saveAct, &QAction::triggered, this, &profiled_designer::save);
}
void profiled_designer::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
}
profiled_designer::~profiled_designer()
{
delete ui;
}