12 #include <QStyleOption>
17 #include <qwt_plot_curve.h>
18 #include <qwt_plot_renderer.h>
19 #include <qwt_plot_grid.h>
20 #include <qwt_scale_draw.h>
21 #include <qwt_text_label.h>
22 #include <qwt_plot_canvas.h>
29 : QQuickPaintedItem { parent }
30 , Color_ { 0xFF, 0x4B, 0x10 }
32 setFlag (ItemHasContents,
true);
53 for (
const auto& set : Multipoints_)
57 {
"color", QVariant::fromValue (set.Color_) },
58 {
"points", QVariant::fromValue (set.Points_) }
62 map [QStringLiteral (
"brushColor")] = *set.BrushColor_;
71 struct UnsupportedType
80 Multipoints_.clear ();
84 for (
const auto& set : variant.toList ())
86 const auto& map = set.toMap ();
88 const auto& colorVar = map [QStringLiteral (
"color")];
89 const auto&
color = colorVar.toString ();
91 throw UnsupportedType {
"`color` expected to be a QString", colorVar };
93 const auto& pointsVar = map [QStringLiteral (
"points")];
97 else if (pointsVar.canConvert<QVariantList> ())
99 [] (
const QVariant& var)
101 if (var.canConvert<QPointF> ())
102 return var.toPointF ();
104 throw UnsupportedType {
"point element expected to be a QPointF", var };
107 std::optional<QColor> brushColor;
108 if (
const auto& brushVar = map [QStringLiteral (
"brushColor")];
111 if (!brushVar.canConvert<QString> ())
112 throw UnsupportedType {
"`brush` expected to be a QString", brushVar };
113 brushColor = QColor { brushVar.toString () };
116 Multipoints_.append ({
color, brushColor,
points });
119 catch (
const UnsupportedType& ty)
121 qCritical () << Q_FUNC_INFO
122 <<
"invalid multipoints map: "
124 <<
" but got instead"
174 return YGridEnabled_;
179 SetNewValue (val, YGridEnabled_, [
this] { emit
yGridChanged (); });
184 return YMinorGridEnabled_;
215 return LeftAxisEnabled_;
225 return BottomAxisEnabled_;
235 return LeftAxisTitle_;
245 return BottomAxisTitle_;
265 return BackgroundColor_;
285 return GridLinesColor_;
305 const auto& rect = contentsBoundingRect ().toRect ();
309 Plot_ = std::make_shared<QwtPlot> ();
310 Plot_->setFrameShape (QFrame::NoFrame);
311 Plot_->setFrameShadow (QFrame::Plain);
312 Plot_->setLineWidth (0);
313 Plot_->setMidLineWidth (0);
315 if (
const auto canvas = qobject_cast<QwtPlotCanvas*> (Plot_->canvas ()))
316 canvas->setBorderRadius (0);
320 plot.enableAxis (QwtPlot::yLeft, LeftAxisEnabled_);
321 plot.enableAxis (QwtPlot::xBottom, BottomAxisEnabled_);
322 plot.setAxisTitle (QwtPlot::yLeft, LeftAxisTitle_);
323 plot.setAxisTitle (QwtPlot::xBottom, BottomAxisTitle_);
325 if (plot.size () != rect.size ())
326 plot.resize (rect.size ());
328 auto setPaletteColor = [&plot] (
const QColor&
color, QPalette::ColorRole role)
330 if (!
color.isValid ())
333 auto pal = plot.palette ();
334 pal.setColor (role, {
color });
335 plot.setPalette (pal);
339 setPaletteColor (TextColor_, QPalette::WindowText);
342 if (!PlotTitle_.isEmpty ())
343 plot.setTitle (QwtText { PlotTitle_ });
345 if (MinYValue_ < MaxYValue_)
347 plot.setAxisAutoScale (QwtPlot::yLeft,
false);
348 plot.setAxisScale (QwtPlot::yLeft, MinYValue_, MaxYValue_);
350 plot.setAutoFillBackground (
false);
351 plot.setCanvasBackground (Qt::transparent);
355 auto grid =
new QwtPlotGrid;
356 grid->enableYMin (YMinorGridEnabled_);
357 grid->enableX (
false);
358 grid->setMajorPen (QPen (GridLinesColor_, 1, Qt::SolidLine));
359 grid->setMinorPen (QPen (GridLinesColor_, 1, Qt::DashLine));
360 grid->attach (&plot);
363 auto items = Multipoints_;
364 if (items.isEmpty ())
365 items.push_back ({ Color_, {}, Points_ });
367 if (MinXValue_ < MaxXValue_)
368 plot.setAxisScale (QwtPlot::xBottom, MinXValue_, MaxXValue_);
369 else if (
const auto ptsCount = items.first ().Points_.size ())
370 plot.setAxisScale (QwtPlot::xBottom, 0, ptsCount - 1);
372 std::vector<std::unique_ptr<QwtPlotCurve>> curves;
373 for (
const auto& item : items)
375 curves.emplace_back (
new QwtPlotCurve);
376 const auto curve = curves.back ().get ();
378 curve->setPen (QPen (item.Color_));
380 if (item.BrushColor_)
381 curve->setBrush (*item.BrushColor_);
384 auto brushColor = item.Color_;
385 brushColor.setAlphaF (Alpha_);
386 curve->setBrush (brushColor);
389 curve->setRenderHint (QwtPlotItem::RenderAntialiased);
390 curve->attach (&plot);
392 curve->setSamples (item.Points_.toVector ());
397 QwtPlotRenderer {}.render (&plot, painter, rect);
399 const auto xExtent = CalcXExtent (plot);
400 const auto yExtent = CalcYExtent (plot);
409 template<
typename T,
typename Notifier>
410 void PlotItem::SetNewValue (T val, T& ourVal, Notifier&& notifier)
420 int PlotItem::CalcXExtent (QwtPlot& plot)
const
423 if (LeftAxisEnabled_)
424 result += plot.axisScaleDraw (QwtPlot::yLeft)->extent (plot.axisFont (QwtPlot::yLeft));
428 int PlotItem::CalcYExtent (QwtPlot& plot)
const
431 if (BottomAxisEnabled_)
432 result += plot.axisScaleDraw (QwtPlot::xBottom)->extent (plot.axisFont (QwtPlot::xBottom));
433 if (!PlotTitle_.isEmpty ())
434 result += plot.titleLabel ()->sizeHint ().height ();