BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
DistributionPlot Class Reference

Description

The DistributionPlot class plots 1d functions corresponding to domain's Distribution1D.

Definition at line 30 of file DistributionPlot.h.

Inheritance diagram for DistributionPlot:
[legend]
Collaboration diagram for DistributionPlot:
[legend]

Public Slots

void onMouseMove (QMouseEvent *event)
 Generates label with current mouse position. More...
 
void onMousePress (QMouseEvent *event)
 

Public Member Functions

 DistributionPlot (QWidget *parent=nullptr)
 
void plotItem ()
 
void setItem (DistributionItem *item)
 
void setShowMouseCoords (bool b)
 
void setXAxisName (const QString &xAxisName)
 

Private Slots

void resetView ()
 Reset zoom range to initial state. More...
 

Private Member Functions

void init_plot ()
 Clears all plottables, resets axes to initial state. More...
 
void plot_distributions ()
 
void plot_multiple_values ()
 
void plot_single_value ()
 Plots a single bar corresponding to the value in DistributionNoteItem. More...
 
void plotBars (const QVector< double > &xbars, const QVector< double > &ybars)
 
void plotFunction (const QVector< double > &xFunc, const QVector< double > &yFunc)
 
void plotLimits (const RealLimits &limits)
 Plots red line denoting lower and upper limits, if any. More...
 
void plotVerticalLine (double xMin, double yMin, double xMax, double yMax, const QColor &color=Qt::blue)
 
QPoint positionForCautionSign ()
 
void setPlotRange (const QPair< double, double > &xRange, const QPair< double, double > &yRange)
 

Private Attributes

CautionSignm_cautionSign
 
DistributionItemm_item
 
QLabel * m_label
 
QCustomPlot * m_plot
 
QAction * m_resetAction
 
QCPRange m_xRange
 
QCPRange m_yRange
 

Constructor & Destructor Documentation

◆ DistributionPlot()

DistributionPlot::DistributionPlot ( QWidget *  parent = nullptr)

Definition at line 39 of file DistributionPlot.cpp.

40  : QWidget(parent)
41  , m_plot(new QCustomPlot)
42  , m_item(nullptr)
43  , m_label(new QLabel)
44  , m_resetAction(new QAction(this))
45  , m_cautionSign(new CautionSign(this))
46 {
47  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
48 
49  m_resetAction->setText("Reset View");
50  connect(m_resetAction, &QAction::triggered, this, &DistributionPlot::resetView);
51 
52  m_label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
53  m_label->setStyleSheet("background-color:white;");
54  m_label->setMargin(3);
55 
56  auto* mainLayout = new QVBoxLayout;
57  mainLayout->setMargin(0);
58  mainLayout->setSpacing(0);
59  mainLayout->addWidget(m_plot, 1);
60  mainLayout->addWidget(m_label);
61  m_plot->setAttribute(Qt::WA_NoMousePropagation, false);
62  setLayout(mainLayout);
63 
64  setStyleSheet("background-color:white;");
65  connect(m_plot, &QCustomPlot::mousePress, this, &DistributionPlot::onMousePress);
66  connect(m_plot, &QCustomPlot::mouseMove, this, &DistributionPlot::onMouseMove);
67 }
The CautionSign controls appearance of CautionSignWidget on top of parent widget.
Definition: CautionSign.h:25
CautionSign * m_cautionSign
QAction * m_resetAction
void onMouseMove(QMouseEvent *event)
Generates label with current mouse position.
DistributionItem * m_item
void onMousePress(QMouseEvent *event)
void resetView()
Reset zoom range to initial state.
QCustomPlot * m_plot

References m_label, m_plot, m_resetAction, onMouseMove(), onMousePress(), and resetView().

Here is the call graph for this function:

Member Function Documentation

◆ init_plot()

void DistributionPlot::init_plot ( )
private

Clears all plottables, resets axes to initial state.

Definition at line 130 of file DistributionPlot.cpp.

131 {
132  m_cautionSign->clear();
133 
134  m_plot->clearGraphs();
135  m_plot->clearItems();
136  m_plot->clearPlottables();
137  m_plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes
138  | QCP::iSelectLegend | QCP::iSelectPlottables);
139  m_plot->yAxis->setLabel("probability");
140  m_plot->xAxis2->setVisible(true);
141  m_plot->yAxis2->setVisible(true);
142  m_plot->xAxis2->setTickLabels(false);
143  m_plot->yAxis2->setTickLabels(false);
144  m_plot->xAxis2->setTicks(false);
145  m_plot->yAxis2->setTicks(false);
146 
147  setPlotRange(default_xrange, default_yrange);
148 }
void clear()
Clears caution message;.
Definition: CautionSign.cpp:42
void setPlotRange(const QPair< double, double > &xRange, const QPair< double, double > &yRange)

