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

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • src
  • meters
meters/graph.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2003 by Hans Karlsson *
3  * karlsson.h@home.se *
4  * *
5  * This program 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 2 of the License, or *
8  * (at your option) any later version. *
9  ***************************************************************************/
10 
11 #include "graph.h"
12 
13 Graph::Graph(Karamba* k, int x, int y, int w, int h, int nbrPts):
14  Meter(k, x, y, w, h), lastValue(0)
15 {
16  nbrPoints = (nbrPts == 0) ? 50 : nbrPts;
17 
18  m_minValue = 0;
19  m_maxValue = 100;
20 
21  fill = false;
22  fillColor = m_color;
23  scrollDir = SCROLL_RIGHT;
24  plotDir = PLOT_UP;
25  //prime m_values with a size of nbrPoints and filled with 0's
26  m_values.resize(nbrPoints);
27  m_values.fill(0);
28 }
29 
30 Graph::~Graph()
31 {}
32 
33 QColor Graph::getFillColor() const
34 {
35  return fillColor;
36 }
37 void Graph::setFillColor(QColor c)
38 {
39  fillColor = c;
40  fill = true;
41  update();
42 }
43 
44 void Graph::shouldFill(bool b)
45 {
46  if (fill != b) {
47  fill = b;
48  update();
49  }
50 }
51 
52 bool Graph::shouldFill() const
53 {
54  return fill;
55 }
56 
57 int Graph::getValue() const
58 {
59  return lastValue;
60 }
61 
62 void Graph::setValue(int v)
63 {
64  //keep lastValue between the bounds
65  if (v > m_maxValue) {
66  lastValue = m_maxValue;
67  } else if (v < m_minValue) {
68  lastValue = m_minValue;
69  } else {
70  lastValue = v;
71  }
72  m_values.prepend(lastValue);
73  if (m_values.count() > nbrPoints) {
74  m_values.erase(m_values.end() - 1);
75  }
76  update();
77 }
78 
79 void Graph::setValue(const QString &v)
80 {
81  double d = v.toDouble();
82  if (d > 0) {
83  d += 0.5;
84  } else if (d < 0) {
85  d -= 0.5;
86  }
87  setValue((int)d);
88 }
89 
90 void Graph::setScrollDirection(const QString& dir)
91 {
92  if (!dir.isEmpty() && dir.toLower() == "left")
93  scrollDir = SCROLL_LEFT;
94  else
95  scrollDir = SCROLL_RIGHT;
96 }
97 
98 QString Graph::getScrollDirection() const
99 {
100  if (scrollDir == SCROLL_LEFT)
101  return "LEFT";
102  return "RIGHT";
103 }
104 
105 void Graph::setPlotDirection(const QString& dir)
106 {
107  if (!dir.isEmpty() && dir.toLower() == "down")
108  plotDir = PLOT_DOWN;
109  else
110  plotDir = PLOT_UP;
111 }
112 QString Graph::getPlotDirection() const
113 {
114  if (plotDir == PLOT_DOWN)
115  return "DOWN";
116  return "UP";
117 }
118 
172 void Graph::paint(QPainter *p, const QStyleOptionGraphicsItem *option,
173  QWidget *widget)
174 {
175  Q_UNUSED(option);
176  Q_UNUSED(widget);
177 
178  if (m_hidden)
179  return;
180 
181  //might want to fudge the width and height by -1 here
182  int width = getWidth();
183  int height = getHeight();
184 
185  int top = 0;
186  int bottom = height;
187 
188  if (plotDir == PLOT_DOWN) {
189  bottom = 0;
190  top = height;
191  }
192 
193  int n = nbrPoints - 1;
194 
195  qreal h_step = ((qreal) width) / n;
196  qreal v_step = ((qreal) height * plotDir) / (m_maxValue - m_minValue) * -1; //neg one shows up here to make subtracting from h (bottom) easier later, can just add the two and not worry about order
197 
198  QPolygonF polygon;
199 
200  for (int k = 0; k <= n;k++) {
201  int i = k;
202  if (scrollDir == SCROLL_RIGHT) {
203  i = n - k;
204  }
205  qreal raw = m_values.at(i);
206  qreal v;
207  //some quick shortcuts for edge cases
208  if (raw <= m_minValue)
209  v = bottom;
210  else if (raw >= m_maxValue)
211  v = top;
212  else
213  v = ((raw - m_minValue) * v_step) + bottom; // since the coordinate system is one with 0 at the top and h at the bottom for the default case
214 
215  polygon << QPointF(h_step * k, v);
216  }
217 
218  p->setRenderHint(QPainter::Antialiasing);
219  p->setRenderHint(QPainter::SmoothPixmapTransform);
220  if (fill) {
221  //compute the 0 boundary for the baseline
222  qreal baseline = ((-m_minValue) * v_step) + bottom;
223  //add "edges" from the baseline to the first and last points of graph
224  polygon.insert(0, QPointF(polygon.first().x(), baseline));
225  polygon.insert(polygon.end(), QPointF(polygon.last().x(), baseline));
226 
227  p->save();
228  QBrush tpBrush(fillColor);
229  p->setPen(Qt::NoPen);
230  p->setBrush(tpBrush);
231  p->drawPolygon(polygon);
232  p->restore();
233  //erase our edges allowing the reuse of this exact polygon
234  polygon.erase(polygon.begin());
235  polygon.erase(polygon.end() - 1);
236  }
237  p->setPen(m_color);
238  p->drawPolyline(polygon);
239 }
240 
241 #include "graph.moc"
Meter::getHeight
virtual int getHeight() const
Definition: meters/meter.cpp:97
graph.h
Meter::getWidth
virtual int getWidth() const
Definition: meters/meter.cpp:83
Graph::getScrollDirection
QString getScrollDirection() const
Definition: meters/graph.cpp:98
QWidget
Karamba
Definition: karamba.h:52
Graph::setPlotDirection
void setPlotDirection(const QString &)
Definition: meters/graph.cpp:105
Meter::m_hidden
bool m_hidden
Definition: meters/meter.h:78
Graph::setScrollDirection
void setScrollDirection(const QString &)
Definition: meters/graph.cpp:90
Meter::m_maxValue
int m_maxValue
Definition: meters/meter.h:80
Graph::Graph
Graph()
Graph::setFillColor
void setFillColor(QColor)
Definition: meters/graph.cpp:37
Graph::getPlotDirection
QString getPlotDirection() const
Definition: meters/graph.cpp:112
Graph::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Notes on graphing rewrite - jwm.
Definition: meters/graph.cpp:172
Meter
Definition: meters/meter.h:23
Graph::setValue
void setValue(int)
Definition: meters/graph.cpp:62
Graph::shouldFill
bool shouldFill() const
Definition: meters/graph.cpp:52
Graph::getValue
int getValue() const
Definition: meters/graph.cpp:57
Graph::getFillColor
QColor getFillColor() const
Definition: meters/graph.cpp:33
Meter::m_minValue
int m_minValue
Definition: meters/meter.h:79
Graph::~Graph
~Graph()
Definition: meters/graph.cpp:30
Meter::m_color
QColor m_color
Definition: meters/meter.h:82
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

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