GobChartsWidget  1.0
gobchartsgrid.cpp
1 /* Copyright (C) 2012 by William Hallatt.
2  *
3  * This file forms part of the "GobChartsWidget" library.
4  *
5  * This library is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This library 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.
14  *
15  * You should have downloaded a copy of the GNU General Public License
16  * (GNUGPL.txt) and GNU Lesser General Public License (GNULGPL.txt)
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * The official website for this project is www.goblincoding.com and,
20  * although not compulsory, it would be appreciated if all works of whatever
21  * nature referring to or using this library include a reference to this site.
22  */
23 
24 #include "gobchartsgrid.h"
25 #include "utils/globalincludes.h"
26 
27 #include <QGraphicsLineItem>
28 #include <QGraphicsScene>
29 
30 /*--------------------------------------------------------------------------------*/
31 
32 /* Distance line intercepts extend past axis. */
33 const int EXTEND = 5;
34 
35 /*--------------------------------------------------------------------------------*/
36 
37 GobChartsGrid::GobChartsGrid( QObject *parent ) :
38  QObject ( parent ),
39  m_gridLines (),
40  m_rectF (),
41  m_gridPen ( QPen( QBrush( Qt::black ), 1, Qt::DotLine ) ),
42  m_axesPen ( QPen( QBrush( Qt::black ), 2 ) ),
43  m_nrHorizontalLines ( 0 ),
44  m_nrVerticalLines ( 0 ),
45  m_showHorizontalGrid( false ),
46  m_showVerticalGrid ( false ),
47  m_loggingOn ( false )
48 {
49 }
50 
51 /*--------------------------------------------------------------------------------*/
52 
54 {
55  for( int i = 0; i < m_gridLines.size(); i++ )
56  {
57  QGraphicsLineItem *line = m_gridLines.at( i );
58 
59  if( line )
60  {
61  delete line;
62  line = NULLPOINTER;
63  }
64  }
65 }
66 
67 /*--------------------------------------------------------------------------------*/
68 
69 void GobChartsGrid::addGridToScene( QGraphicsScene *scene )
70 {
71  if( scene )
72  {
73  foreach( QGraphicsLineItem *line, m_gridLines )
74  {
75  if( line ) scene->addItem( line );
76  }
77  }
78  else
79  {
80  if( m_loggingOn )
81  {
82  emit lastDebugLogMsg( tr( "GobChartsGrid::addGridToScene# Attempting to add grid to an invalid scene." ) );
83  }
84  }
85 }
86 
87 /*--------------------------------------------------------------------------------*/
88 
89 void GobChartsGrid::removeGridFromScene( QGraphicsScene *scene )
90 {
91  if( scene )
92  {
93  foreach( QGraphicsLineItem *line, m_gridLines )
94  {
95  if( line ) scene->removeItem( line );
96  }
97  }
98  else
99  {
100  if( m_loggingOn )
101  {
102  emit lastDebugLogMsg( tr( "GobChartsGrid::removeGridFromScene# Attempting to remove grid from an invalid scene." ) );
103  }
104  }
105 }
106 
107 /*--------------------------------------------------------------------------------*/
108 
110 {
111  /* Clear existing grid and axes lines. */
112  if( !m_gridLines.empty() )
113  {
114  for( int i = 0; i < m_gridLines.size(); i++ )
115  {
116  QGraphicsLineItem *line = m_gridLines.at( i );
117 
118  if( line )
119  {
120  delete line;
121  line = NULLPOINTER;
122  }
123  }
124 
125  m_gridLines.clear(); // remove pointers
126  }
127 
128  /* Create x and y axes. */
129  QGraphicsLineItem *xAxis = new QGraphicsLineItem;
130  xAxis->setLine( m_rectF.left(), m_rectF.bottom(), m_rectF.right(), m_rectF.bottom() );
131  xAxis->setPen ( m_axesPen );
132  m_gridLines.append( xAxis );
133 
134  QGraphicsLineItem *yAxis = new QGraphicsLineItem;
135  yAxis->setLine( m_rectF.left(), m_rectF.top(), m_rectF.left(), m_rectF.bottom() );
136  yAxis->setPen ( m_axesPen );
137  m_gridLines.append( yAxis );
138 
139  /* Create vertical line items. */
140  if( m_nrVerticalLines > 0 )
141  {
142  qreal spacing = ( m_rectF.width()/m_nrVerticalLines );
143 
144  if( m_showVerticalGrid ) // vertical grid lines and "x" intercepts
145  {
146  for( int i = 1; i <= m_nrVerticalLines; i++ )
147  {
148  QGraphicsLineItem *xLine = new QGraphicsLineItem( m_rectF.left() + i * spacing,
149  m_rectF.top(),
150  m_rectF.left() + i * spacing,
151  m_rectF.bottom() + EXTEND );
152  xLine->setPen( m_gridPen );
153  m_gridLines.append( xLine );
154  }
155  }
156  }
157 
158  /* Create horizontal line items. */
159  if( m_nrHorizontalLines > 0 )
160  {
161  qreal spacing = ( m_rectF.height()/m_nrHorizontalLines );
162 
163  if( m_showHorizontalGrid ) // horizontal grid lines and "y" intercepts
164  {
165  for( int i = 0; i <= m_nrHorizontalLines; i++ )
166  {
167  QGraphicsLineItem *yLine = new QGraphicsLineItem( m_rectF.left() - EXTEND,
168  m_rectF.top() + i * spacing,
169  m_rectF.right(),
170  m_rectF.top() + i * spacing );
171  yLine->setPen( m_gridPen );
172  m_gridLines.append( yLine );
173  }
174  }
175  }
176 }
177 
178 /*--------------------------------------------------------------------------------*/
179 
180 void GobChartsGrid::setGridRectF( const QRectF &rect )
181 {
182  m_rectF = rect;
183 }
184 
185 /*--------------------------------------------------------------------------------*/
186 
188 {
189  return m_rectF.width();
190 }
191 
192 /*--------------------------------------------------------------------------------*/
193 
194 void GobChartsGrid::setHorizontalGridLines( bool set, int number )
195 {
196  m_showHorizontalGrid = set;
197  m_nrHorizontalLines = number;
198 }
199 
200 /*--------------------------------------------------------------------------------*/
201 
202 void GobChartsGrid::setVerticalGridLines( bool set, int number )
203 {
204  m_showVerticalGrid = set;
205  m_nrVerticalLines = number;
206 }
207 
208 /*--------------------------------------------------------------------------------*/
209 
210 void GobChartsGrid::setGridColour( QColor colour )
211 {
212  m_gridPen.setColor( colour );
213 }
214 
215 /*--------------------------------------------------------------------------------*/
216 
217 void GobChartsGrid::setGridLineStyle( Qt::PenStyle style )
218 {
219  m_gridPen.setStyle( style );
220 }
221 
222 /*--------------------------------------------------------------------------------*/
223 
225 {
226  m_loggingOn = logging;
227 }
228 
229 /*--------------------------------------------------------------------------------*/