1 //-----------------------------------------------------------------------------
2 // This file is a part of the Qanava software.
4 // \file canMainWindow.cpp
5 // \author Benoit Autheman (benoit@libqanava.org)
6 // \date 2005 November 11
7 //-----------------------------------------------------------------------------
11 #include "../../src/qanGrid.h"
12 #include "../../src/qanSimpleLayout.h"
13 #include "./canMainWindow.h"
19 #include <QMessageBox>
26 void generateBranch( Graph
& _graph
, Node
& superNode
, int depth
, QProgressDialog
* progress
, QApplication
* app
, bool root
= false )
31 int subNodeCount
= 5 + ( rand( ) % 5 );
32 app
->processEvents( );
34 progress
->setMaximum( subNodeCount
);
36 for ( int n
= 0; n
< subNodeCount
; n
++ )
40 app
->processEvents( );
41 progress
->setValue( n
);
42 if ( progress
->wasCanceled( ) )
48 label
+= QString::number( _graph
.getNodeCount( ) );
49 Node
* node
= _graph
.addNode( label
);
50 _graph
.addEdge( superNode
, *node
);
51 generateBranch( _graph
, *node
, depth
, progress
, app
);
55 void generateTree( Graph
& _graph
, QProgressDialog
* progress
, QApplication
* app
, int depth
= 5 )
57 Node
* root
= _graph
.addNode( "Root" );
59 generateBranch( _graph
, *root
, depth
, progress
, app
, true );
62 //-----------------------------------------------------------------------------
63 MainWindow::MainWindow( QApplication
* application
, QWidget
* parent
) :
64 QMainWindow( parent
),
65 _application( application
)
69 QHBoxLayout
*hbox
= new QHBoxLayout( _frame
);
71 hbox
->setSpacing( 2 );
73 // Setup zoom and navigation toolbars
74 QToolBar
* zoomBar
= addToolBar( "Zooming" );
75 QSlider
* sldZoom
= new QSlider( Qt::Horizontal
, zoomBar
);
76 sldZoom
->setMinimum( 1 );
77 sldZoom
->setMaximum( 99 );
78 sldZoom
->setPageStep( 10 );
79 sldZoom
->setSliderPosition( 50 );
80 sldZoom
->setMinimumWidth( 190 );
81 zoomBar
->addWidget( sldZoom
);
83 QToolBar
* navigationBar
= addToolBar( "Navigation" );
84 navigationBar
->setIconSize( QSize( 16, 16 ) );
86 // Generate a simple _graph
87 _graph
= new Graph( );
90 _graphView
= new qan::GraphView( _frame
, QColor( 170, 171, 205 ) );
91 GridItem
* grid
= new GridCheckBoardItem( _graphView
, QColor( 255, 255, 255 ), QColor( 238, 230, 230 ) );
92 hbox
->addWidget( _graphView
);
94 // Add canvas pan and zoom action to the navigation tobar
95 _cbGridType
= new QComboBox( navigationBar
);
96 _cbGridType
->addItem( QIcon(), "<< Grid Type >>" );
97 _cbGridType
->addItem( QIcon(), "Regular" );
98 _cbGridType
->addItem( QIcon(), "Check Board" );
99 connect( _cbGridType
, SIGNAL(activated(int)), this, SLOT(gridChanged(int)));
100 navigationBar
->addWidget( _cbGridType
);
102 connect( sldZoom
, SIGNAL( valueChanged(int) ), _graphView
, SLOT( setZoomPercent(int) ) );
103 navigationBar
->setIconSize( QSize( 16, 16 ) );
105 if ( _graphView
->getAction( "zoom_in" ) != 0 )
106 navigationBar
->addAction( _graphView
->getAction( "zoom_in" ) );
107 if ( _graphView
->getAction( "zoom_out" ) != 0 )
108 navigationBar
->addAction( _graphView
->getAction( "zoom_out" ) );
109 navigationBar
->addSeparator( );
110 if ( _graphView
->getAction( "pan_controller" ) != 0 )
111 navigationBar
->addAction( _graphView
->getAction( "pan_controller" ) );
112 if ( _graphView
->getAction( "zoom_window_controller" ) != 0 )
113 navigationBar
->addAction( _graphView
->getAction( "zoom_window_controller" ) );
116 QTimer::singleShot( 0, this, SLOT( loadGraph( ) ) );
119 void MainWindow::loadGraph( )
121 QProgressDialog
* progress
= new QProgressDialog( "Generating graph...", "Cancel", 0, 100, this );
122 progress
->setWindowModality( Qt::WindowModal
);
124 generateTree( *_graph
, progress
, _application
, 5 );
127 _graphView
->setGraph( *_graph
);
129 qan::VectorF
origin( 2 ); origin( 0 ) = 10.f
; origin( 1 ) = 10.f
;
130 qan::VectorF
spacing( 2 ); spacing( 0 ) = 120.f
; spacing( 1 ) = 55.f
;
131 _graphView
->setGraphLayout( new qan::Concentric( 20., 60. ) );
132 _graphView
->layoutGraph( new QProgressDialog( "Laying out...", "Cancel", 1, 100, this ) );
134 //-----------------------------------------------------------------------------