-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstractfunction.cpp
145 lines (119 loc) · 3.76 KB
/
abstractfunction.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
#include "abstractfunction.h"
#include "function.h"
#include "parametricfunction.h"
AbstractFunction::AbstractFunction(const QString & _id, QWidget *parent)
: QWidget(parent),
id(_id),
is_selected(false),
main_layout(nullptr),
top_layout(nullptr),
check(nullptr),
color_button(nullptr),
icon_color(nullptr),
pix_color(nullptr),
id_func(nullptr),
bottom_layout(nullptr),
bdelete(nullptr)
{
main_layout = new QVBoxLayout(this);
top_layout = new QHBoxLayout;
bottom_layout = new QVBoxLayout;
//Check box
check = new QCheckBox;
check->setChecked(true);
//Id Label
id_func = new QLineEdit;
id_func->setText(QString("%1").arg(id));
//Color_button
pix_color = new QPixmap(10,10);
color = randomColor();
pix_color->fill(color);
icon_color = new QIcon(*pix_color);
color_button = new QPushButton(*icon_color,QString(""), this);
//Delete button
bdelete = new QPushButton(QIcon(":/images/delete_button.png"),QString(""),this);
bdelete->setVisible(false); /* need debug */
//Layout management
top_layout->addWidget(check,0,Qt::AlignLeft);
top_layout->addWidget(color_button,0,Qt::AlignLeft);
top_layout->addWidget(id_func,0,Qt::AlignLeft);
top_layout->addWidget(bdelete);
main_layout->addLayout(top_layout);
main_layout->addLayout(bottom_layout);
setLayout(main_layout);
//Connections
QObject::connect(this, SIGNAL(selected(AbstractFunction*)), parentWidget(), SLOT(updateSelected(AbstractFunction*)));
QObject::connect(this, SIGNAL(delete_me(AbstractFunction*)), parentWidget(), SLOT(delete_func(AbstractFunction*)));
QObject::connect(this, SIGNAL(needUpdate()), parentWidget(), SLOT(updateFunctions()));
QObject::connect(bdelete,SIGNAL(clicked(bool)), this, SLOT(emit_delete_me()));
QObject::connect(color_button, SIGNAL(clicked(bool)), this, SLOT(choseColor()));
QObject::connect(id_func, SIGNAL(returnPressed()), this, SLOT(changeName()));
QObject::connect(check, SIGNAL(toggled(bool)), parentWidget(), SLOT(updateFunctions()));
}
AbstractFunction::~AbstractFunction()
{
delete main_layout;
delete top_layout;
delete id_func;
delete id_func;
delete bottom_layout;
delete color_button;
delete icon_color;
delete pix_color;
delete bdelete;
delete check;
}
void AbstractFunction::mouseReleaseEvent(QMouseEvent *)
{
is_selected = !is_selected;
if (is_selected)
emit selected(this);
else
emit selected(nullptr);
update();
emit needUpdate();
}
void AbstractFunction::choseColor()
{
color = QColorDialog::getColor(Qt::green, this);
pix_color->fill(color);
delete icon_color;
icon_color = new QIcon(*pix_color);
color_button->setIcon(*icon_color);
emit needUpdate();
}
void AbstractFunction::changeName()
{
id = id_func->text();
}
void AbstractFunction::emit_delete_me()
{
emit delete_me(this);
}
void AbstractFunction::new_select(AbstractFunction * new_func)
{
if (new_func == this)
is_selected = true;
else
is_selected = false;
update();
}
void AbstractFunction::paintEvent(QPaintEvent *)
{
QPainter pen(this);
if (is_selected)
pen.fillRect(0, 0, width(), height(), 0xA0A9AA);
else
pen.fillRect(0, 0, width(), height(), Qt::lightGray);
}
//Static function
AbstractFunction *AbstractFunction::loadFunction(const QString & input, QWidget *parent)
{
QStringList list = input.split(QChar(' '));
if ( list[0] == QString("Function") )
return Function::loadFunction(input, parent);
else if (list[0] == QString("Parametric"))
return ParametricFunction::loadFunction(input, parent);
else
throw "Une erreur"; /* need improvement */
}