File: GraphDisplayWidget.h

package info (click to toggle)
camitk 6.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 389,496 kB
  • sloc: cpp: 103,476; sh: 2,448; python: 1,618; xml: 984; makefile: 128; perl: 84; sed: 20
file content (197 lines) | stat: -rw-r--r-- 6,812 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2025 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/


#ifndef GRAPHDISPLAYWIDGET_H
#define GRAPHDISPLAYWIDGET_H

#include "CamiTKAPI.h"

// Qt Stuff
#include <QObject>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QColor>
#include <QPen>
#include <QVector>
#include <QMap>

namespace camitk {

/**
 * GraphDisplayWidget is used to manage a Widget to display a
 * graph with nodes and links onto a 2D canvas. It's inherits from QGraphicsView.
 *
 * It is used by TransformationExplorer to display the graph of FrameOfReference and Transformation
 * but is not aware of the object that are managed graphically (all managed using const void*)
 */
class CAMITK_API GraphDisplayWidget : public QGraphicsView {
    Q_OBJECT

public:
    /// constructor (build the scene and connect signals/slots)
    GraphDisplayWidget(QWidget* parent = nullptr);

    /// destructor
    virtual ~GraphDisplayWidget();

    /**
     * Empty all nodes and links
     */
    void clear();
    /**
     * Add a node to the graph.
     *
     * @param ptr the Object referred to by the node
     * @param tooltip The tooltip to be displayed when the mouse hovers over the node
     * @param color The color of the disc representing the node
     * @param lineColor The color of the circle around the disc
     * @param internalText Text to display inside the node (can be any UTF-8 character)
     */
    void addNode(const void* ptr, QString tooltip, QColor color, QColor lineColor, QString internalText = "");
    /**
     * Add a link in the graph.
     *
     * @param ptr a pointer to the object that is represented by this link.
     * @param from a pointer to the object represented as a node (and added before by addNode) that this links from
     * @param to a pointer to the object represented as a node (and added before by addNode) that this links to
     * @param tooltip The tooltip to be displayed when the mouse hovers over the link
     * @param pen The pen used to draw the line (color, brush, width...)
     * @param arrowHead if true adds an arrow pointing to the to object
     */
    void addLink(const void* ptr, const void* from, const void* to, QString tooltip, const QPen& pen, bool arrowHead);

    /**
     * Refresh the 2D canvas display
     */
    void refresh();

    /**
     * Resize the content of the canvas to the size of the widget
     */
    void autoScale();

    /**
     * Set the diameter of nodes' graphical representation
     */
    void setNodeDiameter(float d);

signals:
    /**
     * Signal emitted when a node was selected in the 2D canvas
     * It sends the pointer to the object represented by the node
     */
    void nodeSelectionChanged(const void* node);

    /**
     * Signal emitted when a link was selected in the 2D canvas
     *  It sends the pointer to the object represented by the link
     */
    void linkSelectionChanged(const void* link);

    /// node was double clicked
    void nodeDoubleClicked(const void* node);

    /// link was double clicked
    void linkDoubleClicked(const void* node);

public slots:
    /**
     * Specify to the view that a node was selected
     * @param node a pointer to the object represented by the node
     */
    void setSelectedNode(const void* node);
    /**
     * Specify to the view that a link was selected
     * @param link a pointer to the object represented by the link
     */
    void setSelectedLink(const void* link);

    /// Get the selection of nodes and links from the 2D canvas
    void updateView();

protected:
    /// process double clicked nodes and link
    void mouseDoubleClickEvent(QMouseEvent*) override;

    /// make sure the scene fit the new size entirely
    void resizeEvent(QResizeEvent*) override;

    // make sure the scene fits all the potentially moved nodes
    void mouseReleaseEvent(QMouseEvent* e) override;

private:
    /// Diameter of a node representation
    double diameter;

    /// selection pen half width used to shrink selected nodes
    double halfPenWidth;

    /// Finding graphical items from objects and links, finding objects and links from graphical items
    /// node2item[nodeObjectPtr] = graphicalItem
    QMap<const void*, QGraphicsEllipseItem*> node2item;
    // same but for internal text decoration
    QMap<const void*, QGraphicsSimpleTextItem*> node2textItem;
    /// item2node[graphicalItem] = nodeObjectPtr
    QMap<QGraphicsEllipseItem*, const void*> item2node;
    /// textItem2node[graphicalItem] = nodeObjectPtr
    QMap<QGraphicsSimpleTextItem*, const void*> textItem2node;

    /// link2item[linkObjectPtr] = graphicalItem
    QMap<const void*, QGraphicsLineItem*> link2item;
    // same but for the arrow heads
    QMap<const void*, QGraphicsPolygonItem*> link2arrowHead;
    /// item2link[graphicalItem] = linkObjectPtr
    QMap<QGraphicsLineItem*, const void*> item2link;

    // Selections
    const void* currentSelectedNode;
    const void* currentSelectedLink;

    // Pens to manage to selected/unselected
    QPen selectedPen;
    QMap<const void*, QPen> unselectedPen;

    // For each link, from and to nodes
    QMap<const void*, std::pair<const void*, const void*>> links;
    // nodeArity[nodeObjectPtr] is the number of links from and to the node
    QMap<const void*, int> nodeArity;

    // Move the graphical representation of a node in the canvas
    void moveNode(const void* ptr, float posX, float posY);
    // Move the graphical representation of a link in the canvas
    void moveLink(const void* ptr, float posX1, float posY1, float posX2, float posY2);



    // update link position using the node (new) center, called when nodes are moved
    void updateLinkPosition();

    // currently being update, n
};

} // namespace camitk
#endif // GRAPHDISPLAYWIDGET_H