BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
viewmodelbase.h
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/viewmodel/mvvm/viewmodel/viewmodelbase.h
6 //! @brief Defines 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 
15 #ifndef BORNAGAIN_MVVM_VIEWMODEL_MVVM_VIEWMODEL_VIEWMODELBASE_H
16 #define BORNAGAIN_MVVM_VIEWMODEL_MVVM_VIEWMODEL_VIEWMODELBASE_H
17 
18 #include "mvvm/viewmodel_export.h"
19 #include <QAbstractItemModel>
20 #include <memory>
21 
22 namespace ModelView {
23 
24 class ViewItem;
25 
26 //! Base class for all view models to show content of SessionModel in Qt views.
27 //! ViewModelBase is made of ViewItems, where each ViewItem represents some concrete data role
28 //! of SessionItem. ViewModelBase doesn't have own logic and needs ViewModelController to listen
29 //! for SessionModel changes.
30 
31 class MVVM_VIEWMODEL_EXPORT ViewModelBase : public QAbstractItemModel {
32  Q_OBJECT
33 public:
34  explicit ViewModelBase(QObject* parent = nullptr);
35  ~ViewModelBase() override;
36 
37  QModelIndex index(int row, int column,
38  const QModelIndex& parent = QModelIndex()) const override;
39 
40  QModelIndex parent(const QModelIndex& child) const override;
41 
42  int rowCount(const QModelIndex& parent = QModelIndex()) const override;
43 
44  int columnCount(const QModelIndex& parent = QModelIndex()) const override;
45 
46  QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
47 
48  bool setData(const QModelIndex& index, const QVariant& value, int role) override;
49 
50  ViewItem* rootItem() const;
51 
52  ViewItem* itemFromIndex(const QModelIndex& index) const;
53 
54  QModelIndex indexFromItem(const ViewItem* item) const;
55 
56  void removeRow(ViewItem* parent, int row);
57 
58  void clearRows(ViewItem* parent);
59 
60  void insertRow(ViewItem* parent, int row, std::vector<std::unique_ptr<ViewItem>> items);
61 
62  void appendRow(ViewItem* parent, std::vector<std::unique_ptr<ViewItem>> items);
63 
64  Qt::ItemFlags flags(const QModelIndex& index) const override;
65 
66 private:
67  void setRootViewItem(std::unique_ptr<ViewItem> root_item);
68  friend class ViewModelController;
69  struct ViewModelBaseImpl;
70  std::unique_ptr<ViewModelBaseImpl> p_impl;
71 };
72 
73 } // namespace ModelView
74 
75 #endif // BORNAGAIN_MVVM_VIEWMODEL_MVVM_VIEWMODEL_VIEWMODELBASE_H
Represents the view of SessionItem's data in a single cell of ViewModel.
Definition: viewitem.h:29
Base class for all view models to show content of SessionModel in Qt views.
Definition: viewmodelbase.h:31
std::unique_ptr< ViewModelBaseImpl > p_impl
Definition: viewmodelbase.h:69
Propagates changes from SessionModel to its ViewModelBase.
void removeRow(QGridLayout *layout, int row, bool deleteWidgets=true)
Removes row from grid layout (important: doesn't change row count).
Definition: LayoutUtils.cpp:58
materialitems.h Collection of materials to populate MaterialModel.