Salome HOME
8b060f647c19d1c999e820f0673fac252cc3aeaf
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_FieldSelectorWdg.cxx
1 // Copyright (C) 2007-2016  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           GEOM::field_data_type dataType = fields[ iF ]->GetDataType();
117           if ( dataType == GEOM::FDT_String )
118             continue;
119           GEOM::ListOfLong_var stepIDs = fields[ iF ]->GetSteps();
120           if ( stepIDs->length() < 1 )
121             continue;
122           GEOM::string_array_var comps = fields[ iF ]->GetComponents();
123           if ( comps->length() < 1 )
124             continue;
125           CORBA::Short dim = fields[iF]->GetDimension();
126           if ( dim < 0 )
127             continue; // "whole shape" field ignored
128
129           CORBA::String_var name = fields[iF]->GetName();
130           createItem( myTree, name.in(), iF, meshItem );
131         }
132         QString geoAss;
133         for ( size_t i = 0; i < elemTypes->length(); ++i )
134         {
135           QString name;
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;
141           default: continue;
142           }
143           geoAss += name[1];
144           createItem( myTree, name, -1, meshItem );
145         }
146         if ( !geoAss.isEmpty() && !geoAss.count('v') )
147         {
148           createItem( myTree, "_vertices_", -1, meshItem );
149         }
150       }
151     } // if ( mesh->HasShapeToMesh() )
152
153     if ( ! &fields.in() )
154       fields = new GEOM::ListOfFields();
155
156     myFields->push_back( qMakePair( fields, QString() ));
157
158   } // loop on meshes
159
160   setChecked( false );
161
162   return myTree->topLevelItemCount();
163 }
164
165 //--------------------------------------------------------------------------------
166 /*!
167  * \brief Filter off not selected fields from myFields
168  */
169 bool SMESHGUI_FieldSelectorWdg::GetSelectedFeilds()
170 {
171   int nbSelected = 0;
172   if ( myTree->isEnabled() )
173     for ( int i = 0; i < myTree->topLevelItemCount(); ++i )
174     {
175       QTreeWidgetItem* meshItem = myTree->topLevelItem( i );
176       int iM = meshItem->data( 0, Qt::UserRole ).toInt();
177
178       GEOM::ListOfFields& fields = (*myFields)[ iM ].first.inout();
179       QString&            geoAss = (*myFields)[ iM ].second;
180
181       int nbF = 0;
182       QTreeWidgetItemIterator it ( meshItem, QTreeWidgetItemIterator::Checked );
183       if ( *it == meshItem ) ++it;
184       for ( ; *it ; ++it, ++nbSelected )
185       {
186         if ( !(*it)->parent() )
187           break; // next mesh item
188
189         int iF = (*it)->data( 0, Qt::UserRole ).toInt();
190         if ( iF < 0 )
191         {
192           geoAss += (*it)->text(0)[1];
193         }
194         else
195         {
196           if ( nbF != iF )
197             fields[ nbF ] = fields[ iF ];
198           ++nbF;
199         }
200       }
201       fields.length( nbF );
202     }
203   else
204   {
205     for ( int iF = 0; iF < myFields->count(); ++iF )
206     {
207       GEOM::ListOfFields& fields = (*myFields)[ iF ].first.inout();
208       fields.length( 0 );
209     }
210   }
211   return nbSelected;
212 }
213
214 //--------------------------------------------------------------------------------
215 /*!
216  * \brief SLOT called when a tree item is checked
217  */
218 void SMESHGUI_FieldSelectorWdg::onItemCheck(QTreeWidgetItem * item, int column)
219 {
220   myTree->blockSignals( true );
221   if ( !item->parent() ) // mesh item
222   {
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
228       else
229         (*it)->setCheckState( 0, st );
230   }
231   else // field item
232   {
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() )
239       {
240         break; // next mesh item
241       }
242       else if ( (*it)->checkState(0) != st )
243       {
244         st = Qt::PartiallyChecked;
245         break;
246       }
247     meshItem->setCheckState( 0, st );
248   }
249   myTree->blockSignals( false );
250 }