BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MaterialEditor.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/MaterialEditor/MaterialEditor.cpp
6 //! @brief Implements class MaterialEditor
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 
22 #include <QListView>
23 #include <QSplitter>
24 #include <QVBoxLayout>
25 
26 MaterialEditor::MaterialEditor(MaterialModel* materialModel, QWidget* parent)
27  : QWidget(parent)
28  , m_materialModel(materialModel)
29  , m_toolBar(new MaterialEditorToolBar(materialModel, this))
30  , m_splitter(new QSplitter)
31  , m_listView(new QListView)
32  , m_componentEditor(new ComponentEditor)
33  , m_model_was_modified(false)
34 {
35  setWindowTitle("MaterialEditorWidget");
36  setMinimumSize(128, 128);
37  resize(512, 400);
38  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
39 
40  QVBoxLayout* layout = new QVBoxLayout;
41  layout->setContentsMargins(0, 0, 0, 0);
42 
43  m_splitter->addWidget(m_listView);
44  m_splitter->addWidget(m_componentEditor);
45  m_splitter->setCollapsible(0, false);
46  m_splitter->setCollapsible(1, false);
47 
48  layout->addWidget(m_toolBar);
49  layout->addWidget(m_splitter);
50 
51  setLayout(layout);
52 
53  init_views();
54 }
55 
56 QItemSelectionModel* MaterialEditor::selectionModel()
57 {
59  return m_listView->selectionModel();
60 }
61 
63 {
64  auto selected = selectionModel()->currentIndex();
65  return selected.isValid() ? m_materialModel->materialFromIndex(selected) : nullptr;
66 }
67 
68 //! Sets selection corresponding to initial material property
70 {
72  selectionModel()->clearSelection();
73  selectionModel()->setCurrentIndex(m_materialModel->indexOfItem(mat),
74  QItemSelectionModel::ClearAndSelect);
76  QItemSelectionModel::ClearAndSelect);
77  }
78 }
79 
81 {
82  return m_model_was_modified;
83 }
84 
85 void MaterialEditor::onSelectionChanged(const QItemSelection& selected, const QItemSelection&)
86 {
87  QModelIndexList indices = selected.indexes();
88 
89  if (indices.isEmpty()) {
90  m_componentEditor->setItem(nullptr);
91  } else {
92  if (SessionItem* item = m_materialModel->itemForIndex(indices.front()))
94  }
95 }
96 
97 void MaterialEditor::onDataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)
98 {
99  m_model_was_modified = true;
100 }
101 
102 void MaterialEditor::onRowsInserted(const QModelIndex&, int, int)
103 {
104  m_model_was_modified = true;
105 }
106 
107 void MaterialEditor::onRowsRemoved(const QModelIndex&, int, int)
108 {
109  m_model_was_modified = true;
110 }
111 
112 //! Context menu reimplemented to supress default menu
113 void MaterialEditor::contextMenuEvent(QContextMenuEvent* event)
114 {
115  Q_UNUSED(event);
116 }
117 
119 {
120  connect(m_materialModel, &MaterialModel::dataChanged, this, &MaterialEditor::onDataChanged);
121  connect(m_materialModel, &MaterialModel::rowsInserted, this, &MaterialEditor::onRowsInserted);
122  connect(m_materialModel, &MaterialModel::rowsRemoved, this, &MaterialEditor::onRowsRemoved);
123 
124  m_listView->setContextMenuPolicy(Qt::CustomContextMenu);
126  m_listView->setMovement(QListView::Static);
127  m_listView->setMinimumWidth(50);
128  m_listView->setMaximumWidth(220);
129  m_listView->setSpacing(5);
130 
132 
133  connect(selectionModel(), &QItemSelectionModel::selectionChanged, this,
135 
136  // making first material selected
137  if (!selectionModel()->hasSelection()) {
138  QModelIndex itemIndex = m_materialModel->index(0, 0, QModelIndex());
139  selectionModel()->select(itemIndex, QItemSelectionModel::ClearAndSelect);
140  }
141 
142  connect(m_listView, &QListView::customContextMenuRequested, m_toolBar,
144 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines ComponentEditor class.
Defines class ExternalProperty.
Defines class MaterialEditorToolBar.
Defines class MaterialEditor.
Defines class MaterialItem.
Defines class MaterialModel.
Defines class SessionDecorationModel.
Component editor for SessionItem.
void setItem(SessionItem *item)
The ExternalProperty class defines custom QVariant property to carry the text, color and an identifie...
QString identifier() const
Toolbar for MaterialEditor.
void setSelectionModel(QItemSelectionModel *selectionModel)
void onCustomContextMenuRequested(const QPoint &point)
MaterialModel * m_materialModel
bool modelWasChanged() const
MaterialItem * selectedMaterial()
void onDataChanged(const QModelIndex &, const QModelIndex &, const QVector< int > &)
void onRowsInserted(const QModelIndex &, int, int)
void contextMenuEvent(QContextMenuEvent *event)
Context menu reimplemented to supress default menu.
MaterialEditorToolBar * m_toolBar
bool m_model_was_modified
void onSelectionChanged(const QItemSelection &selected, const QItemSelection &)
QItemSelectionModel * selectionModel()
QSplitter * m_splitter
ComponentEditor * m_componentEditor
MaterialEditor(MaterialModel *materialModel, QWidget *parent=nullptr)
void setInitialMaterialProperty(const ExternalProperty &matProperty)
Sets selection corresponding to initial material property.
void onRowsRemoved(const QModelIndex &, int, int)
QListView * m_listView
MaterialItem * materialFromIndex(const QModelIndex &index)
MaterialItem * materialFromIdentifier(const QString &identifier)
Provides decorations (text color, icons, etc) for SessionModel in SessionModelView context.
SessionItem * itemForIndex(const QModelIndex &index) const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QModelIndex indexOfItem(SessionItem *item) const