• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • plotting
kplotobject.cpp
Go to the documentation of this file.
1 /* -*- C++ -*-
2  This file is part of the KDE libraries
3  Copyright (C) 2003 Jason Harris <kstars@30doradus.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (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 GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kplotobject.h"
22 
23 #include <QtAlgorithms>
24 #include <QPainter>
25 
26 #include <kdebug.h>
27 
28 #include "kplotpoint.h"
29 #include "kplotwidget.h"
30 
31 class KPlotObject::Private
32 {
33  public:
34  Private( KPlotObject * qq )
35  : q( qq )
36  {
37  }
38 
39  ~Private()
40  {
41  qDeleteAll( pList );
42  }
43 
44  KPlotObject *q;
45 
46  QList<KPlotPoint*> pList;
47  PlotTypes type;
48  PointStyle pointStyle;
49  double size;
50  QPen pen, linePen, barPen, labelPen;
51  QBrush brush, barBrush;
52 };
53 
54 KPlotObject::KPlotObject( const QColor &c, PlotType t, double size, PointStyle ps )
55  : d( new Private( this ) )
56 {
57  //By default, all pens and brushes are set to the given color
58  setBrush( c );
59  setBarBrush( c );
60  setPen( QPen( brush(), 1 ) );
61  setLinePen( pen() );
62  setBarPen( pen() );
63  setLabelPen( pen() );
64 
65  d->type |= t;
66  setSize( size );
67  setPointStyle( ps );
68 }
69 
70 KPlotObject::~KPlotObject()
71 {
72  delete d;
73 }
74 
75 KPlotObject::PlotTypes KPlotObject::plotTypes() const
76 {
77  return d->type;
78 }
79 
80 void KPlotObject::setShowPoints( bool b )
81 {
82  if ( b )
83  {
84  d->type |= KPlotObject::Points;
85  }
86  else
87  {
88  d->type &= ~KPlotObject::Points;
89  }
90 }
91 
92 void KPlotObject::setShowLines( bool b )
93 {
94  if ( b )
95  {
96  d->type |= KPlotObject::Lines;
97  }
98  else
99  {
100  d->type &= ~KPlotObject::Lines;
101  }
102 }
103 
104 void KPlotObject::setShowBars( bool b )
105 {
106  if ( b )
107  {
108  d->type |= KPlotObject::Bars;
109  }
110  else
111  {
112  d->type &= ~KPlotObject::Bars;
113  }
114 }
115 
116 double KPlotObject::size() const
117 {
118  return d->size;
119 }
120 
121 void KPlotObject::setSize( double s )
122 {
123  d->size = s;
124 }
125 
126 KPlotObject::PointStyle KPlotObject::pointStyle() const
127 {
128  return d->pointStyle;
129 }
130 
131 void KPlotObject::setPointStyle( PointStyle p )
132 {
133  d->pointStyle = p;
134 }
135 
136 const QPen& KPlotObject::pen() const
137 {
138  return d->pen;
139 }
140 
141 void KPlotObject::setPen( const QPen &p )
142 {
143  d->pen = p;
144 }
145 
146 const QPen& KPlotObject::linePen() const
147 {
148  return d->linePen;
149 }
150 
151 void KPlotObject::setLinePen( const QPen &p )
152 {
153  d->linePen = p;
154 }
155 
156 const QPen& KPlotObject::barPen() const
157 {
158  return d->barPen;
159 }
160 
161 void KPlotObject::setBarPen( const QPen &p )
162 {
163  d->barPen = p;
164 }
165 
166 const QPen& KPlotObject::labelPen() const
167 {
168  return d->labelPen;
169 }
170 
171 void KPlotObject::setLabelPen( const QPen &p )
172 {
173  d->labelPen = p;
174 }
175 
176 const QBrush KPlotObject::brush() const
177 {
178  return d->brush;
179 }
180 
181 void KPlotObject::setBrush( const QBrush &b )
182 {
183  d->brush = b;
184 }
185 
186 const QBrush KPlotObject::barBrush() const
187 {
188  return d->barBrush;
189 }
190 
191 void KPlotObject::setBarBrush( const QBrush &b )
192 {
193  d->barBrush = b;
194 }
195 
196 QList< KPlotPoint* > KPlotObject::points() const
197 {
198  return d->pList;
199 }
200 
201 void KPlotObject::addPoint( const QPointF &p, const QString &label, double barWidth )
202 {
203  addPoint( new KPlotPoint( p.x(), p.y(), label, barWidth ) );
204 }
205 
206 void KPlotObject::addPoint( KPlotPoint *p )
207 {
208  if ( !p )
209  return;
210  d->pList.append( p );
211 }
212 
213 void KPlotObject::addPoint( double x, double y, const QString &label, double barWidth )
214 {
215  addPoint( new KPlotPoint( x, y, label, barWidth ) );
216 }
217 
218 void KPlotObject::removePoint( int index ) {
219  if ( ( index < 0 ) || ( index >= d->pList.count() ) ) {
220  kWarning() << "KPlotObject::removePoint(): index " << index << " out of range!";
221  return;
222  }
223 
224  d->pList.removeAt( index );
225 }
226 
227 void KPlotObject::clearPoints()
228 {
229  qDeleteAll( d->pList );
230  d->pList.clear();
231 }
232 
233 void KPlotObject::draw( QPainter *painter, KPlotWidget *pw ) {
234  //Order of drawing determines z-distance: Bars in the back, then lines,
235  //then points, then labels.
236 
237  if ( d->type & Bars ) {
238  painter->setPen( barPen() );
239  painter->setBrush( barBrush() );
240 
241  double w = 0;
242  for ( int i=0; i<d->pList.size(); ++i ) {
243  if ( d->pList[i]->barWidth() == 0.0 ) {
244  if ( i<d->pList.size()-1 )
245  w = d->pList[i+1]->x() - d->pList[i]->x();
246  //For the last bin, we'll just keep the previous width
247 
248  } else {
249  w = d->pList[i]->barWidth();
250  }
251 
252  QPointF pp = d->pList[i]->position();
253  QPointF p1( pp.x() - 0.5*w, 0.0 );
254  QPointF p2( pp.x() + 0.5*w, pp.y() );
255  QPointF sp1 = pw->mapToWidget( p1 );
256  QPointF sp2 = pw->mapToWidget( p2 );
257 
258  QRectF barRect = QRectF( sp1.x(), sp1.y(), sp2.x()-sp1.x(), sp2.y()-sp1.y() ).normalized();
259  painter->drawRect( barRect );
260  pw->maskRect( barRect, 0.25 );
261  }
262  }
263 
264  //Draw lines:
265  if ( d->type & Lines ) {
266  painter->setPen( linePen() );
267 
268  QPointF Previous = QPointF(); //Initialize to null
269 
270  foreach ( KPlotPoint *pp, d->pList ) {
271  //q is the position of the point in screen pixel coordinates
272  QPointF q = pw->mapToWidget( pp->position() );
273 
274  if ( ! Previous.isNull() ) {
275  painter->drawLine( Previous, q );
276  pw->maskAlongLine( Previous, q );
277  }
278 
279  Previous = q;
280  }
281  }
282 
283  //Draw points:
284  if ( d->type & Points ) {
285 
286  foreach( KPlotPoint *pp, d->pList ) {
287  //q is the position of the point in screen pixel coordinates
288  QPointF q = pw->mapToWidget( pp->position() );
289  if ( pw->pixRect().contains( q.toPoint(), false ) ) {
290  double x1 = q.x() - size();
291  double y1 = q.y() - size();
292  QRectF qr = QRectF( x1, y1, 2*size(), 2*size() );
293 
294  //Mask out this rect in the plot for label avoidance
295  pw->maskRect( qr, 2.0 );
296 
297  painter->setPen( pen() );
298  painter->setBrush( brush() );
299 
300  switch ( pointStyle() ) {
301  case Circle:
302  painter->drawEllipse( qr );
303  break;
304 
305  case Letter:
306  painter->drawText( qr, Qt::AlignCenter, pp->label().left(1) );
307  break;
308 
309  case Triangle:
310  {
311  QPolygonF tri;
312  tri << QPointF( q.x() - size(), q.y() + size() )
313  << QPointF( q.x(), q.y() - size() )
314  << QPointF( q.x() + size(), q.y() + size() );
315  painter->drawPolygon( tri );
316  break;
317  }
318 
319  case Square:
320  painter->drawRect( qr );
321  break;
322 
323  case Pentagon:
324  {
325  QPolygonF pent;
326  pent << QPointF( q.x(), q.y() - size() )
327  << QPointF( q.x() + size(), q.y() - 0.309*size() )
328  << QPointF( q.x() + 0.588*size(), q.y() + size() )
329  << QPointF( q.x() - 0.588*size(), q.y() + size() )
330  << QPointF( q.x() - size(), q.y() - 0.309*size() );
331  painter->drawPolygon( pent );
332  break;
333  }
334 
335  case Hexagon:
336  {
337  QPolygonF hex;
338  hex << QPointF( q.x(), q.y() + size() )
339  << QPointF( q.x() + size(), q.y() + 0.5*size() )
340  << QPointF( q.x() + size(), q.y() - 0.5*size() )
341  << QPointF( q.x(), q.y() - size() )
342  << QPointF( q.x() - size(), q.y() + 0.5*size() )
343  << QPointF( q.x() - size(), q.y() - 0.5*size() );
344  painter->drawPolygon( hex );
345  break;
346  }
347 
348  case Asterisk:
349  painter->drawLine( q, QPointF( q.x(), q.y() + size() ) );
350  painter->drawLine( q, QPointF( q.x() + size(), q.y() + 0.5*size() ) );
351  painter->drawLine( q, QPointF( q.x() + size(), q.y() - 0.5*size() ) );
352  painter->drawLine( q, QPointF( q.x(), q.y() - size() ) );
353  painter->drawLine( q, QPointF( q.x() - size(), q.y() + 0.5*size() ) );
354  painter->drawLine( q, QPointF( q.x() - size(), q.y() - 0.5*size() ) );
355  break;
356 
357  case Star:
358  {
359  QPolygonF star;
360  star << QPointF( q.x(), q.y() - size() )
361  << QPointF( q.x() + 0.2245*size(), q.y() - 0.309*size() )
362  << QPointF( q.x() + size(), q.y() - 0.309*size() )
363  << QPointF( q.x() + 0.363*size(), q.y() + 0.118*size() )
364  << QPointF( q.x() + 0.588*size(), q.y() + size() )
365  << QPointF( q.x(), q.y() + 0.382*size() )
366  << QPointF( q.x() - 0.588*size(), q.y() + size() )
367  << QPointF( q.x() - 0.363*size(), q.y() + 0.118*size() )
368  << QPointF( q.x() - size(), q.y() - 0.309*size() )
369  << QPointF( q.x() - 0.2245*size(), q.y() - 0.309*size() );
370  painter->drawPolygon( star );
371  break;
372  }
373 
374  default:
375  break;
376  }
377  }
378  }
379  }
380 
381  //Draw labels
382  painter->setPen( labelPen() );
383 
384  foreach ( KPlotPoint *pp, d->pList ) {
385  QPoint q = pw->mapToWidget( pp->position() ).toPoint();
386  if ( pw->pixRect().contains(q, false) && ! pp->label().isEmpty() ) {
387  pw->placeLabel( painter, pp );
388  }
389  }
390 
391 }
QColor
KPlotPoint
Encapsulates a point in the plot.
Definition: kplotpoint.h:40
KPlotWidget::maskRect
void maskRect(const QRectF &r, float value=1.0)
Indicate that object labels should try to avoid the given rectangle in the plot.
Definition: kplotwidget.cpp:412
KPlotObject::setShowLines
void setShowLines(bool b)
Set whether lines will be drawn for this object.
Definition: kplotobject.cpp:92
KPlotObject::Pentagon
Definition: kplotobject.h:83
kdebug.h
KPlotObject::setPointStyle
void setPointStyle(PointStyle p)
Set a new style for drawing the points in this object.
Definition: kplotobject.cpp:131
KPlotObject::brush
const QBrush brush() const
Definition: kplotobject.cpp:176
KPlotObject::setSize
void setSize(double s)
Set the size for plotted points in this object, in pixels.
Definition: kplotobject.cpp:121
KPlotObject::draw
void draw(QPainter *p, KPlotWidget *pw)
Draw this KPlotObject on the given QPainter.
Definition: kplotobject.cpp:233
KPlotObject::pointStyle
PointStyle pointStyle() const
Definition: kplotobject.cpp:126
KPlotObject::Lines
each KPlotPoint is connected with a line
Definition: kplotobject.h:68
KStandardShortcut::label
QString label(StandardShortcut id)
Returns a localized label for user-visible display.
Definition: kstandardshortcut.cpp:267
KPlotObject::Asterisk
Definition: kplotobject.h:85
KPlotWidget::maskAlongLine
void maskAlongLine(const QPointF &p1, const QPointF &p2, float value=1.0)
Indicate that object labels should try to avoid the line joining the two given points (in pixel coord...
Definition: kplotwidget.cpp:427
KPlotObject::setShowPoints
void setShowPoints(bool b)
Set whether points will be drawn for this object.
Definition: kplotobject.cpp:80
QString
KPlotObject::removePoint
void removePoint(int index)
Remove the QPointF at position index from the list of points.
Definition: kplotobject.cpp:218
KPlotPoint::label
QString label() const
Definition: kplotpoint.cpp:93
KPlotObject::Triangle
Definition: kplotobject.h:81
KPlotObject::pen
const QPen & pen() const
Definition: kplotobject.cpp:136
KPlotObject::Square
Definition: kplotobject.h:82
KPlotObject::points
QList< KPlotPoint * > points() const
Definition: kplotobject.cpp:196
KPlotObject::labelPen
const QPen & labelPen() const
Definition: kplotobject.cpp:166
KPlotObject::Bars
each KPlotPoint is shown as a vertical bar
Definition: kplotobject.h:69
KPlotObject::setShowBars
void setShowBars(bool b)
Set whether bars will be drawn for this object.
Definition: kplotobject.cpp:104
KPlotObject::setLabelPen
void setLabelPen(const QPen &p)
Set the pen to use for labels for this object The pen to use.
Definition: kplotobject.cpp:171
kplotpoint.h
KPlotObject::~KPlotObject
~KPlotObject()
Destructor.
Definition: kplotobject.cpp:70
KPlotObject::setPen
void setPen(const QPen &p)
Set the default pen for this object The pen to use.
Definition: kplotobject.cpp:141
KPlotObject
Encapsulates a data set to be plotted in a KPlotWidget.
Definition: kplotobject.h:53
KPlotObject::PlotType
PlotType
The type classification of the KPlotObject.
Definition: kplotobject.h:64
KPlotObject::setLinePen
void setLinePen(const QPen &p)
Set the pen to use for drawing lines for this object The pen to use.
Definition: kplotobject.cpp:151
KPlotObject::KPlotObject
KPlotObject(const QColor &color=Qt::white, PlotType otype=Points, double size=2, PointStyle ps=Circle)
Constructor.
Definition: kplotobject.cpp:54
KPlotObject::Hexagon
Definition: kplotobject.h:84
KPlotWidget::placeLabel
void placeLabel(QPainter *painter, KPlotPoint *pp)
Place an object label optimally in the plot.
Definition: kplotwidget.cpp:489
KPlotObject::Circle
Definition: kplotobject.h:79
KPlotWidget
Generic data plotting widget.
Definition: kplotwidget.h:80
KPlotObject::addPoint
void addPoint(const QPointF &p, const QString &label=QString(), double barWidth=0.0)
Add a point to the object's list of points, using input data to construct a KPlotPoint.
Definition: kplotobject.cpp:201
KPlotObject::size
double size() const
Definition: kplotobject.cpp:116
KPlotObject::Letter
Definition: kplotobject.h:80
KPlotObject::barPen
const QPen & barPen() const
Definition: kplotobject.cpp:156
KPlotObject::Points
each KPlotPoint is represented with a drawn point
Definition: kplotobject.h:67
KPlotWidget::mapToWidget
QPointF mapToWidget(const QPointF &p) const
Map a coordinate.
Definition: kplotwidget.cpp:405
KPlotWidget::pixRect
QRect pixRect() const
Definition: kplotwidget.cpp:357
KPlotObject::setBrush
void setBrush(const QBrush &b)
Set the default brush to use for this object The brush to use.
Definition: kplotobject.cpp:181
QPoint
KPlotObject::barBrush
const QBrush barBrush() const
Definition: kplotobject.cpp:186
kplotwidget.h
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KPlotObject::linePen
const QPen & linePen() const
Definition: kplotobject.cpp:146
KPlotObject::PointStyle
PointStyle
The available shape styles for plotted points.
Definition: kplotobject.h:76
KPlotObject::clearPoints
void clearPoints()
Remove and destroy the points of this object.
Definition: kplotobject.cpp:227
KPlotObject::setBarBrush
void setBarBrush(const QBrush &b)
Set the brush to use for drawing bars for this object The brush to use.
Definition: kplotobject.cpp:191
KPlotPoint::position
QPointF position() const
Definition: kplotpoint.cpp:63
KPlotObject::Star
Definition: kplotobject.h:86
QList< KPlotPoint * >
KPlotObject::plotTypes
PlotTypes plotTypes() const
Definition: kplotobject.cpp:75
kplotobject.h
KPlotObject::setBarPen
void setBarPen(const QPen &p)
Set the pen to use for drawing bars for this object The pen to use.
Definition: kplotobject.cpp:161
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal