1 // Copyright (C) 2010-2014 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay
21 #include "pqMEDReaderPanel.h"
22 #include "ui_MEDReaderPanel.h"
23 #include "VectBoolWidget.h"
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"
36 #include "vtkMutableDirectedGraph.h"
37 #include "vtkAdjacentVertexIterator.h"
38 #include "vtkSMPropertyHelper.h"
39 #include "vtkStringArray.h"
40 #include "vtkDataSetAttributes.h"
41 #include "vtkMEDReader.h"
43 #include "pqTreeWidgetItemObject.h"
44 #include "pqSMAdaptor.h"
46 #include "pqPropertyManager.h"
47 #include "pqSILModel.h"
48 #include "pqProxySILModel.h"
49 #include "pqTreeViewSelectionHelper.h"
50 #include "pqTreeWidgetSelectionHelper.h"
52 #include <QHeaderView>
56 static const char ZE_SEP[]="@@][@@";
61 static const PixSingle &GetInstance();
62 QPixmap getPixFromStr(const std::string& st) const;
63 QPixmap getWholeMeshPix() const;
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;
73 PixSingle *PixSingle::UNIQUE_PIX_SINGLE=0;
75 const PixSingle &PixSingle::GetInstance()
77 if(!UNIQUE_PIX_SINGLE)
78 UNIQUE_PIX_SINGLE=new PixSingle;
79 return *UNIQUE_PIX_SINGLE;
82 PixSingle::PixSingle()
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");
95 QPixmap PixSingle::getPixFromStr(const std::string& st) const
97 std::map<std::string,int>::const_iterator it(_ze_map.find(st));
99 return _pixmaps[(*it).second];
104 QPixmap PixSingle::getWholeMeshPix() const
109 class pqMEDReaderPanel::pqUI: public QObject, public Ui::MEDReaderPanel
112 pqUI(pqMEDReaderPanel *p):QObject(p)
114 this->VTKConnect = vtkSmartPointer<vtkEventQtSlotConnect>::New();
119 vtkSmartPointer<vtkEventQtSlotConnect> VTKConnect;
120 QMap<QTreeWidgetItem*, QString> TreeItemToPropMap;
123 pqMEDReaderPanel::pqMEDReaderPanel(pqProxy *object_proxy, QWidget *p):Superclass(object_proxy,p),_reload_req(false),_optional_widget(0)
128 void pqMEDReaderPanel::initAll()
131 this->UI=new pqUI(this);
132 this->UI->setupUi(this);
133 this->UI->Fields->setHeaderHidden(true);
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...
142 vtkMutableDirectedGraph *g2(vtkMutableDirectedGraph::SafeDownCast(g));
144 vtkAbstractArray *verticesNames(g2->GetVertexData()->GetAbstractArray("Names",idNames));
145 vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
148 for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
150 vtkStdString &st(verticesNames2->GetValue(i));
151 if(st=="FieldsStatusTree")
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);
162 while(it0->HasNext())
164 vtkIdType idToolTipForTS(it0->Next());
165 QString toolTipName0(QString::fromStdString((const char *)verticesNames2->GetValue(idToolTipForTS)));
167 QList<QString> dts,its,tts;
169 vtkAdjacentVertexIterator *itForTS(vtkAdjacentVertexIterator::New());
170 g2->GetAdjacentVertices(idToolTipForTS,itForTS);
171 vtkIdType idForNbTS(itForTS->Next());
172 nbTS=QString::fromStdString((const char *)verticesNames2->GetValue(idForNbTS));
174 int nbTSInt(nbTS.toInt());
175 for(int ii=0;ii<nbTSInt;ii++)
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)));
182 vtkIdType id1(it0->Next());
184 vtkSMProperty *SMProperty(this->proxy()->GetProperty("FieldsStatus"));
185 SMProperty->ResetToDefault();//this line is very important !
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);
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));
201 vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());//mesh
202 g2->GetAdjacentVertices(id1,it1);
203 while(it1->HasNext())
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())
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())
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(")"));
234 vtkAdjacentVertexIterator *it4(vtkAdjacentVertexIterator::New());// is it a special field ? A field mesh ?
235 g2->GetAdjacentVertices(id4,it4);
236 bool isSpecial(it4->HasNext());
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);
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());
252 item3->setData(0,Qt::ToolTipRole,toolTipName3);
253 item3->setData(0,Qt::DecorationRole,PixSingle::GetInstance().getPixFromStr(spatialDiscr));
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()));
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())
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()));
279 item2->setData(0,Qt::ToolTipRole,toolTipName2);
290 this->UI->Fields->header()->setStretchLastSection(true);
291 this->UI->Fields->expandAll();
293 this->UI->stdMode->setChecked(true);
294 this->UI->VTKConnect->Connect(this->proxy(),vtkCommand::UpdateInformationEvent, this, SLOT(updateSIL()));
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);
301 vtkSMProperty *SMProperty0(this->proxy()->GetProperty("GenerateVectors"));
302 this->propertyManager()->registerLink(this->UI->GenerateVects,"checked",SIGNAL(stateChanged(int)),this->proxy(),SMProperty0);
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);
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());
317 foreach(VectBoolItem *item,items)
319 this->propertyManager()->registerLink(item,"activated",SIGNAL(changed()),this->proxy(),SMProperty3,itt++);
323 pqMEDReaderPanel::~pqMEDReaderPanel()
325 delete _optional_widget;
328 void pqMEDReaderPanel::linkServerManagerProperties()
330 this->Superclass::linkServerManagerProperties();
333 void pqMEDReaderPanel::updateSIL()
338 this->UI->geometryGroupBox->hide();
340 foreach(QObject *child,children())
342 QLayout *layout(qobject_cast<QLayout *>(child));
350 void pqMEDReaderPanel::aLev4HasBeenFired()
352 pqTreeWidgetItemObject *zeItem(qobject_cast<pqTreeWidgetItemObject *>(sender()));
355 pqTreeWidgetItemObject *father(dynamic_cast<pqTreeWidgetItemObject *>(zeItem->QTreeWidgetItem::parent()));
356 QTreeWidgetItem *godFather(father->QTreeWidgetItem::parent()->parent());
359 if(zeItem->isChecked())
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)
365 QTreeWidgetItem *testFath(elt->QTreeWidgetItem::parent());
369 elt->setChecked(false);
370 if(godFather!=testFath->parent()->parent())
371 isActivatedTSChanged=true;
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++)
378 pqTreeWidgetItemObject *elt(dynamic_cast<pqTreeWidgetItemObject *>(father->child(i)));
379 if(elt && !elt->isChecked())
380 allItemsAreChked=false;
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)
387 QStringList its,dts,tts;
388 getCurrentTS(its,dts,tts);
389 _optional_widget->setItems(its,dts,tts);
394 // if all are unchecked - check it again
395 bool allItemsAreUnChked(true);
396 foreach(pqTreeWidgetItemObject* elt,this->_all_lev4)
398 if(elt && elt->isChecked())
399 allItemsAreUnChked=false;
401 if(allItemsAreUnChked)
402 zeItem->setChecked(true);// OK zeItem was required to be unchecked but as it is the last one. Recheck it !
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++)
408 pqTreeWidgetItemObject *elt(dynamic_cast<pqTreeWidgetItemObject *>(father->child(i)));
409 if(elt && elt->isChecked())
410 allItemsAreUnChked=false;
412 if(allItemsAreUnChked && father->isChecked())
413 father->setChecked(false);
418 void pqMEDReaderPanel::reloadFired()
422 this->UI->Reload->setProperty("NbOfReloadDynProp",v);
424 for(std::set<std::pair<pqTreeWidgetItemObject *,int> >::const_iterator it=_leaves.begin();it!=_leaves.end();it++)
425 ((*it).first)->disconnect(SIGNAL(checkedStateChanged(bool)));
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);
435 void pqMEDReaderPanel::vectOfBoolWidgetRequested(bool isMode)
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);
447 _optional_widget->hide();
448 this->UI->timeStepsInspector->setMinimumSize(QSize(0,0));
452 void pqMEDReaderPanel::getCurrentTS(QStringList& its, QStringList& dts, QStringList& tts) const
455 for(std::vector<pqTreeWidgetItemObject *>::const_iterator it=_all_lev4.begin();it!=_all_lev4.end();it++)
457 if((*it)->property("checked").toInt())
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();
467 std::cerr << "pqMEDReaderPanel::getCurrentTS : internal error ! Something is going wrong !" << std::endl;
470 int pqMEDReaderPanel::getMaxNumberOfTS() const
473 for(std::vector<pqTreeWidgetItemObject *>::const_iterator it=_all_lev4.begin();it!=_all_lev4.end();it++)
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());