1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SMESHGUI_FieldSelectorWdg.h"
25 #include "SMESHGUI_Utils.h"
26 #include "SMESHGUI_GEOMGenUtils.h"
28 #include <SALOMEDSClient_Study.hxx>
29 #include <GEOM_wrap.hxx>
32 #include <QTreeWidget>
33 #include <QTreeWidgetItem>
34 #include <QTreeWidgetItemIterator>
35 #include <QVBoxLayout>
41 QTreeWidgetItem* createItem( QTreeWidget* tree,
44 QTreeWidgetItem* parentItem=0)
46 QTreeWidgetItem* item;
48 item = new QTreeWidgetItem( parentItem );
50 item = new QTreeWidgetItem( tree );
51 item->setText( 0, text );
52 item->setData( 0, Qt::UserRole, index );
53 item->setFlags( item->flags() | Qt::ItemIsUserCheckable | Qt::ItemIsEditable );
54 item->setCheckState( 0, Qt::Unchecked );
55 item->setExpanded( true );
58 QFont f = item->font( 0 );
60 item->setFont( 0, f );
66 //--------------------------------------------------------------------------------
68 * \brief Constructor of SMESHGUI_FieldSelectorWdg
70 SMESHGUI_FieldSelectorWdg::SMESHGUI_FieldSelectorWdg( QWidget* p )
71 :QGroupBox( tr("FIELDS_TO_EXPORT"), p )
74 myTree = new QTreeWidget( this );
75 myTree->setHeaderHidden( true );
77 QVBoxLayout* lay = new QVBoxLayout( this );
78 lay->addWidget( myTree );
80 connect( myTree, SIGNAL( itemChanged(QTreeWidgetItem*, int)),
81 this, SLOT ( onItemCheck(QTreeWidgetItem*, int)));
84 //--------------------------------------------------------------------------------
86 * \brief Retrieves all fields defined on geometry of given meshes
88 bool SMESHGUI_FieldSelectorWdg::
89 GetAllFeilds(const QList< QPair< SMESH::SMESH_IDSource_var, QString > >& meshes,
90 QList< QPair< GEOM::ListOfFields_var, QString > >& fields)
95 _PTR(Study) study = SMESH::GetActiveStudyDocument();
96 GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
97 GEOM::GEOM_IFieldOperations_wrap fieldOp = geomGen->GetIFieldOperations( study->StudyId() );
99 for ( int iM = 0; iM < meshes.count(); ++iM )
101 GEOM::ListOfFields_var fields;
104 SMESH::SMESH_Mesh_var mesh = meshes[iM].first->GetMesh();
105 if ( !mesh->_is_nil() && mesh->HasShapeToMesh() )
107 SMESH::array_of_ElementType_var elemTypes = meshes[iM].first->GetTypes();
108 if ( &elemTypes.in() && elemTypes->length() > 0 )
110 QTreeWidgetItem* meshItem = createItem( myTree, meshes[iM].second, iM );
112 GEOM::GEOM_Object_var shape = mesh->GetShapeToMesh();
113 fields = fieldOp->GetFields( shape );
114 for ( size_t iF = 0; iF < fields->length(); ++iF )
116 GEOM::field_data_type dataType = fields[ iF ]->GetDataType();
117 if ( dataType == GEOM::FDT_String )
119 GEOM::ListOfLong_var stepIDs = fields[ iF ]->GetSteps();
120 if ( stepIDs->length() < 1 )
122 GEOM::string_array_var comps = fields[ iF ]->GetComponents();
123 if ( comps->length() < 1 )
125 CORBA::Short dim = fields[iF]->GetDimension();
127 continue; // "whole shape" field ignored
129 CORBA::String_var name = fields[iF]->GetName();
130 createItem( myTree, name.in(), iF, meshItem );
133 for ( size_t i = 0; i < elemTypes->length(); ++i )
136 switch ( elemTypes[i] ) {
137 case SMESH::NODE: name = "_vertices_"; break;
138 case SMESH::EDGE: name = "_edges_" ; break;
139 case SMESH::FACE: name = "_faces_" ; break;
140 case SMESH::VOLUME: name = "_solids_" ; break;
144 createItem( myTree, name, -1, meshItem );
146 if ( !geoAss.isEmpty() && !geoAss.count('v') )
148 createItem( myTree, "_vertices_", -1, meshItem );
151 } // if ( mesh->HasShapeToMesh() )
153 if ( ! &fields.in() )
154 fields = new GEOM::ListOfFields();
156 myFields->push_back( qMakePair( fields, QString() ));
162 return myTree->topLevelItemCount();
165 //--------------------------------------------------------------------------------
167 * \brief Filter off not selected fields from myFields
169 bool SMESHGUI_FieldSelectorWdg::GetSelectedFeilds()
172 if ( myTree->isEnabled() )
173 for ( int i = 0; i < myTree->topLevelItemCount(); ++i )
175 QTreeWidgetItem* meshItem = myTree->topLevelItem( i );
176 int iM = meshItem->data( 0, Qt::UserRole ).toInt();
178 GEOM::ListOfFields& fields = (*myFields)[ iM ].first.inout();
179 QString& geoAss = (*myFields)[ iM ].second;
182 QTreeWidgetItemIterator it ( meshItem, QTreeWidgetItemIterator::Checked );
183 if ( *it == meshItem ) ++it;
184 for ( ; *it ; ++it, ++nbSelected )
186 if ( !(*it)->parent() )
187 break; // next mesh item
189 int iF = (*it)->data( 0, Qt::UserRole ).toInt();
192 geoAss += (*it)->text(0)[1];
197 fields[ nbF ] = fields[ iF ];
201 fields.length( nbF );
205 for ( int iF = 0; iF < myFields->count(); ++iF )
207 GEOM::ListOfFields& fields = (*myFields)[ iF ].first.inout();
214 //--------------------------------------------------------------------------------
216 * \brief SLOT called when a tree item is checked
218 void SMESHGUI_FieldSelectorWdg::onItemCheck(QTreeWidgetItem * item, int column)
220 myTree->blockSignals( true );
221 if ( !item->parent() ) // mesh item
223 Qt::CheckState st = item->checkState(0);
224 QTreeWidgetItemIterator it( item );
225 for ( ++it; *it ; ++it )
226 if ( !(*it)->parent() )
227 break; // next mesh item
229 (*it)->setCheckState( 0, st );
233 // update CheckState of a parent mesh item
234 QTreeWidgetItem* meshItem = item->parent();
235 Qt::CheckState st = item->checkState(0);
236 QTreeWidgetItemIterator it( meshItem );
237 for ( ++it; *it ; ++it )
238 if ( !(*it)->parent() )
240 break; // next mesh item
242 else if ( (*it)->checkState(0) != st )
244 st = Qt::PartiallyChecked;
247 meshItem->setCheckState( 0, st );
249 myTree->blockSignals( false );