-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphicsfibnode.cpp
196 lines (163 loc) · 4.89 KB
/
graphicsfibnode.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
#include "graphicsfibnode.h"
#include <limits>
//static members
GraphicsFibNode *GraphicsFibNode::minfNode = 0;
GraphicsFibNode *GraphicsFibNode::minfNode2 = 0;
GraphicsFibNode *GraphicsFibNode::selected = 0;
bool GraphicsFibNode::colorMins = true;
GraphicsFibNode::GraphicsFibNode() : GraphicsNode(), FibNodeBase(), root(true)
{}
GraphicsFibNode::GraphicsFibNode(int k) : GraphicsNode(), FibNodeBase(k), root(true)
{}
GraphicsFibNode::GraphicsFibNode(int k, int d, bool m) : GraphicsNode(), FibNodeBase(k,d,m), root(true)
{}
GraphicsFibNode::~GraphicsFibNode()
{
if(this->selected == this)
{
this->selected = 0;
}
}
void GraphicsFibNode::makeChildLink(GraphicsFibNode *child)
{
FibNodeBase::makeChildLink(child);
if(this->Child->degree < child->degree)
this->Child = child;
//the update fuction is only for the visuals/ graphics and has nothing to do with the algorithm
child->update(); //
}
void GraphicsFibNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
QBrush brush(Qt::lightGray);
QPen textPen(Qt::black, 0);
if (option->state & QStyle::State_Sunken || this == this->selected) {
brush.setColor(Qt::blue);
}
else {
if(this->mark)
{
brush.setColor(Qt::black);
textPen.setColor(Qt::white);
}
else if(this->colorMins && (this == this->minfNode || this == this->minfNode2) )
brush.setColor(Qt::red/*Qt::lightGray*/);
else
brush.setColor(Qt::lightGray);
}
painter->setBrush(brush);
painter->setPen(textPen);
painter->drawEllipse(-10, -10, 20, 20);
if(this->key != std::numeric_limits<int>::min())
{
QString num = QString::number(this->key);
painter->drawText(-4*num.size(),5, num);
}
else
{
QString num = QObject::trUtf8("-\u221e");
painter->drawText(
QRect(-10, -10, 20, 20), Qt::AlignCenter
, num);
}
}
void GraphicsFibNode::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
GraphicsFibNode *tmpNode = this->selected;
this->selected = this;
if(tmpNode != 0) // tu se pojavlja problem sigsegv, najverjetneje kaze an zbrisano lokacijo
tmpNode->update();
Pos = this->pos();
update();
QGraphicsItem::mousePressEvent(event);
}
void GraphicsFibNode::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
#if 0 // debugging
this->setPos(this->pos().rx(), Pos.ry()); //this->setPos(Pos);
GraphicsFibNode *tmp = this->child();
if(tmp)
{
tmp->setPos(this->pos().rx(), tmp->pos().ry());
}
#else
this->setPos(Pos);
#endif
update();
QGraphicsItem::mouseReleaseEvent(event);
}
void GraphicsFibNode::setStates()
{
GraphicsFibNode *first = this;
GraphicsFibNode *tmp = first->prev();
tmp->setPos(referencePoint); // 0 , 0
// tmp->positions << tmp->pos();
GraphicsFibNode * fNode;
GraphicsFibNode * sNode;
float offset = 1;
do
{
fNode = tmp;
sNode = tmp->prev();
offset = (float)getOffset(tmp/*, true*/)/*+2*/;//tmp->degree;
if(offset <= 4)
offset += 6;
offset = offset*((5*offset+offset)/(offset*0.7));
sNode->setPos(fNode->x()-offset/*(50*offset)*1.3*/, fNode->y());
// sNode->positions << sNode->pos();
formTree(tmp->child());
tmp = tmp->prev();
} while (tmp != first);
formTree(tmp->child());
//save states positions
savePositions(this); // otherwise the positions are screwed
}
void GraphicsFibNode::formTree(GraphicsFibNode * node)
{
if(!node)
return;
GraphicsFibNode *tmp = node->prev();
GraphicsFibNode *pNode = node->parent();
tmp->setPos(pNode->x(), pNode->y()+50);
GraphicsFibNode * fNode;
GraphicsFibNode * sNode;
float offset = 1;
while(tmp != node)
{
fNode = tmp;
sNode = tmp->prev();
offset = (float)getOffset(tmp/*, true*/)/*+2*/;// * (int)(100/(tmp->degree+1));
if(offset <= 4)
offset += 5;
offset = offset*((5*offset+offset)/(offset*0.7));
sNode->setPos(fNode->x()-offset/*(50*offset)*/, /*y+(50*offset)*/ fNode->y());
formTree(tmp->child());
tmp = tmp->prev();
}
formTree(tmp->child());
}
int GraphicsFibNode::getOffset(GraphicsFibNode *node)
{
int off = node->degree+1;
GraphicsFibNode *child = node->child();
if(child == 0)
return off;
do {
off++;
off += getOffset(child);
child = child->next();
} while (child != node->child());
return off;
}
void GraphicsFibNode::savePositions(GraphicsFibNode *node)
{
if(!node)
return;
GraphicsFibNode *nodeSet = node;
do
{
nodeSet->positions << nodeSet->pos();
savePositions(nodeSet->child());
nodeSet = nodeSet->next();
} while(nodeSet != node);
}