BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
RangeUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/PlotUtil/RangeUtils.cpp
6 //! @brief Implements RangeUtils namespace
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include "GUI/Util/Error.h"
19 
20 using gradient_map_t = QMap<QString, QCPColorGradient::GradientPreset>;
21 
22 namespace {
23 
24 gradient_map_t createGradientMap()
25 {
26  gradient_map_t result;
27 
28  result["Grayscale"] = QCPColorGradient::gpGrayscale;
29  result["Hot"] = QCPColorGradient::gpHot;
30  result["Cold"] = QCPColorGradient::gpCold;
31  result["Night"] = QCPColorGradient::gpNight;
32  result["Candy"] = QCPColorGradient::gpCandy;
33  result["Geography"] = QCPColorGradient::gpGeography;
34  result["Ion"] = QCPColorGradient::gpIon;
35  result["Thermal"] = QCPColorGradient::gpThermal;
36  result["Polar"] = QCPColorGradient::gpPolar;
37  result["Spectrum"] = QCPColorGradient::gpSpectrum;
38  result["Jet"] = QCPColorGradient::gpJet;
39  result["Hues"] = QCPColorGradient::gpHues;
40 
41  return result;
42 }
43 
44 // Converts xmin (low edge of first bin) and xmax (upper edge of last bin) to the
45 // range expected by QCPColorMapData::setRange.
46 QCPRange qcpRange(double xmin, double xmax, int nbins)
47 {
48  double dx = (xmax - xmin) / nbins;
49  return QCPRange(xmin + dx / 2., xmax - dx / 2.);
50 }
51 
52 QMargins defaultMargins(const QWidget& widget)
53 {
54  auto base_size = GUI::Util::Style::SizeOfLetterM(&widget);
55  int left = static_cast<int>(base_size.width() * 6.0);
56  int top = static_cast<int>(base_size.height() * 1.5);
57  int right = static_cast<int>(base_size.width() * 1.2);
58  int bottom = static_cast<int>(base_size.height() * 4.5);
59  return QMargins(left, top, right, bottom);
60 }
61 
62 } // namespace
63 
64 QCPColorGradient GUI::View::RangeUtils::getGradient(const QString& gradientName)
65 {
66  static gradient_map_t gradient_map = createGradientMap();
67 
68  auto it = gradient_map.find(gradientName);
69  if (it == gradient_map.end())
70  throw Error("ColorMapHelper::getGradient() -> Error. No such gradient" + gradientName);
71  return QCPColorGradient(it.value());
72 }
73 
75 {
76  return getGradient(item->getGradientValue());
77 }
78 
80 {
81  return qcpRange(item->xMin(), item->xMax(), item->xSize());
82 }
83 
85 {
86  return QCPRange(item->getLowerX(), item->getUpperX());
87 }
88 
90 {
91  return qcpRange(item->yMin(), item->yMax(), item->ySize());
92 }
93 
95 {
96  return QCPRange(item->getLowerY(), item->getUpperY());
97 }
98 
100 {
101  QPair<double, double> range = item->dataRange();
102  return QCPRange(range.first, range.second);
103 }
104 
106 {
107  return QCPRange(item->getLowerZ(), item->getUpperZ());
108 }
109 
110 void GUI::View::RangeUtils::setLogz(QCPColorScale* scale, bool isLogz)
111 {
112  if (isLogz && scale->dataScaleType() != QCPAxis::stLogarithmic)
113  scale->setDataScaleType(QCPAxis::stLogarithmic);
114 
115  else if (!isLogz && scale->dataScaleType() != QCPAxis::stLinear)
116  scale->setDataScaleType(QCPAxis::stLinear);
117 
118  setLogz(scale->axis(), isLogz);
119 }
120 
121 void GUI::View::RangeUtils::setLogz(QCPAxis* axis, bool isLogz)
122 {
123  if (isLogz) {
124  axis->setNumberFormat("eb");
125  axis->setNumberPrecision(0);
126  axis->setScaleType(QCPAxis::stLogarithmic);
127  QSharedPointer<QCPAxisTicker> ticker(new QCPAxisTickerLog);
128  axis->setTicker(ticker);
129  } else {
130  axis->setNumberFormat("f");
131  axis->setNumberPrecision(0);
132  axis->setScaleType(QCPAxis::stLinear);
133  QSharedPointer<QCPAxisTicker> ticker(new QCPAxisTicker);
134  axis->setTicker(ticker);
135  }
136 }
137 
138 void GUI::View::RangeUtils::setDefaultMargins(QCustomPlot* customPlot)
139 {
140  auto* axisRectangle = customPlot->axisRect();
141  axisRectangle->setAutoMargins(QCP::msTop | QCP::msBottom);
142  axisRectangle->setMargins(defaultMargins(*customPlot));
143 }
Defines error class.
Defines class IntensityDataItem.
QMap< QString, QCPColorGradient::GradientPreset > gradient_map_t
Definition: RangeUtils.cpp:20
Defines RangeUtils namespace.
Defines GUI::StyleUtils namespace.
Definition: Error.h:21
double getLowerZ() const
Returns lower and upper zoom ranges of z-axis.
QPair< double, double > dataRange() const
Init zmin, zmax to match the intensity values range.
double xMin() const
Returns min and max range of x-axis as given by IntensityData.
double getLowerY() const
Returns lower and upper zoom ranges of y-axis.
QString getGradientValue() const
double getUpperY() const
double getUpperX() const
double getLowerX() const
Returns lower and upper zoom ranges of x-axis.
double yMin() const
Returns min and max range of y-axis as given by IntensityData.
double getUpperZ() const
QSize SizeOfLetterM(const QWidget *widget=nullptr)
Returns size of largest letter of default system font.
Definition: StyleUtils.cpp:113
QCPRange itemYrange(const IntensityDataItem *item)
Returns y-axis range.
Definition: RangeUtils.cpp:89
QCPColorGradient itemGradient(const IntensityDataItem *item)
Definition: RangeUtils.cpp:74
QCPRange itemZoomY(const IntensityDataItem *item)
Returns y-axis vizible range (zoom).
Definition: RangeUtils.cpp:94
void setLogz(QCPColorScale *scale, bool isLogz)
Definition: RangeUtils.cpp:110
QCPColorGradient getGradient(const QString &gradientName)
Definition: RangeUtils.cpp:64
void setDefaultMargins(QCustomPlot *customPlot)
Sets default margins for axes rectangle plot.
Definition: RangeUtils.cpp:138
QCPRange itemXrange(const IntensityDataItem *item)
Returns x-axis range.
Definition: RangeUtils.cpp:79
QCPRange itemDataRange(const IntensityDataItem *item)
Returns z-axis data range.
Definition: RangeUtils.cpp:99
QCPRange itemDataZoom(const IntensityDataItem *item)
Returns z-axis visible range (zoom).
Definition: RangeUtils.cpp:105
QCPRange itemZoomX(const IntensityDataItem *item)
Returns x-axis vizible range (zoom).
Definition: RangeUtils.cpp:84