BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
viewportaxisplotcontroller.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/view/mvvm/plotting/viewportaxisplotcontroller.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
20 #include "qcustomplot.h"
21 #include <QObject>
22 #include <stdexcept>
23 
24 using namespace ModelView;
25 
27 
29  QCPAxis* m_axis{nullptr};
30  bool m_blockUpdate{false};
31  std::unique_ptr<QMetaObject::Connection> m_axisConn;
32  std::unique_ptr<AxisTitleController> m_titleController;
33 
35  : m_self(controller), m_axis(axis)
36  {
37  if (!axis)
38  throw std::runtime_error("AxisPlotController: axis is not initialized.");
39  m_axisConn = std::make_unique<QMetaObject::Connection>();
40  }
41 
42  //! Connects QCustomPlot signals with controller methods.
43  void setConnected()
44  {
45  auto on_axis_range = [this](const QCPRange& newRange) {
46  m_blockUpdate = true;
47  auto item = m_self->currentItem();
48  item->set_range(newRange.lower, newRange.upper);
49  m_blockUpdate = false;
50  };
51 
52  *m_axisConn = QObject::connect(
53  m_axis, static_cast<void (QCPAxis::*)(const QCPRange&)>(&QCPAxis::rangeChanged),
54  on_axis_range);
55  }
56 
57  //! Disonnects QCustomPlot signals.
58 
59  void setDisconnected() { QObject::disconnect(*m_axisConn); }
60 
61  //! Sets axesRange from SessionItem.
63  {
64  auto [lower, upper] = m_self->currentItem()->range();
65  m_axis->setRange(QCPRange(lower, upper));
66  }
67 
68  //! Sets log scale from item.
69 
71  {
73  }
74 
75  //! Init axis from item and setup connections.
76 
77  void init_axis()
78  {
79  m_titleController = std::make_unique<AxisTitleController>(m_axis);
81  m_titleController->setItem(text_item);
84  setConnected();
85  }
86 
88  {
90  m_axis->setRangeLower(item->property<double>(ViewportAxisItem::P_MIN));
91  setConnected();
92  }
93 
95  {
97  m_axis->setRangeUpper(item->property<double>(ViewportAxisItem::P_MAX));
98  setConnected();
99  }
100 
102 };
103 
105  : p_impl(std::make_unique<AxesPlotControllerImpl>(this, axis))
106 
107 {
108 }
109 
111 
113 {
114  auto on_property_change = [this](SessionItem*, std::string name) {
115  if (p_impl->m_blockUpdate)
116  return;
117 
119  p_impl->updateLowerRange(currentItem());
120 
122  p_impl->updateUpperRange(currentItem());
123 
125  p_impl->setAxisLogScaleFromItem();
126 
127  p_impl->m_axis->parentPlot()->replot();
128  };
129  setOnPropertyChange(on_property_change);
130 
131  p_impl->init_axis();
132 }
133 
135 {
136  p_impl->setDisconnected();
137 }
Defines class CLASS?
Defines class CLASS?
static const std::string P_MAX
Definition: axisitems.h:32
static const std::string P_MIN
Definition: axisitems.h:31
void setOnPropertyChange(Callbacks::item_str_t f)
Sets callback to be notified on item's property change.
SessionItem * item() const
For necessary manipulations on unsubscription.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
T property(const std::string &tag) const
Returns data stored in property item.
Definition: sessionitem.h:181
T * item(const std::string &tag) const
Returns first item under given tag casted to a specified type.
Definition: sessionitem.h:156
Represent text item on plot.
Item to represent viewport axis.
Definition: axisitems.h:43
static const std::string P_IS_LOG
Definition: axisitems.h:46
static const std::string P_TITLE
Definition: axisitems.h:45
std::pair< double, double > range() const
Returns pair of lower, upper axis range.
Definition: axisitems.cpp:44
Establishes communication between QCPAxis and ViewportAxisItem.
std::unique_ptr< AxesPlotControllerImpl > p_impl
void unsubscribe() override
For necessary manipulations on new item.
Defines class CLASS?
MVVM_VIEW_EXPORT void SetLogarithmicScale(QCPColorScale *axis, bool is_log_scale)
Switch axis to logarithmic scale mode.
materialitems.h Collection of materials to populate MaterialModel.
QString const & name(EShape k)
Definition: particles.cpp:21
Definition: filesystem.h:81
Defines class CLASS?
void setConnected()
Connects QCustomPlot signals with controller methods.
void init_axis()
Init axis from item and setup connections.
AxesPlotControllerImpl(ViewportAxisPlotController *controller, QCPAxis *axis)
Defines class CLASS?