(empty message)
[qanava.git] / src / can / canGrid.cpp
blob67013bfcfffcd602c66bd6bd39b1c845366cbdc1
1 /*
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.
23 // \file qlGrid.cpp
24 // \author Benoit Autheman (benoit@faktiss.net)
25 // \date 2004 December 05
26 //-----------------------------------------------------------------------------
29 // Qanava headers
30 #include "canGrid.h"
31 #include "canCanvas.h"
34 // QT headers
35 #include <qcanvas.h>
36 #include <qpen.h>
39 #include <math.h>
41 #ifndef max
42 #define max( x, y ) ( x > y ? x : y )
43 #endif
45 namespace qan { // ::qan
46 namespace can { // ::qan::can
49 /* Grid Constructor/Destructor *///-------------------------------------------
50 Grid::Grid( can::Canvas& canvas ) :
51 _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( );
70 pen.setWidth( 0 );
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( );
94 font.setBold( bold );
95 textItem->setFont( font );
97 textItem->setX( x );
98 textItem->setY( y );
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( ) );
150 pen.setWidth( w );
151 if ( dash )
152 pen.setStyle( Qt::DashLine );
153 if ( dot )
154 pen.setStyle( Qt::DotLine );
155 line->setPen( pen );
158 line->setZ( 0.5 );
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 ) ) );
166 return line;
168 //-----------------------------------------------------------------------------
171 } // ::qan::can
172 } // ::qan