BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
IntensityDataCanvas.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Plot2D/IntensityDataCanvas.cpp
6 //! @brief Implements class IntensityDataCanvas
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 
20 #include "GUI/Util/ComboProperty.h"
26 #include <QAction>
27 #include <QMouseEvent>
28 #include <QSettings>
29 #include <QVBoxLayout>
30 
31 namespace {
32 
33 const QString SettingsGroup = "IntensityDataCanvas/";
34 
35 const QString SettingsKeyGradient = SettingsGroup + "Gradient";
36 
37 const QString SettingsKeyInterpolation = SettingsGroup + "Interpolation";
38 
39 } // namespace
40 
42  : SessionItemWidget(parent)
43  , m_colorMap(new ColorMapCanvas)
44  , m_resetViewAction(nullptr)
45  , m_rotateDataAction(nullptr)
46  , m_savePlotAction(nullptr)
47 {
48  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
49 
50  auto* layout = new QVBoxLayout;
51  layout->setMargin(0);
52  layout->setSpacing(0);
53  layout->addWidget(m_colorMap);
54  setLayout(layout);
55 
57 
58  initActions();
59 
60  connect(m_colorMap->customPlot(), &QCustomPlot::mousePress, this,
61  &IntensityDataCanvas::onMousePress, Qt::UniqueConnection);
62 }
63 
65 {
66  SessionItemWidget::setItem(intensityItem);
68 
70  if (!m_rotateDataAction)
71  initRotation();
72 }
73 
75 {
76  return QSize(500, 400);
77 }
78 
80 {
81  return QSize(128, 128);
82 }
83 
85 {
88  else
90 }
91 
93 {
95 }
96 
98 {
99  QString dirname = gSessionData->projectDocument.value()->userExportDir();
100  GUI::Plot::savePlot(dirname, m_colorMap->customPlot(), intensityDataItem()->getDatafield());
101 }
102 
103 void IntensityDataCanvas::onMousePress(QMouseEvent* event)
104 {
105  if (event->button() == Qt::RightButton)
106  emit customContextMenuRequested(event->globalPos());
107 }
108 
110 {
112  [this](const QString& name) { onPropertyChanged(name); }, this);
113 }
114 
116 {
117  auto* realDataItem = dynamic_cast<RealDataItem*>(intensityDataItem()->parentItem());
118  if (!realDataItem || !realDataItem->isIntensityData())
119  // should never happen because of action disabling => no
120  // dialog necessary
121  return;
122 
123  if (realDataItem->rotationAffectsSetup()) {
124  const QString title("Rotate data");
125  const QString message("Rotation will break the link between the data and the instrument. "
126  "Detector masks or projections, if they exist, will be removed.");
128  "Do you wish to rotate the data?", "Yes, please rotate",
129  "No, cancel data rotation"))
130  return;
131  }
132 
133  QApplication::setOverrideCursor(Qt::WaitCursor);
134  realDataItem->rotateData();
135  QApplication::restoreOverrideCursor();
136 }
137 
139 {
140  auto* result = dynamic_cast<IntensityDataItem*>(currentItem());
141  ASSERT(result);
142  return result;
143 }
144 
146 {
147  m_resetViewAction = new QAction(this);
148  m_resetViewAction->setText("Center view");
149  m_resetViewAction->setIcon(QIcon(":/images/camera-metering-center.svg"));
150  m_resetViewAction->setToolTip("Reset view\n"
151  "x,y,z axes range will be set to default");
152  connect(m_resetViewAction, &QAction::triggered, this, &IntensityDataCanvas::onResetViewAction);
153 
154  m_savePlotAction = new QAction(this);
155  m_savePlotAction->setText("Save");
156  m_savePlotAction->setIcon(QIcon(":/images/content-save-outline.svg"));
157  m_savePlotAction->setToolTip("Save plot");
158  connect(m_savePlotAction, &QAction::triggered, this, &IntensityDataCanvas::onSavePlotAction);
159 }
160 
162 {
163  if (dynamic_cast<RealDataItem*>(intensityDataItem()->parentItem())) {
164  m_rotateDataAction = new QAction(this);
165  m_rotateDataAction->setText("Rotate");
166  m_rotateDataAction->setIcon(QIcon(":/images/rotate-left.svg"));
167  m_rotateDataAction->setIconText("Rotate");
168  m_rotateDataAction->setToolTip("Rotate intensity data by 90 deg counterclockwise");
169  connect(m_rotateDataAction, &QAction::triggered, this, &IntensityDataCanvas::rotateData);
170  }
171 }
172 
173 //! Reads gradient/ interpolation settings from IntensityDataItem and writes to persistant
174 //! project settings.
175 
177 {
179  QSettings settings;
180  settings.setValue(SettingsKeyGradient, intensityDataItem()->getGradientValue());
182  QSettings settings;
183  settings.setValue(SettingsKeyInterpolation, intensityDataItem()->isInterpolated());
184  }
185 }
186 
187 //! Apply persistent settings (gradient, interpolation) to IntensityDataItem.
188 
190 {
191  QSettings settings;
192 
193  if (settings.contains(SettingsKeyGradient)) {
195  QString persistentGradient = settings.value(SettingsKeyGradient).toString();
196  if (combo.getValue() != persistentGradient) {
197  combo.setValue(persistentGradient);
198  intensityDataItem()->setGradient(combo);
199  }
200  }
201 
202  if (settings.contains(SettingsKeyInterpolation)) {
203  bool value = settings.value(SettingsKeyInterpolation).toBool();
205  }
206 }
Defines class ColorMapCanvas.
Defines class ColorMap.
Defines class ComboProperty.
Defines global pointers.
Defines class IntensityDataCanvas.
Defines class IntensityDataItem.
Defines class GUIHelpers functions.
Defines class ProjectDocument.
Defines class RealDataItem.
Defines class SavePlotAssistant.
SessionData * gSessionData
global pointer to the single instance
Definition: SessionData.cpp:17
Defines struct SessionData.
The ColorMapCanvas class contains ColorMap for intensity data presentation, and provide control of fo...
void setItem(SessionItem *intensityDataItem) override
void setStatusLabelEnabled(bool flag)
QCustomPlot * customPlot()
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
void setValue(const QString &name)
QString getValue() const
QSize minimumSizeHint() const override
void onMousePress(QMouseEvent *event)
IntensityDataCanvas(QWidget *parent=nullptr)
QSize sizeHint() const override
QList< QAction * > actionList() override
void setItem(SessionItem *intensityItem) override
void onPropertyChanged(const QString &name)
Reads gradient/ interpolation settings from IntensityDataItem and writes to persistant project settin...
void applyPersistentSettings()
Apply persistent settings (gradient, interpolation) to IntensityDataItem.
IntensityDataItem * intensityDataItem()
ColorMapCanvas * m_colorMap
void subscribeToItem() override
void setGradient(const ComboProperty &gradient)
void resetView()
Set axes viewport to original data.
void setInterpolated(bool interp)
ComboProperty getGradient() const
static bool isInterpolatedPropertyName(const QString &name)
static bool isGradientPropertyName(const QString &name)
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:39
Provides access to experimental data, for display and fitting. Owns an AbstractDataLoader.
Definition: RealDataItem.h:33
The SessionItemWidget class is a base for all widgets representing the content of SessionItem....
SessionItem * currentItem()
virtual void setItem(SessionItem *item)
Base class for a GUI data item.
Definition: SessionItem.h:204
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
SessionItem * parentItem() const
Returns parent of this item.
Definition: SessionItem.cpp:67
static QMainWindow * mainWindow
Definition: Globals.h:22
void savePlot(const QString &dirname, QCustomPlot *plot, Datafield *output_data)
QString const & name(EShape k)
Definition: particles.cpp:20
bool question(QWidget *parent, const QString &title, const QString &text, const QString &detailedText, const QString &yesText, const QString &noText)
Definition: MessageBox.cpp:52
std::optional< ProjectDocument * > projectDocument
Definition: SessionData.h:27