2 Qanava - Graph drawing library for QT
3 Copyright (C) 2005 Benoit AUTHEMAN
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 //-----------------------------------------------------------------------------
21 // This file is a part of the Qanava software.
24 // \author Benoit Autheman (benoit@faktiss.net)
25 // \date 2004 December 05
26 //-----------------------------------------------------------------------------
31 #include "canCanvas.h"
42 #define max( x, y ) ( x > y ? x : y )
45 namespace qan
{ // ::qan
46 namespace can
{ // ::qan::can
49 /* Grid Constructor/Destructor *///-------------------------------------------
50 Grid::Grid( can::Canvas
& canvas
) :
53 // canvas.setGrid( this );
55 //-----------------------------------------------------------------------------
59 /* Grid Lines Management *///-------------------------------------------------
60 void Grid::addLine( int ax
, int ay
, int bx
, int by
, int w
, bool dash
, bool dot
)
62 createLine( ax
, ay
, bx
, by
, w
, dash
, dot
);
65 void Grid::addRectangle( int x
, int y
, int w
, int h
, int r
, int g
, int b
)
67 QCanvasRectangle
* rectangle
= new QCanvasRectangle( x
, y
, w
, h
, &_canvas
);
69 QPen pen
= rectangle
->pen( );
71 pen
.setStyle( Qt::NoPen
);
72 rectangle
->setPen( pen
);
74 QBrush brush
= rectangle
->brush( );
75 brush
.setColor( QColor( r
, g
, b
) );
76 brush
.setStyle( Qt::SolidPattern
);
77 rectangle
->setBrush( brush
);
79 rectangle
->setZ( 0.0 );
80 rectangle
->setVisible( true );
81 _canvas
.setFreezed( rectangle
);
83 if ( ( ( x
+ w
) > _canvas
.width( ) ) ||
84 ( ( y
+ h
) > _canvas
.height( ) ) )
85 _canvas
.resize( max( _canvas
.width( ), ( x
+ w
) ), max( _canvas
.height( ), ( y
+ h
) ) );
88 void Grid::addText( const std::string
& text
, int x
, int y
, bool bold
)
90 QCanvasText
* textItem
= new QCanvasText( &_canvas
);
91 textItem
->setText( text
.c_str( ) );
93 QFont font
= textItem
->font( );
95 textItem
->setFont( font
);
99 textItem
->setZ( 0.5 );
100 textItem
->setVisible( true );
101 _canvas
.setFreezed( textItem
);
103 if ( ( x
> _canvas
.width( ) ) ||
104 ( y
> _canvas
.height( ) ) )
105 _canvas
.resize( max( _canvas
.width( ), x
), max( _canvas
.height( ), y
) );
108 void Grid::resize( int w
, int h
)
110 // Resize the existing horizontal and vertical grid lines
111 for ( Lines::iterator lineIter
= _horizontal
.begin( ); lineIter
!= _horizontal
.end( ); lineIter
++ )
113 QCanvasLine
* line
= *lineIter
;
114 QPoint start
= line
->startPoint( );
115 QPoint end
= line
->endPoint( );
116 line
->setPoints( start
.x( ), start
.y( ), w
, end
.y( ) );
118 for ( Lines::iterator lineIter
= _vertical
.begin( ); lineIter
!= _vertical
.end( ); lineIter
++ )
120 QCanvasLine
* line
= *lineIter
;
121 QPoint start
= line
->startPoint( );
122 QPoint end
= line
->endPoint( );
123 line
->setPoints( start
.x( ), start
.y( ), end
.x( ), h
);
126 // Inform the grid that its size has changed and that it must eventually be re-layouted (to add missing lines for exemple)
127 if ( getLayout( ) != 0 )
128 getLayout( )->resize( w
, h
);
131 void Grid::addHorizontalLine( int ax
, int ay
, int bx
, int by
, int w
, bool dash
, bool dot
)
133 QCanvasLine
* line
= createLine( ax
, ay
, bx
, by
, w
, dash
, dot
);
134 _horizontal
.push_back( line
);
137 void Grid::addVerticalLine( int ax
, int ay
, int bx
, int by
, int w
, bool dash
, bool dot
)
139 QCanvasLine
* line
= createLine( ax
, ay
, bx
, by
, w
, dash
, dot
);
140 _vertical
.push_back( line
);
143 QCanvasLine
* Grid::createLine( int ax
, int ay
, int bx
, int by
, int w
, bool dash
, bool dot
)
145 QCanvasLine
* line
= new QCanvasLine( &_canvas
);
146 line
->setPoints( ax
, ay
, bx
, by
);
147 if ( w
!= 1 || dash
|| dot
)
149 QPen
pen( line
->pen( ) );
152 pen
.setStyle( Qt::DashLine
);
154 pen
.setStyle( Qt::DotLine
);
159 line
->setVisible( true );
160 _canvas
.setFreezed( line
);
162 if ( ( max( ax
, bx
) > _canvas
.width( ) ) ||
163 ( max( ay
, by
) > _canvas
.height( ) ) )
164 _canvas
.resize( max( _canvas
.width( ), max( ax
, bx
) ), max( _canvas
.height( ), max( ay
, by
) ) );
168 //-----------------------------------------------------------------------------