References CautionSign::clear(), m_cautionSign, m_plot, and setPlotRange().

Referenced by plotItem().

Here is the call graph for this function:

◆ onMouseMove

void DistributionPlot::onMouseMove ( QMouseEvent *  event)
slot

Generates label with current mouse position.

Definition at line 97 of file DistributionPlot.cpp.

98 {
99  QPoint point = event->pos();
100  double xPos = m_plot->xAxis->pixelToCoord(point.x());
101  double yPos = m_plot->yAxis->pixelToCoord(point.y());
102 
103  if (m_plot->xAxis->range().contains(xPos) && m_plot->yAxis->range().contains(yPos)) {
104  QString text = QString("[x:%1, y:%2]").arg(xPos).arg(yPos);
105  m_label->setText(text);
106  }
107 }

References m_label, and m_plot.

Referenced by DistributionPlot().

◆ onMousePress

void DistributionPlot::onMousePress ( QMouseEvent *  event)
slot

Definition at line 109 of file DistributionPlot.cpp.

110 {
111  if (event->button() == Qt::RightButton) {
112  QPoint point = event->globalPos();
113  QMenu menu;
114  menu.addAction(m_resetAction);
115  menu.exec(point);
116  }
117 }

References m_resetAction.

Referenced by DistributionPlot().

◆ plot_distributions()

void DistributionPlot::plot_distributions ( )
private

Definition at line 150 of file DistributionPlot.cpp.

151 {
154 
155  else
157 }
void plot_single_value()
Plots a single bar corresponding to the value in DistributionNoteItem.

References DistributionItem::is(), m_item, plot_multiple_values(), and plot_single_value().

Referenced by plotItem().

Here is the call graph for this function:

◆ plot_multiple_values()

void DistributionPlot::plot_multiple_values ( )
private

Definition at line 174 of file DistributionPlot.cpp.

175 {
176  ASSERT(!m_item->is<DistributionNoneItem>());
177 
178  size_t numberOfSamples = m_item->numberOfSamples();
179  double sigmafactor(0.0);
180  if (m_item->hasSigmaFactor())
181  sigmafactor = m_item->sigmaFactor();
182 
184 
185  auto dist = m_item->createDistribution();
186 
187  // Calculating bars
188  std::vector<double> xp =
189  dist->equidistantPoints(numberOfSamples, sigmafactor, m_item->limits());
190  std::vector<double> yp(xp.size());
191  std::transform(xp.begin(), xp.end(), yp.begin(),
192  [&](double value) { return dist->probabilityDensity(value); });
193  double sumOfWeights = std::accumulate(yp.begin(), yp.end(), 0.0);
194  ASSERT(sumOfWeights != 0.0);
195 
196 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
197  QVector<double> xBar(xp.begin(), xp.end());
198 #else
199  QVector<double> xBar = QVector<double>::fromStdVector(xp);
200 #endif
201 
202  QVector<double> yBar(xBar.size());
203  std::transform(yp.begin(), yp.end(), yBar.begin(),
204  [&](double value) { return value / sumOfWeights; });
205 
206  plotBars(xBar, yBar);
207 
208  // calculating function points (for interval, bigger than bars)
209  auto xRange = xRangeForValues(xBar);
210  const int number_of_points = 400;
211  std::vector<double> xf =
212  dist->equidistantPointsInRange(number_of_points, xRange.first, xRange.second);
213  std::vector<double> yf(xf.size());
214  std::transform(xf.begin(), xf.end(), yf.begin(),
215  [&](double value) { return dist->probabilityDensity(value); });
216 
217 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
218  QVector<double> xFunc(xf.begin(), xf.end());
219 #else
220  QVector<double> xFunc = QVector<double>::fromStdVector(xf);
221 #endif
222  QVector<double> yFunc(xFunc.size());
223  std::transform(yf.begin(), yf.end(), yFunc.begin(),
224  [&](double value) { return value / sumOfWeights; });
225 
226  plotFunction(xFunc, yFunc);
227 }
virtual std::unique_ptr< IDistribution1D > createDistribution(double scale=1.0) const =0
RealLimits limits() const
bool hasSigmaFactor() const
void plotFunction(const QVector< double > &xFunc, const QVector< double > &yFunc)
void plotLimits(const RealLimits &limits)
Plots red line denoting lower and upper limits, if any.
void plotBars(const QVector< double > &xbars, const QVector< double > &ybars)

