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