Salome HOME
0022921: [CEA 1438] Remove the checkbox in the "time steps"
[modules/paravis.git] / src / Plugins / MEDReader / ParaViewPlugin / pqExtractGroupPanel.cxx
1 // Copyright (C) 2010-2015  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 "pqExtractGroupPanel.h"
22 #include "ui_ExtractGroupPanel.h"
23
24 #include "vtkProcessModule.h"
25 #include "vtkMultiBlockDataSet.h"
26 #include "vtkInformation.h"
27 #include "vtkIntArray.h"
28 #include "vtkSMDoubleVectorProperty.h"
29 #include "vtkSMIntVectorProperty.h"
30 #include "vtkSMStringVectorProperty.h"
31 #include "vtkSMProxy.h"
32 #include "vtkSMSourceProxy.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 "vtkExtractGroup.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 #include "pqPropertyLinks.h"
52
53 #include <QHeaderView>
54
55 #include <sstream>
56
57 static const char ZE_SEP[]="@@][@@";
58
59 class PixSingleExtractPanel
60 {
61 public:
62   static const PixSingleExtractPanel &GetInstance();
63   QPixmap getPixFromStr(int pos) const;
64   PixSingleExtractPanel();
65 private:
66   static const int NB_OF_DISCR=4;
67   static PixSingleExtractPanel *UNIQUE_INSTANCE;
68   QPixmap _pixmaps[NB_OF_DISCR];
69 };
70
71 PixSingleExtractPanel *PixSingleExtractPanel::UNIQUE_INSTANCE=0;
72
73 const PixSingleExtractPanel &PixSingleExtractPanel::GetInstance()
74 {
75   if(!UNIQUE_INSTANCE)
76     UNIQUE_INSTANCE=new PixSingleExtractPanel;
77   return *UNIQUE_INSTANCE;
78 }
79
80 PixSingleExtractPanel::PixSingleExtractPanel()
81 {
82   _pixmaps[0]=QPixmap(":/ParaViewResources/Icons/pqCellData16.png");
83   _pixmaps[1]=QPixmap(":/ParaViewResources/Icons/pqPointData16.png");
84 }
85
86 QPixmap PixSingleExtractPanel::getPixFromStr(int pos) const
87 {
88   if(pos>=0 && pos<=1)
89     return _pixmaps[pos];
90   else
91     return QPixmap();
92 }
93
94 class pqExtractGroupPanel::pqUI: public QObject, public Ui::ExtractGroupPanel
95 {
96 public:
97   pqUI(pqExtractGroupPanel* p):QObject(p)
98   {
99     this->VTKConnect = vtkSmartPointer<vtkEventQtSlotConnect>::New();
100     this->SILUpdateStamp = -1;
101   }
102
103   ~pqUI() { }
104
105   pqSILModel SILModel;
106   vtkSmartPointer<vtkEventQtSlotConnect> VTKConnect;
107   pqPropertyLinks Links;
108   QMap<QTreeWidgetItem*, QString> TreeItemToPropMap;
109   int SILUpdateStamp;
110 };
111
112 pqExtractGroupPanel::pqExtractGroupPanel(pqProxy* object_proxy, QWidget* p):Superclass(object_proxy, p)
113 {
114   this->UI=new pqUI(this);
115   this->UI->setupUi(this);
116   pqProxySILModel*proxyModel2 = new pqProxySILModel("GroupsFlagsStatusTree", &this->UI->SILModel);
117   proxyModel2->setSourceModel(&this->UI->SILModel);
118   this->UI->Fields->setHeaderHidden(true);
119   this->updateSIL();
120   this->linkServerManagerProperties();
121   ////////////////////
122   vtkSMProperty *SMProperty(this->proxy()->GetProperty("GroupsFlagsStatus"));
123   ////////////////////
124   vtkSMProxy* reader = this->referenceProxy()->getProxy();
125   vtkPVSILInformation* info=vtkPVSILInformation::New();
126   reader->GatherInformation(info);
127   vtkGraph *g(info->GetSIL());
128   //vtkMutableDirectedGraph *g2(vtkMutableDirectedGraph::SafeDownCast(g));// agy: this line does not work in client/server mode ! but it works in standard mode ! Don't know why. ParaView bug ?
129   vtkMutableDirectedGraph *g2(static_cast<vtkMutableDirectedGraph *>(g));
130   int idNames(0);
131   if(!g2)
132     return ;
133   vtkAbstractArray *verticesNames(g2->GetVertexData()->GetAbstractArray("Names",idNames));
134   vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
135   vtkIdType id0;
136   bool found(false);
137   for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
138     {
139       vtkStdString &st(verticesNames2->GetValue(i));
140       if(st=="MeshesFamsGrps")
141         {
142           id0=i;
143           found=true;
144         }
145     }
146   if(!found)
147     std::cerr << "There is an internal error ! The tree on server side has not the expected look !" << std::endl;
148   vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
149   g2->GetAdjacentVertices(id0,it0);
150   int kk(0),ll(0);
151   while(it0->HasNext())
152     {
153       vtkIdType id1(it0->Next());
154       QString meshName(QString::fromStdString((const char *)verticesNames2->GetValue(id1)));
155       vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());
156       g2->GetAdjacentVertices(id1,it1);
157       vtkIdType idZeGrps(it1->Next());//zeGroups
158       QList<QString> strs0; strs0.append(QString("Groups of \"%1\"").arg(meshName));
159       pqTreeWidgetItemObject *item0(new pqTreeWidgetItemObject(this->UI->Fields,strs0));
160       std::map<std::string,int> famIds(DeduceMapOfFamilyFromSIL(g2));
161       //item0->setData(0,Qt::CheckStateRole,0);
162       vtkAdjacentVertexIterator *itGrps(vtkAdjacentVertexIterator::New());
163       g2->GetAdjacentVertices(idZeGrps,itGrps);
164       while(itGrps->HasNext())
165         {
166           vtkIdType idg(itGrps->Next());
167           QString name0(QString::fromStdString((const char *)verticesNames2->GetValue(idg))); QList<QString> strs0; strs0.append(name0);
168           QString toolTipName0(name0);
169           pqTreeWidgetItemObject *item1(new pqTreeWidgetItemObject(item0,strs0));
170           //
171           vtkAdjacentVertexIterator *itFamsOnGrp(vtkAdjacentVertexIterator::New());
172           g2->GetAdjacentVertices(idg,itFamsOnGrp);
173           bool isOnCell(true),isOnPoint(true);
174           while(itFamsOnGrp->HasNext())
175             {
176               vtkIdType idfg(itFamsOnGrp->Next());
177               std::string namefg((const char *)verticesNames2->GetValue(idfg));
178               std::map<std::string,int>::const_iterator ittt(famIds.find(namefg));
179               if(ittt==famIds.end())
180                 { isOnCell=false; isOnPoint=false; break; }
181               int zeId((*ittt).second);
182               if(zeId<0)
183                 {
184                   if(!isOnCell)
185                     { isOnCell=false; isOnPoint=false; break; }
186                   else
187                     isOnPoint=false;
188                 }
189               if(zeId>0)
190                 {
191                   if(!isOnPoint)
192                     { isOnCell=false; isOnPoint=false; break; }
193                   else
194                     isOnCell=false;
195                 }
196             }
197           itFamsOnGrp->Delete();
198           item1->setData(0,Qt::UserRole,name0);
199           item1->setData(0,Qt::ToolTipRole,toolTipName0);
200           item1->setData(0,Qt::CheckStateRole,0);
201           if(isOnCell && !isOnPoint)
202             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(0));
203           if(!isOnCell && isOnPoint)
204             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(1));
205           item1->setProperty("PosInStringVector",QVariant(ll++));
206           connect(item1,SIGNAL(checkedStateChanged(bool)),this,SLOT(anItemAsBeenFired()));
207         }
208       itGrps->Delete();
209       // families 
210       vtkIdType idZeFams(it1->Next());//zeFams
211       strs0.clear(); strs0.append(QString("Families of \"%1\"").arg(meshName));
212       pqTreeWidgetItemObject *item00(new pqTreeWidgetItemObject(this->UI->Fields,strs0));
213       //item00->setData(0,Qt::CheckStateRole,0);
214       vtkAdjacentVertexIterator *itFams(vtkAdjacentVertexIterator::New());
215       g2->GetAdjacentVertices(idZeFams,itFams);
216       while(itFams->HasNext())
217         {
218           vtkIdType idf(itFams->Next());
219           std::string crudeFamName((const char *)verticesNames2->GetValue(idf));
220           std::size_t pos(crudeFamName.find_first_of(ZE_SEP));
221           std::string famName(crudeFamName.substr(0,pos)); std::string idStr(crudeFamName.substr(pos+strlen(ZE_SEP)));
222           int idInt(QString(idStr.c_str()).toInt());
223           famIds[famName]=idInt;
224           QString name0(famName.c_str()); QList<QString> strs0; strs0.append(name0);
225           QString toolTipName0(QString("%1 (%2)").arg(QString(famName.c_str())).arg(QString(idStr.c_str())));
226           pqTreeWidgetItemObject *item1(new pqTreeWidgetItemObject(item00,strs0));
227           item1->setData(0,Qt::UserRole,name0);
228           item1->setData(0,Qt::ToolTipRole,toolTipName0);
229           item1->setData(0,Qt::CheckStateRole,0);
230           if(idInt<0)
231             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(0));
232           if(idInt>0)
233             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(1));
234           item1->setProperty("PosInStringVector",QVariant(ll++));
235           connect(item1,SIGNAL(checkedStateChanged(bool)),this,SLOT(anItemAsBeenFired()));
236         }
237       itFams->Delete();
238     }
239   it0->Delete(); 
240   this->UI->Fields->header()->setStretchLastSection(true);
241   this->UI->Fields->expandAll();
242   info->Delete();
243   ////////////////////
244   vtkSMProperty *SMPropertyExtractComp(this->proxy()->GetProperty("InsideOut"));
245   this->propertyManager()->registerLink(this->UI->ExtractComplementary,"checked",SIGNAL(stateChanged(int)),this->proxy(),SMPropertyExtractComp);
246   ////////////////////
247   this->UI->VTKConnect->Connect(this->proxy(),vtkCommand::UpdateInformationEvent, this, SLOT(updateSIL()));
248 }
249
250 pqExtractGroupPanel::~pqExtractGroupPanel()
251 {
252 }
253
254 void pqExtractGroupPanel::linkServerManagerProperties()
255 {
256   this->Superclass::linkServerManagerProperties();
257 }
258
259 void pqExtractGroupPanel::updateSIL()
260 {
261   vtkSMProxy* reader = this->referenceProxy()->getProxy();
262   reader->UpdatePropertyInformation(reader->GetProperty("SILUpdateStamp"));
263   int stamp = vtkSMPropertyHelper(reader, "SILUpdateStamp").GetAsInt();
264   if (stamp != this->UI->SILUpdateStamp)
265     {
266     this->UI->SILUpdateStamp = stamp;
267     vtkPVSILInformation* info = vtkPVSILInformation::New();
268     reader->GatherInformation(info);
269     vtkGraph *sil(info->GetSIL());
270     if(sil)
271       {
272         this->UI->SILModel.update(sil);
273         this->UI->Fields->expandAll();
274         info->Delete();
275       }
276     }
277 }
278
279 void pqExtractGroupPanel::anItemAsBeenFired()
280 {
281   ///
282   vtkSMProxy *proxy(this->proxy());
283   vtkSMProperty *SMProperty(proxy->GetProperty("GroupsFlagsStatus"));
284   vtkSMStringVectorProperty *sm(dynamic_cast<vtkSMStringVectorProperty *>(SMProperty));
285   unsigned int nb(sm->GetNumberOfElements());
286   std::vector<std::string> sts(nb);
287   for(unsigned int i=0;i<nb;i++)
288     sts[i]=sm->GetElement(i);
289   ///
290   pqTreeWidget *sc(this->UI->Fields);
291   for(int i0=0;i0<sc->topLevelItemCount();i0++)
292     {
293       QTreeWidgetItem *lev0(sc->topLevelItem(i0));//Group and Fam
294       for(int i1=0;i1<lev0->childCount();i1++)
295         {
296           QTreeWidgetItem *lev1(lev0->child(i1));
297           pqTreeWidgetItemObject *scc(dynamic_cast<pqTreeWidgetItemObject *>(lev1));
298           int ll(scc->property("PosInStringVector").toInt());
299           int v(scc->isChecked());
300           std::ostringstream oss; oss << v;
301           sts[2*ll+1]=oss.str();
302         }
303     }
304   ///
305   const char **args=new const char *[nb];
306   for(unsigned int i=0;i<nb;i++)
307     {
308       args[i]=sts[i].c_str();
309     }
310   {
311     int iup(sm->GetImmediateUpdate());
312     //sm->SetNumberOfElements(0);
313     sm->SetElements(args,nb);
314     proxy->UpdateVTKObjects();
315     sm->SetImmediateUpdate(iup);
316   }
317   delete [] args;
318   //
319   ((vtkSMSourceProxy *)proxy)->UpdatePipelineInformation();
320   setModified();
321 }
322
323 std::map<std::string,int> pqExtractGroupPanel::DeduceMapOfFamilyFromSIL(vtkMutableDirectedGraph *graph)
324 {
325   std::map<std::string,int> ret;
326   int idNames(0);
327   vtkAbstractArray *verticesNames(graph->GetVertexData()->GetAbstractArray("Names",idNames));
328   vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
329   vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
330   vtkIdType id0;
331   bool found(false);
332   for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
333     {
334       vtkStdString &st(verticesNames2->GetValue(i));
335       if(st=="MeshesFamsGrps")
336         {
337           id0=i;
338           found=true;
339         }
340     }
341   if(!found)
342     std::cerr << "There is an internal error ! The tree on server side has not the expected look !" << std::endl;
343   graph->GetAdjacentVertices(id0,it0);
344   while(it0->HasNext())
345     {
346       vtkIdType id1(it0->Next());//meshName
347       vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());
348       graph->GetAdjacentVertices(id1,it1);
349       it1->Next();//zeGroups
350       vtkIdType idZeFams(it1->Next());//zeFams
351       vtkAdjacentVertexIterator *itFams(vtkAdjacentVertexIterator::New());
352       graph->GetAdjacentVertices(idZeFams,itFams);
353       while(itFams->HasNext())
354         {
355           vtkIdType idf(itFams->Next());
356           std::string crudeFamName((const char *)verticesNames2->GetValue(idf));
357           std::size_t pos(crudeFamName.find_first_of(ZE_SEP));
358           std::string famName(crudeFamName.substr(0,pos)); std::string idStr(crudeFamName.substr(pos+strlen(ZE_SEP)));
359           int idInt(QString(idStr.c_str()).toInt());
360           ret[famName]=idInt;
361         }
362       it1->Delete();
363     }
364   it0->Delete();
365   return ret;
366 }
367
368 void pqExtractGroupPanel::updateInformationAndDomains()
369 {
370   pqNamedObjectPanel::updateInformationAndDomains();
371   vtkSMProxy *proxy(this->proxy());
372   vtkSMProperty *SMProperty(proxy->GetProperty("GroupsFlagsStatus"));
373   SMProperty->Modified();// agy : THE LINE FOR 7.5.1 !
374 }