Fix manual.tex qmake configuration settings (thanks to Thomas K.!)
[qanava.git] / src / qanGraphItemView.h
blob6b84a83b188c44e48edb3e76cb2bc9d244bc587a
1 /*
2 Qanava - Graph drawing library for QT
3 Copyright (C) 2006 Benoit AUTHEMAN
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 //-----------------------------------------------------------------------------
21 // This file is a part of the Qanava software.
23 // \file canGraphItemView.h
24 // \author Benoit Autheman (benoit@libqanava.org)
25 // \date 2005 November 22
26 //-----------------------------------------------------------------------------
29 #ifndef canGraphItemView_h
30 #define canGraphItemView_h
33 // Qanava headers
34 #include "./qanNode.h"
35 #include "./qanGraph.h"
36 #include "./qanLayout.h"
37 #include "./qanNodeItem.h"
38 #include "./qanEdgeItem.h"
39 #include "./qanGraphicsView.h"
42 // QT headers
43 #include <QAbstractItemModel>
44 #include <QAbstractItemView>
45 #include <QGraphicsScene>
46 #include <QGraphicsView>
47 #include <QGraphicsItem>
48 #include <QMap>
51 //-----------------------------------------------------------------------------
52 namespace qan { // ::qan
54 class Grid;
57 //! Display a graph provided as a QT Interview based model.
58 /*!
59 \nosubgrouping
61 class GraphItemView : public QAbstractItemView
63 Q_OBJECT
65 /*! \name View Construction *///---------------------------------------
66 //@{
67 public:
69 //! GraphItemView constructor with optionnal settings for graph widget initialization.
70 GraphItemView( QWidget* parent, Graph* graph = 0,
71 QColor backColor = QColor( 170, 171, 205 ), QSize size = QSize( 300, 150 ),
72 bool autoExpand = true );
74 //! GraphItemView virtual destructor.
75 virtual ~GraphItemView( );
77 //! Clear this item view of all elements and set its model to 0.
78 void clear( );
80 QGraphicsScene* getGraphicsScene( ) { return _graphicsScene; }
82 GraphicsView* getGraphicsView( ) { return _graphicsView; }
84 void setGrid( GridItem* grid ) { _graphicsView->setGrid( grid ); }
86 virtual QSize sizeHint( ) const { return QSize( 300, 250 ); }
88 private:
90 QGraphicsScene* _graphicsScene;
92 GraphicsView* _graphicsView;
94 Graph* _graph;
95 //@}
96 //---------------------------------------------------------------------
100 /*! \name QT View Interface Management *///----------------------------
101 //@{
102 public:
104 virtual void setModel ( QAbstractItemModel* model );
106 virtual QModelIndex indexAt( const QPoint& p ) const;
108 virtual void scrollTo( const QModelIndex& index, ScrollHint hint = EnsureVisible ) { }
110 virtual QRect visualRect( const QModelIndex& index ) const { return QRect( 0, 0, 0, 0 ); }
112 virtual int horizontalOffset( ) const { return 0; }
114 virtual bool isIndexHidden ( const QModelIndex & index ) const { return false; }
116 virtual QModelIndex moveCursor ( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) { return QModelIndex( ); }
118 virtual void setSelection ( const QRect& rect, QItemSelectionModel::SelectionFlags flags ) { }
120 virtual int verticalOffset( ) const { return 0; }
122 virtual QRegion visualRegionForSelection ( const QItemSelection& selection ) const { return QRegion( 0, 0, 0, 0 ); }
124 protected:
126 //! Visit a model index and its child index, and map encoutered indexes.
127 void visitModelIndex( QAbstractItemModel& model, QModelIndex& modelIndex, QModelIndex& parentIndex, int depth = -1 );
129 //! Map a given model index to a graph node and a canvas geometric items.
130 void mapIndex( QModelIndex& index, QModelIndex& parent );
132 //! Update an index associed graphic item (and eventually node) with the current index data.
133 void updateModelIndexGraphicItem( QModelIndex& index );
135 //! Update a node from data comming from its associed model index (assuming the model data are more accurate than the node ones).
136 void updateModelIndexNode( QModelIndex& index, Node* node = 0 );
138 public slots:
140 void expand( const Node* node );
142 protected slots:
144 void graphicItemDoubleClicked( QGraphicsItem* item );
146 virtual void reset( );
148 virtual void rowsAboutToBeInserted( const QModelIndex& parent, int start, int end );
150 virtual void rowsInserted( const QModelIndex& parent, int start, int end );
152 virtual void rowsAboutToBeRemoved( const QModelIndex & parent, int start, int end );
154 virtual void rowsRemoved( const QModelIndex &parent, int first, int last );
156 virtual void dataChanged( const QModelIndex& topLeft, const QModelIndex& bottomRight );
157 //@}
158 //---------------------------------------------------------------------
162 /*! \name Model Index and Graphic Item Management *///-----------------
163 //@{
164 public:
166 //! Get a graph node associed to a given graphic item.
167 Node* getGraphicItemNode( const QGraphicsItem* item );
169 //! Get a graphic item associed to a given graph node.
170 QGraphicsItem* getNodeGraphicItem( const Node* node );
172 //! Get a graph node associed to a given model item index.
173 Node* getModelIndexNode( QModelIndex& index );
175 //! Get the node that is currently under the mouse pointer.
176 Node* getCurrentNode( QPoint& p );
178 signals:
180 void nodeDoubleClicked( qan::Node* node );
182 protected:
184 typedef QMap< void*, Node* > ModelIndexNodeMap;
186 typedef QMap< void*, AbstractNodeItem* > ModelIndexGraphicItemMap;
188 typedef QMap< const Edge*, EdgeItem* > EdgeGraphicItemMap;
190 typedef QMap< const Node*, AbstractNodeItem* > NodeGraphicItemMap;
192 typedef QMap< const QGraphicsItem*, void* > GraphicItemModelIndexMap;
194 typedef QMap< const Node*, QModelIndex > NodeModelIndexMap;
196 ModelIndexNodeMap _modelIndexNodeMap;
198 ModelIndexGraphicItemMap _modelIndexGraphicItemMap;
200 EdgeGraphicItemMap _edgeGraphicItemMap;
202 NodeGraphicItemMap _nodeGraphicItemMap;
204 GraphicItemModelIndexMap _graphicItemModelIndexMap;
206 NodeModelIndexMap _nodeModelIndexMap;
208 bool _autoExpand;
209 //@}
210 //---------------------------------------------------------------------
214 /*! \name Style Management *///----------------------------------------
215 //@{
216 public:
218 //! Get the graph view style manager.
219 Style::Manager& getStyleManager( ) { return _styleManager; }
221 //! Set a node style.
222 void applyStyle( Node* node, Style* style );
224 protected:
226 //! Graph view style manager.
227 Style::Manager _styleManager;
228 //@}
229 //---------------------------------------------------------------------
233 /*! \name Layout Management *///---------------------------------------
234 //@{
235 public:
237 //! .
238 void setGraphLayout( Layout* layout, QProgressDialog* progress = 0 );
240 //! .
241 Layout* getGraphLayout( Layout& layout ) { return _layout; }
243 //! .
244 const Layout* getGraphLayout( Layout& layout ) const { return _layout; }
246 //! .
247 void layoutGraph( QProgressDialog* progress = 0, Layout* layout = 0, int step = -1, Node* except = 0 );
249 //! Get bounding box for a group of nodes.
250 static VectorF getBoundingBox( const Node::List& nodes );
252 protected:
254 //! .
255 Layout* _layout;
256 //@}
257 //---------------------------------------------------------------------
261 /*! \name ScrollArea Management *///-----------------------------------
262 //@{
263 protected:
265 void resizeEvent( QResizeEvent* event );
267 bool eventFilter( QObject* o, QEvent* e );
268 //@}
269 //---------------------------------------------------------------------
273 /*! \name Graphic Node Factory Management *///-------------------------
274 //@{
275 public:
277 //! Register a graphic node factory for a certain type of nodes (from graph topology to graph graphic visualization).
278 void registerGraphicNodeFactory( AbstractNodeItem::AbstractFactory* factory );
280 //! Create a graphic node from a topological node using the currently registered graphic node factories.
281 AbstractNodeItem* createGraphicNode( Node& node, Style::Manager& styleManager, Style& style,
282 QGraphicsItem* parent, QGraphicsScene* scene,
283 QPoint origin, const QString& label );
285 private:
287 AbstractNodeItem::AbstractFactory::List _factories;
288 //@}
289 //---------------------------------------------------------------------
291 } // ::qan
292 //-----------------------------------------------------------------------------
295 #endif // canGraphItemView_h