Salome HOME
Correct bug of SIGSEGV when attempting to filter on an empty group/family.
[modules/paravis.git] / src / Plugins / MEDReader / ParaViewPlugin / pqMEDReaderPanel.cxx
1 // Copyright (C) 2010-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay
20
21 #include "pqMEDReaderPanel.h"
22 #include "ui_MEDReaderPanel.h"
23 #include "VectBoolWidget.h"
24
25 #include "vtkProcessModule.h"
26 #include "vtkMultiBlockDataSet.h"
27 #include "vtkInformation.h"
28 #include "vtkIntArray.h"
29 #include "vtkSMDoubleVectorProperty.h"
30 #include "vtkSMIntVectorProperty.h"
31 #include "vtkSMStringVectorProperty.h"
32 #include "vtkSMProxy.h"
33 #include "vtkEventQtSlotConnect.h"
34 #include "vtkPVSILInformation.h"
35 #include "vtkGraph.h"
36 #include "vtkMutableDirectedGraph.h"
37 #include "vtkAdjacentVertexIterator.h"
38 #include "vtkSMPropertyHelper.h"
39 #include "vtkStringArray.h"
40 #include "vtkDataSetAttributes.h"
41 #include "vtkMEDReader.h"
42
43 #include "pqTreeWidgetItemObject.h"
44 #include "pqSMAdaptor.h"
45 #include "pqProxy.h"
46 #include "pqPropertyManager.h"
47 #include "pqSILModel.h"
48 #include "pqProxySILModel.h"
49 #include "pqTreeViewSelectionHelper.h"
50 #include "pqTreeWidgetSelectionHelper.h"
51
52 #include <QHeaderView>
53
54 static const char ZE_SEP[]="@@][@@";
55
56 class PixSingle
57 {
58 public:
59   static const PixSingle &GetInstance();
60   QPixmap getPixFromStr(const std::string& st) const;
61   QPixmap getWholeMeshPix() const;
62   PixSingle();
63 private:
64   static const int NB_OF_DISCR=4;
65   static PixSingle *UNIQUE_PIX_SINGLE;
66   QPixmap _pixmaps[NB_OF_DISCR];
67   std::map<std::string,int> _ze_map;
68   QPixmap _whole_mesh;
69 };
70
71 PixSingle *PixSingle::UNIQUE_PIX_SINGLE=0;
72
73 const PixSingle &PixSingle::GetInstance()
74 {
75   if(!UNIQUE_PIX_SINGLE)
76     UNIQUE_PIX_SINGLE=new PixSingle;
77   return *UNIQUE_PIX_SINGLE;
78 }
79
80 PixSingle::PixSingle()
81 {
82   _pixmaps[0]=QPixmap(":/ParaViewResources/Icons/pqCellData16.png");
83   _pixmaps[1]=QPixmap(":/ParaViewResources/Icons/pqPointData16.png");
84   _pixmaps[2]=QPixmap(":/ParaViewResources/Icons/pqQuadratureData16.png");
85   _pixmaps[3]=QPixmap(":/ParaViewResources/Icons/pqElnoData16.png");
86   _ze_map[std::string("P0")]=0;
87   _ze_map[std::string("P1")]=1;
88   _ze_map[std::string("GAUSS")]=2;
89   _ze_map[std::string("GSSNE")]=3;
90   _whole_mesh=QPixmap(":/ParaViewResources/Icons/pqCellDataForWholeMesh16.png");
91 }
92
93 QPixmap PixSingle::getPixFromStr(const std::string& st) const
94 {
95   std::map<std::string,int>::const_iterator it(_ze_map.find(st));
96   if(it!=_ze_map.end())
97     return _pixmaps[(*it).second];
98   else
99     return QPixmap();
100 }
101
102 QPixmap PixSingle::getWholeMeshPix() const
103 {
104   return _whole_mesh;
105 }
106
107 class pqMEDReaderPanel::pqUI: public QObject, public Ui::MEDReaderPanel
108 {
109 public:
110   pqUI(pqMEDReaderPanel *p):QObject(p)
111   {
112     this->VTKConnect = vtkSmartPointer<vtkEventQtSlotConnect>::New();
113   }
114
115   ~pqUI() { }
116   
117   vtkSmartPointer<vtkEventQtSlotConnect> VTKConnect;
118   QMap<QTreeWidgetItem*, QString> TreeItemToPropMap;
119 };
120
121 pqMEDReaderPanel::pqMEDReaderPanel(pqProxy *object_proxy, QWidget *p):Superclass(object_proxy,p),_reload_req(false),_optional_widget(0)
122 {
123   initAll();
124 }
125
126 void pqMEDReaderPanel::initAll()
127 {
128   _all_lev4.clear();
129   this->UI=new pqUI(this);
130   this->UI->setupUi(this);
131   this->UI->Fields->setHeaderHidden(true);
132   this->updateSIL();
133   ////////////////////
134   vtkSMProxy *reader(this->referenceProxy()->getProxy());
135   vtkPVSILInformation *info(vtkPVSILInformation::New());
136   reader->GatherInformation(info);
137   vtkGraph *g(info->GetSIL());
138   if(!g)//something wrong server side...
139     return ;
140   vtkMutableDirectedGraph *g2(vtkMutableDirectedGraph::SafeDownCast(g));
141   int idNames(0);
142   vtkAbstractArray *verticesNames(g2->GetVertexData()->GetAbstractArray("Names",idNames));
143   vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
144   vtkIdType id0;
145   bool found(false);
146   for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
147     {
148       vtkStdString &st(verticesNames2->GetValue(i));
149       if(st=="FieldsStatusTree")
150         {
151           id0=i;
152           found=true;
153         }
154     }
155   if(!found)
156     std::cerr << "There is an internal error ! The tree on server side has not the expected look !" << std::endl;
157   vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
158   g2->GetAdjacentVertices(id0,it0);
159   int kk(0),ll(0);
160   while(it0->HasNext())
161     {
162       vtkIdType idToolTipForTS(it0->Next());
163       QString toolTipName0(QString::fromStdString((const char *)verticesNames2->GetValue(idToolTipForTS)));
164       QString nbTS;
165       QList<QString> dts,its,tts;
166       {
167         vtkAdjacentVertexIterator *itForTS(vtkAdjacentVertexIterator::New());
168         g2->GetAdjacentVertices(idToolTipForTS,itForTS);
169         vtkIdType idForNbTS(itForTS->Next());
170         nbTS=QString::fromStdString((const char *)verticesNames2->GetValue(idForNbTS));
171         itForTS->Delete();
172         int nbTSInt(nbTS.toInt());
173         for(int ii=0;ii<nbTSInt;ii++)
174           {
175             dts.push_back(QString::fromStdString((const char *)verticesNames2->GetValue(idForNbTS+3*ii+1)));
176             its.push_back(QString::fromStdString((const char *)verticesNames2->GetValue(idForNbTS+3*ii+2)));
177             tts.push_back(QString::fromStdString((const char *)verticesNames2->GetValue(idForNbTS+3*ii+3)));
178           }
179       }
180       vtkIdType id1(it0->Next());
181       //
182       vtkSMProperty *SMProperty(this->proxy()->GetProperty("FieldsStatus"));
183       SMProperty->ResetToDefault();//this line is very important !
184       //
185       QString name0(QString::fromStdString((const char *)verticesNames2->GetValue(id1))); QList<QString> strs0; strs0.append(name0);
186       pqTreeWidgetItemObject *item0(new pqTreeWidgetItemObject(this->UI->Fields,strs0));
187       item0->setData(0,Qt::UserRole,name0);
188       item0->setData(0,Qt::ToolTipRole,toolTipName0);
189       //
190       QList<QVariant> modulesAct;
191       for(int i=0;i<nbTS.toInt();i++)
192         modulesAct.push_back(QVariant(true));
193       item0->setProperty("NbOfTS",nbTS);
194       item0->setProperty("DTS",QVariant(dts));
195       item0->setProperty("ITS",QVariant(its));
196       item0->setProperty("TTS",QVariant(tts));
197       item0->setProperty("ChosenTS",QVariant(modulesAct));
198       //
199       vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());//mesh
200       g2->GetAdjacentVertices(id1,it1);
201       while(it1->HasNext())
202         {
203           vtkIdType id2(it1->Next());
204           QString name1(QString::fromStdString((const char *)verticesNames2->GetValue(id2))); QList<QString> strs1; strs1.append(name1);
205           QString toolTipName1(name1);
206           pqTreeWidgetItemObject *item1(new pqTreeWidgetItemObject(item0,strs1));
207           item1->setData(0,Qt::UserRole,name1);
208           item1->setData(0,Qt::ToolTipRole,toolTipName1);
209           vtkAdjacentVertexIterator *it2(vtkAdjacentVertexIterator::New());//common support
210           g2->GetAdjacentVertices(id2,it2);
211           while(it2->HasNext())
212             {
213               vtkIdType id3(it2->Next());
214               QString name2(QString::fromStdString((const char *)verticesNames2->GetValue(id3))); QList<QString> strs2; strs2.append(name2);
215               pqTreeWidgetItemObject *item2(new pqTreeWidgetItemObject(item1,strs2));
216               item2->setData(0,Qt::UserRole,name2);
217               item2->setData(0,Qt::CheckStateRole,0);
218               vtkAdjacentVertexIterator *it3(vtkAdjacentVertexIterator::New());//fields !
219               g2->GetAdjacentVertices(id3,it3);
220               vtkIdType id3Arrs(it3->Next());
221               vtkAdjacentVertexIterator *it3Arrs(vtkAdjacentVertexIterator::New());//arrs in fields !
222               g2->GetAdjacentVertices(id3Arrs,it3Arrs);
223               while(it3Arrs->HasNext())
224                 {
225                   vtkIdType id4(it3Arrs->Next());
226                   std::string name3CppFull((const char *)verticesNames2->GetValue(id4));
227                   std::size_t pos(name3CppFull.find_first_of(ZE_SEP));
228                   std::string name3Only(name3CppFull.substr(0,pos)); std::string spatialDiscr(name3CppFull.substr(pos+sizeof(ZE_SEP)-1));
229                   QString name3(QString::fromStdString(name3Only)); QList<QString> strs3; strs3.append(name3);
230                   QString toolTipName3(name3+QString(" (")+spatialDiscr.c_str()+QString(")"));
231                   //
232                   vtkAdjacentVertexIterator *it4(vtkAdjacentVertexIterator::New());// is it a special field ? A field mesh ?
233                   g2->GetAdjacentVertices(id4,it4);
234                   bool isSpecial(it4->HasNext());
235                   it4->Delete();
236                   //
237                   pqTreeWidgetItemObject *item3(new pqTreeWidgetItemObject(item2,strs3));
238                   _all_lev4.push_back(item3);
239                   item3->setData(0,Qt::UserRole,name3);
240                   item3->setData(0,Qt::CheckStateRole,0);
241                   if(isSpecial)
242                     {
243                       QFont font; font.setItalic(true); font.setUnderline(true);
244                       item3->setData(0,Qt::FontRole,QVariant(font));
245                       item3->setData(0,Qt::ToolTipRole,QString("Whole \"%1\" mesh").arg(name3));
246                       item3->setData(0,Qt::DecorationRole,PixSingle::GetInstance().getWholeMeshPix());
247                     }
248                   else
249                     {
250                       item3->setData(0,Qt::ToolTipRole,toolTipName3);
251                       item3->setData(0,Qt::DecorationRole,PixSingle::GetInstance().getPixFromStr(spatialDiscr));
252                     }
253                   this->propertyManager()->registerLink(item3,"checked",SIGNAL(checkedStateChanged(bool)),this->proxy(),SMProperty,ll);
254                   _leaves.insert(std::pair<pqTreeWidgetItemObject *,int>(item3,ll));
255                   connect(item2,SIGNAL(checkedStateChanged(bool)),item3,SLOT(setChecked(bool)));
256                   connect(item3,SIGNAL(checkedStateChanged(bool)),this,SLOT(aLev4HasBeenFired()));
257                   ll++;
258                 }
259               vtkIdType id3Gts(it3->Next());
260               vtkAdjacentVertexIterator *it3Gts(vtkAdjacentVertexIterator::New());//geo types in fields !
261               g2->GetAdjacentVertices(id3Gts,it3Gts);
262               QString toolTipName2(name2);
263               while(it3Gts->HasNext())
264                 {
265                   vtkIdType idGt(it3Gts->Next());
266                   std::string gtName((const char *)verticesNames2->GetValue(idGt));
267                   toolTipName2=QString("%1\n- %2").arg(toolTipName2).arg(QString(gtName.c_str()));
268                 }
269               item2->setData(0,Qt::ToolTipRole,toolTipName2);
270               it3Gts->Delete();
271               it3->Delete();
272               it3Arrs->Delete();
273               if(kk==0)
274                 item2->setChecked(true);
275               kk++;
276             }
277           it2->Delete();
278         }
279       it1->Delete();
280     }
281   it0->Delete();
282   this->UI->Fields->header()->setStretchLastSection(true);
283   this->UI->Fields->expandAll();
284   info->Delete();
285   this->UI->stdMode->setChecked(true);
286   this->UI->VTKConnect->Connect(this->proxy(),vtkCommand::UpdateInformationEvent, this, SLOT(updateSIL()));
287   ///
288   this->UI->Reload->setProperty("NbOfReloadDynProp",QVariant(1));
289   vtkSMProperty *SMProperty(this->proxy()->GetProperty("ReloadReq"));
290   connect(this->UI->Reload,SIGNAL(pressed()),this,SLOT(reloadFired()));
291   this->propertyManager()->registerLink(this->UI->Reload,"NbOfReloadDynProp",SIGNAL(pressed()),this->proxy(),SMProperty);
292   ///
293   vtkSMProperty *SMProperty0(this->proxy()->GetProperty("GenerateVectors"));
294   this->propertyManager()->registerLink(this->UI->GenerateVects,"checked",SIGNAL(stateChanged(int)),this->proxy(),SMProperty0);
295   ///
296   vtkSMProperty *SMProperty2(this->proxy()->GetProperty("TimeOrModal"));
297   SMProperty2->ResetToDefault();//this line is very important !
298   this->propertyManager()->registerLink(this->UI->modeMode,"checked",SIGNAL(toggled(bool)),this->proxy(),SMProperty2);
299   ///
300   delete _optional_widget;
301   _optional_widget=new VectBoolWidget(this->UI->timeStepsInspector,getMaxNumberOfTS());
302   _optional_widget->hide();
303   this->UI->timeStepsInspector->setMinimumSize(QSize(0,0));
304   connect(this->UI->modeMode,SIGNAL(toggled(bool)),this,SLOT(vectOfBoolWidgetRequested(bool)));
305   vtkSMProperty *SMProperty3(this->proxy()->GetProperty("TimesFlagsStatus"));
306   SMProperty3->ResetToDefault();
307   const QVector<VectBoolItem *>& items(_optional_widget->getItems());
308   int itt(0);
309   foreach(VectBoolItem *item,items)
310     {
311       this->propertyManager()->registerLink(item,"activated",SIGNAL(changed()),this->proxy(),SMProperty3,itt++);
312     }
313 }
314
315 pqMEDReaderPanel::~pqMEDReaderPanel()
316 {
317   delete _optional_widget;
318 }
319
320 void pqMEDReaderPanel::linkServerManagerProperties()
321 {
322   this->Superclass::linkServerManagerProperties();
323 }
324
325 void pqMEDReaderPanel::updateSIL()
326 {
327   if(_reload_req)
328     {
329       _reload_req=false;
330       this->UI->geometryGroupBox->hide();
331       delete this->UI;
332       foreach(QObject *child,children())
333         {
334           QLayout *layout(qobject_cast<QLayout *>(child));
335           if(layout)
336             delete layout;
337         }
338       initAll();
339     }
340 }
341
342 void pqMEDReaderPanel::aLev4HasBeenFired()
343 {
344   pqTreeWidgetItemObject *zeItem(qobject_cast<pqTreeWidgetItemObject *>(sender()));
345   if(!zeItem)
346     return;
347   pqTreeWidgetItemObject *father(dynamic_cast<pqTreeWidgetItemObject *>(zeItem->QTreeWidgetItem::parent()));
348   QTreeWidgetItem *godFather(father->QTreeWidgetItem::parent()->parent());
349   if(!father)
350     return ;
351   if(zeItem->isChecked())
352     {
353       bool isActivatedTSChanged(false);
354       // This part garantees that all leaves having not the same father than zeItem are desactivated
355       foreach(pqTreeWidgetItemObject* elt,this->_all_lev4)
356         {
357           QTreeWidgetItem *testFath(elt->QTreeWidgetItem::parent());
358           if(testFath!=father)
359             if(elt->isChecked())
360               {
361                 elt->setChecked(false);
362                 if(godFather!=testFath->parent()->parent())
363                   isActivatedTSChanged=true;
364               }
365         }
366       //If all leaves are checked the father is check too
367       bool allItemsAreChked(true);
368       for(int i=0;i<father->childCount() && allItemsAreChked;i++)
369         {
370           pqTreeWidgetItemObject *elt(dynamic_cast<pqTreeWidgetItemObject *>(father->child(i)));
371           if(elt && !elt->isChecked())
372             allItemsAreChked=false;
373         }
374       if(allItemsAreChked && !father->isChecked())
375         father->setChecked(true);
376       // the user by clicking to a new entry has changed of TimeStepSeries -> notify it to thee time step selector widget
377       if(isActivatedTSChanged)
378         {
379           QStringList its,dts,tts;
380           getCurrentTS(its,dts,tts);
381           _optional_widget->setItems(its,dts,tts);
382         }
383     }
384   else
385     {
386       // if all are unchecked - check it again
387       bool allItemsAreUnChked(true);
388       foreach(pqTreeWidgetItemObject* elt,this->_all_lev4)
389         {
390           if(elt && elt->isChecked())
391             allItemsAreUnChked=false;
392         }
393       if(allItemsAreUnChked)
394         zeItem->setChecked(true);// OK zeItem was required to be unchecked but as it is the last one. Recheck it !
395       else
396         {// if all items are uncheked inside a same parent - uncheck the parent
397           allItemsAreUnChked=true;
398           for(int i=0;i<father->childCount() && allItemsAreUnChked;i++)
399             {
400               pqTreeWidgetItemObject *elt(dynamic_cast<pqTreeWidgetItemObject *>(father->child(i)));
401               if(elt && elt->isChecked())
402             allItemsAreUnChked=false;
403             }
404           if(allItemsAreUnChked && father->isChecked())
405             father->setChecked(false);
406         } 
407     }
408 }
409
410 void pqMEDReaderPanel::reloadFired()
411 {
412   static int iii(1);
413   QVariant v(iii++);
414   this->UI->Reload->setProperty("NbOfReloadDynProp",v);
415   _reload_req=true;
416   for(std::set<std::pair<pqTreeWidgetItemObject *,int> >::const_iterator it=_leaves.begin();it!=_leaves.end();it++)
417     ((*it).first)->disconnect(SIGNAL(checkedStateChanged(bool)));
418   //
419   vtkSMProperty *SMProperty(this->proxy()->GetProperty("FieldsStatus"));
420   for(std::set<std::pair<pqTreeWidgetItemObject *,int> >::const_iterator it=_leaves.begin();it!=_leaves.end();it++)
421     this->propertyManager()->unregisterLink((*it).first,"checked",SIGNAL(checkedStateChanged(bool)),this->proxy(),SMProperty,(*it).second);
422   this->propertyManager()->propertyChanged();
423   vtkSMProperty *SMProperty3(this->proxy()->GetProperty("TimeOrModal"));
424   this->propertyManager()->unregisterLink(this->UI->modeMode,"checked",SIGNAL(toggled(bool)),this->proxy(),SMProperty3);
425 }
426
427 void pqMEDReaderPanel::vectOfBoolWidgetRequested(bool isMode)
428 {
429   if(isMode)
430     {
431       this->UI->timeStepsInspector->setMinimumSize(QSize(200,250));
432       _optional_widget->show();
433       QStringList its,dts,tts;
434       getCurrentTS(its,dts,tts);
435       _optional_widget->setItems(its,dts,tts);
436     }
437   else
438     {
439       _optional_widget->hide();
440       this->UI->timeStepsInspector->setMinimumSize(QSize(0,0));
441     }
442 }
443
444 void pqMEDReaderPanel::getCurrentTS(QStringList& its, QStringList& dts, QStringList& tts) const
445 {
446   
447   for(std::vector<pqTreeWidgetItemObject *>::const_iterator it=_all_lev4.begin();it!=_all_lev4.end();it++)
448     {
449       if((*it)->property("checked").toInt())
450         {
451           QTreeWidgetItem *obj((*it)->QTreeWidgetItem::parent()->QTreeWidgetItem::parent()->QTreeWidgetItem::parent());
452           pqTreeWidgetItemObject *objC(dynamic_cast<pqTreeWidgetItemObject *>(obj));
453           its=objC->property("ITS").toStringList();
454           dts=objC->property("DTS").toStringList();
455           tts=objC->property("TTS").toStringList();
456           return;
457         }
458     }
459   std::cerr << "pqMEDReaderPanel::getCurrentTS : internal error ! Something is going wrong !" << std::endl;
460 }
461
462 int pqMEDReaderPanel::getMaxNumberOfTS() const
463 {
464   int ret(0);
465   for(std::vector<pqTreeWidgetItemObject *>::const_iterator it=_all_lev4.begin();it!=_all_lev4.end();it++)
466     {
467       QTreeWidgetItem *obj((*it)->QTreeWidgetItem::parent()->QTreeWidgetItem::parent()->QTreeWidgetItem::parent());
468       pqTreeWidgetItemObject *objC(dynamic_cast<pqTreeWidgetItemObject *>(obj));
469       ret=std::max(ret,objC->property("NbOfTS").toInt());
470     }
471   return ret;
472 }