]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/Node.cxx
Pipeline editor added.
[cpPlugins.git] / appli / cpPipelineEditor / Node.cxx
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QGraphicsScene>
42 #include <QGraphicsSceneMouseEvent>
43 #include <QPainter>
44 #include <QStyleOption>
45
46 #include "Edge.h"
47 #include "Node.h"
48 #include "GraphWidget.h"
49
50 Node::Node(GraphWidget *graphWidget, const std::string& label)
51   : graph(graphWidget),
52     m_Label( label )
53 {
54     setFlag(ItemIsMovable);
55     setFlag(ItemSendsGeometryChanges);
56     setCacheMode(DeviceCoordinateCache);
57     setZValue(-1);
58     this->setToolTip( this->m_Label.c_str( ) );
59 }
60
61 void Node::addEdge(Edge *edge)
62 {
63     edgeList << edge;
64     edge->adjust();
65 }
66
67 QList<Edge *> Node::edges() const
68 {
69     return edgeList;
70 }
71
72 void Node::calculateForces()
73 {
74     if (!scene() || scene()->mouseGrabberItem() == this) {
75         newPos = pos();
76         return;
77     }
78
79     // Sum up all forces pushing this item away
80     qreal xvel = 0;
81     qreal yvel = 0;
82     foreach (QGraphicsItem *item, scene()->items()) {
83         Node *node = qgraphicsitem_cast<Node *>(item);
84         if (!node)
85             continue;
86
87         QPointF vec = mapToItem(node, 0, 0);
88         qreal dx = vec.x();
89         qreal dy = vec.y();
90         double l = 2.0 * (dx * dx + dy * dy);
91         if (l > 0) {
92             xvel += (dx * 150.0) / l;
93             yvel += (dy * 150.0) / l;
94         }
95     }
96
97     // Now subtract all forces pulling items together
98     double weight = (edgeList.size() + 1) * 10;
99     foreach (Edge *edge, edgeList) {
100         QPointF vec;
101         if (edge->sourceNode() == this)
102             vec = mapToItem(edge->destNode(), 0, 0);
103         else
104             vec = mapToItem(edge->sourceNode(), 0, 0);
105         xvel -= vec.x() / weight;
106         yvel -= vec.y() / weight;
107     }
108
109     if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
110         xvel = yvel = 0;
111
112     QRectF sceneRect = scene()->sceneRect();
113     newPos = pos() + QPointF(xvel, yvel);
114     newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
115     newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
116
117     newPos = pos(); // + QPointF(xvel, yvel);
118 }
119
120 bool Node::advance()
121 {
122     if (newPos == pos())
123         return false;
124
125     setPos(newPos);
126     return true;
127 }
128
129 QRectF Node::boundingRect() const
130 {
131 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
132     // Add some extra space around the circle for easier touching with finger
133     qreal adjust = 30;
134     return QRectF( -10 - adjust, -10 - adjust,
135                   20 + adjust * 2, 20 + adjust * 2);
136 #else
137     qreal adjust = 2;
138     return QRectF( -10 - adjust, -10 - adjust,
139                   23 + adjust, 23 + adjust);
140 #endif
141 }
142
143 QPainterPath Node::shape() const
144 {
145     QPainterPath path;
146 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
147     // Add some extra space around the circle for easier touching with finger
148     path.addEllipse( -40, -40, 80, 80);
149 #else
150     path.addEllipse(-10, -10, 20, 20);
151 #endif
152     return path;
153 }
154
155 void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
156 {
157     painter->setPen(Qt::NoPen);
158     painter->setBrush(Qt::darkGray);
159     painter->drawEllipse(-7, -7, 20, 20);
160
161     QPointF text_point = this->boundingRect( ).bottomRight( ) + this->pos( );
162     painter->drawText( text_point, this->m_Label.c_str( ) );
163     std::cout << text_point.x( ) << " " << text_point.y( ) << std::endl;
164
165     QRadialGradient gradient(-3, -3, 10);
166     if (option->state & QStyle::State_Sunken) {
167         gradient.setCenter(3, 3);
168         gradient.setFocalPoint(3, 3);
169         gradient.setColorAt(1, QColor(Qt::yellow).light(120));
170         gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
171     } else {
172         gradient.setColorAt(0, Qt::yellow);
173         gradient.setColorAt(1, Qt::darkYellow);
174     }
175     painter->setBrush(gradient);
176
177     painter->setPen(QPen(Qt::black, 0));
178     painter->drawEllipse(-10, -10, 20, 20);
179 }
180
181 QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
182 {
183     switch (change) {
184     case ItemPositionHasChanged:
185         foreach (Edge *edge, edgeList)
186             edge->adjust();
187         graph->itemMoved();
188         break;
189     default:
190         break;
191     };
192
193     return QGraphicsItem::itemChange(change, value);
194 }
195
196 void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
197 {
198     update();
199     QGraphicsItem::mousePressEvent(event);
200 }
201
202 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
203 {
204     update();
205     QGraphicsItem::mouseReleaseEvent(event);
206 }
207