GobChartsWidget  1.0
gobchartslineview.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 "gobchartslineview.h"
25 #include "utils/gobchartscolours.h"
26 
27 #include <QGraphicsEllipseItem>
28 #include <QPen>
29 
30 /*--------------------------------------------------------------------------------*/
31 
32 const qreal DOT_SIDE = 7;
33 
34 /*--------------------------------------------------------------------------------*/
35 
37  GobChartsView( parent )
38 {
39 }
40 
41 /*--------------------------------------------------------------------------------*/
42 
43 
45 {
46  // Default destructor
47 }
48 
49 /*--------------------------------------------------------------------------------*/
50 
52 {
53  if( nrValidItems() > 0 )
54  {
55  emit clearLegend();
56 
57  qreal pointSpacing = gridWidth()/nrValidItems();
58  qreal previousX = innerSceneRectF().left();
59  qreal nextX = innerSceneRectF().left();
60  qreal previousY = innerSceneRectF().bottom();
61  qreal nextY = innerSceneRectF().bottom();
62 
63  QList< int > activeRows = validRowList();
64 
65  for( int row = 0; row < nrValidItems(); row++ )
66  {
67  QString cat = category( activeRows.at( row ) );
68  qreal val = value( activeRows.at( row ) );
69 
70  /* If the value doesn't fall within the specified data range, then ignore this particular category. */
71  if( !isWithinAllowedRange( val ) )
72  {
73  /* Increment colours so that the look of the chart doesn't change
74  when the user toggles between restricted and full ranges. */
76  continue;
77  }
78 
79  qreal dataPercentage = ( totalValue() > 0.0 ) ? ( val/totalValue() ) : 0.0;
80 
81  nextX = innerSceneRectF().left() + pointSpacing * row + pointSpacing/2;
82  nextY = ( innerSceneRectF().bottom() - stripSpace( dataPercentage ) ) - innerSceneRectF().height() * dataPercentage;
83 
84  /* Create dot. */
85  QGraphicsEllipseItem *dot = new QGraphicsEllipseItem( nextX - DOT_SIDE/2, nextY - DOT_SIDE/2, DOT_SIDE, DOT_SIDE);
86 
87  QColor colour;
88 
89  if( useFixedColour() )
90  {
91  colour = fixedColour();
92  dot->setPen( QPen( colour, 1 ) );
93  dot->setBrush( colour );
94  }
95  else
96  {
98  dot->setPen( QPen( colour, 1 ) );
99  dot->setBrush( colour );
100  }
101 
102  dot->setZValue( nrValidItems() - row );
103 
104  /* Create line. */
105  QGraphicsLineItem *lineItem = new QGraphicsLineItem( previousX, previousY, nextX, nextY, dot );
106  lineItem->setPen( QPen( Qt::DotLine ) );
107  lineItem->setFlag( QGraphicsItem::ItemStacksBehindParent );
108 
109  QString legendText = QString( "%1 - %2" ).arg( cat ).arg( val );
110  emit createLegendItem( colour, legendText );
111 
112  addToGraphItemsContainer( model()->index( activeRows.at( row ),VALUE ), dot, legendText );
113 
114  previousX = nextX;
115  previousY = nextY;
116  }
117  }
118  else
119  {
120  debugLog( tr( "GobChartsLineView::generateGraphicsItems# No valid items." ) );
121  }
122 }
123 
124 /*--------------------------------------------------------------------------------*/
125 
127 {
128  return true;
129 }
130 
131 /*--------------------------------------------------------------------------------*/
132 
134 {
135  return QString( "%1" ).arg( static_cast< int >( LINE ) );
136 }
137 
138 /*--------------------------------------------------------------------------------*/