BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
RealSpaceCanvas Class Reference

Provides 3D object generation for RealSpaceWidget. More...

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

Public Slots

void onChangeLayerSizeAction (double layer_size_scale)
 
void onDefaultViewAction ()
 
void onLockViewAction (bool view_locked)
 
void onRowsAboutToBeRemoved (const QModelIndex &parent, int first, int last)
 
void onSavePictureAction ()
 
void onSelectionChanged (const QItemSelection &, const QItemSelection &)
 
void onSideViewAction ()
 
void onTopViewAction ()
 
void updateToSelection ()
 

Public Member Functions

 RealSpaceCanvas (QWidget *parent=nullptr)
 
 ~RealSpaceCanvas ()
 
void setModel (SampleModel *sampleModel=nullptr, QItemSelectionModel *selectionModel=nullptr)
 

Private Slots

void defaultView ()
 
void onDataChanged (const QModelIndex &index)
 
void resetScene ()
 
void sideView ()
 
void topView ()
 
void updateScene ()
 

Private Member Functions

void savePicture (const QPixmap &pixmap)
 
void setConnected (SampleModel *model, bool makeConnected)
 

Private Attributes

QModelIndex m_currentSelection
 
std::unique_ptr< RealSpaceModelm_realSpaceModel
 
SampleModelm_sampleModel
 
std::unique_ptr< SceneGeometrym_sceneGeometry
 
QItemSelectionModel * m_selectionModel
 
RealSpaceViewm_view
 
bool m_view_locked
 
WarningSignm_warningSign
 

Detailed Description

Provides 3D object generation for RealSpaceWidget.

Definition at line 57 of file RealSpaceCanvas.h.

Constructor & Destructor Documentation

◆ RealSpaceCanvas()

RealSpaceCanvas::RealSpaceCanvas ( QWidget *  parent = nullptr)

Definition at line 30 of file RealSpaceCanvas.cpp.

31  : QWidget(parent)
32  , m_sampleModel(nullptr)
33  , m_view(new RealSpaceView)
34  , m_selectionModel(nullptr)
35  , m_view_locked(false)
37  , m_warningSign(new WarningSign(this))
38 {
39  auto layout = new QVBoxLayout;
40  layout->setMargin(0);
41  layout->setSpacing(0);
42  layout->addWidget(m_view);
43  setLayout(layout);
44 }
QItemSelectionModel * m_selectionModel
WarningSign * m_warningSign
std::unique_ptr< SceneGeometry > m_sceneGeometry
RealSpaceView * m_view
SampleModel * m_sampleModel
Contains 3D view.
Definition: RealSpaceView.h:29
The WarningSign controls appearance of WarningSignWidget on top of parent widget.
Definition: WarningSign.h:25

References m_view.

◆ ~RealSpaceCanvas()

RealSpaceCanvas::~RealSpaceCanvas ( )
default

Member Function Documentation

◆ defaultView

void RealSpaceCanvas::defaultView ( )
privateslot

Definition at line 219 of file RealSpaceCanvas.cpp.

220 {
221  m_view->defaultView();
222 }

References RealSpaceView::defaultView(), and m_view.

Referenced by onDefaultViewAction(), and updateScene().

Here is the call graph for this function:

◆ onChangeLayerSizeAction

void RealSpaceCanvas::onChangeLayerSizeAction ( double  layer_size_scale)
slot

Definition at line 122 of file RealSpaceCanvas.cpp.

123 {
124  // when no object is selected --> take no action
125  if (m_currentSelection == QModelIndex())
126  return;
127 
128  m_sceneGeometry->set_layer_size(m_sceneGeometry->layer_size() * layerSizeChangeScale);
129  updateScene();
130 }
QModelIndex m_currentSelection

References m_currentSelection, m_sceneGeometry, and updateScene().

Referenced by RealSpaceWidget::RealSpaceWidget().

Here is the call graph for this function:

◆ onDataChanged

void RealSpaceCanvas::onDataChanged ( const QModelIndex &  index)
privateslot

Definition at line 170 of file RealSpaceCanvas.cpp.

171 {
172  auto item = m_sampleModel->itemForIndex(index);
173  if (!item)
174  return;
175 
177  updateScene();
178 }
SessionItem * itemForIndex(const QModelIndex &index) const
bool IsPositionRelated(const SessionItem &item)
Returns true if current item is related to the position on sample editor canvas.

