0.3.1
[qanava.git] / tests / customnodes / canMainWindow.cpp
blob18a042f52ba5a0169a26726a3abb8846dc7d9e2c
1 //-----------------------------------------------------------------------------
2 // This file is a part of the Qanava software.
3 //
4 // \file canMainWindow.cpp
5 // \author Benoit Autheman (benoit@libqanava.org)
6 // \date 2005 November 11
7 //-----------------------------------------------------------------------------
10 // Qanava headers
11 #include "../../src/qanGraphView.h"
12 #include "../../src/qanGrid.h"
13 #include "../../src/qanSimpleLayout.h"
14 #include "canMainWindow.h"
17 // QT headers
18 #include <QToolBar>
19 #include <QAction>
20 #include <QMessageBox>
22 using namespace qan;
24 qan::Node* current;
26 QVariant MyNode::itemChange( GraphicsItemChange change, const QVariant& value )
28 if ( change == ItemPositionChange )
30 QPointF p = value.toPointF( );
31 const qan::VectorF& np = getNode( ).getPosition( );
32 //if ( p.x( ) != np( 0 ) && p.y( ) != np( 1 ) )
33 getNode( ).setPosition( p.x( ), p.y( ) );
34 updateEdges( );
35 current = &getNode( );
37 return QGraphicsItem::itemChange( change, value );
41 //-----------------------------------------------------------------------------
42 MainWindow::MainWindow( QWidget* parent ) :
43 QMainWindow( parent )
45 setupUi( this );
46 current = 0;
48 QHBoxLayout *hbox = new QHBoxLayout( _frame );
49 hbox->setMargin( 0 );
50 hbox->setSpacing( 2 );
52 // Setup zoom and navigation toolbars
53 QToolBar* zoomBar = addToolBar( "Zooming" );
54 QSlider* sldZoom = new QSlider( Qt::Horizontal, zoomBar );
55 sldZoom->setMinimum( 1 );
56 sldZoom->setMaximum( 99 );
57 sldZoom->setPageStep( 10 );
58 sldZoom->setSliderPosition( 50 );
59 sldZoom->setMinimumWidth( 190 );
60 zoomBar->addWidget( sldZoom );
62 QToolBar* navigationBar = addToolBar( "Navigation" );
63 navigationBar->setIconSize( QSize( 16, 16 ) );
65 // Generate a simple graph
66 Graph* graph = new Graph( );
67 Node& n1 = *graph->addNode( "1", 2 );
68 Node& n2 = *graph->addNode( "2", 2 );
69 Node& n3 = *graph->addNode( "3", 2 );
70 Node& n4 = *graph->addNode( "4", 2 );
71 Node& n5 = *graph->addNode( "5", 2 );
72 Node& n6 = *graph->addNode( "6", 2 );
73 Node& n7 = *graph->addNode( "7", 2 );
74 Node& n8 = *graph->addNode( "8", 2 );
75 Node& n9 = *graph->addNode( "9", 2 );
76 graph->addEdge( n1, n2 ); graph->addEdge( n2, n3 );
77 graph->addEdge( n4, n5 ); graph->addEdge( n5, n6 );
78 graph->addEdge( n7, n8 ); graph->addEdge( n8, n9 );
80 graph->addEdge( n1, n4 ); graph->addEdge( n2, n5 ); graph->addEdge( n3, n6 );
81 graph->addEdge( n4, n7 ); graph->addEdge( n5, n8 ); graph->addEdge( n6, n9 );
84 // Setup item views
85 _graphView = new qan::GraphView( _frame, QColor( 170, 171, 205 ) );
86 GridItem* grid = new GridCheckBoardItem( _graphView, QColor( 255, 255, 255 ), QColor( 238, 230, 230 ) );
87 _graphView->setRenderHint( QPainter::Antialiasing );
88 hbox->addWidget( _graphView );
89 graph->registerGraphicNodeFactory( new MyNode::Factory( _graphView->getStyleManager( ) ) );
92 // Add canvas pan and zoom action to the navigation tobar
93 _cbGridType = new QComboBox( navigationBar );
94 _cbGridType->addItem( QIcon(), "<< Grid Type >>" );
95 _cbGridType->addItem( QIcon(), "Regular" );
96 _cbGridType->addItem( QIcon(), "Check Board" );
97 connect( _cbGridType, SIGNAL(activated(int)), this, SLOT(gridChanged(int)));
98 navigationBar->addWidget( _cbGridType );
100 connect( sldZoom, SIGNAL( valueChanged(int) ), _graphView, SLOT( setZoomPercent(int) ) );
101 navigationBar->setIconSize( QSize( 16, 16 ) );
103 Style* orangeNode = new Style( "orangenode" );
104 orangeNode->addImageName( "backimage", "images/gradorange.png" );
105 orangeNode->addColor( "bordercolor", 50, 100, 200 );
106 orangeNode->addT< int >( "maximumwidth", 125 );
107 orangeNode->addT< int >( "maximumheight", 25 );
108 // _graphView->applyStyle( &nd, *orangeNode );
110 _graphView->setGraph( *graph );
111 _graphView->setGraphLayout( new qan::Concentric( 45., 60. ) ); // View will take care of layout destruction
112 _graphView->layoutGraph( );
114 _layout = new qan::UndirectedGraph( );
115 _timer = new QTimer( this );
116 _timer->start( 100 );
117 connect( _timer, SIGNAL(timeout()), this, SLOT( layoutGraph() ) );
121 void MainWindow::selectNode( qan::Node * node, QPoint p )
123 if ( node != 0 )
125 QString msg( "Node \"" );
126 msg += node->getLabel( );
127 msg += "\" selected.";
129 QMessageBox::information( this, "Qanava Basic Test", msg );
133 void MainWindow::layoutGraph( )
135 _graphView->layoutGraph( 0, _layout, 1, current ); // Perform 1 step of layout, do not modify the dragged node position
137 //-----------------------------------------------------------------------------