BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
RealSpaceCanvas.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Realspace/RealSpaceCanvas.cpp
6 //! @brief Implements class RealSpaceScene
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 
23 #include "GUI/ba3d/model/model.h"
24 #include "GUI/ba3d/view/canvas.h"
25 #include <QApplication>
26 #include <QFileDialog>
27 #include <QMessageBox>
28 #include <QVBoxLayout>
29 #include <memory>
30 
32  : QWidget(parent)
33  , m_view(new GUI::RealSpace::Canvas)
34  , m_cautionSign(new CautionSign(this))
35  , m_containingMultiLayerItem(nullptr)
36  , m_firstView(true)
37 {
38  auto* layout = new QVBoxLayout;
39  layout->setMargin(0);
40  layout->setSpacing(0);
41  layout->addWidget(m_view);
42  setLayout(layout);
43 }
44 
45 void RealSpaceCanvas::setCurrentItem(MultiLayerItem* containingMultiLayerItem,
46  ItemForRealSpace item)
47 {
48  bool isValid = false;
49  std::visit([&](auto&& arg) { isValid = arg != nullptr; }, item);
50 
51  if (isValid) {
52  m_containingMultiLayerItem = containingMultiLayerItem;
53  m_currentItem = item;
54  updateScene();
55  defaultView(); // Enforces default view and also sets the zoomLevel to default i.e. 0
56  } else
57  resetScene();
58 }
59 
60 void RealSpaceCanvas::changeLayerSize(double layerSizeChangeScale)
61 {
62  // when no object is selected --> take no action
63  if (!m_currentItem)
64  return;
65 
66  m_sceneGeometry.layerSize = m_sceneGeometry.layerSize * layerSizeChangeScale;
67  updateScene();
68 }
69 
71 {
72  QPixmap pixmap(this->size());
73  render(&pixmap, QPoint(), childrenRegion());
74  savePicture(pixmap);
75 }
76 
77 void RealSpaceCanvas::showEvent(QShowEvent*)
78 {
79  updateScene();
80  if (m_firstView)
82  m_firstView = false;
83 }
84 
85 void RealSpaceCanvas::savePicture(const QPixmap& pixmap)
86 {
87  QString dirname = gSessionData->projectDocument.value()->userExportDir();
88  QString defaultExtension = ".png";
89  QString selectedFilter("*" + defaultExtension);
90  QString defaultName = dirname + "/untitled";
91  QString fileName = QFileDialog::getSaveFileName(
92  nullptr, "Save 3D real space view", defaultName, selectedFilter, nullptr,
93  appSettings->useNativeFileDialog() ? QFileDialog::Options()
94  : QFileDialog::DontUseNativeDialog);
95  QString nameToSave =
96  fileName.endsWith(defaultExtension) ? fileName : fileName + defaultExtension;
97 
98  if (!nameToSave.isEmpty()) {
99  try {
100  pixmap.save(nameToSave);
101  } catch (const std::exception& ex) {
102  QString message = "Attempt to save file with the name '";
103  message.append(nameToSave);
104  message.append("' has failed with following error message\n\n");
105  message.append(QString::fromStdString(ex.what()));
106  QMessageBox::warning(nullptr, "Houston, we have a problem.", message);
107  }
108  }
109 }
110 
112 {
113  if (!isVisible()) // to improve performance
114  return;
115 
116  if (!m_view->isValid()) // GL not initialized (widget not shown so far) -> no updating possible
117  return;
118 
119  QApplication::setOverrideCursor(Qt::WaitCursor);
120 
121  m_realSpaceModel = std::make_unique<GUI::RealSpace::Model>();
122 
123  const auto colorForMaterialName = [&](const QString& materialName) {
125  };
126 
127  RealSpaceBuilder builder3D(colorForMaterialName);
128 
129  try {
130  m_cautionSign->clear();
131  if (m_currentItem)
133  m_view->cam()->getPos());
134  } catch (const std::exception& ex) {
135  m_cautionSign->setCautionMessage(ex.what());
136  } catch (...) {
137  // ignore other exceptions thrown
138  }
140 
141  QApplication::restoreOverrideCursor();
142 }
143 
145 {
146  m_realSpaceModel.reset();
147  m_view->setModel(nullptr);
148  m_currentItem.reset();
149  m_containingMultiLayerItem = nullptr;
150 }
151 
153 {
154  m_view->defaultView();
155 }
156 
158 {
159  m_view->sideView();
160 }
161 
163 {
164  m_view->topView();
165 }
ApplicationSettings * appSettings
global pointer to the instance
Defines class ApplicationSettings.
Defines class CautionSign.
Defines class MaterialItem.
Defines class MultiLayerItem.
Defines class ProjectDocument.
Defines class RealSpaceBuilder.
Defines class RealSpaceCanvas.
SessionData * gSessionData
global pointer to the single instance
Definition: SessionData.cpp:17
Defines struct SessionData.
Defines Canvas class.
bool useNativeFileDialog() const
The CautionSign controls appearance of CautionSignWidget on top of parent widget.
Definition: CautionSign.h:25
void clear()
Clears caution message;.
Definition: CautionSign.cpp:42
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
const Position & getPos() const
Definition: camera.h:50
Camera * cam()
Definition: canvas.h:59
void setModel(Model *)
Definition: canvas.cpp:94
QColor color() const
MaterialItem * materialFromName(const QString &name) const
MaterialItems & materialItems()
void populate(RealSpace::Model *model, ItemForRealSpace item, const SceneGeometry &sceneGeometry, const RealSpace::Camera::Position &cameraPosition=RealSpace::Camera::Position(RealSpace::Vector3D(0, -200, 120), RealSpace::Vector3D(0, 0, 0), RealSpace::Vector3D::_z)) const
SceneGeometry m_sceneGeometry
void setCurrentItem(MultiLayerItem *containingMultiLayerItem, ItemForRealSpace item)
GUI::RealSpace::Canvas * m_view
std::variant< MultiLayerItem *, LayerItem *, ParticleLayoutItem *, ItemWithParticles * > ItemForRealSpace
Defines the item types which can be the "current item".
std::optional< ItemForRealSpace > m_currentItem
void showEvent(QShowEvent *) override
CautionSign * m_cautionSign
void changeLayerSize(double layerSizeChangeScale)
MultiLayerItem * m_containingMultiLayerItem
std::unique_ptr< GUI::RealSpace::Model > m_realSpaceModel
RealSpaceCanvas(QWidget *parent=nullptr)
Defines Model class.
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: MessageBox.cpp:37
Definition: def.cpp:20
std::optional< ProjectDocument * > projectDocument
Definition: SessionData.h:27