1 /* This file is part of WarpTree
3 * Copyright (C) 2007 Jos van den Oever <jos@vandenoever.info>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 // parts of code for layout was adapted from HyperTree published under
22 // MIT License 2001 www.bouthier.net
23 #include "modelnode.h"
24 #include <QtCore/QDebug>
27 ModelNode::layout(float angle
, float /*width*/, float length
) {
28 if (parent
== 0) return;
29 WarpCoord
zp(parent
->original
);
30 original
.set(length
* cos(angle
), length
* sin(angle
));
31 original
.translate(zp
);
34 ParentModelNode::layout(float angle
, float width
, float length
) {
35 ModelNode::layout(angle
, width
, length
);
38 this->length
= length
;
40 WarpCoord
a(cos(angle
), sin(angle
));
41 a
.translate(parent
->original
);
42 a
.translate(WarpCoord(-original
.x(), -original
.y()));
46 float A
= 1 + length
* length
;
48 width
= acos((A
* c
- B
) / (A
- B
* c
));
50 #define MODELLENGTH 0.5
51 int nbrChild
= children
.size();
52 float l1
= (0.95 - MODELLENGTH
);
53 float l2
= cos((20.0 * M_PI
) / (2.0 * nbrChild
+ 38.0));
54 length
= MODELLENGTH
+ (l1
* l2
);
56 float startAngle
= angle
- width
;
57 foreach(ModelNode
* child
, children
) {
58 float percent
= child
->weight
/ globalWeight
;
59 float childWidth
= width
* percent
;
60 float childAngle
= startAngle
+ childWidth
;
61 child
->layout(childAngle
, childWidth
, length
);
62 startAngle
+= 2.0 * childWidth
;
66 ModelNode::translate(const WarpCoord
& o
, float zoomFactor
) {
67 translated
= original
;
68 translated
.translate(o
);
69 if (zoomFactor
< 0.99 || zoomFactor
> 1.01) {
70 translated
.zoom(zoomFactor
);
73 showLine
= parent
&& translated
.valid() && parent
->translated
.valid()
74 && (translated
.visible() || parent
->translated
.visible());
76 lineCenter.set((original.x() + parent->original.x()) / 2,
77 (original.y() + parent->original.y()) / 2);
78 lineCenter.translate(o);
79 showLine = showLine && lineCenter.valid();
83 ModelNode::updateScreenCoords(float halfw
, float halfh
) {
84 int w
= node
.rect
.width();
85 int h
= node
.rect
.height();
86 int x
= (int)(((translated
.x() + 1) * halfw
) - w
/2);
87 int y
= (int)(((translated
.y() + 1) * halfh
) - h
/2);
90 node
.rect
.setCoords(x
, y
, w
, h
);