LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
plotitem.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "plotitem.h"
31 #include <cmath>
32 #include <limits>
33 #include <vector>
34 #include <memory>
35 #include <QStyleOption>
36 #include <QColor>
37 #include <qwt_plot.h>
38 #include <qwt_plot_curve.h>
39 #include <qwt_plot_renderer.h>
40 #include <qwt_plot_grid.h>
41 #include <util.h>
42 
44 
45 namespace LeechCraft
46 {
47 namespace Util
48 {
49  PlotItem::PlotItem (QDeclarativeItem *parent)
50  : QDeclarativeItem { parent }
51  , Color_ { "#FF4B10" }
52  {
53  setFlag (QGraphicsItem::ItemHasNoContents, false);
54  }
55 
57  {
58  return Points_;
59  }
60 
62  {
63  if (pts == Points_)
64  return;
65 
66  Points_ = pts;
67  emit pointsChanged ();
68  update ();
69  }
70 
71  QVariant PlotItem::GetMultipoints () const
72  {
73  QVariantList result;
74  for (const auto& set : Multipoints_)
75  result << Util::MakeMap<QString, QVariant> ({
76  { "color", QVariant::fromValue (set.Color_) },
77  { "points", QVariant::fromValue (set.Points_) }
78  });
79  return result;
80  }
81 
82  void PlotItem::SetMultipoints (const QVariant& variant)
83  {
84  Multipoints_.clear ();
85 
86  for (const auto& set : variant.toList ())
87  {
88  const auto& map = set.toMap ();
89  Multipoints_.append ({
90  map ["color"].toString (),
91  map ["points"].value<QList<QPointF>> ()
92  });
93  }
94  update ();
95  }
96 
97  double PlotItem::GetMinYValue () const
98  {
99  return MinYValue_;
100  }
101 
102  void PlotItem::SetMinYValue (double val)
103  {
104  SetNewValue (val, MinYValue_, [this] { emit minYValueChanged (); });
105  }
106 
107  double PlotItem::GetMaxYValue () const
108  {
109  return MaxYValue_;
110  }
111 
112  void PlotItem::SetMaxYValue (double val)
113  {
114  SetNewValue (val, MaxYValue_, [this] { emit maxYValueChanged (); });
115  }
116 
118  {
119  return YGridEnabled_;
120  }
121 
123  {
124  SetNewValue (val, YGridEnabled_, [this] { emit yGridChanged (); });
125  }
126 
128  {
129  return YMinorGridEnabled_;
130  }
131 
133  {
134  SetNewValue (val, YMinorGridEnabled_, [this] { emit yMinorGridChanged (); });
135  }
136 
137  double PlotItem::GetAlpha () const
138  {
139  return Alpha_;
140  }
141 
142  void PlotItem::SetAlpha (double a)
143  {
144  Alpha_ = a;
145  emit alphaChanged ();
146  }
147 
148  QColor PlotItem::GetColor () const
149  {
150  return Color_;
151  }
152 
153  void PlotItem::SetColor (const QColor& color)
154  {
155  SetNewValue (color, Color_, [this] { emit colorChanged (); });
156  }
157 
159  {
160  return LeftAxisEnabled_;
161  }
162 
163  void PlotItem::SetLeftAxisEnabled (bool enabled)
164  {
165  SetNewValue (enabled, LeftAxisEnabled_, [this] { emit leftAxisEnabledChanged (); });
166  }
167 
169  {
170  return BottomAxisEnabled_;
171  }
172 
173  void PlotItem::SetBottomAxisEnabled (bool enabled)
174  {
175  SetNewValue (enabled, BottomAxisEnabled_, [this] { emit bottomAxisEnabledChanged (); });
176  }
177 
178  QString PlotItem::GetLeftAxisTitle () const
179  {
180  return LeftAxisTitle_;
181  }
182 
183  void PlotItem::SetLeftAxisTitle (const QString& title)
184  {
185  SetNewValue (title, LeftAxisTitle_, [this] { emit leftAxisTitleChanged (); });
186  }
187 
189  {
190  return BottomAxisTitle_;
191  }
192 
193  void PlotItem::SetBottomAxisTitle (const QString& title)
194  {
195  SetNewValue (title, BottomAxisTitle_, [this] { emit bottomAxisTitleChanged (); });
196  }
197 
198  QString PlotItem::GetPlotTitle () const
199  {
200  return PlotTitle_;
201  }
202 
203  void PlotItem::SetPlotTitle (const QString& title)
204  {
205  SetNewValue (title, PlotTitle_, [this] { emit plotTitleChanged (); });
206  }
207 
208  QColor PlotItem::GetBackground () const
209  {
210  return BackgroundColor_;
211  }
212 
213  void PlotItem::SetBackground (const QColor& bg)
214  {
215  SetNewValue (bg, BackgroundColor_, [this] { emit backgroundChanged (); });
216  }
217 
218  QColor PlotItem::GetTextColor () const
219  {
220  return TextColor_;
221  }
222 
223  void PlotItem::SetTextColor (const QColor& color)
224  {
225  SetNewValue (color, TextColor_, [this] { emit textColorChanged (); });
226  }
227 
228  void PlotItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget*)
229  {
230  QwtPlot plot;
231  plot.setFrameShape (QFrame::NoFrame);
232  plot.enableAxis (QwtPlot::yLeft, LeftAxisEnabled_);
233  plot.enableAxis (QwtPlot::xBottom, BottomAxisEnabled_);
234  plot.setAxisTitle (QwtPlot::yLeft, LeftAxisTitle_);
235  plot.setAxisTitle (QwtPlot::xBottom, BottomAxisTitle_);
236  plot.resize (option->rect.size ());
237 
238  auto setPaletteColor = [&plot] (const QColor& color, QPalette::ColorRole role) -> void
239  {
240  if (!color.isValid ())
241  return;
242 
243  auto pal = plot.palette ();
244  pal.setColor (role, { color });
245  plot.setPalette (pal);
246  };
247 
248  setPaletteColor (BackgroundColor_, QPalette::Window);
249  setPaletteColor (TextColor_, QPalette::WindowText);
250  setPaletteColor (TextColor_, QPalette::Text);
251 
252  if (!PlotTitle_.isEmpty ())
253  plot.setTitle (QwtText { PlotTitle_ });
254 
255  if (MinYValue_ < MaxYValue_)
256  {
257  plot.setAxisAutoScale (QwtPlot::yLeft, false);
258  plot.setAxisScale (QwtPlot::yLeft, MinYValue_, MaxYValue_);
259  }
260  plot.setAutoFillBackground (false);
261  plot.setCanvasBackground (Qt::transparent);
262 
263  if (YGridEnabled_)
264  {
265  auto grid = new QwtPlotGrid;
266  grid->enableYMin (YMinorGridEnabled_);
267  grid->enableX (false);
268 #if QWT_VERSION >= 0x060100
269  grid->setMinorPen (QPen (Qt::gray, 1, Qt::DashLine));
270 #else
271  grid->setMinPen (QPen (Qt::gray, 1, Qt::DashLine));
272 #endif
273  grid->attach (&plot);
274  }
275 
276  auto items = Multipoints_;
277  if (items.isEmpty ())
278  items.push_back ({ Color_, Points_ });
279 
280  const auto ptsCount = items.first ().Points_.size ();
281  if (ptsCount)
282  plot.setAxisScale (QwtPlot::xBottom, 0, ptsCount - 1);
283 
284  std::vector<std::unique_ptr<QwtPlotCurve>> curves;
285  for (const auto& item : items)
286  {
287  auto curve = new QwtPlotCurve;
288 
289  curve->setPen (QPen (item.Color_));
290  auto transpColor = item.Color_;
291  transpColor.setAlphaF (Alpha_);
292  curve->setBrush (transpColor);
293 
294  curve->setRenderHint (QwtPlotItem::RenderAntialiased);
295  curve->attach (&plot);
296 
297  curve->setSamples (item.Points_.toVector ());
298  }
299  plot.replot ();
300 
301  QwtPlotRenderer {}.render (&plot, painter, option->rect);
302  }
303 
304  template<typename T>
305  void PlotItem::SetNewValue (T val, T& ourVal, const std::function<void ()>& notifier)
306  {
307  if (val == ourVal)
308  return;
309 
310  ourVal = val;
311  notifier ();
312  update ();
313  }
314 }
315 }
QList< QPointF > GetPoints() const
Definition: plotitem.cpp:56
QColor GetTextColor() const
Definition: plotitem.cpp:218
QString GetBottomAxisTitle() const
Definition: plotitem.cpp:188
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override
Definition: plotitem.cpp:228
bool GetLeftAxisEnabled() const
Definition: plotitem.cpp:158
void SetBottomAxisTitle(const QString &)
Definition: plotitem.cpp:193
void SetLeftAxisTitle(const QString &)
Definition: plotitem.cpp:183
void SetPoints(const QList< QPointF > &)
Definition: plotitem.cpp:61
QColor GetColor() const
Definition: plotitem.cpp:148
void SetYMinorGridEnabled(bool)
Definition: plotitem.cpp:132
QString GetPlotTitle() const
Definition: plotitem.cpp:198
QString GetLeftAxisTitle() const
Definition: plotitem.cpp:178
void SetMultipoints(const QVariant &)
Definition: plotitem.cpp:82
bool GetBottomAxisEnabled() const
Definition: plotitem.cpp:168
bool GetYMinorGridEnabled() const
Definition: plotitem.cpp:127
double GetAlpha() const
Definition: plotitem.cpp:137
double GetMaxYValue() const
Definition: plotitem.cpp:107
double GetMinYValue() const
Definition: plotitem.cpp:97
QVariant GetMultipoints() const
Definition: plotitem.cpp:71
void SetColor(const QColor &)
Definition: plotitem.cpp:153
void SetBackground(const QColor &)
Definition: plotitem.cpp:213
unsigned long Window
Definition: xwrapper.h:43
bool GetYGridEnabled() const
Definition: plotitem.cpp:117
Q_DECLARE_METATYPE(LeechCraft::ANFieldData)
void SetPlotTitle(const QString &)
Definition: plotitem.cpp:203
QColor GetBackground() const
Definition: plotitem.cpp:208
void SetBottomAxisEnabled(bool)
Definition: plotitem.cpp:173
void SetTextColor(const QColor &)
Definition: plotitem.cpp:223