File: Node.pm

package info (click to toggle)
qt4-perl 4.8.4-1.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,636 kB
  • ctags: 8,100
  • sloc: perl: 42,963; cpp: 28,039; makefile: 160; xml: 98; sh: 4
file content (171 lines) | stat: -rw-r--r-- 4,105 bytes parent folder | download | duplicates (3)
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
package Node;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::GraphicsItem );
use GraphWidget;
use List::Util qw( min max );

sub edgeList() {
    return this->{edgeList};
}

sub newPos() {
    return this->{newPos};
}

sub graph() {
    return this->{graph};
}

sub NEW
{
    my ($class, $graphWidget) = @_;
    $class->SUPER::NEW();
    this->{graph} = $graphWidget;
    this->setFlag(Qt::GraphicsItem::ItemIsMovable());
    this->setFlag(Qt::GraphicsItem::ItemSendsGeometryChanges());
    this->setCacheMode(Qt::GraphicsItem::DeviceCoordinateCache());
    this->setZValue(-1);
    this->{edgeList} = [];
}

sub addEdge
{
    my ($edge) = @_;
    push @{this->edgeList}, $edge;
    $edge->adjust();
}

sub edges
{
    return this->edgeList;
}

sub calculateForces
{
    my $mouseGrabberItem = this->scene()->mouseGrabberItem();
    if (!this->scene() || ($mouseGrabberItem && $mouseGrabberItem == this)) {
        this->{newPos} = this->pos();
        return;
    }
    
    # Sum up all forces pushing this item away
    my $xvel = 0;
    my $yvel = 0;
    foreach my $item (@{this->scene()->items()}) {
        my $node = $item;
        if (!$node->isa('Node')) {
            next;
        }

        my $line = Qt::LineF(this->mapFromItem($node, 0, 0), Qt::PointF(0, 0));
        my $dx = $line->dx();
        my $dy = $line->dy();
        my $l = 2.0 * ($dx * $dx + $dy * $dy);
        if ($l > 0) {
            $xvel += ($dx * 150.0) / $l;
            $yvel += ($dy * 150.0) / $l;
        }
    }

    # Now subtract all forces pulling items together
    my $weight = scalar @{this->edgeList} + 1 * 10;
    foreach my $edge (@{this->edgeList}) {
        my $pos;
        if ($edge->sourceNode() == this) {
            $pos = this->mapFromItem($edge->destNode(), 0, 0);
        }
        else {
            $pos = this->mapFromItem($edge->sourceNode(), 0, 0);
        }
        $xvel += $pos->x() / $weight;
        $yvel += $pos->y() / $weight;
    }
 
    if (abs($xvel) < 0.1 && abs($yvel) < 0.1) {
        $xvel = $yvel = 0;
    }

    my $sceneRect = this->scene()->sceneRect();
    this->{newPos} = this->pos() + Qt::PointF($xvel, $yvel);
    this->newPos->setX(min(max(this->newPos->x(), $sceneRect->left() + 10), $sceneRect->right() - 10));
    this->newPos->setY(min(max(this->newPos->y(), $sceneRect->top() + 10), $sceneRect->bottom() - 10));
}

sub advance
{
    if (this->newPos == this->pos()) {
        return 0;
    }

    this->setPos(this->newPos);
    return 1;
}

sub boundingRect
{
    my $adjust = 2;
    return Qt::RectF(-10 - $adjust, -10 - $adjust,
                  23 + $adjust, 23 + $adjust);
}

sub shape
{
    my $path = Qt::PainterPath();
    $path->addEllipse(-10, -10, 20, 20);
    return $path;
}

sub paint
{
    my ($painter, $option) = @_;
    $painter->setPen(Qt::NoPen());
    $painter->setBrush(Qt::Brush(Qt::Color(Qt::darkGray())));
    $painter->drawEllipse(-7, -7, 20, 20);

    my $gradient = Qt::RadialGradient(-3, -3, 10);
    if ($option->state & Qt::Style::State_Sunken()) {
        $gradient->setCenter(3, 3);
        $gradient->setFocalPoint(3, 3);
        $gradient->setColorAt(1, Qt::Color(Qt::yellow())->light(120));
        $gradient->setColorAt(0, Qt::Color(Qt::darkYellow())->light(120));
    } else {
        $gradient->setColorAt(0, Qt::Color(Qt::yellow()));
        $gradient->setColorAt(1, Qt::Color(Qt::darkYellow()));
    }
    $painter->setBrush(Qt::Brush($gradient));
    $painter->setPen(Qt::Pen(Qt::Brush(Qt::Color(Qt::black())), 0));
    $painter->drawEllipse(-10, -10, 20, 20);
}

sub itemChange
{
    my ($change, $value) = @_;
    if ($change == Qt::GraphicsItem::ItemPositionHasChanged()) {
        foreach my $edge (@{this->edgeList}) {
            $edge->adjust();
        }
        this->graph->itemMoved();
    };

    return this->SUPER::itemChange($change, $value);
}

sub mousePressEvent
{
    my ($event) = @_;
    this->update();
    this->SUPER::mousePressEvent($event);
}

sub mouseReleaseEvent
{
    my ($event) = @_;
    this->update();
    this->SUPER::mouseReleaseEvent($event);
}

1;