1 //-----------------------------------------------------------------------------
2 // This file is a part of the Qanava software.
4 // \file canMainWindow.cpp
5 // \author Benoit Autheman (benoit@libqanava.org)
6 // \date 2006 August 15
7 //-----------------------------------------------------------------------------
11 #include "../../src/qanRepository.h"
12 #include "../../src/qanGrid.h"
13 #include "../../src/qanSimpleLayout.h"
14 #include "../../src/qanGraphView.h"
15 #include "../../src/qanGraphScene.h"
16 #include "../../src/qanTreeLayout.h"
17 #include "../../src/ui/uiStyleView.h"
18 #include "../../src/ui/uiStyleModel.h"
19 #include "../../src/ui/uiNodesItemModel.h"
20 #include "canMainWindow.h"
26 #include <QMessageBox>
28 #include <QHeaderView>
29 #include <QToolButton>
36 //-----------------------------------------------------------------------------
37 MainWindow::MainWindow( QWidget
* parent
) :
38 QMainWindow( parent
),
44 // Setup zoom and navigation toolbars
45 QToolBar
* zoomBar
= addToolBar( "Zooming" );
46 QSlider
* sldZoom
= new QSlider( Qt::Horizontal
, zoomBar
);
47 sldZoom
->setMinimum( 1 );
48 sldZoom
->setMaximum( 99 );
49 sldZoom
->setPageStep( 10 );
50 sldZoom
->setSliderPosition( 50 );
51 sldZoom
->setMinimumWidth( 190 );
52 sldZoom
->resize( 150, 20 );
53 zoomBar
->addWidget( sldZoom
);
55 QToolBar
* navigationBar
= addToolBar( "Navigation" );
56 navigationBar
->setIconSize( QSize( 16, 16 ) );
59 // Generate a simple graph
60 Graph
* graph
= new Graph( );
61 qan::Repository
* gmlRepository
= new qan::GMLRepository( "terror.xml" );
62 gmlRepository
->load( graph
);
63 graph
->generateRootNodes( );
66 _graphView
= new qan::GraphView( _frame
, QColor( 170, 171, 205 ) );
67 GridItem
* grid
= new GridCheckBoardItem( _graphView
, QColor( 255, 255, 255 ), QColor( 238, 230, 230 ) );
68 QSplitter
* splitter
= new QSplitter( Qt::Vertical
, this );
69 splitter
->setHandleWidth( 2 );
70 splitter
->addWidget( _graphView
);
71 setCentralWidget( splitter
);
74 // Configure node selection and tooltip popup rendering
75 //connect( _graphView->getTooltipModifier( ), SIGNAL( nodeTipped(qan::Node*, QPoint) ), SLOT( showNodeTooltip(qan::Node*, QPoint) ) );
76 //connect( _graphView->getSelectionModifier( ), SIGNAL( nodeSelected(qan::Node*, QPoint) ), SLOT( selectNode(qan::Node*, QPoint) ) );
79 // Add canvas pan and zoom action to the navigation tobar
80 _cbGridType
= new QComboBox( navigationBar
);
81 _cbGridType
->addItem( QIcon(), "<< Grid Type >>" );
82 _cbGridType
->addItem( QIcon(), "Regular" );
83 _cbGridType
->addItem( QIcon(), "Check Board" );
84 connect( _cbGridType
, SIGNAL(activated(int)), this, SLOT(gridChanged(int)));
85 navigationBar
->addWidget( _cbGridType
);
87 connect( sldZoom
, SIGNAL( valueChanged(int) ), _graphView
, SLOT( setZoomPercent(int) ) );
88 navigationBar
->setIconSize( QSize( 16, 16 ) );
89 if ( _graphView
->getAction( "zoom_in" ) != 0 )
90 navigationBar
->addAction( _graphView
->getAction( "zoom_in" ) );
91 if ( _graphView
->getAction( "zoom_out" ) != 0 )
92 navigationBar
->addAction( _graphView
->getAction( "zoom_out" ) );
93 navigationBar
->addSeparator( );
94 if ( _graphView
->getAction( "pan_controller" ) != 0 )
95 navigationBar
->addAction( _graphView
->getAction( "pan_controller" ) );
96 if ( _graphView
->getAction( "zoom_window_controller" ) != 0 )
97 navigationBar
->addAction( _graphView
->getAction( "zoom_window_controller" ) );
99 QToolBar
* layoutBar
= addToolBar( "Layout" );
100 layoutBar
->setIconSize( QSize( 16, 16 ) );
102 _cbLayoutType
= new QComboBox( layoutBar
);
103 _cbLayoutType
->addItem( QIcon(), "<< Layout Type >>" );
104 _cbLayoutType
->addItem( QIcon(), "Random" );
105 _cbLayoutType
->addItem( QIcon(), "Concentric 90" );
106 _cbLayoutType
->addItem( QIcon(), "Concentric 45" );
107 _cbLayoutType
->addItem( QIcon(), "Colimacon log" );
108 _cbLayoutType
->addItem( QIcon(), "Hierarchy" );
109 _cbLayoutType
->addItem( QIcon(), "Graph" );
110 layoutBar
->addWidget( _cbLayoutType
);
112 QToolButton
* layoutButton
= new QToolButton( layoutBar
);
113 layoutButton
->setText( "Layout" );
114 layoutBar
->addWidget( layoutButton
);
115 connect( layoutButton
, SIGNAL( clicked() ), this, SLOT( layoutGraph() ) );
117 Style
* pilotStyle
= new Style( "pilot" );
118 pilotStyle
->addImageName( "backimage", "images/gradgreen.png" );
119 pilotStyle
->addColor( "bordercolor", 0, 0, 0 );
120 pilotStyle
->addImageName( "icon", "images/pilot.png" );
121 pilotStyle
->addT
< bool >( "hasshadow", true );
123 Style
* wtc1Style
= new Style( "wtc1" );
124 wtc1Style
->addColor( "backcolor", 120, 140, 180 );
125 wtc1Style
->addColor( "bordercolor", 0, 0, 0 );
126 wtc1Style
->addImageName( "icon", "images/wtc.png" );
128 Style
* wtc2Style
= new Style( "wtc2" );
129 wtc2Style
->addColor( "backcolor", 80, 120, 210 );
130 wtc2Style
->addColor( "bordercolor", 0, 0, 0 );
131 wtc2Style
->addImageName( "icon", "images/wtc.png" );
133 Style
* pentagonStyle
= new Style( "pentagon" );
134 pentagonStyle
->addImageName( "backimage", "images/gradblue.png" );
135 pentagonStyle
->addImageName( "icon", "images/pentagon.png" );
136 pentagonStyle
->addColor( "bordercolor", 0, 0, 0 );
138 Style
* pennsylvaniaStyle
= new Style( "pennsylvania" );
139 pennsylvaniaStyle
->addImageName( "backimage", "images/gradblue.png" );
140 pennsylvaniaStyle
->addColor( "bordercolor", 0, 0, 0 );
143 // Affect specific style according to nodes attributes
144 int pilotRole
= graph
->hasAttribute( "pilot" );
145 int flightRole
= graph
->hasAttribute( "flight" );
146 if ( pilotRole
!= -1 && flightRole
!= -1 )
148 Node::List::iterator nodeIter
= graph
->getNodes( ).begin( );
149 for ( ; nodeIter
!= graph
->getNodes( ).end( ); nodeIter
++ )
151 Node
* node
= *nodeIter
;
153 QString
* pilotAttr
= node
->getAttribute
< QString
>( pilotRole
);
154 if ( pilotAttr
!= 0 && *pilotAttr
== "true" )
155 _graphView
->applyStyle( node
, pilotStyle
);
157 QString
* flightAttr
= node
->getAttribute
< QString
>( flightRole
);
158 if ( flightAttr
!= 0 )
160 if ( *flightAttr
== "American Airlines Flight 11 (WTC1)" )
161 _graphView
->applyStyle( node
, wtc1Style
);
162 else if ( *flightAttr
== "United Airlines Flight 175 (WTC2)" )
163 _graphView
->applyStyle( node
, wtc2Style
);
164 else if ( *flightAttr
== "American Airlines Flight 77 (Pentagon)" )
165 _graphView
->applyStyle( node
, pentagonStyle
);
166 else if ( *flightAttr
== "United Airlines Flight 93 (Pennsylvania)" )
167 _graphView
->applyStyle( node
, pennsylvaniaStyle
);
172 _graphView
->setGraph( *graph
);
173 _graphView
->setGraphLayout( new qan::Random( ) ); // View will take care of layout destruction
174 _graphView
->layoutGraph( );
176 ui::NodesItemModel
* nodesItemModel
= new ui::NodesItemModel( *graph
, this );
177 QTableView
* nodesTableView
= new QTableView( this );
178 nodesTableView
->setAlternatingRowColors( true );
179 nodesTableView
->horizontalHeader( )->resizeSections( QHeaderView::Fixed
); // Fixed
180 nodesTableView
->horizontalHeader( )->setStretchLastSection( true );
181 nodesTableView
->verticalHeader( )->resizeSections( QHeaderView::Fixed
);
182 nodesTableView
->verticalHeader( )->setDefaultSectionSize( 20 );
183 nodesTableView
->verticalHeader( )->hide( );
184 nodesTableView
->setModel( nodesItemModel
);
185 nodesTableView
->adjustSize( );
187 splitter
->addWidget( nodesTableView
);
190 void MainWindow::gridChanged( int index
)
192 /* GridItem* grid = 0;
194 grid = new GridItemRegularItem( 0, QColor( 170, 171, 205 ) );
195 else if ( index == 2 )
196 grid = new GridCheckBoardItem( 0, QColor( 255, 255, 255 ), QColor( 238, 230, 230 ) );
197 if ( grid != 0 && _graphView != 0 && _graphView->getCanvas( ) != 0 )
198 _graphView->getCanvas( )->setGrid( grid );*/
201 void MainWindow::selectNode( qan::Node
* node
, QPoint p
)
205 QString msg( "Node \"" );
206 msg += node->getLabel( );
207 msg += "\" selected.";
208 QMessageBox::information( this, "Qanava Basic Test", msg.c_str( ) );
212 void MainWindow::showNodeTooltip( qan::Node
* node
, QPoint p
)
214 /* NodeItemInfoPopup* popup = new NodeItemInfoPopup( this );
215 popup->popup( QString( node->getLabel( ).c_str( ) ), p );*/
218 void MainWindow::layoutGraph( )
221 switch ( _cbLayoutType
->currentIndex( ) )
227 layout
= new qan::Random( );
229 case 2: // "Concentric 90"
230 layout
= new qan::Concentric( 90., 60. );
232 case 3: // "Concentric 45"
233 layout
= new qan::Concentric( 45., 60. );
235 case 4: // "Colimacon log"
236 layout
= new qan::Colimacon( 15., 8. );
239 layout
= new qan::HierarchyTree( QPointF( 120., 55. ), QPointF( 10., 10. ), Qt::Horizontal
);
242 layout
= new qan::UndirectedGraph( );
249 // Apply the settings if all needed parameters have been specified
250 if ( layout
!= 0 && _graphView
!= 0 )
252 _graphView
->setGraphLayout( layout
); // View will take care of layout destruction
253 _graphView
->layoutGraph( new QProgressDialog( "Laying out...", "Cancel", 1, 100, this ) );
256 //-----------------------------------------------------------------------------