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 | |
55a40de8 |
41 | #include <Standard_WarningsDisable.hxx> |
aff5997d |
42 | #include <QGraphicsScene> |
43 | #include <QGraphicsSceneMouseEvent> |
44 | #include <QPainter> |
45 | #include <QStyleOption> |
55a40de8 |
46 | #include <Standard_WarningsRestore.hxx> |
aff5997d |
47 | |
48 | #include "edge.h" |
49 | #include "node.h" |
50 | #include "graphwidget.h" |
51 | |
52 | #include <TFunction_IFunction.hxx> |
53 | #include <TFunction_GraphNode.hxx> |
54 | #include <TDataStd_Name.hxx> |
55 | #include <TDataStd_Tick.hxx> |
56 | |
57 | Node::Node(GraphWidget *graphWidget) |
58 | : graph(graphWidget) |
59 | { |
60 | setFlag(ItemIsMovable); |
61 | setZValue(1); |
62 | } |
63 | |
64 | void Node::setFunction(const TDF_Label& func) |
65 | { |
66 | myFunction = func; |
67 | } |
68 | |
69 | const TDF_Label& Node::getFunction() const |
70 | { |
71 | return myFunction; |
72 | } |
73 | |
74 | void Node::addEdge(Edge *edge) |
75 | { |
76 | edgeList << edge; |
77 | edge->adjust(); |
78 | } |
79 | |
80 | QList<Edge *> Node::edges() const |
81 | { |
82 | return edgeList; |
83 | } |
84 | |
85 | QRectF Node::boundingRect() const |
86 | { |
87 | qreal adjust = 2; |
88 | return QRectF(-15 - adjust, -15 - adjust, |
89 | 33 + adjust, 33 + adjust); |
90 | } |
91 | |
92 | QPainterPath Node::shape() const |
93 | { |
94 | QPainterPath path; |
95 | path.addEllipse(-15, -15, 30, 30); |
96 | return path; |
97 | } |
98 | |
99 | void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) |
100 | { |
101 | painter->setPen(Qt::NoPen); |
102 | painter->setBrush(Qt::darkGray); |
103 | painter->drawEllipse(-12, -12, 30, 30); |
104 | |
105 | QColor light_color(Qt::yellow); |
106 | TFunction_IFunction iFunc(myFunction); |
107 | Handle(TFunction_GraphNode) graphNode = iFunc.GetGraphNode(); |
108 | TFunction_ExecutionStatus status = graphNode->GetStatus(); |
109 | switch (status) |
110 | { |
111 | case TFunction_ES_WrongDefinition: |
112 | case TFunction_ES_Failed: |
113 | light_color = Qt::red; |
114 | break; |
115 | case TFunction_ES_NotExecuted: |
116 | light_color = Qt::green; |
117 | break; |
118 | case TFunction_ES_Executing: |
119 | light_color = Qt::yellow; |
120 | break; |
121 | case TFunction_ES_Succeeded: |
122 | light_color = Qt::blue; |
123 | break; |
124 | } |
125 | if (myFunction.IsAttribute(TDataStd_Tick::GetID())) |
126 | light_color = Qt::white; |
127 | QColor dark_color = light_color.dark(150); |
128 | |
129 | QRadialGradient gradient(-3, -3, 10); |
130 | if (option->state & QStyle::State_Sunken) { |
131 | gradient.setCenter(3, 3); |
132 | gradient.setFocalPoint(3, 3); |
133 | gradient.setColorAt(1, light_color.light(120)); |
134 | gradient.setColorAt(0, dark_color.light(120)); |
135 | } else { |
136 | gradient.setColorAt(0, light_color); |
137 | gradient.setColorAt(1, dark_color); |
138 | } |
139 | painter->setBrush(gradient); |
140 | painter->setPen(QPen(Qt::black, 0)); |
141 | painter->drawEllipse(-15, -15, 30, 30); |
967905a3 |
142 | |
aff5997d |
143 | QString s; |
144 | Handle(TDataStd_Name) n; |
145 | if (myFunction.FindAttribute(TDataStd_Name::GetID(), n)) |
146 | s = TCollection_AsciiString(n->Get()).ToCString(); |
147 | painter->drawText(-7, 3, s); |
148 | } |
149 | |
150 | QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) |
151 | { |
152 | switch (change) { |
153 | case ItemPositionChange: |
154 | foreach (Edge *edge, edgeList) |
155 | edge->adjust(); |
156 | break; |
157 | default: |
158 | break; |
159 | }; |
160 | |
161 | return QGraphicsItem::itemChange(change, value); |
162 | } |
163 | |
164 | void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) |
165 | { |
166 | update(); |
167 | QGraphicsItem::mousePressEvent(event); |
168 | } |
169 | |
170 | void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
171 | { |
172 | update(); |
173 | QGraphicsItem::mouseReleaseEvent(event); |
174 | } |