0029559: Samples - wrong copyright statement in FuncDemo
[occt.git] / samples / qt / FuncDemo / src / node.cpp
CommitLineData
aff5997d 1/****************************************************************************
2**
967905a3 3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
aff5997d 5**
967905a3 6** This file is part of the examples of the Qt Toolkit.
aff5997d 7**
967905a3 8** $QT_BEGIN_LICENSE:BSD$
9** You may use this file under the terms of the BSD license as follows:
aff5997d 10**
967905a3 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 Digia Plc and its Subsidiary(-ies) nor the names
21** of its contributors may be used to endorse or promote products derived
22** from this software without specific prior written permission.
aff5997d 23**
967905a3 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$
aff5997d 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#include <TFunction_IFunction.hxx>
51#include <TFunction_GraphNode.hxx>
52#include <TDataStd_Name.hxx>
53#include <TDataStd_Tick.hxx>
54
55Node::Node(GraphWidget *graphWidget)
56 : graph(graphWidget)
57{
58 setFlag(ItemIsMovable);
59 setZValue(1);
60}
61
62void Node::setFunction(const TDF_Label& func)
63{
64 myFunction = func;
65}
66
67const TDF_Label& Node::getFunction() const
68{
69 return myFunction;
70}
71
72void Node::addEdge(Edge *edge)
73{
74 edgeList << edge;
75 edge->adjust();
76}
77
78QList<Edge *> Node::edges() const
79{
80 return edgeList;
81}
82
83QRectF Node::boundingRect() const
84{
85 qreal adjust = 2;
86 return QRectF(-15 - adjust, -15 - adjust,
87 33 + adjust, 33 + adjust);
88}
89
90QPainterPath Node::shape() const
91{
92 QPainterPath path;
93 path.addEllipse(-15, -15, 30, 30);
94 return path;
95}
96
97void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
98{
99 painter->setPen(Qt::NoPen);
100 painter->setBrush(Qt::darkGray);
101 painter->drawEllipse(-12, -12, 30, 30);
102
103 QColor light_color(Qt::yellow);
104 TFunction_IFunction iFunc(myFunction);
105 Handle(TFunction_GraphNode) graphNode = iFunc.GetGraphNode();
106 TFunction_ExecutionStatus status = graphNode->GetStatus();
107 switch (status)
108 {
109 case TFunction_ES_WrongDefinition:
110 case TFunction_ES_Failed:
111 light_color = Qt::red;
112 break;
113 case TFunction_ES_NotExecuted:
114 light_color = Qt::green;
115 break;
116 case TFunction_ES_Executing:
117 light_color = Qt::yellow;
118 break;
119 case TFunction_ES_Succeeded:
120 light_color = Qt::blue;
121 break;
122 }
123 if (myFunction.IsAttribute(TDataStd_Tick::GetID()))
124 light_color = Qt::white;
125 QColor dark_color = light_color.dark(150);
126
127 QRadialGradient gradient(-3, -3, 10);
128 if (option->state & QStyle::State_Sunken) {
129 gradient.setCenter(3, 3);
130 gradient.setFocalPoint(3, 3);
131 gradient.setColorAt(1, light_color.light(120));
132 gradient.setColorAt(0, dark_color.light(120));
133 } else {
134 gradient.setColorAt(0, light_color);
135 gradient.setColorAt(1, dark_color);
136 }
137 painter->setBrush(gradient);
138 painter->setPen(QPen(Qt::black, 0));
139 painter->drawEllipse(-15, -15, 30, 30);
967905a3 140
aff5997d 141 QString s;
142 Handle(TDataStd_Name) n;
143 if (myFunction.FindAttribute(TDataStd_Name::GetID(), n))
144 s = TCollection_AsciiString(n->Get()).ToCString();
145 painter->drawText(-7, 3, s);
146}
147
148QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
149{
150 switch (change) {
151 case ItemPositionChange:
152 foreach (Edge *edge, edgeList)
153 edge->adjust();
154 break;
155 default:
156 break;
157 };
158
159 return QGraphicsItem::itemChange(change, value);
160}
161
162void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
163{
164 update();
165 QGraphicsItem::mousePressEvent(event);
166}
167
168void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
169{
170 update();
171 QGraphicsItem::mouseReleaseEvent(event);
172}