FotoSHOCK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
Node.h
1 /*
2  * Copyright 2011, 2012 Lukas Jirkovsky
3  *
4  * This file is part of FotoSHOCK.
5  *
6  * FotoSHOCK is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, version 3 of the License.
9  *
10  * FotoSHOCK 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with FotoSHOCK. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef NODE_H
20 #define NODE_H
21 
22 #include "PixelData.hpp"
23 
24 #include <QGraphicsItem>
25 #include <QFont>
26 
27 class Edge;
28 class Node;
29 class FotoSHOCKOperation;
30 namespace FotoSHOCKcore {
31  class ImageOperationDescriptor;
32  class GraphManager;
33  class GraphNode;
34 }
35 
36 class Connector : public QGraphicsItem {
37  public:
38  enum Type {INPUT, OUTPUT};
39 
40  Connector(Node* parent, Type type, int ID);
41 
42  virtual void paint (QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
43  virtual QRectF boundingRect() const;
44 
45  virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
46  virtual void dropEvent (QGraphicsSceneDragDropEvent* event);
47 
48  virtual void hoverEnterEvent(QGraphicsSceneHoverEvent* event);
49  virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent* event);
50 
51  Type connectorType() const;
52 
53  int ID() const;
54 
55  void addEdge(Edge* edge);
56  void removeEdge();
57 
58  void updateEdges();
59  private:
60  // TODO: check: the edges may have to be freed manually
61  QList<Edge*> m_edges;
62  int m_ID;
63  Type m_type;
64  class EdgeMimeData;
65 };
66 
67 // declare pointer to connecter as QMetaType
68 // this allows us to use such pointers in QVariant
69 Q_DECLARE_METATYPE(Connector*)
70 
71 class Node : public QGraphicsItem {
72  public:
74 
77  Node(QGraphicsScene* scene, FotoSHOCKcore::GraphManager* graph, FotoSHOCKcore::ImageOperationDescriptor* operationDescriptor);
78 
80  Node(QGraphicsScene* scene, FotoSHOCKcore::GraphManager* graph, FotoSHOCKcore::GraphNode* node);
81 
82  QGraphicsScene* parentScene() const;
83 
84  virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
85  // returns the bounding rectangle, if more accurate bounding shape was required,
86  // it would be necessary to reimplement the shape() function
87  virtual QRectF boundingRect() const;
88 
90  bool tryEnable();
91 
92  FotoSHOCKcore::GraphManager* GraphManager() const;
93  FotoSHOCKcore::GraphNode* GraphNode() const;
94 
95  QVector<Connector*> inputConnectors() const;
96  QVector<Connector*> outputConnectors() const;
97 
98  /*********
99  * events
100  *********/
101  virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
102 
103  protected:
104  // updates position of connected edges when the node is moved
105  virtual QVariant itemChange(GraphicsItemChange change, const QVariant& value);
106 
107  // pointer to the node after it has been constructed
108  // it is freed automatically when the graphmanager is deleted
109  // NOTE: what happens to m_node when Node is removed but GraphManager is still operational?
115 
120  QVector<Connector*> m_inputs;
125  QVector<Connector*> m_outputs;
126  private:
127  // name whose width fits into the widget
128  QString m_shortName;
129  // font used for labels
130  QFont m_font;
131 
132  QGraphicsScene* m_parentScene;
133 
135 
136  QRectF m_boundingRectangle;
137  // position of the node
138  QPointF m_position;
139 };
140 
141 #endif // NODE_H