Salome HOME
ILMAB: export GEOM fields to MED file
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_FieldSelectorWdg.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SMESHGUI_FieldSelectorWdg.h"
24
25 #include "SMESHGUI_Utils.h"
26 #include "SMESHGUI_GEOMGenUtils.h"
27
28 #include <SALOMEDSClient_Study.hxx>
29 #include <GEOM_wrap.hxx>
30
31 #include <QGroupBox>
32 #include <QTreeWidget>
33 #include <QTreeWidgetItem>
34 #include <QTreeWidgetItemIterator>
35 #include <QVBoxLayout>
36 #include <QLabel>
37 #include "SMESHGUI.h"
38
39 namespace
40 {
41   QTreeWidgetItem* createItem( QTreeWidget*     tree,
42                                const QString&   text,
43                                const int        index,
44                                QTreeWidgetItem* parentItem=0)
45   {
46     QTreeWidgetItem* item;
47     if ( parentItem )
48       item = new QTreeWidgetItem( parentItem );
49     else
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 );
56     if ( index < 0 )
57     {
58       QFont f = item->font( 0 );
59       f.setItalic( true );
60       item->setFont( 0, f );
61     }
62     return item;
63   }
64 }
65
66 //--------------------------------------------------------------------------------
67 /*!
68  * \brief Constructor of SMESHGUI_FieldSelectorWdg
69  */
70 SMESHGUI_FieldSelectorWdg::SMESHGUI_FieldSelectorWdg( QWidget* p )
71   :QGroupBox( tr("FIELDS_TO_EXPORT"), p )
72 {
73   setCheckable( true );
74   myTree = new QTreeWidget( this );
75   myTree->setHeaderHidden( true );
76   
77   QVBoxLayout* lay = new QVBoxLayout( this );
78   lay->addWidget( myTree );
79
80   connect( myTree, SIGNAL( itemChanged(QTreeWidgetItem*, int)),
81            this,   SLOT  ( onItemCheck(QTreeWidgetItem*, int)));
82 }
83
84 //--------------------------------------------------------------------------------
85 /*!
86  * \brief Retrieves all fields defined on geometry of given meshes
87  */
88 bool SMESHGUI_FieldSelectorWdg::
89 GetAllFeilds(const QList< QPair< SMESH::SMESH_IDSource_var, QString > >& meshes,
90              QList< QPair< GEOM::ListOfFields_var, QString > >&          fields)
91 {
92   myFields = & fields;
93   myTree->clear();
94   
95   _PTR(Study) study = SMESH::GetActiveStudyDocument();
96   GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
97   GEOM::GEOM_IFieldOperations_wrap fieldOp = geomGen->GetIFieldOperations( study->StudyId() );
98
99   for ( int iM = 0; iM < meshes.count(); ++iM )
100   {
101     GEOM::ListOfFields_var fields;
102     QString                geoAss;
103
104     SMESH::SMESH_Mesh_var mesh = meshes[iM].first->GetMesh();
105     if ( !mesh->_is_nil() && mesh->HasShapeToMesh() )
106     {
107       SMESH::array_of_ElementType_var elemTypes = meshes[iM].first->GetTypes();
108       if ( &elemTypes.in() && elemTypes->length() > 0 )
109       {
110         QTreeWidgetItem* meshItem = createItem( myTree, meshes[iM].second, iM );
111
112         GEOM::GEOM_Object_var shape = mesh->GetShapeToMesh();
113         fields = fieldOp->GetFields( shape );
114         for ( size_t iF = 0; iF < fields->length(); ++iF )
115         {
116           CORBA::String_var name = fields[iF]->GetName();
117           createItem( myTree, name.in(), iF, meshItem );
118         }
119         QString geoAss;
120         for ( size_t i = 0; i < elemTypes->length(); ++i )
121         {
122           QString name;
123           switch ( elemTypes[i] ) {
124           case SMESH::NODE:   name = "_vertices_"; break;
125           case SMESH::EDGE:   name = "_edges_"   ; break;
126           case SMESH::FACE:   name = "_faces_"   ; break;
127           case SMESH::VOLUME: name = "_solids_"  ; break;
128           default: continue;
129           }
130           geoAss += name[1];
131           createItem( myTree, name, -1, meshItem );
132         }
133         if ( !geoAss.isEmpty() && !geoAss.count('v') )
134         {
135           createItem( myTree, "_vertices_", -1, meshItem );
136         }
137       }
138     } // if ( mesh->HasShapeToMesh() )
139
140     if ( ! &fields.in() )
141       fields = new GEOM::ListOfFields();
142
143     myFields->push_back( qMakePair( fields, QString() ));
144
145   } // loop on meshes
146
147   setChecked( false );
148
149   return myTree->topLevelItemCount();
150 }
151
152 //--------------------------------------------------------------------------------
153 /*!
154  * \brief Filter off not selected fields from myFields
155  */
156 bool SMESHGUI_FieldSelectorWdg::GetSelectedFeilds()
157 {
158   int nbSelected = 0;
159   if ( myTree->isEnabled() )
160     for ( size_t i = 0; i < myTree->topLevelItemCount(); ++i )
161     {
162       QTreeWidgetItem* meshItem = myTree->topLevelItem( i );
163       int iM = meshItem->data( 0, Qt::UserRole ).toInt();
164
165       GEOM::ListOfFields& fields = (*myFields)[ iM ].first.inout();
166       QString&            geoAss = (*myFields)[ iM ].second;
167
168       int nbF = 0;
169       QTreeWidgetItemIterator it ( meshItem, QTreeWidgetItemIterator::Checked );
170       if ( *it == meshItem ) ++it;
171       for ( ; *it ; ++it, ++nbSelected )
172       {
173         if ( !(*it)->parent() )
174           break; // next mesh item
175
176         int iF = (*it)->data( 0, Qt::UserRole ).toInt();
177         if ( iF < 0 )
178         {
179           geoAss += (*it)->text(0)[1];
180         }
181         else
182         {
183           if ( nbF != iF )
184             fields[ nbF ] = fields[ iF ];
185           ++nbF;
186         }
187       }
188       fields.length( nbF );
189     }
190   else
191   {
192     for ( size_t iF = 0; iF < myFields->count(); ++iF )
193     {
194       GEOM::ListOfFields& fields = (*myFields)[ iF ].first.inout();
195       fields.length( 0 );
196     }
197   }
198   return nbSelected;
199 }
200
201 //--------------------------------------------------------------------------------
202 /*!
203  * \brief SLOT called when a tree item is checked
204  */
205 void SMESHGUI_FieldSelectorWdg::onItemCheck(QTreeWidgetItem * item, int column)
206 {
207   myTree->blockSignals( true );
208   if ( !item->parent() ) // mesh item
209   {
210     Qt::CheckState st = item->checkState(0);
211     QTreeWidgetItemIterator it( item );
212     for ( ++it; *it ; ++it )
213       if ( !(*it)->parent() )
214         break; // next mesh item
215       else
216         (*it)->setCheckState( 0, st );
217   }
218   else // field item
219   {
220     // update CheckState of a parent mesh item
221     QTreeWidgetItem* meshItem = item->parent();
222     Qt::CheckState st = item->checkState(0);
223     QTreeWidgetItemIterator it( meshItem );
224     for ( ++it; *it ; ++it )
225       if ( !(*it)->parent() )
226       {
227         break; // next mesh item
228       }
229       else if ( (*it)->checkState(0) != st )
230       {
231         st = Qt::PartiallyChecked;
232         break;
233       }
234     meshItem->setCheckState( 0, st );
235   }
236   myTree->blockSignals( false );
237 }