ec0d07df4a221bf93fb24d55d76e473c67cedca1
[occt.git] / samples / qt / FuncDemo / src / node.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of the example classes of the Qt Toolkit.
6 **
7 ** Licensees holding a valid Qt License Agreement may use this file in
8 ** accordance with the rights, responsibilities and obligations
9 ** contained therein.  Please consult your licensing agreement or
10 ** contact sales@trolltech.com if any conditions of this licensing
11 ** agreement are not clear to you.
12 **
13 ** Further information about Qt licensing is available at:
14 ** http://www.trolltech.com/products/qt/licensing.html or by
15 ** contacting info@trolltech.com.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ****************************************************************************/
21
22 #include <QGraphicsScene>
23 #include <QGraphicsSceneMouseEvent>
24 #include <QPainter>
25 #include <QStyleOption>
26
27 #include "edge.h"
28 #include "node.h"
29 #include "graphwidget.h"
30
31 #include <TFunction_IFunction.hxx>
32 #include <TFunction_GraphNode.hxx>
33 #include <TDataStd_Name.hxx>
34 #include <TDataStd_Tick.hxx>
35
36 Node::Node(GraphWidget *graphWidget)
37     : graph(graphWidget)
38 {
39     setFlag(ItemIsMovable);
40     setZValue(1);
41 }
42
43 void Node::setFunction(const TDF_Label& func)
44 {
45     myFunction = func;
46 }
47
48 const TDF_Label& Node::getFunction() const
49 {
50     return myFunction;
51 }
52
53 void Node::addEdge(Edge *edge)
54 {
55     edgeList << edge;
56     edge->adjust();
57 }
58
59 QList<Edge *> Node::edges() const
60 {
61     return edgeList;
62 }
63
64 QRectF Node::boundingRect() const
65 {
66     qreal adjust = 2;
67     return QRectF(-15 - adjust, -15 - adjust,
68                   33 + adjust, 33 + adjust);
69 }
70
71 QPainterPath Node::shape() const
72 {
73     QPainterPath path;
74     path.addEllipse(-15, -15, 30, 30);
75     return path;
76 }
77
78 void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
79 {
80     painter->setPen(Qt::NoPen);
81     painter->setBrush(Qt::darkGray);
82     painter->drawEllipse(-12, -12, 30, 30);
83
84     QColor light_color(Qt::yellow);
85     TFunction_IFunction iFunc(myFunction);
86     Handle(TFunction_GraphNode) graphNode = iFunc.GetGraphNode();
87     TFunction_ExecutionStatus status = graphNode->GetStatus();
88     switch (status)
89     {
90     case TFunction_ES_WrongDefinition:
91     case TFunction_ES_Failed:
92         light_color = Qt::red;
93         break;
94     case TFunction_ES_NotExecuted:
95         light_color = Qt::green;
96         break;
97     case TFunction_ES_Executing:
98         light_color = Qt::yellow;
99         break;
100     case TFunction_ES_Succeeded:
101         light_color = Qt::blue;
102         break;
103     }
104     if (myFunction.IsAttribute(TDataStd_Tick::GetID()))
105         light_color = Qt::white;
106     QColor dark_color = light_color.dark(150);
107
108     QRadialGradient gradient(-3, -3, 10);
109     if (option->state & QStyle::State_Sunken) {
110         gradient.setCenter(3, 3);
111         gradient.setFocalPoint(3, 3);
112         gradient.setColorAt(1, light_color.light(120));
113         gradient.setColorAt(0, dark_color.light(120));
114     } else {
115         gradient.setColorAt(0, light_color);
116         gradient.setColorAt(1, dark_color);
117     }
118     painter->setBrush(gradient);
119     painter->setPen(QPen(Qt::black, 0));
120     painter->drawEllipse(-15, -15, 30, 30);
121     
122     QString s;
123     Handle(TDataStd_Name) n;
124     if (myFunction.FindAttribute(TDataStd_Name::GetID(), n))
125         s = TCollection_AsciiString(n->Get()).ToCString();
126     painter->drawText(-7, 3, s);
127 }
128
129 QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
130 {
131     switch (change) {
132     case ItemPositionChange:
133         foreach (Edge *edge, edgeList)
134             edge->adjust();
135         break;
136     default:
137         break;
138     };
139
140     return QGraphicsItem::itemChange(change, value);
141 }
142
143 void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
144 {
145     update();
146     QGraphicsItem::mousePressEvent(event);
147 }
148
149 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
150 {
151     update();
152     QGraphicsItem::mouseReleaseEvent(event);
153 }