References SessionItemUtils::IsPositionRelated(), SessionModel::itemForIndex(), m_sampleModel, and updateScene().

Referenced by setConnected().

Here is the call graph for this function:

◆ onDefaultViewAction

void RealSpaceCanvas::onDefaultViewAction ( )
slot

Definition at line 99 of file RealSpaceCanvas.cpp.

100 {
101  defaultView();
102 }

References defaultView().

Referenced by RealSpaceWidget::RealSpaceWidget().

Here is the call graph for this function:

◆ onLockViewAction

void RealSpaceCanvas::onLockViewAction ( bool  view_locked)
slot

Definition at line 114 of file RealSpaceCanvas.cpp.

115 {
116  if (m_view_locked != view_locked) {
117  m_view_locked = view_locked;
119  }
120 }

References m_view_locked, and updateToSelection().

Referenced by RealSpaceWidget::RealSpaceWidget().

Here is the call graph for this function:

◆ onRowsAboutToBeRemoved

void RealSpaceCanvas::onRowsAboutToBeRemoved ( const QModelIndex &  parent,
int  first,
int  last 
)
slot

Definition at line 139 of file RealSpaceCanvas.cpp.

140 {
141  // clear scene if current selection will be removed
142  if (m_currentSelection == m_sampleModel->index(first, 0, parent))
143  resetScene();
144 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const

References SessionModel::index(), m_currentSelection, m_sampleModel, and resetScene().

Referenced by setConnected().

Here is the call graph for this function:

◆ onSavePictureAction

void RealSpaceCanvas::onSavePictureAction ( )
slot

Definition at line 132 of file RealSpaceCanvas.cpp.

133 {
134  QPixmap pixmap(this->size());
135  render(&pixmap, QPoint(), childrenRegion());
136  savePicture(pixmap);
137 }
void savePicture(const QPixmap &pixmap)

References savePicture().

Referenced by RealSpaceWidget::RealSpaceWidget().

Here is the call graph for this function:

◆ onSelectionChanged

void RealSpaceCanvas::onSelectionChanged ( const QItemSelection &  ,
const QItemSelection &   
)
slot

Definition at line 76 of file RealSpaceCanvas.cpp.

77 {
79 }

References updateToSelection().

Referenced by setModel().

Here is the call graph for this function:

◆ onSideViewAction

void RealSpaceCanvas::onSideViewAction ( )
slot

Definition at line 104 of file RealSpaceCanvas.cpp.

105 {
106  sideView();
107 }

References sideView().

Referenced by RealSpaceWidget::RealSpaceWidget().

Here is the call graph for this function:

◆ onTopViewAction

void RealSpaceCanvas::onTopViewAction ( )
slot

Definition at line 109 of file RealSpaceCanvas.cpp.

110 {
111  topView();
112 }

References topView().

Referenced by RealSpaceWidget::RealSpaceWidget().

Here is the call graph for this function:

◆ resetScene

void RealSpaceCanvas::resetScene ( )
privateslot

Definition at line 212 of file RealSpaceCanvas.cpp.

213 {
214  m_realSpaceModel.reset();
215  m_view->setModel(nullptr);
216  m_currentSelection = {};
217 }
std::unique_ptr< RealSpaceModel > m_realSpaceModel
void setModel(RealSpaceModel *model)

References m_currentSelection, m_realSpaceModel, m_view, and RealSpaceView::setModel().

Referenced by onRowsAboutToBeRemoved(), and setConnected().

Here is the call graph for this function:

◆ savePicture()

void RealSpaceCanvas::savePicture ( const QPixmap &  pixmap)
private

Definition at line 146 of file RealSpaceCanvas.cpp.

147 {
148  QString dirname = AppSvc::projectManager()->userExportDir();
149  QString defaultExtension = ".png";
150  QString selectedFilter("*" + defaultExtension);
151  QString defaultName = dirname + "/untitled";
152  QString fileName = QFileDialog::getSaveFileName(nullptr, "Save 3D real space view", defaultName,
153  selectedFilter);
154  QString nameToSave =
155  fileName.endsWith(defaultExtension) ? fileName : fileName + defaultExtension;
156 
157  if (!nameToSave.isEmpty()) {
158  try {
159  pixmap.save(nameToSave);
160  } catch (const std::exception& ex) {
161  QString message = "Attempt to save file with the name '";
162  message.append(nameToSave);
163  message.append("' has failed with following error message\n\n");
164  message.append(QString::fromStdString(ex.what()));
165  QMessageBox::warning(nullptr, "Houston, we have a problem.", message);
166  }
167  }
168 }
static ProjectManager * projectManager()
Definition: AppSvc.cpp:18
QString userExportDir() const
Returns directory name suitable for saving plots.
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: GUIHelpers.cpp:74

References AppSvc::projectManager(), ProjectManager::userExportDir(), and GUIHelpers::warning().

Referenced by onSavePictureAction().

Here is the call graph for this function:

◆ setConnected()

void RealSpaceCanvas::setConnected ( SampleModel model,
bool  makeConnected 
)
private

Definition at line 234 of file RealSpaceCanvas.cpp.

235 {
236  if (!model)
237  return;
238 
239  if (makeConnected) {
240  connect(model, &SampleModel::rowsInserted, this, &RealSpaceCanvas::updateScene,
241  Qt::UniqueConnection);
242  connect(model, &SampleModel::rowsRemoved, this, &RealSpaceCanvas::updateScene,
243  Qt::UniqueConnection);
244  connect(model, &SampleModel::rowsAboutToBeRemoved, this,
245  &RealSpaceCanvas::onRowsAboutToBeRemoved, Qt::UniqueConnection);
246  connect(model, &SampleModel::dataChanged, this, &RealSpaceCanvas::onDataChanged,
247  Qt::UniqueConnection);
248  connect(model, &SampleModel::modelReset, this, &RealSpaceCanvas::resetScene,
249  Qt::UniqueConnection);
250  connect(
251  model, &SampleModel::modelAboutToBeReset, this, [&]() { m_currentSelection = {}; },
252  Qt::UniqueConnection);
253 
254  } else {
255  disconnect(model, &SampleModel::rowsInserted, this, &RealSpaceCanvas::updateScene);
256  disconnect(model, &SampleModel::rowsRemoved, this, &RealSpaceCanvas::updateScene);
257  connect(model, &SampleModel::rowsAboutToBeMoved, this,
259  disconnect(model, &SampleModel::dataChanged, this, &RealSpaceCanvas::onDataChanged);
260  disconnect(model, &SampleModel::modelReset, this, &RealSpaceCanvas::resetScene);
261  }
262 }
void onDataChanged(const QModelIndex &index)
void onRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)

References m_currentSelection, onDataChanged(), onRowsAboutToBeRemoved(), resetScene(), and updateScene().

Referenced by setModel().

Here is the call graph for this function:

◆ setModel()

void RealSpaceCanvas::setModel ( SampleModel sampleModel = nullptr,
QItemSelectionModel *  selectionModel = nullptr 
)

Definition at line 48 of file RealSpaceCanvas.cpp.

49 {
50  if (sampleModel != m_sampleModel) {
51 
52  if (m_sampleModel)
54 
55  m_sampleModel = sampleModel;
56 
57  if (m_sampleModel)
59  }
60 
61  if (selectionModel != m_selectionModel) {
62  if (m_selectionModel)
63  disconnect(m_selectionModel, &QItemSelectionModel::selectionChanged, this,
65 
66  m_selectionModel = selectionModel;
67 
68  if (m_selectionModel)
69  connect(m_selectionModel, &QItemSelectionModel::selectionChanged, this,
71  }
72 
74 }
void onSelectionChanged(const QItemSelection &, const QItemSelection &)
void setConnected(SampleModel *model, bool makeConnected)

References m_sampleModel, m_selectionModel, onSelectionChanged(), setConnected(), and updateToSelection().

Referenced by RealSpaceWidget::hideEvent(), and RealSpaceWidget::showEvent().

Here is the call graph for this function:

◆ sideView

void RealSpaceCanvas::sideView ( )
privateslot

Definition at line 224 of file RealSpaceCanvas.cpp.

225 {
226  m_view->sideView();
227 }

References m_view, and RealSpaceView::sideView().

Referenced by onSideViewAction().

Here is the call graph for this function:

◆ topView

void RealSpaceCanvas::topView ( )
privateslot

Definition at line 229 of file RealSpaceCanvas.cpp.

230 {
231  m_view->topView();
232 }

References m_view, and RealSpaceView::topView().

Referenced by onTopViewAction().

Here is the call graph for this function:

◆ updateScene

void RealSpaceCanvas::updateScene ( )
privateslot

Definition at line 180 of file RealSpaceCanvas.cpp.

181 {
182  QApplication::setOverrideCursor(Qt::WaitCursor);
183 
184  m_realSpaceModel.reset(new RealSpaceModel);
186  if (!item)
187  return;
188 
189  RealSpaceBuilder builder3D;
190 
191  try {
192  m_warningSign->clear();
193  // if the view is locked, keep the current orientation of the camera
194  if (m_view_locked)
195  builder3D.populate(m_realSpaceModel.get(), *item, *m_sceneGeometry,
196  m_view->getCamera().getPos());
197  // otherwise use default orientation of camera
198  else {
199  builder3D.populate(m_realSpaceModel.get(), *item, *m_sceneGeometry);
200  defaultView(); // Enforces default view and also sets the zoomLevel to default i.e. 0
201  }
202  } catch (const std::exception& ex) {
203  m_warningSign->setWarningMessage(ex.what());
204  } catch (...) {
205  // ignore other exceptions thrown
206  }
208 
209  QApplication::restoreOverrideCursor();
210 }
void populate(RealSpaceModel *model, const SessionItem &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))
RealSpace::Camera & getCamera()
const Position & getPos() const
Definition: camera.h:51
void clear()
Clears warning message;.
Definition: WarningSign.cpp:40
void setWarningMessage(const QString &warningMessage)
Shows warning sign on the screen.
Definition: WarningSign.cpp:58

References WarningSign::clear(), defaultView(), RealSpaceView::getCamera(), RealSpace::Camera::getPos(), SessionModel::itemForIndex(), m_currentSelection, m_realSpaceModel, m_sampleModel, m_sceneGeometry, m_view, m_view_locked, m_warningSign, RealSpaceBuilder::populate(), RealSpaceView::setModel(), and WarningSign::setWarningMessage().

Referenced by onChangeLayerSizeAction(), onDataChanged(), setConnected(), and updateToSelection().

Here is the call graph for this function:

◆ updateToSelection

void RealSpaceCanvas::updateToSelection ( )
slot

Definition at line 81 of file RealSpaceCanvas.cpp.

82 {
83  if (!m_selectionModel)
84  return;
85 
86  if (!m_view_locked) {
87  QModelIndexList indices = m_selectionModel->selection().indexes();
88 
89  if (!indices.empty())
91  else
92  m_currentSelection = {};
93  // if no object is selected then display nothing on canvas
94 
95  updateScene();
96  }
97 }
static QModelIndex toSourceIndex(QModelIndex index)

References m_currentSelection, m_selectionModel, m_view_locked, FilterPropertyProxy::toSourceIndex(), and updateScene().

Referenced by onLockViewAction(), onSelectionChanged(), and setModel().

Here is the call graph for this function:

Member Data Documentation

◆ m_currentSelection

QModelIndex RealSpaceCanvas::m_currentSelection
private

◆ m_realSpaceModel

std::unique_ptr<RealSpaceModel> RealSpaceCanvas::m_realSpaceModel
private

Definition at line 94 of file RealSpaceCanvas.h.

Referenced by resetScene(), and updateScene().

◆ m_sampleModel

SampleModel* RealSpaceCanvas::m_sampleModel
private

Definition at line 91 of file RealSpaceCanvas.h.

Referenced by onDataChanged(), onRowsAboutToBeRemoved(), setModel(), and updateScene().

◆ m_sceneGeometry

std::unique_ptr<SceneGeometry> RealSpaceCanvas::m_sceneGeometry
private

Definition at line 97 of file RealSpaceCanvas.h.

Referenced by onChangeLayerSizeAction(), and updateScene().

◆ m_selectionModel

QItemSelectionModel* RealSpaceCanvas::m_selectionModel
private

Definition at line 95 of file RealSpaceCanvas.h.

Referenced by setModel(), and updateToSelection().

◆ m_view

RealSpaceView* RealSpaceCanvas::m_view
private

◆ m_view_locked

bool RealSpaceCanvas::m_view_locked
private

Definition at line 96 of file RealSpaceCanvas.h.

Referenced by onLockViewAction(), updateScene(), and updateToSelection().

◆ m_warningSign

WarningSign* RealSpaceCanvas::m_warningSign
private

Definition at line 98 of file RealSpaceCanvas.h.

Referenced by updateScene().


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