0.3.1
[qanava.git] / tests / simpleqtmodel / canMainWindow.cpp
blob10e5ee44a4745a82c539003586c136873e840cf7
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 "./canMainWindow.h"
16 // QT headers
17 #include <QToolBar>
18 #include <QAction>
19 #include <QSplitter>
21 // STD headers
22 #include <sstream>
25 using namespace qan;
28 //-----------------------------------------------------------------------------
29 MainWindow::MainWindow( QApplication* application, QWidget* parent ) :
30 QMainWindow( parent ),
31 _application( application )
33 // Setup ui
34 setupUi( this );
35 QHBoxLayout* hbox = new QHBoxLayout( _frame );
36 hbox->setMargin( 0 );
38 // Setup zoom and navigation toolbars
39 QToolBar* navigationBar = addToolBar( "Navigation" );
40 navigationBar->setIconSize( QSize( 16, 16 ) );
41 QAction* layoutGraph = navigationBar->addAction( QIcon( ), "Layout" );
42 connect( layoutGraph, SIGNAL( triggered( ) ), this, SLOT( layoutGraph( ) ) );
43 navigationBar->addSeparator( );
45 QAction* addNode = navigationBar->addAction( QIcon( ), "Add Node" );
46 connect( addNode, SIGNAL( triggered( ) ), this, SLOT( addNode( ) ) );
47 QAction* removeNode = navigationBar->addAction( QIcon( ), "Remove Node" );
48 connect( removeNode, SIGNAL( triggered( ) ), this, SLOT( removeNode( ) ) );
49 QAction* modifyNode = navigationBar->addAction( QIcon( ), "Modify Node" );
50 connect( modifyNode, SIGNAL( triggered( ) ), this, SLOT( modifyNode( ) ) );
51 QAction* addEdge = navigationBar->addAction( QIcon( ), "Add Edge" );
52 connect( addEdge, SIGNAL( triggered( ) ), this, SLOT( addEdge( ) ) );
53 navigationBar->addSeparator( );
55 // Generate a simple graph
56 _graph = new Graph( );
57 Node& na = *_graph->addNode( "Node A", 2 );
58 Node& nb = *_graph->addNode( "Node B", 1 );
59 _graph->addEdge( na, nb );
61 Node& nc = *_graph->addNode( "Node C", 1 );
62 _graph->addEdge( nb, nc );
64 Node& nd = *_graph->addNode( "<b>Node D</b>", 1 );
66 _whiteNode = new Style( "whitenode" );
67 _whiteNode->addColor( "backcolor", 164, 206, 57 );
69 // Display the dir model in a standard qt tree view
70 _graphTreeView = new QTreeView( _frame );
71 _graphTreeView->setAlternatingRowColors( true );
72 _graphTreeView->setMaximumWidth( 130 );
73 _graph->updateModels( );
74 _graphTreeView->setModel( _graph );
75 _graphTreeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
77 // Create a grah item view and display the dir model
78 _graphView = new qan::GraphView( _frame );
79 GridItem* grid = new GridCheckBoardItem( _graphView, palette( ).color( QPalette::Base ), palette( ).color( QPalette::AlternateBase ) );
81 _graphView->applyStyle( &na, _whiteNode );
82 _graphView->applyStyle( &nb, _whiteNode );
83 _graphView->applyStyle( &nc, _whiteNode );
84 _graphView->applyStyle( &nd, _whiteNode );
86 _graphView->setGraph( *_graph );
87 _graphView->setGraphLayout( new qan::UndirectedGraph( ) );
88 _graphView->layoutGraph( );
90 QSplitter* qSplitter = new QSplitter( _frame );
91 qSplitter->addWidget( _graphTreeView );
92 qSplitter->addWidget( _graphView );
93 hbox->addWidget( qSplitter );
95 QToolBar* zoomBar = addToolBar( "Zooming" );
96 QSlider* sldZoom = new QSlider( Qt::Horizontal, zoomBar );
97 sldZoom->setMinimum( 1 );
98 sldZoom->setMaximum( 99 );
99 sldZoom->setPageStep( 10 );
100 sldZoom->setSliderPosition( 50 );
101 sldZoom->setMinimumWidth( 190 );
102 zoomBar->addWidget( sldZoom );
104 connect( sldZoom, SIGNAL( valueChanged(int) ), _graphView, SLOT( setZoomPercent(int) ) );
105 zoomBar->setIconSize( QSize( 16, 16 ) );
106 if ( _graphView->getAction( "zoom_in" ) != 0 )
107 zoomBar->addAction( _graphView->getAction( "zoom_in" ) );
108 if ( _graphView->getAction( "zoom_out" ) != 0 )
109 zoomBar->addAction( _graphView->getAction( "zoom_out" ) );
110 zoomBar->addSeparator( );
111 if ( _graphView->getAction( "pan_controller" ) != 0 )
112 zoomBar->addAction( _graphView->getAction( "pan_controller" ) );
113 if ( _graphView->getAction( "zoom_window_controller" ) != 0 )
114 zoomBar->addAction( _graphView->getAction( "zoom_window_controller" ) );
117 void MainWindow::layoutGraph( )
119 _graphView->layoutGraph( );
122 void MainWindow::removeNode( )
124 if ( _graphTreeView->selectionModel( )->selection( ).size( ) != 0 )
126 const QModelIndexList& indexes = _graphTreeView->selectionModel( )->selection( ).indexes( );
127 if ( indexes.begin( ) != indexes.end( ) )
129 QModelIndex id = *indexes.begin( );
130 Node* node = _graph->getModelIndexNode( id );
131 if ( node != 0 )
132 _graph->removeNode( *node );
137 void MainWindow::addNode( )
139 Node* parentNode = 0;
140 if ( _graphTreeView->selectionModel( )->selection( ).size( ) != 0 )
142 const QModelIndexList& indexes = _graphTreeView->selectionModel( )->selection( ).indexes( );
143 if ( indexes.begin( ) != indexes.end( ) )
145 QModelIndex id = *indexes.begin( );
146 parentNode = _graph->getModelIndexNode( id );
151 // Insert root node
152 Node* node = _graph->insertNode( QString( "Node " ) + QString::number( _graph->getNodeCount( ) ), -1 );
153 _graphView->applyStyle( node, _whiteNode );
154 //_graphView->updateGraphStyles( );
158 void MainWindow::addEdge( )
160 Node* srcNode = 0;
161 Node* dstNode = 0;
162 if ( _graphTreeView->selectionModel( )->selection( ).size( ) == 2 )
164 const QModelIndexList& indexes = _graphTreeView->selectionModel( )->selection( ).indexes( );
165 QModelIndex srcId = indexes.at( 0 );
166 srcNode = _graph->getModelIndexNode( srcId );
167 QModelIndex dstId = indexes.at( 1 );
168 dstNode = _graph->getModelIndexNode( dstId );
171 if ( srcNode != 0 && dstNode != 0 )
172 _graph->insertEdge( *srcNode, *dstNode );
175 void MainWindow::modifyNode( )
177 Node* node = 0;
178 if ( _graphTreeView->selectionModel( )->selection( ).size( ) != 0 )
180 const QModelIndexList& indexes = _graphTreeView->selectionModel( )->selection( ).indexes( );
181 if ( indexes.begin( ) != indexes.end( ) )
183 QModelIndex id = *indexes.begin( );
184 node = _graph->getModelIndexNode( id );
188 // if ( node != 0 )
189 // _graph->modifyNode( GraphViewnode, node->getLabel( ) + "_youpi!" );
191 //-----------------------------------------------------------------------------