GobChartsWidget  1.0
gobchartspieview.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 "gobchartspieview.h"
25 #include "utils/gobchartscolours.h"
26 #include "utils/globalincludes.h"
27 
28 #include <QGraphicsDropShadowEffect>
29 #include <QGraphicsEllipseItem>
30 
31 /*--------------------------------------------------------------------------------*/
32 
34  GobChartsView( parent )
35 {
36 }
37 
38 /*--------------------------------------------------------------------------------*/
39 
41 {
42  // Default destructor
43 }
44 
45 /*--------------------------------------------------------------------------------*/
46 
48 {
49  if( nrValidItems() > 0 )
50  {
51  emit clearLegend();
52 
53  /* Calculate pie positioning specs. */
54  qreal pieWidth = gridWidth();
55  qreal pieHeight = innerSceneRectF().height();
56  QRectF pieRectangle( innerSceneRectF().left(), innerSceneRectF().top(), pieWidth, pieHeight );
57 
58  /* Add pie segments. */
59  qreal fullElipse = 5760; // span angles in 16th of a degree (360*16)
60  int lastStopAngle = 0;
61 
62  QList< int > activeRows = validRowList();
63 
64  for( int row = 0; row < nrValidItems(); row++ )
65  {
66  QString cat = category( activeRows.at( row ) );
67  qreal val = value( activeRows.at( row ) );
68 
69  /* If the value doesn't fall within the specified data range, then ignore this particular category. */
70  if( !isWithinAllowedRange( val ) )
71  {
72  /* Increment colours so that the look of the chart doesn't change
73  when the user toggles between restricted and full ranges. */
75  continue;
76  }
77 
78  qreal dataPercentage = ( totalValue() > 0.0 ) ? ( val/totalValue()) : 0.0;
79 
80  /* Draw ellipse. */
81  QGraphicsEllipseItem *graphSegment = new QGraphicsEllipseItem( pieRectangle );
82 
83  QColor colour;
84 
85  if( useFixedColour() )
86  {
87  colour = fixedColour();
88  graphSegment->setBrush( colour );
89  }
90  else
91  {
93  graphSegment->setBrush( colour );
94  }
95 
96  graphSegment->setStartAngle( lastStopAngle );
97  graphSegment->setSpanAngle( qRound( dataPercentage * fullElipse ) );
98 
99  QString legendText = QString( "%1 - %2" ).arg( cat ).arg( val );
100  emit createLegendItem( colour, legendText );
101 
102  addToGraphItemsContainer( model()->index( activeRows.at( row ), VALUE ), graphSegment, legendText );
103 
104  lastStopAngle += graphSegment->spanAngle();
105  }
106  }
107  else
108  {
109  debugLog( tr( "GobChartsPieView::generateGraphicsItems# No valid items." ) );
110  }
111 }
112 
113 /*--------------------------------------------------------------------------------*/
114 
116 {
117  return false;
118 }
119 
120 /*--------------------------------------------------------------------------------*/
121 
123 {
124  return QString( "%1" ).arg( static_cast< int >( PIE ) );
125 }
126 
127 /*--------------------------------------------------------------------------------*/