35 #include <QStyleOption> 38 #include <qwt_plot_curve.h> 39 #include <qwt_plot_renderer.h> 40 #include <qwt_plot_grid.h> 41 #include <qwt_scale_draw.h> 42 #include <qwt_text_label.h> 43 #include <qwt_plot_canvas.h> 52 #if QT_VERSION < 0x050000 53 PlotItem::PlotItem (QDeclarativeItem *parent)
54 : QDeclarativeItem { parent }
57 : QQuickPaintedItem { parent }
59 , Color_ {
"#FF4B10" }
61 #if QT_VERSION < 0x050000 62 setFlag (QGraphicsItem::ItemHasNoContents,
false);
64 setFlag (ItemHasContents,
true);
86 for (
const auto&
set : Multipoints_)
88 auto map = Util::MakeMap<QString, QVariant> ({
89 {
"color", QVariant::fromValue (
set.Color_) },
90 {
"points", QVariant::fromValue (
set.Points_) }
94 map [
"brushColor"] = *
set.BrushColor_;
103 Multipoints_.clear ();
105 for (
const auto&
set : variant.toList ())
107 const auto& map =
set.toMap ();
109 const auto& colorVar = map [
"color"];
110 const auto& pointsVar = map [
"points"];
112 boost::optional<QColor> brushColor;
113 if (map.contains (
"brushColor"))
115 const auto& brushVar = map [
"brushColor"];
116 if (!brushVar.canConvert<QString> ())
117 qWarning () << Q_FUNC_INFO
118 <<
"invalid brush color" 121 brushColor = QColor { brushVar.toString () };
124 if (!colorVar.canConvert<QString> () ||
127 qWarning () << Q_FUNC_INFO
130 qWarning () << Q_FUNC_INFO
131 <<
"ignoring this point";
135 Multipoints_.append ({
136 map [
"color"].toString (),
186 return YGridEnabled_;
191 SetNewValue (val, YGridEnabled_, [
this] { emit
yGridChanged (); });
196 return YMinorGridEnabled_;
222 SetNewValue (color, Color_, [
this] { emit
colorChanged (); });
227 return LeftAxisEnabled_;
237 return BottomAxisEnabled_;
247 return LeftAxisTitle_;
257 return BottomAxisTitle_;
277 return BackgroundColor_;
297 return GridLinesColor_;
315 #if QT_VERSION < 0x050000 316 void PlotItem::paint (QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget*)
318 const auto& rect = option->rect;
322 const auto& rect = contentsBoundingRect ().toRect ();
327 Plot_ = std::make_shared<QwtPlot> ();
328 Plot_->setFrameShape (QFrame::NoFrame);
329 Plot_->setFrameShadow (QFrame::Plain);
330 Plot_->setLineWidth (0);
331 Plot_->setMidLineWidth (0);
333 if (
const auto canvas = qobject_cast<QwtPlotCanvas*> (Plot_->canvas ()))
334 canvas->setBorderRadius (0);
338 plot.enableAxis (QwtPlot::yLeft, LeftAxisEnabled_);
339 plot.enableAxis (QwtPlot::xBottom, BottomAxisEnabled_);
340 plot.setAxisTitle (QwtPlot::yLeft, LeftAxisTitle_);
341 plot.setAxisTitle (QwtPlot::xBottom, BottomAxisTitle_);
343 if (plot.size () != rect.size ())
344 plot.resize (rect.size ());
346 auto setPaletteColor = [&plot] (
const QColor&
color, QPalette::ColorRole role)
348 if (!color.isValid ())
351 auto pal = plot.palette ();
352 pal.setColor (role, { color });
353 plot.setPalette (pal);
357 setPaletteColor (TextColor_, QPalette::WindowText);
358 setPaletteColor (TextColor_, QPalette::Text);
360 if (!PlotTitle_.isEmpty ())
361 plot.setTitle (QwtText { PlotTitle_ });
363 if (MinYValue_ < MaxYValue_)
365 plot.setAxisAutoScale (QwtPlot::yLeft,
false);
366 plot.setAxisScale (QwtPlot::yLeft, MinYValue_, MaxYValue_);
368 plot.setAutoFillBackground (
false);
369 plot.setCanvasBackground (Qt::transparent);
373 auto grid =
new QwtPlotGrid;
374 grid->enableYMin (YMinorGridEnabled_);
375 grid->enableX (
false);
376 #if QWT_VERSION >= 0x060100 377 grid->setMajorPen (QPen (GridLinesColor_, 1, Qt::SolidLine));
378 grid->setMinorPen (QPen (GridLinesColor_, 1, Qt::DashLine));
380 grid->setMajPen (QPen (GridLinesColor_, 1, Qt::SolidLine));
381 grid->setMinPen (QPen (GridLinesColor_, 1, Qt::DashLine));
383 grid->attach (&plot);
386 auto items = Multipoints_;
387 if (items.isEmpty ())
388 items.push_back ({ Color_, {}, Points_ });
390 if (MinXValue_ < MaxXValue_)
391 plot.setAxisScale (QwtPlot::xBottom, MinXValue_, MaxXValue_);
392 else if (
const auto ptsCount = items.first ().Points_.size ())
393 plot.setAxisScale (QwtPlot::xBottom, 0, ptsCount - 1);
395 std::vector<std::unique_ptr<QwtPlotCurve>> curves;
396 for (
const auto& item : items)
398 curves.emplace_back (
new QwtPlotCurve);
399 const auto curve = curves.back ().get ();
401 curve->setPen (QPen (item.Color_));
403 if (item.BrushColor_)
404 curve->setBrush (*item.BrushColor_);
407 auto brushColor = item.Color_;
408 brushColor.setAlphaF (Alpha_);
409 curve->setBrush (brushColor);
412 curve->setRenderHint (QwtPlotItem::RenderAntialiased);
413 curve->attach (&plot);
415 curve->setSamples (item.Points_.toVector ());
420 QwtPlotRenderer {}.render (&plot, painter, rect);
422 const auto xExtent = CalcXExtent (plot);
423 const auto yExtent = CalcYExtent (plot);
433 void PlotItem::SetNewValue (T val, T& ourVal,
const std::function<
void ()>& notifier)
443 int PlotItem::CalcXExtent (QwtPlot& plot)
const 446 if (LeftAxisEnabled_)
447 result += plot.axisScaleDraw (QwtPlot::yLeft)->
448 extent (plot.axisFont (QwtPlot::yLeft));
452 int PlotItem::CalcYExtent (QwtPlot& plot)
const 455 if (BottomAxisEnabled_)
456 result += plot.axisScaleDraw (QwtPlot::xBottom)->
457 extent (plot.axisFont (QwtPlot::xBottom));
458 if (!PlotTitle_.isEmpty ())
459 result += plot.titleLabel ()->sizeHint ().height ();
void SetMinXValue(double)
QList< QPointF > GetPoints() const
void leftAxisTitleChanged()
QColor GetTextColor() const
double GetMinXValue() const
double GetMaxXValue() const
QString GetBottomAxisTitle() const
void gridLinesColorChanged()
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override
bool GetLeftAxisEnabled() const
void SetGridLinesColor(const QColor &)
void leftAxisEnabledChanged()
void SetBottomAxisTitle(const QString &)
void SetLeftAxisTitle(const QString &)
void bottomAxisEnabledChanged()
void SetPoints(const QList< QPointF > &)
void SetYMinorGridEnabled(bool)
QString GetPlotTitle() const
QString GetLeftAxisTitle() const
void SetMultipoints(const QVariant &)
void SetMaxYValue(double)
bool GetBottomAxisEnabled() const
void SetMaxXValue(double)
bool GetYMinorGridEnabled() const
double GetMaxYValue() const
void SetLeftAxisEnabled(bool)
double GetMinYValue() const
QVariant GetMultipoints() const
void SetYGridEnabled(bool)
void SetColor(const QColor &)
void SetBackground(const QColor &)
bool GetYGridEnabled() const
Q_DECLARE_METATYPE(LeechCraft::ANFieldData)
void SetPlotTitle(const QString &)
QColor GetGridLinesColor() const
void SetMinYValue(double)
QColor GetBackground() const
PlotItem(QDeclarativeItem *=0)
void bottomAxisTitleChanged()
void SetBottomAxisEnabled(bool)
void SetTextColor(const QColor &)