GobChartsWidget  1.0
gobchartsbarview.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 "gobchartsbarview.h"
25 #include "utils/gobchartscolours.h"
26 
27 #include <QGraphicsDropShadowEffect>
28 #include <QGraphicsRectItem>
29 
30 /*--------------------------------------------------------------------------------*/
31 
32 const int BAR_SPACING = 5;
33 
34 /*--------------------------------------------------------------------------------*/
35 
37  GobChartsView( parent )
38 {
39 }
40 
41 /*--------------------------------------------------------------------------------*/
42 
44 {
45 }
46 
47 /*--------------------------------------------------------------------------------*/
48 
50 {
51  /* Add the chart columns. */
52  if( nrValidItems() > 0 )
53  {
54  emit clearLegend();
55 
56  qreal barColWidth = gridWidth()/nrValidItems();
57  QList< int > validRows = validRowList();
58 
59  for( int row = 0; row < nrValidItems(); row++)
60  {
61  QString cat = category( validRows.at( row ) );
62  qreal val = value( validRows.at( row ) );
63 
64  /* If the value doesn't fall within the specified data range, then ignore this particular category. */
65  if( !isWithinAllowedRange( val ) )
66  {
67  /* Increment colours so that the look of the chart doesn't change
68  when the user toggles between restricted and full ranges. */
70  continue;
71  }
72 
73  qreal dataPercentage = ( totalValue() > 0.0 ) ? ( val/totalValue() ) : 0.0;
74 
75  /* Determine bar rectangle dimensions. */
76  QPointF topLeft;
77  QPointF bottomRight;
78 
79  if( dataPercentage < 0.01 )
80  {
81  /* If we don't have at least a snippet of a graphics item, a lot of the selection model's functionality
82  doesn't work as well as it could. Create at least the semblance of a bar if the value is zero. */
83  topLeft = QPointF( innerSceneRectF().left() + barColWidth * row,
84  innerSceneRectF().bottom() - 1 /* pixel */ );
85 
86  bottomRight = QPointF( innerSceneRectF().left() + barColWidth * row + barColWidth - BAR_SPACING,
87  innerSceneRectF().bottom() );
88  }
89  else
90  {
91  topLeft = QPointF( innerSceneRectF().left() + barColWidth * row,
92  innerSceneRectF().bottom() - stripSpace( dataPercentage ) - dataPercentage * innerSceneRectF().height() );
93 
94  bottomRight = QPointF( innerSceneRectF().left() + barColWidth * row + barColWidth - BAR_SPACING,
95  innerSceneRectF().bottom() );
96  }
97 
98  QRectF barRectangle( topLeft, bottomRight );
99  QLinearGradient columnGradient( barRectangle.bottomLeft(), barRectangle.topRight() );
100  columnGradient.setColorAt( 0, QColor( ( Qt::GlobalColor ) 2 ) );
101 
102  QColor colour;
103 
104  if( useFixedColour() )
105  {
106  colour = fixedColour();
107  columnGradient.setColorAt( 1, colour );
108  }
109  else
110  {
112  columnGradient.setColorAt( 1, colour );
113  }
114 
115  /* Create bar item and add to graph items container. */
116  QBrush columnBrush( columnGradient );
117  QGraphicsRectItem *graphicsBar = new QGraphicsRectItem( barRectangle );
118  graphicsBar->setBrush( columnBrush );
119 
120  QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect;
121  dropShadow->setOffset( QPointF( 2,-2 ) ); // two points to the top and right
122  graphicsBar->setGraphicsEffect( dropShadow ); // takes ownership
123 
124  QString legendText = QString( "%1 - %2" ).arg( cat ).arg( val );
125  emit createLegendItem( colour, legendText );
126 
127  addToGraphItemsContainer( model()->index( validRows.at( row ), VALUE ), graphicsBar, legendText );
128  }
129  }
130  else
131  {
132  debugLog( tr( "GobChartsBarView::generateGraphicsItems# No valid items." ) );
133  }
134 }
135 
136 /*--------------------------------------------------------------------------------*/
137 
139 {
140  return true;
141 }
142 
143 /*--------------------------------------------------------------------------------*/
144 
146 {
147  return QString( "%1" ).arg( static_cast< int >( BAR ) );
148 }
149 
150 /*--------------------------------------------------------------------------------*/