References DistributionItem::createDistribution(), DistributionItem::hasSigmaFactor(), DistributionItem::is(), DistributionItem::limits(), m_item, plotBars(), plotFunction(), and plotLimits().

Referenced by plot_distributions().

Here is the call graph for this function:

◆ plot_single_value()

void DistributionPlot::plot_single_value ( )
private

Plots a single bar corresponding to the value in DistributionNoteItem.

Definition at line 161 of file DistributionPlot.cpp.

162 {
163  ASSERT(m_item->is<DistributionNoneItem>());
164 
165  double value = dynamic_cast<SymmetricResolutionItem*>(m_item)->mean();
166 
167  QVector<double> xPos = QVector<double>() << value;
168  QVector<double> yPos = QVector<double>() << 1.0;
169  plotBars(xPos, yPos);
170 
171  plotVerticalLine(value, default_yrange.first, value, default_yrange.second);
172 }
void plotVerticalLine(double xMin, double yMin, double xMax, double yMax, const QColor &color=Qt::blue)

References DistributionItem::is(), m_item, plotBars(), and plotVerticalLine().

Referenced by plot_distributions().

Here is the call graph for this function:

◆ plotBars()

void DistributionPlot::plotBars ( const QVector< double > &  xbars,
const QVector< double > &  ybars 
)
private

Definition at line 238 of file DistributionPlot.cpp.

239 {
240  ASSERT(!xbars.empty());
241 
242  auto xRange = xRangeForValues(xbars);
243  auto yRange = yRangeForValues(ybars);
244  setPlotRange(xRange, yRange);
245 
246  double barWidth(0.0);
247  if (xbars.size() == 1)
248  barWidth = optimalBarWidth(xRange.first, xRange.second, xbars.size());
249  else
250  barWidth = optimalBarWidth(xbars.front(), xbars.back(), xbars.size());
251 
252  auto* bars = new QCPBars(m_plot->xAxis, m_plot->yAxis);
253 
254  bars->setWidth(barWidth);
255  bars->setData(xbars, ybars);
256 }

References m_plot, and setPlotRange().

Referenced by plot_multiple_values(), and plot_single_value().

Here is the call graph for this function:

◆ plotFunction()

void DistributionPlot::plotFunction ( const QVector< double > &  xFunc,
const QVector< double > &  yFunc 
)
private

Definition at line 258 of file DistributionPlot.cpp.

259 {
260  auto xRange = xRangeForValues(xFunc);
261  auto yRange = yRangeForValues(yFunc);
262  setPlotRange(xRange, yRange);
263 
264  m_plot->addGraph();
265  m_plot->graph(0)->setData(xFunc, yFunc);
266 }

References m_plot, and setPlotRange().

Referenced by plot_multiple_values().

Here is the call graph for this function:

◆ plotItem()

void DistributionPlot::plotItem ( )

Definition at line 79 of file DistributionPlot.cpp.

80 {
81  init_plot();
82 
83  try {
85  } catch (const std::exception& ex) {
86  init_plot();
87 
88  QString message = QString("Wrong parameters\n\n").append(QString::fromStdString(ex.what()));
90  }
91 
92  m_plot->replot();
93 }
void setCautionMessage(const QString &cautionMessage)
Shows caution sign on the screen. If clear of previous caution sign had happened just few msec ago,...
Definition: CautionSign.cpp:60
void init_plot()
Clears all plottables, resets axes to initial state.

References init_plot(), m_cautionSign, m_plot, plot_distributions(), and CautionSign::setCautionMessage().

Referenced by setItem(), DistributionEditor::updatePlot(), and InclinationAnglesEditor::updatePlot().

Here is the call graph for this function:

◆ plotLimits()

void DistributionPlot::plotLimits ( const RealLimits &  limits)
private

Plots red line denoting lower and upper limits, if any.

Definition at line 283 of file DistributionPlot.cpp.

