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 : QQuickPaintedItem { parent }
53 , Color_ {
"#FF4B10" }
55 setFlag (ItemHasContents,
true);
76 for (
const auto& set : Multipoints_)
80 {
"color", QVariant::fromValue (set.Color_) },
81 {
"points", QVariant::fromValue (set.Points_) }
85 map [
"brushColor"] = *set.BrushColor_;
94 Multipoints_.clear ();
96 for (
const auto& set : variant.toList ())
98 const auto& map = set.toMap ();
100 const auto& colorVar = map [
"color"];
101 const auto& pointsVar = map [
"points"];
103 boost::optional<QColor> brushColor;
104 if (map.contains (
"brushColor"))
106 const auto& brushVar = map [
"brushColor"];
107 if (!brushVar.canConvert<QString> ())
108 qWarning () << Q_FUNC_INFO
109 <<
"invalid brush color"
112 brushColor = QColor { brushVar.toString () };
115 if (!colorVar.canConvert<QString> () ||
118 qWarning () << Q_FUNC_INFO
121 qWarning () << Q_FUNC_INFO
122 <<
"ignoring this point";
126 Multipoints_.append ({
127 map [
"color"].toString (),
177 return YGridEnabled_;
182 SetNewValue (val, YGridEnabled_, [
this] { emit
yGridChanged (); });
187 return YMinorGridEnabled_;
218 return LeftAxisEnabled_;
228 return BottomAxisEnabled_;
238 return LeftAxisTitle_;
248 return BottomAxisTitle_;
268 return BackgroundColor_;
288 return GridLinesColor_;
308 const auto& rect = contentsBoundingRect ().toRect ();
312 Plot_ = std::make_shared<QwtPlot> ();
313 Plot_->setFrameShape (QFrame::NoFrame);
314 Plot_->setFrameShadow (QFrame::Plain);
315 Plot_->setLineWidth (0);
316 Plot_->setMidLineWidth (0);
318 if (
const auto canvas = qobject_cast<QwtPlotCanvas*> (Plot_->canvas ()))
319 canvas->setBorderRadius (0);
323 plot.enableAxis (QwtPlot::yLeft, LeftAxisEnabled_);
324 plot.enableAxis (QwtPlot::xBottom, BottomAxisEnabled_);
325 plot.setAxisTitle (QwtPlot::yLeft, LeftAxisTitle_);
326 plot.setAxisTitle (QwtPlot::xBottom, BottomAxisTitle_);
328 if (plot.size () != rect.size ())
329 plot.resize (rect.size ());
331 auto setPaletteColor = [&plot] (
const QColor&
color, QPalette::ColorRole role)
336 auto pal = plot.palette ();
337 pal.setColor (role, {
color });
338 plot.setPalette (pal);
342 setPaletteColor (TextColor_, QPalette::WindowText);
343 setPaletteColor (TextColor_, QPalette::Text);
345 if (!PlotTitle_.isEmpty ())
346 plot.setTitle (QwtText { PlotTitle_ });
348 if (MinYValue_ < MaxYValue_)
350 plot.setAxisAutoScale (QwtPlot::yLeft,
false);
351 plot.setAxisScale (QwtPlot::yLeft, MinYValue_, MaxYValue_);
353 plot.setAutoFillBackground (
false);
354 plot.setCanvasBackground (Qt::transparent);
358 auto grid =
new QwtPlotGrid;
359 grid->enableYMin (YMinorGridEnabled_);
360 grid->enableX (
false);
361 grid->setMajorPen (QPen (GridLinesColor_, 1, Qt::SolidLine));
362 grid->setMinorPen (QPen (GridLinesColor_, 1, Qt::DashLine));
363 grid->attach (&plot);
366 auto items = Multipoints_;
367 if (items.isEmpty ())
368 items.push_back ({ Color_, {}, Points_ });
370 if (MinXValue_ < MaxXValue_)
371 plot.setAxisScale (QwtPlot::xBottom, MinXValue_, MaxXValue_);
372 else if (
const auto ptsCount = items.first ().Points_.size ())
373 plot.setAxisScale (QwtPlot::xBottom, 0, ptsCount - 1);
375 std::vector<std::unique_ptr<QwtPlotCurve>> curves;
376 for (
const auto& item : items)
378 curves.emplace_back (
new QwtPlotCurve);
379 const auto curve = curves.back ().get ();
381 curve->setPen (QPen (item.Color_));
383 if (item.BrushColor_)
384 curve->setBrush (*item.BrushColor_);
387 auto brushColor = item.Color_;
388 brushColor.setAlphaF (Alpha_);
389 curve->setBrush (brushColor);
392 curve->setRenderHint (QwtPlotItem::RenderAntialiased);
393 curve->attach (&plot);
395 curve->setSamples (item.Points_.toVector ());
400 QwtPlotRenderer {}.render (&plot, painter, rect);
402 const auto xExtent = CalcXExtent (plot);
403 const auto yExtent = CalcYExtent (plot);
404 if (xExtent != XExtent_ || yExtent != YExtent_)
408 emit extentsChanged ();
413 void PlotItem::SetNewValue (T val, T& ourVal,
const std::function<
void ()>& notifier)
423 int PlotItem::CalcXExtent (QwtPlot& plot)
const
426 if (LeftAxisEnabled_)
427 result += plot.axisScaleDraw (QwtPlot::yLeft)->
428 extent (plot.axisFont (QwtPlot::yLeft));
432 int PlotItem::CalcYExtent (QwtPlot& plot)
const
435 if (BottomAxisEnabled_)
436 result += plot.axisScaleDraw (QwtPlot::xBottom)->
437 extent (plot.axisFont (QwtPlot::xBottom));
438 if (!PlotTitle_.isEmpty ())
439 result += plot.titleLabel ()->sizeHint ().height ();