Import the trees sample
[qanava.git] / tests / trees / MainWindow.cpp
blob6811b61170e15ab0bc96280e1c7ef21d50c263e1
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/qanRepository.h"
12 #include "../../src/qanGraphItemModel.h"
13 #include "../../src/qanGraphItemView.h"
14 #include "../../src/qanGrid.h"
15 #include "../../src/qanTreeLayout.h"
16 #include "./mainWindow.h"
19 // QT headers
20 #include <QToolBar>
21 #include <QAction>
22 #include <QHeaderView>
23 #include <QTextCursor>
24 #include <QDirModel>
25 #include <QToolButton>
28 using namespace qan;
30 void generateBranch( Graph& _graph, Node& superNode, int depth, QProgressDialog* progress, QApplication* app, bool root = false )
32 if ( --depth < 1 )
33 return;
35 int subNodeCount = 1 + ( rand( ) % 4 );
36 app->processEvents( );
37 if ( root )
38 progress->setMaximum( subNodeCount );
40 for ( int n = 0; n < subNodeCount; n++ )
42 if ( root )
44 app->processEvents( );
45 progress->setValue( n );
46 if ( progress->wasCanceled( ) )
47 break;
50 QString label;
51 label = "Node ";
52 label += QString::number( _graph.getNodeCount( ) );
53 Node* node = _graph.addNode( label );
54 _graph.addEdge( superNode, *node );
56 bool goDeeper = ( rand( ) % 3 );
57 if ( goDeeper )
58 generateBranch( _graph, *node, depth, progress, app );
62 void generateTree( Graph& _graph, QProgressDialog* progress, QApplication* app, int depth = 5 )
64 Node* root = _graph.addNode( "Root" );
65 if ( root != 0 )
66 generateBranch( _graph, *root, depth, progress, app, true );
69 //-----------------------------------------------------------------------------
70 MainWindow::MainWindow( QApplication* application, QWidget* parent ) :
71 QMainWindow( parent ),
72 _application( application )
74 setupUi( this );
76 QHBoxLayout *hboxA = new QHBoxLayout( _frameA );
77 hboxA->setMargin( 0 );
78 QHBoxLayout *hboxB = new QHBoxLayout( _frameB );
79 hboxB->setMargin( 0 );
81 // Create a graph item view and generate a simple graph
82 Graph* graph = new Graph( );
83 _graphItemViewA = new qan::GraphItemView( _frameA, graph, QColor( 255, 255, 255 ) );
84 GridItem* grid = new GridCheckBoardItem( _graphItemViewA->getGraphicsView( ), palette( ).color( QPalette::Base ), palette( ).color( QPalette::AlternateBase ) );
86 _graphItemViewB = new qan::GraphItemView( _frameB, graph, QColor( 255, 255, 255 ) );
87 new GridCheckBoardItem( _graphItemViewB->getGraphicsView( ), palette( ).color( QPalette::Base ), palette( ).color( QPalette::AlternateBase ) );
89 QProgressDialog* progress = new QProgressDialog( "Generating graph...", "Cancel", 0, 100, this );
90 progress->setWindowModality( Qt::WindowModal );
91 progress->show( );
92 generateTree( *graph, progress, _application, 4 );
93 progress->close( );
95 hboxA->addWidget( _graphItemViewA );
96 _graphItemViewA->setModel( graph );
97 _graphItemViewA->layoutGraph( );
99 hboxB->addWidget( _graphItemViewB );
100 _graphItemViewB->setModel( graph );
101 _graphItemViewB->layoutGraph( );
104 _graphItemViewA->setGraphLayout( new qan::HierarchyTree( QPointF( 120., 55. ), QPointF( 10., 10. ), Qt::Horizontal ) );
105 _graphItemViewA->layoutGraph( );
107 _graphItemViewB->setGraphLayout( new qan::ChanTree( ) );
108 _graphItemViewB->layoutGraph( );
110 //-----------------------------------------------------------------------------