284 {
285  if (limits.hasLowerLimit()) {
286  double value = limits.lowerLimit();
287  plotVerticalLine(value, default_yrange.first, value, default_yrange.second, Qt::red);
288  }
289 
290  if (limits.hasUpperLimit()) {
291  double value = limits.upperLimit();
292  plotVerticalLine(value, default_yrange.first, value, default_yrange.second, Qt::red);
293  }
294 }

References plotVerticalLine().

Referenced by plot_multiple_values().

Here is the call graph for this function:

◆ plotVerticalLine()

void DistributionPlot::plotVerticalLine ( double  xMin,
double  yMin,
double  xMax,
double  yMax,
const QColor &  color = Qt::blue 
)
private

Definition at line 268 of file DistributionPlot.cpp.

270 {
271  auto* line = new QCPItemLine(m_plot);
272 
273  QPen pen(color, 1, Qt::DashLine);
274  line->setPen(pen);
275  line->setSelectable(true);
276 
277  line->start->setCoords(xMin, yMin);
278  line->end->setCoords(xMax, yMax);
279 }

References m_plot.

Referenced by plot_single_value(), and plotLimits().

◆ positionForCautionSign()

QPoint DistributionPlot::positionForCautionSign ( )
private

◆ resetView

void DistributionPlot::resetView ( )
privateslot

Reset zoom range to initial state.

Definition at line 121 of file DistributionPlot.cpp.

122 {
123  m_plot->xAxis->setRange(m_xRange);
124  m_plot->yAxis->setRange(m_yRange);
125  m_plot->replot();
126 }

References m_plot, m_xRange, and m_yRange.

Referenced by DistributionPlot().

◆ setItem()

void DistributionPlot::setItem ( DistributionItem item)

Definition at line 69 of file DistributionPlot.cpp.

70 {
71  ASSERT(item);
72  if (m_item == item)
73  return;
74 
75  m_item = item;
76  plotItem();
77 }

References m_item, and plotItem().

Referenced by DistributionEditor::updatePlot(), and InclinationAnglesEditor::updatePlot().

Here is the call graph for this function:

◆ setPlotRange()

void DistributionPlot::setPlotRange ( const QPair< double, double > &  xRange,
const QPair< double, double > &  yRange 
)
private

Definition at line 229 of file DistributionPlot.cpp.

231 {
232  m_xRange = QCPRange(xRange.first, xRange.second);
233  m_yRange = QCPRange(yRange.first, yRange.second);
234  m_plot->xAxis->setRange(m_xRange);
235  m_plot->yAxis->setRange(m_yRange);
236 }

References m_plot, m_xRange, and m_yRange.

Referenced by init_plot(), plotBars(), and plotFunction().

◆ setShowMouseCoords()

void DistributionPlot::setShowMouseCoords ( bool  b)

Definition at line 301 of file DistributionPlot.cpp.

302 {
303  m_label->setVisible(b);
304 }

References m_label.

Referenced by DistributionEditor::DistributionEditor(), and InclinationAnglesEditor::InclinationAnglesEditor().

◆ setXAxisName()

void DistributionPlot::setXAxisName ( const QString &  xAxisName)

Definition at line 296 of file DistributionPlot.cpp.

297 {
298  m_plot->xAxis->setLabel(xAxisName);
299 }

References m_plot.

Member Data Documentation

◆ m_cautionSign

CautionSign* DistributionPlot::m_cautionSign
private

Definition at line 66 of file DistributionPlot.h.

Referenced by init_plot(), and plotItem().

◆ m_item

DistributionItem* DistributionPlot::m_item
private

◆ m_label

QLabel* DistributionPlot::m_label
private

Definition at line 63 of file DistributionPlot.h.

Referenced by DistributionPlot(), onMouseMove(), and setShowMouseCoords().

◆ m_plot

QCustomPlot* DistributionPlot::m_plot
private

◆ m_resetAction

QAction* DistributionPlot::m_resetAction
private

Definition at line 64 of file DistributionPlot.h.

Referenced by DistributionPlot(), and onMousePress().

◆ m_xRange

QCPRange DistributionPlot::m_xRange
private

Definition at line 65 of file DistributionPlot.h.

Referenced by resetView(), and setPlotRange().

◆ m_yRange

QCPRange DistributionPlot::m_yRange
private

Definition at line 65 of file DistributionPlot.h.

Referenced by resetView(), and setPlotRange().


The documentation for this class was generated from the following files: