GobChartsWidget  1.0
gobchartsgraphitems.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 "gobchartsgraphitems.h"
25 #include "utils/globalincludes.h"
26 
27 #include <QGraphicsItem>
28 #include <QGraphicsScene>
29 #include <QModelIndex>
30 
31 /*--------------------------------------------------------------------------------*/
32 
33 const qreal OPACITY = 0.65;
34 
35 /*--------------------------------------------------------------------------------*/
36 
38  QObject ( parent ),
39  m_graphItemMap (),
40  m_legendItemMap(),
41  m_loggingOn ( false )
42 {
43 }
44 
45 /*--------------------------------------------------------------------------------*/
46 
48 {
49  deleteItems();
50 }
51 
52 /*--------------------------------------------------------------------------------*/
53 
54 void GobChartsGraphItems::addItemsToScene( QGraphicsScene *scene )
55 {
56  if( scene )
57  {
58  foreach( QGraphicsItem *item, m_graphItemMap.values() )
59  {
60  if( item ) scene->addItem( item );
61  }
62  }
63  else
64  {
65  if( m_loggingOn )
66  {
67  emit lastDebugLogMsg( tr( "GobChartsGraphItems::addItemsToScene# Attempting to add items to an invalid scene." ) );
68  }
69  }
70 }
71 
72 /*--------------------------------------------------------------------------------*/
73 
74 void GobChartsGraphItems::removeItemsFromScene( QGraphicsScene *scene )
75 {
76  if( scene )
77  {
78  foreach( QGraphicsItem *item, m_graphItemMap.values() )
79  {
80  if( item ) scene->removeItem( item );
81  }
82  }
83  else
84  {
85  if( m_loggingOn )
86  {
87  emit lastDebugLogMsg( tr( "GobChartsGraphItems::removeItemsFromScene# Attempting to remove items from an invalid scene." ) );
88  }
89  }
90 }
91 
92 /*--------------------------------------------------------------------------------*/
93 
94 void GobChartsGraphItems::setSelected( int categoryRow )
95 {
96  QMap<QModelIndex, QGraphicsItem*>::iterator it = m_graphItemMap.begin();
97 
98  while( it != m_graphItemMap.end() )
99  {
100  if( ( it.key().row() == categoryRow ) && ( it.value()->opacity() != OPACITY ) )
101  {
102  it.value()->setOpacity( OPACITY );
103  }
104  else
105  {
106  it.value()->setOpacity( 1.0 );
107  }
108 
109  it++;
110  }
111 }
112 
113 /*--------------------------------------------------------------------------------*/
114 
116 {
117  QMap<QModelIndex, QGraphicsItem*>::iterator it = m_graphItemMap.begin();
118 
119  while( it != m_graphItemMap.end() )
120  {
121  it.value()->setOpacity( 1.0 );
122  it++;
123  }
124 }
125 
126 /*--------------------------------------------------------------------------------*/
127 
128 void GobChartsGraphItems::addItem( const QModelIndex &valueIndex, QGraphicsItem *item, const QString &legendText )
129 {
130  if( item )
131  {
132  m_graphItemMap.insert( valueIndex, item );
133  m_legendItemMap.insert( legendText, item );
134  }
135 }
136 
137 /*--------------------------------------------------------------------------------*/
138 
140 {
141  foreach( QGraphicsItem *item, m_graphItemMap )
142  {
143  if( item )
144  {
145  delete item;
146  item = NULLPOINTER;
147  }
148  }
149 
150  m_graphItemMap.clear();
151  m_legendItemMap.clear();
152 }
153 
154 /*--------------------------------------------------------------------------------*/
155 
156 QModelIndex GobChartsGraphItems::getModelIndex( QGraphicsItem *item ) const
157 {
158  QModelIndex defaultKey;
159 
160  if( item )
161  {
162  return m_graphItemMap.key( item, defaultKey );
163  }
164 
165  return defaultKey;
166 }
167 
168 /*--------------------------------------------------------------------------------*/
169 
170 QModelIndex GobChartsGraphItems::getModelIndex( const QString &text ) const
171 {
172  QModelIndex defaultKey;
173 
174  if( m_legendItemMap.contains( text ) )
175  {
176  return m_graphItemMap.key( m_legendItemMap.value( text ), defaultKey );
177  }
178 
179  return defaultKey;
180 }
181 
182 /*--------------------------------------------------------------------------------*/
183 
184 QRectF GobChartsGraphItems::getItemRectF( const QModelIndex &index ) const
185 {
186  if( m_graphItemMap.contains( index ) )
187  {
188  QPointF point = m_graphItemMap.value( index )->sceneBoundingRect().center();
189  QRectF rect( point, point );
190  rect.setHeight( 1 );
191  rect.setWidth( 1 );
192  return rect;
193  }
194 
195  return QRectF();
196 }
197 
198 /*--------------------------------------------------------------------------------*/
199 
200 QRectF GobChartsGraphItems::getItemRectF( const QString &text ) const
201 {
202  if( m_legendItemMap.contains( text ) )
203  {
204  QPointF point = m_legendItemMap.value( text )->sceneBoundingRect().center();
205  QRectF rect( point, point );
206  rect.setHeight( 1 );
207  rect.setWidth( 1 );
208  return rect;
209  }
210 
211  return QRectF();
212 }
213 
214 /*--------------------------------------------------------------------------------*/
215 
216 QString GobChartsGraphItems::getItemLegend( QGraphicsItem *item ) const
217 {
218  if( item )
219  {
220  return m_legendItemMap.key( item );
221  }
222 
223  return QString();
224 }
225 
226 /*--------------------------------------------------------------------------------*/
227 
229 {
230  m_loggingOn = logging;
231 }
232 
233 /*--------------------------------------------------------------------------------*/