File: Edge.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 (148 lines) | stat: -rw-r--r-- 3,724 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
package Edge;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use Node;
use QtCore4::isa qw( Qt::GraphicsItem );
use constant { Type => Qt::GraphicsItem::UserType() + 2 };
use Math::Trig qw(acos);
sub type() { return Type; }
    
sub dest() {
    return this->{dest};
}

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

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

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

use constant {
    Pi => 3.14159265358979323846264338327950288419717
};
use constant {
    TwoPi => 2.0 * Pi
};

sub NEW {
    my ($class, $sourceNode, $destNode) = @_;
    $class->SUPER::NEW();
    this->{arrowSize} = 10;
    this->setAcceptedMouseButtons(Qt::NoButton());
    this->{source} = $sourceNode;
    this->{dest} = $destNode;
    this->{source}->addEdge(this);
    this->{dest}->addEdge(this);
    this->adjust();
}

sub sourceNode
{
    return this->{source};
}

sub source
{
    return this->{source};
}

sub setSourceNode
{
    my ($node) = @_;
    this->{source} = $node;
    this->adjust();
}

sub destNode
{
    return this->{dest};
}

sub setDestNode
{
    my ($node) = @_;
    this->{dest} = $node;
    this->adjust();
}

sub adjust
{
    if (!this->source || !this->dest) {
        return;
    }

    my $line = Qt::LineF(this->mapFromItem(this->source, 1, 0), this->mapFromItem(this->dest, 0, 0));
    my $length = $line->length();

    this->prepareGeometryChange();

    if ($length > 20) {
        my $edgeOffset = Qt::PointF(($line->dx() * 10) / $length, ($line->dy() * 10) / $length);
        this->{sourcePoint} = $line->p1() + $edgeOffset;
        this->{destPoint} = $line->p2() - $edgeOffset;
    } else {
        this->{sourcePoint} = this->{destPoint} = $line->p1();
    }
}

sub boundingRect
{
    if (!this->source || !this->dest) {
        return Qt::RectF();
    }

    my $penWidth = 1;
    my $extra = ($penWidth + this->arrowSize) / 2.0;

    return Qt::RectF(this->sourcePoint, Qt::SizeF(this->destPoint->x() - this->sourcePoint->x(),
                                      this->destPoint->y() - this->sourcePoint->y()))
        ->normalized()
        ->adjusted(-$extra, -$extra, $extra, $extra);
}

sub paint
{
    my ($painter) = @_;
    if (!this->source || !this->dest) {
        return;
    }

    my $line = Qt::LineF(this->sourcePoint, this->destPoint);
    if (sprintf( '%02f', $line->length() ) == 0) {
        return;
    }

    # Draw the line itself
    $painter->setPen(Qt::Pen(Qt::Brush(Qt::Color(Qt::black())), 1, Qt::SolidLine(), Qt::RoundCap(), Qt::RoundJoin()));
    $painter->drawLine($line);

    # Draw the arrows
    my $angle = Math::Trig::acos($line->dx() / $line->length());
    if ($line->dy() >= 0) {
        $angle = TwoPi - $angle;
    }

    my $sourceArrowP1 = this->sourcePoint + Qt::PointF(sin($angle + Pi / 3) * this->arrowSize,
                                                  cos($angle + Pi / 3) * this->arrowSize);
    my $sourceArrowP2 = this->sourcePoint + Qt::PointF(sin($angle + Pi - Pi / 3) * this->arrowSize,
                                                  cos($angle + Pi - Pi / 3) * this->arrowSize);   
    my $destArrowP1 = this->destPoint + Qt::PointF(sin($angle - Pi / 3) * this->arrowSize,
                                              cos($angle - Pi / 3) * this->arrowSize);
    my $destArrowP2 = this->destPoint + Qt::PointF(sin($angle - Pi + Pi / 3) * this->arrowSize,
                                              cos($angle - Pi + Pi / 3) * this->arrowSize);

    $painter->setBrush(Qt::Brush(Qt::black()));
    $painter->drawPolygon(Qt::PolygonF([ $line->p1(), $sourceArrowP1, $sourceArrowP2 ]));
    $painter->drawPolygon(Qt::PolygonF([ $line->p2(), $destArrowP1, $destArrowP2 ]));
}

1;