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)
333 if (!
color.isValid ())
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 #if QWT_VERSION >= 0x060100 362 grid->setMajorPen (QPen (GridLinesColor_, 1, Qt::SolidLine));
363 grid->setMinorPen (QPen (GridLinesColor_, 1, Qt::DashLine));
365 grid->setMajPen (QPen (GridLinesColor_, 1, Qt::SolidLine));
366 grid->setMinPen (QPen (GridLinesColor_, 1, Qt::DashLine));
368 grid->attach (&plot);
371 auto items = Multipoints_;
372 if (items.isEmpty ())
373 items.push_back ({ Color_, {}, Points_ });
375 if (MinXValue_ < MaxXValue_)
376 plot.setAxisScale (QwtPlot::xBottom, MinXValue_, MaxXValue_);
377 else if (
const auto ptsCount = items.first ().Points_.size ())
378 plot.setAxisScale (QwtPlot::xBottom, 0, ptsCount - 1);
380 std::vector<std::unique_ptr<QwtPlotCurve>> curves;
381 for (
const auto& item : items)
383 curves.emplace_back (
new QwtPlotCurve);
384 const auto curve = curves.back ().get ();
386 curve->setPen (QPen (item.Color_));
388 if (item.BrushColor_)
389 curve->setBrush (*item.BrushColor_);
392 auto brushColor = item.Color_;
393 brushColor.setAlphaF (Alpha_);
394 curve->setBrush (brushColor);
397 curve->setRenderHint (QwtPlotItem::RenderAntialiased);
398 curve->attach (&plot);
400 curve->setSamples (item.Points_.toVector ());
405 QwtPlotRenderer {}.render (&plot, painter, rect);
407 const auto xExtent = CalcXExtent (plot);
408 const auto yExtent = CalcYExtent (plot);
409 if (xExtent != XExtent_ || yExtent != YExtent_)
413 emit extentsChanged ();
418 void PlotItem::SetNewValue (T val, T& ourVal,
const std::function<
void ()>& notifier)
428 int PlotItem::CalcXExtent (QwtPlot& plot)
const 431 if (LeftAxisEnabled_)
432 result += plot.axisScaleDraw (QwtPlot::yLeft)->
433 extent (plot.axisFont (QwtPlot::yLeft));
437 int PlotItem::CalcYExtent (QwtPlot& plot)
const 440 if (BottomAxisEnabled_)
441 result += plot.axisScaleDraw (QwtPlot::xBottom)->
442 extent (plot.axisFont (QwtPlot::xBottom));
443 if (!PlotTitle_.isEmpty ())
444 result += plot.titleLabel ()->sizeHint ().height ();
void SetMinXValue(double)
double GetMinYValue() const
void leftAxisTitleChanged()
QList< QPointF > GetPoints() const
void gridLinesColorChanged()
QString GetPlotTitle() const
void SetGridLinesColor(const QColor &)
bool GetBottomAxisEnabled() const
void leftAxisEnabledChanged()
QVariant GetMultipoints() const
void SetBottomAxisTitle(const QString &)
QString GetBottomAxisTitle() const
bool GetLeftAxisEnabled() const
void SetLeftAxisTitle(const QString &)
double GetMinXValue() const
void bottomAxisEnabledChanged()
void SetPoints(const QList< QPointF > &)
QColor GetTextColor() const
void SetYMinorGridEnabled(bool)
void paint(QPainter *) override
void SetMultipoints(const QVariant &)
void SetMaxYValue(double)
void SetMaxXValue(double)
QColor GetGridLinesColor() const
void SetLeftAxisEnabled(bool)
double GetMaxXValue() const
QColor GetBackground() const
void SetYGridEnabled(bool)
Q_DECLARE_METATYPE(QVariantList *)
void SetColor(const QColor &)
void SetBackground(const QColor &)
double GetMaxYValue() const
void SetPlotTitle(const QString &)
bool GetYGridEnabled() const
void SetMinYValue(double)
bool GetYMinorGridEnabled() const
void bottomAxisTitleChanged()
QString GetLeftAxisTitle() const
void SetBottomAxisEnabled(bool)
void SetTextColor(const QColor &)