Salome HOME
Merge branch 'abn/port_pv42'
[modules/paravis.git] / src / Plugins / MEDReader / ParaViewPlugin / pqExtractGroupPanel.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 "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 "vtkEventQtSlotConnect.h"
33 #include "vtkPVSILInformation.h"
34 #include "vtkGraph.h"
35 #include "vtkMutableDirectedGraph.h"
36 #include "vtkAdjacentVertexIterator.h"
37 #include "vtkSMPropertyHelper.h"
38 #include "vtkStringArray.h"
39 #include "vtkDataSetAttributes.h"
40 #include "vtkExtractGroup.h"
41
42 #include "pqTreeWidgetItemObject.h"
43 #include "pqSMAdaptor.h"
44 #include "pqProxy.h"
45 #include "pqPropertyManager.h"
46 #include "pqSILModel.h"
47 #include "pqProxySILModel.h"
48 #include "pqTreeViewSelectionHelper.h"
49 #include "pqTreeWidgetSelectionHelper.h"
50 #include "pqPropertyLinks.h"
51
52 #include <QHeaderView>
53
54 static const char ZE_SEP[]="@@][@@";
55
56 class PixSingleExtractPanel
57 {
58 public:
59   static const PixSingleExtractPanel &GetInstance();
60   QPixmap getPixFromStr(int pos) const;
61   PixSingleExtractPanel();
62 private:
63   static const int NB_OF_DISCR=4;
64   static PixSingleExtractPanel *UNIQUE_INSTANCE;
65   QPixmap _pixmaps[NB_OF_DISCR];
66 };
67
68 PixSingleExtractPanel *PixSingleExtractPanel::UNIQUE_INSTANCE=0;
69
70 const PixSingleExtractPanel &PixSingleExtractPanel::GetInstance()
71 {
72   if(!UNIQUE_INSTANCE)
73     UNIQUE_INSTANCE=new PixSingleExtractPanel;
74   return *UNIQUE_INSTANCE;
75 }
76
77 PixSingleExtractPanel::PixSingleExtractPanel()
78 {
79   _pixmaps[0]=QPixmap(":/ParaViewResources/Icons/pqCellData16.png");
80   _pixmaps[1]=QPixmap(":/ParaViewResources/Icons/pqPointData16.png");
81 }
82
83 QPixmap PixSingleExtractPanel::getPixFromStr(int pos) const
84 {
85   if(pos>=0 && pos<=1)
86     return _pixmaps[pos];
87   else
88     return QPixmap();
89 }
90
91 class pqExtractGroupPanel::pqUI: public QObject, public Ui::ExtractGroupPanel
92 {
93 public:
94   pqUI(pqExtractGroupPanel* p):QObject(p)
95   {
96     this->VTKConnect = vtkSmartPointer<vtkEventQtSlotConnect>::New();
97     this->SILUpdateStamp = -1;
98   }
99
100   ~pqUI() { }
101
102   pqSILModel SILModel;
103   vtkSmartPointer<vtkEventQtSlotConnect> VTKConnect;
104   pqPropertyLinks Links;
105   QMap<QTreeWidgetItem*, QString> TreeItemToPropMap;
106   int SILUpdateStamp;
107 };
108
109 pqExtractGroupPanel::pqExtractGroupPanel(pqProxy* object_proxy, QWidget* p):Superclass(object_proxy, p)
110 {
111   this->UI=new pqUI(this);
112   this->UI->setupUi(this);
113   pqProxySILModel*proxyModel2 = new pqProxySILModel("GroupsFlagsStatusTree", &this->UI->SILModel);
114   proxyModel2->setSourceModel(&this->UI->SILModel);
115   this->UI->Fields->setHeaderHidden(true);
116   this->updateSIL();
117   this->linkServerManagerProperties();
118   ////////////////////
119   vtkSMProperty *SMProperty(this->proxy()->GetProperty("GroupsFlagsStatus"));
120   ////////////////////
121   vtkSMProxy* reader = this->referenceProxy()->getProxy();
122   vtkPVSILInformation* info=vtkPVSILInformation::New();
123   reader->GatherInformation(info);
124   vtkGraph *g(info->GetSIL());
125   //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 ?
126   vtkMutableDirectedGraph *g2(static_cast<vtkMutableDirectedGraph *>(g));
127   int idNames(0);
128   if(!g2)
129     return ;
130   vtkAbstractArray *verticesNames(g2->GetVertexData()->GetAbstractArray("Names",idNames));
131   vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
132   vtkIdType id0;
133   bool found(false);
134   for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
135     {
136       vtkStdString &st(verticesNames2->GetValue(i));
137       if(st=="MeshesFamsGrps")
138         {
139           id0=i;
140           found=true;
141         }
142     }
143   if(!found)
144     std::cerr << "There is an internal error ! The tree on server side has not the expected look !" << std::endl;
145   vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
146   g2->GetAdjacentVertices(id0,it0);
147   int kk(0),ll(0);
148   while(it0->HasNext())
149     {
150       vtkIdType id1(it0->Next());
151       QString meshName(QString::fromStdString((const char *)verticesNames2->GetValue(id1)));
152       vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());
153       g2->GetAdjacentVertices(id1,it1);
154       vtkIdType idZeGrps(it1->Next());//zeGroups
155       QList<QString> strs0; strs0.append(QString("Groups of \"%1\"").arg(meshName));
156       pqTreeWidgetItemObject *item0(new pqTreeWidgetItemObject(this->UI->Fields,strs0));
157       std::map<std::string,int> famIds(DeduceMapOfFamilyFromSIL(g2));
158       //item0->setData(0,Qt::CheckStateRole,0);
159       vtkAdjacentVertexIterator *itGrps(vtkAdjacentVertexIterator::New());
160       g2->GetAdjacentVertices(idZeGrps,itGrps);
161       while(itGrps->HasNext())
162         {
163           vtkIdType idg(itGrps->Next());
164           QString name0(QString::fromStdString((const char *)verticesNames2->GetValue(idg))); QList<QString> strs0; strs0.append(name0);
165           QString toolTipName0(name0);
166           pqTreeWidgetItemObject *item1(new pqTreeWidgetItemObject(item0,strs0));
167           //
168           vtkAdjacentVertexIterator *itFamsOnGrp(vtkAdjacentVertexIterator::New());
169           g2->GetAdjacentVertices(idg,itFamsOnGrp);
170           bool isOnCell(true),isOnPoint(true);
171           while(itFamsOnGrp->HasNext())
172             {
173               vtkIdType idfg(itFamsOnGrp->Next());
174               std::string namefg((const char *)verticesNames2->GetValue(idfg));
175               std::map<std::string,int>::const_iterator ittt(famIds.find(namefg));
176               if(ittt==famIds.end())
177                 { isOnCell=false; isOnPoint=false; break; }
178               int zeId((*ittt).second);
179               if(zeId<0)
180                 {
181                   if(!isOnCell)
182                     { isOnCell=false; isOnPoint=false; break; }
183                   else
184                     isOnPoint=false;
185                 }
186               if(zeId>0)
187                 {
188                   if(!isOnPoint)
189                     { isOnCell=false; isOnPoint=false; break; }
190                   else
191                     isOnCell=false;
192                 }
193             }
194           itFamsOnGrp->Delete();
195           item1->setData(0,Qt::UserRole,name0);
196           item1->setData(0,Qt::ToolTipRole,toolTipName0);
197           item1->setData(0,Qt::CheckStateRole,0);
198           if(isOnCell && !isOnPoint)
199             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(0));
200           if(!isOnCell && isOnPoint)
201             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(1));
202           this->propertyManager()->registerLink(item1,"checked",SIGNAL(checkedStateChanged(bool)),this->proxy(),SMProperty,ll++);
203         }
204       itGrps->Delete();
205       // families 
206       vtkIdType idZeFams(it1->Next());//zeFams
207       strs0.clear(); strs0.append(QString("Families of \"%1\"").arg(meshName));
208       pqTreeWidgetItemObject *item00(new pqTreeWidgetItemObject(this->UI->Fields,strs0));
209       //item00->setData(0,Qt::CheckStateRole,0);
210       vtkAdjacentVertexIterator *itFams(vtkAdjacentVertexIterator::New());
211       g2->GetAdjacentVertices(idZeFams,itFams);
212       while(itFams->HasNext())
213         {
214           vtkIdType idf(itFams->Next());
215           std::string crudeFamName((const char *)verticesNames2->GetValue(idf));
216           std::size_t pos(crudeFamName.find_first_of(ZE_SEP));
217           std::string famName(crudeFamName.substr(0,pos)); std::string idStr(crudeFamName.substr(pos+strlen(ZE_SEP)));
218           int idInt(QString(idStr.c_str()).toInt());
219           famIds[famName]=idInt;
220           QString name0(famName.c_str()); QList<QString> strs0; strs0.append(name0);
221           QString toolTipName0(QString("%1 (%2)").arg(QString(famName.c_str())).arg(QString(idStr.c_str())));
222           pqTreeWidgetItemObject *item1(new pqTreeWidgetItemObject(item00,strs0));
223           item1->setData(0,Qt::UserRole,name0);
224           item1->setData(0,Qt::ToolTipRole,toolTipName0);
225           item1->setData(0,Qt::CheckStateRole,0);
226           if(idInt<0)
227             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(0));
228           if(idInt>0)
229             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(1));
230           this->propertyManager()->registerLink(item1,"checked",SIGNAL(checkedStateChanged(bool)),this->proxy(),SMProperty,ll++);
231         }
232       itFams->Delete();
233     }
234   it0->Delete(); 
235   this->UI->Fields->header()->setStretchLastSection(true);
236   this->UI->Fields->expandAll();
237   info->Delete();
238   ////////////////////
239   vtkSMProperty *SMPropertyExtractComp(this->proxy()->GetProperty("InsideOut"));
240   this->propertyManager()->registerLink(this->UI->ExtractComplementary,"checked",SIGNAL(stateChanged(int)),this->proxy(),SMPropertyExtractComp);
241   ////////////////////
242   this->UI->VTKConnect->Connect(this->proxy(),vtkCommand::UpdateInformationEvent, this, SLOT(updateSIL()));
243 }
244
245 pqExtractGroupPanel::~pqExtractGroupPanel()
246 {
247 }
248
249 void pqExtractGroupPanel::linkServerManagerProperties()
250 {
251   this->Superclass::linkServerManagerProperties();
252 }
253
254 void pqExtractGroupPanel::updateSIL()
255 {
256   vtkSMProxy* reader = this->referenceProxy()->getProxy();
257   reader->UpdatePropertyInformation(reader->GetProperty("SILUpdateStamp"));
258   int stamp = vtkSMPropertyHelper(reader, "SILUpdateStamp").GetAsInt();
259   if (stamp != this->UI->SILUpdateStamp)
260     {
261     this->UI->SILUpdateStamp = stamp;
262     vtkPVSILInformation* info = vtkPVSILInformation::New();
263     reader->GatherInformation(info);
264     vtkGraph *sil(info->GetSIL());
265     if(sil)
266       {
267         this->UI->SILModel.update(sil);
268         this->UI->Fields->expandAll();
269         info->Delete();
270       }
271     }
272 }
273
274 std::map<std::string,int> pqExtractGroupPanel::DeduceMapOfFamilyFromSIL(vtkMutableDirectedGraph *graph)
275 {
276   std::map<std::string,int> ret;
277   int idNames(0);
278   vtkAbstractArray *verticesNames(graph->GetVertexData()->GetAbstractArray("Names",idNames));
279   vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
280   vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
281   vtkIdType id0;
282   bool found(false);
283   for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
284     {
285       vtkStdString &st(verticesNames2->GetValue(i));
286       if(st=="MeshesFamsGrps")
287         {
288           id0=i;
289           found=true;
290         }
291     }
292   if(!found)
293     std::cerr << "There is an internal error ! The tree on server side has not the expected look !" << std::endl;
294   graph->GetAdjacentVertices(id0,it0);
295   while(it0->HasNext())
296     {
297       vtkIdType id1(it0->Next());//meshName
298       vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());
299       graph->GetAdjacentVertices(id1,it1);
300       it1->Next();//zeGroups
301       vtkIdType idZeFams(it1->Next());//zeFams
302       vtkAdjacentVertexIterator *itFams(vtkAdjacentVertexIterator::New());
303       graph->GetAdjacentVertices(idZeFams,itFams);
304       while(itFams->HasNext())
305         {
306           vtkIdType idf(itFams->Next());
307           std::string crudeFamName((const char *)verticesNames2->GetValue(idf));
308           std::size_t pos(crudeFamName.find_first_of(ZE_SEP));
309           std::string famName(crudeFamName.substr(0,pos)); std::string idStr(crudeFamName.substr(pos+strlen(ZE_SEP)));
310           int idInt(QString(idStr.c_str()).toInt());
311           ret[famName]=idInt;
312         }
313       it1->Delete();
314     }
315   it0->Delete();
316   return ret;
317 }