OGS
MeshModel Class Reference

Detailed Description

The MeshModel is a Qt model which represents Mesh objects.

Definition at line 26 of file MeshModel.h.

#include <MeshModel.h>

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

Public Slots

void addMesh (MeshLib::Mesh *mesh)
 Adds a new mesh (using no unique_ptr as this interferes with Qt's signal/slot policy)
const MeshLib::MeshgetMesh (const QModelIndex &idx) const
 Returns the mesh with the given index.
const MeshLib::MeshgetMesh (const std::string &name) const
 Returns the mesh with the given name.
bool removeMesh (const QModelIndex &idx)
 Removes the mesh with the given index.
bool removeMesh (const std::string &name)
 Removes the mesh with the given name.
void updateMesh (MeshLib::Mesh *)
 Updates the model/view for a mesh.
void updateModel ()
 Updates the model based on the ProjectData-object.
vtkUnstructuredGridAlgorithm * vtkSource (const QModelIndex &idx) const
 Returns the VTK source item for the mesh with the given index.
vtkUnstructuredGridAlgorithm * vtkSource (const std::string &name) const
 Returns the VTK source item for the mesh with the given name.
Public Slots inherited from TreeModel
void updateData ()

Signals

void meshAdded (MeshModel *, const QModelIndex &)
void meshRemoved (MeshModel *, const QModelIndex &)

Public Member Functions

 MeshModel (DataHolderLib::Project &project, QObject *parent=nullptr)
void addMesh (std::unique_ptr< MeshLib::Mesh > mesh)
 Adds a new mesh.
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 Returns the number of columns used for the data list.
Public Member Functions inherited from TreeModel
 TreeModel (QObject *parent=nullptr)
 ~TreeModel () override
QVariant data (const QModelIndex &index, int role) const override
bool setData (const QModelIndex &index, const QVariant &value, int role) override
Qt::ItemFlags flags (const QModelIndex &index) const override
TreeItemgetItem (const QModelIndex &index) const
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
QModelIndex parent (const QModelIndex &index) const override
bool removeRows (int position, int count, const QModelIndex &parent) override
int rowCount (const QModelIndex &parent=QModelIndex()) const override
int columnCount (const QModelIndex &parent=QModelIndex()) const override
TreeItemrootItem () const

Private Member Functions

void addMeshObject (const MeshLib::Mesh *mesh)
 Adds the mesh to the GUI-Mesh-Model und -View.

Static Private Member Functions

static std::map< MeshLib::MeshElemType, QVariant > createMeshElemTypeMap ()
 Creates a static map of all element type name-strings in QVariant format.

Private Attributes

DataHolderLib::Project_project
 Checks if the name of the mesh is already exists, if so it generates a unique name.

Static Private Attributes

static const std::map< MeshLib::MeshElemType, QVariant > elem_type_map
static const QVariant element_str = "Element"

Additional Inherited Members

Protected Attributes inherited from TreeModel
TreeItem_rootItem

Constructor & Destructor Documentation

◆ MeshModel()

MeshModel::MeshModel ( DataHolderLib::Project & project,
QObject * parent = nullptr )
explicit

Definition at line 23 of file MeshModel.cpp.

24 : TreeModel(parent), _project(project)
25{
26 delete _rootItem;
27 QList<QVariant> rootData;
28 rootData << "Mesh Name"
29 << "#"
30 << "Type";
31 _rootItem = new TreeItem(rootData, nullptr);
32}
DataHolderLib::Project & _project
Checks if the name of the mesh is already exists, if so it generates a unique name.
Definition MeshModel.h:66
QModelIndex parent(const QModelIndex &index) const override
Definition TreeModel.cpp:70
TreeModel(QObject *parent=nullptr)
Definition TreeModel.cpp:15
TreeItem * _rootItem
Definition TreeModel.h:47

References TreeModel::TreeModel(), _project, TreeModel::_rootItem, and TreeModel::parent().

Referenced by meshAdded(), and meshRemoved().

Member Function Documentation

◆ addMesh [1/2]

void MeshModel::addMesh ( MeshLib::Mesh * mesh)
slot

Adds a new mesh (using no unique_ptr as this interferes with Qt's signal/slot policy)

Definition at line 48 of file MeshModel.cpp.

49{
50 _project.addMesh(std::unique_ptr<MeshLib::Mesh>(mesh));
51 auto const& meshes(_project.getMeshObjects());
52 this->addMeshObject(meshes.back().get());
53}
void addMeshObject(const MeshLib::Mesh *mesh)
Adds the mesh to the GUI-Mesh-Model und -View.
Definition MeshModel.cpp:55

References _project, and addMeshObject().

◆ addMesh() [2/2]

void MeshModel::addMesh ( std::unique_ptr< MeshLib::Mesh > mesh)

Adds a new mesh.

Definition at line 41 of file MeshModel.cpp.

42{
43 _project.addMesh(std::move(mesh));
44 auto const& meshes(_project.getMeshObjects());
45 this->addMeshObject(meshes.back().get());
46}

References _project, and addMeshObject().

◆ addMeshObject()

void MeshModel::addMeshObject ( const MeshLib::Mesh * mesh)
private

Adds the mesh to the GUI-Mesh-Model und -View.

Definition at line 55 of file MeshModel.cpp.

56{
57 beginResetModel();
58
59 INFO("name: {:s}", mesh->getName());
60 QVariant const display_name(QString::fromStdString(mesh->getName()));
61 QList<QVariant> meshData;
62 meshData << display_name << ""
63 << "";
64 auto* const newMesh = new MeshItem(meshData, _rootItem, mesh);
65 _rootItem->appendChild(newMesh);
66
67 // display elements
68 std::vector<MeshLib::Element*> const& elems = mesh->getElements();
69 auto const nElems(static_cast<int>(elems.size()));
70
71 for (int i = 0; i < nElems; i++)
72 {
73 QList<QVariant> elemData;
74 elemData.reserve(3);
75 elemData << element_str << i
76 << elem_type_map.at(elems[i]->getGeomType());
77 newMesh->appendChild(new TreeItem(elemData, newMesh));
78 }
79
80 endResetModel();
81 emit meshAdded(this,
82 this->index(_rootItem->childCount() - 1, 0, QModelIndex()));
83}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:100
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:94
void meshAdded(MeshModel *, const QModelIndex &)
static const std::map< MeshLib::MeshElemType, QVariant > elem_type_map
Definition MeshModel.h:71
static const QVariant element_str
Definition MeshModel.h:72
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition TreeModel.cpp:39

References TreeModel::_rootItem, elem_type_map, element_str, MeshLib::Mesh::getElements(), MeshLib::Mesh::getName(), TreeModel::index(), INFO(), and meshAdded().

Referenced by addMesh(), addMesh(), updateMesh(), and updateModel().

◆ columnCount()

int MeshModel::columnCount ( const QModelIndex & parent = QModelIndex()) const
override

Returns the number of columns used for the data list.

Definition at line 34 of file MeshModel.cpp.

35{
36 Q_UNUSED(parent)
37
38 return 3;
39}

References TreeModel::parent().

◆ createMeshElemTypeMap()

std::map< MeshLib::MeshElemType, QVariant > MeshModel::createMeshElemTypeMap ( )
staticprivate

Creates a static map of all element type name-strings in QVariant format.

Definition at line 175 of file MeshModel.cpp.

176{
177 std::vector<MeshLib::MeshElemType> const& elem_types(
179 std::map<MeshLib::MeshElemType, QVariant> elem_map;
180
181 for (MeshLib::MeshElemType t : elem_types)
182 {
183 elem_map[t] =
184 QVariant(QString::fromStdString(MeshLib::MeshElemType2String(t)));
185 }
186
187 return elem_map;
188}
std::string MeshElemType2String(const MeshElemType t)
Given a MeshElemType this returns the appropriate string.
Definition MeshEnums.cpp:10
std::vector< MeshElemType > getMeshElemTypes()
Returns a vector of all mesh element types.
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition MeshEnums.h:37

References MeshLib::getMeshElemTypes(), and MeshLib::MeshElemType2String().

◆ getMesh [1/2]

const MeshLib::Mesh * MeshModel::getMesh ( const QModelIndex & idx) const
slot

Returns the mesh with the given index.

Definition at line 85 of file MeshModel.cpp.

86{
87 if (idx.isValid())
88 {
89 auto* item = dynamic_cast<MeshItem*>(this->getItem(idx));
90 if (item)
91 {
92 return item->getMesh();
93 }
94
95 return nullptr;
96 }
97 WARN("MeshModel::getMesh(): Specified index does not exist.");
98 return nullptr;
99}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
TreeItem * getItem(const QModelIndex &index) const

References TreeModel::getItem(), and WARN().

Referenced by AddFaultsToVoxelGridDialog::AddFaultsToVoxelGridDialog(), Layers2GridDialog::Layers2GridDialog(), Vtu2GridDialog::Vtu2GridDialog(), MeshView::openMap2dMeshDialog(), MeshView::openMeshEditDialog(), MeshView::openRasterDataToMeshDialog(), MeshView::openValuesEditDialog(), and updateModel().

◆ getMesh [2/2]

const MeshLib::Mesh * MeshModel::getMesh ( const std::string & name) const
slot

Returns the mesh with the given name.

Definition at line 101 of file MeshModel.cpp.

102{
103 for (int i = 0; i < _rootItem->childCount(); i++)
104 {
105 auto* item = static_cast<MeshItem*>(_rootItem->child(i));
106 if (item->data(0).toString().toStdString() == name)
107 {
108 return item->getMesh();
109 }
110 }
111
112 INFO("MeshModel::getMesh(): No entry found with name \"{:s}\".", name);
113 return nullptr;
114}

References TreeModel::_rootItem, and INFO().

◆ meshAdded

void MeshModel::meshAdded ( MeshModel * ,
const QModelIndex &  )
signal

References MeshModel().

Referenced by addMeshObject().

◆ meshRemoved

void MeshModel::meshRemoved ( MeshModel * ,
const QModelIndex &  )
signal

References MeshModel().

Referenced by removeMesh(), and updateMesh().

◆ removeMesh [1/2]

bool MeshModel::removeMesh ( const QModelIndex & idx)
slot

Removes the mesh with the given index.

Definition at line 116 of file MeshModel.cpp.

117{
118 if (idx.isValid())
119 {
120 auto* item = dynamic_cast<MeshItem*>(this->getItem(idx));
121 if (item)
122 {
123 return this->removeMesh(item->getMesh()->getName());
124 }
125 return false;
126 }
127 return false;
128}
bool removeMesh(const QModelIndex &idx)
Removes the mesh with the given index.

References TreeModel::getItem(), and removeMesh().

Referenced by removeMesh().

◆ removeMesh [2/2]

bool MeshModel::removeMesh ( const std::string & name)
slot

Removes the mesh with the given name.

Definition at line 130 of file MeshModel.cpp.

131{
132 for (int i = 0; i < _rootItem->childCount(); i++)
133 {
134 TreeItem* item = _rootItem->child(i);
135 if (item->data(0).toString().toStdString() == name)
136 {
137 beginResetModel();
138 emit meshRemoved(this, this->index(i, 0, QModelIndex()));
139 _rootItem->removeChildren(i, 1);
140 endResetModel();
141 return _project.removeMesh(name);
142 }
143 }
144
145 INFO("MeshModel::removeMesh(): No entry found with name \"{:s}\".", name);
146 return false;
147}
void meshRemoved(MeshModel *, const QModelIndex &)
virtual QVariant data(int column) const
Definition TreeItem.cpp:83

References _project, TreeModel::_rootItem, TreeItem::data(), TreeModel::index(), INFO(), and meshRemoved().

◆ updateMesh

void MeshModel::updateMesh ( MeshLib::Mesh * mesh)
slot

Updates the model/view for a mesh.

Definition at line 149 of file MeshModel.cpp.

150{
151 for (int i = 0; i < _rootItem->childCount(); i++)
152 {
153 if (dynamic_cast<MeshItem*>(this->_rootItem->child(i))->getMesh() ==
154 mesh)
155 {
156 emit meshRemoved(this, this->index(i, 0, QModelIndex()));
157 _rootItem->removeChildren(i, 1);
158 }
159 }
160 this->addMeshObject(mesh);
161}

References TreeModel::_rootItem, addMeshObject(), MeshItem::getMesh(), TreeModel::index(), and meshRemoved().

◆ updateModel

void MeshModel::updateModel ( )
slot

Updates the model based on the ProjectData-object.

Definition at line 163 of file MeshModel.cpp.

164{
165 auto const& mesh_vec = _project.getMeshObjects();
166 for (auto const& mesh : mesh_vec)
167 {
168 if (!getMesh(mesh->getName()))
169 { // if Mesh is not yet added to GUI, do it now
170 addMeshObject(mesh.get());
171 }
172 }
173}
const MeshLib::Mesh * getMesh(const QModelIndex &idx) const
Returns the mesh with the given index.
Definition MeshModel.cpp:85

References _project, addMeshObject(), and getMesh().

◆ vtkSource [1/2]

vtkUnstructuredGridAlgorithm * MeshModel::vtkSource ( const QModelIndex & idx) const
slot

Returns the VTK source item for the mesh with the given index.

Definition at line 190 of file MeshModel.cpp.

191{
192 if (idx.isValid())
193 {
194 auto* item = static_cast<MeshItem*>(this->getItem(idx));
195 return item->vtkSource();
196 }
197
198 INFO("MeshModel::vtkSource(): Specified index does not exist.");
199 return nullptr;
200}

References TreeModel::getItem(), INFO(), and MeshItem::vtkSource().

◆ vtkSource [2/2]

vtkUnstructuredGridAlgorithm * MeshModel::vtkSource ( const std::string & name) const
slot

Returns the VTK source item for the mesh with the given name.

Definition at line 202 of file MeshModel.cpp.

204{
205 for (int i = 0; i < _rootItem->childCount(); i++)
206 {
207 auto* item = static_cast<MeshItem*>(_rootItem->child(i));
208 if (item->data(0).toString().toStdString() == name)
209 {
210 return item->vtkSource();
211 }
212 }
213
214 INFO("MeshModel::vtkSource(): No entry found with name \"{:s}\".", name);
215 return nullptr;
216}

References TreeModel::_rootItem, and INFO().

Member Data Documentation

◆ _project

DataHolderLib::Project& MeshModel::_project
private

Checks if the name of the mesh is already exists, if so it generates a unique name.

Definition at line 66 of file MeshModel.h.

Referenced by MeshModel(), addMesh(), addMesh(), removeMesh(), and updateModel().

◆ elem_type_map

const std::map< MeshLib::MeshElemType, QVariant > MeshModel::elem_type_map
staticprivate
Initial value:
=
static std::map< MeshLib::MeshElemType, QVariant > createMeshElemTypeMap()
Creates a static map of all element type name-strings in QVariant format.

Definition at line 71 of file MeshModel.h.

Referenced by addMeshObject().

◆ element_str

const QVariant MeshModel::element_str = "Element"
staticprivate

Definition at line 72 of file MeshModel.h.

Referenced by addMeshObject().


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