Salome HOME
Merge branch 'abn/port_pv42' into abn/rearch
[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   vtkAbstractArray *verticesNames(g2->GetVertexData()->GetAbstractArray("Names",idNames));
129   vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
130   vtkIdType id0;
131   bool found(false);
132   for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
133     {
134       vtkStdString &st(verticesNames2->GetValue(i));
135       if(st=="MeshesFamsGrps")
136         {
137           id0=i;
138           found=true;
139         }
140     }
141   if(!found)
142     std::cerr << "There is an internal error ! The tree on server side has not the expected look !" << std::endl;
143   vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
144   g2->GetAdjacentVertices(id0,it0);
145   int kk(0),ll(0);
146   while(it0->HasNext())
147     {
148       vtkIdType id1(it0->Next());
149       QString meshName(QString::fromStdString((const char *)verticesNames2->GetValue(id1)));
150       vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());
151       g2->GetAdjacentVertices(id1,it1);
152       vtkIdType idZeGrps(it1->Next());//zeGroups
153       QList<QString> strs0; strs0.append(QString("Groups of \"%1\"").arg(meshName));
154       pqTreeWidgetItemObject *item0(new pqTreeWidgetItemObject(this->UI->Fields,strs0));
155       std::map<std::string,int> famIds(DeduceMapOfFamilyFromSIL(g2));
156       //item0->setData(0,Qt::CheckStateRole,0);
157       vtkAdjacentVertexIterator *itGrps(vtkAdjacentVertexIterator::New());
158       g2->GetAdjacentVertices(idZeGrps,itGrps);
159       while(itGrps->HasNext())
160         {
161           vtkIdType idg(itGrps->Next());
162           QString name0(QString::fromStdString((const char *)verticesNames2->GetValue(idg))); QList<QString> strs0; strs0.append(name0);
163           QString toolTipName0(name0);
164           pqTreeWidgetItemObject *item1(new pqTreeWidgetItemObject(item0,strs0));
165           //
166           vtkAdjacentVertexIterator *itFamsOnGrp(vtkAdjacentVertexIterator::New());
167           g2->GetAdjacentVertices(idg,itFamsOnGrp);
168           bool isOnCell(true),isOnPoint(true);
169           while(itFamsOnGrp->HasNext())
170             {
171               vtkIdType idfg(itFamsOnGrp->Next());
172               std::string namefg((const char *)verticesNames2->GetValue(idfg));
173               std::map<std::string,int>::const_iterator ittt(famIds.find(namefg));
174               if(ittt==famIds.end())
175                 { isOnCell=false; isOnPoint=false; break; }
176               int zeId((*ittt).second);
177               if(zeId<0)
178                 {
179                   if(!isOnCell)
180                     { isOnCell=false; isOnPoint=false; break; }
181                   else
182                     isOnPoint=false;
183                 }
184               if(zeId>0)
185                 {
186                   if(!isOnPoint)
187                     { isOnCell=false; isOnPoint=false; break; }
188                   else
189                     isOnCell=false;
190                 }
191             }
192           itFamsOnGrp->Delete();
193           item1->setData(0,Qt::UserRole,name0);
194           item1->setData(0,Qt::ToolTipRole,toolTipName0);
195           item1->setData(0,Qt::CheckStateRole,0);
196           if(isOnCell && !isOnPoint)
197             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(0));
198           if(!isOnCell && isOnPoint)
199             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(1));
200           this->propertyManager()->registerLink(item1,"checked",SIGNAL(checkedStateChanged(bool)),this->proxy(),SMProperty,ll++);
201         }
202       itGrps->Delete();
203       // families 
204       vtkIdType idZeFams(it1->Next());//zeFams
205       strs0.clear(); strs0.append(QString("Families of \"%1\"").arg(meshName));
206       pqTreeWidgetItemObject *item00(new pqTreeWidgetItemObject(this->UI->Fields,strs0));
207       //item00->setData(0,Qt::CheckStateRole,0);
208       vtkAdjacentVertexIterator *itFams(vtkAdjacentVertexIterator::New());
209       g2->GetAdjacentVertices(idZeFams,itFams);
210       while(itFams->HasNext())
211         {
212           vtkIdType idf(itFams->Next());
213           std::string crudeFamName((const char *)verticesNames2->GetValue(idf));
214           std::size_t pos(crudeFamName.find_first_of(ZE_SEP));
215           std::string famName(crudeFamName.substr(0,pos)); std::string idStr(crudeFamName.substr(pos+strlen(ZE_SEP)));
216           int idInt(QString(idStr.c_str()).toInt());
217           famIds[famName]=idInt;
218           QString name0(famName.c_str()); QList<QString> strs0; strs0.append(name0);
219           QString toolTipName0(QString("%1 (%2)").arg(QString(famName.c_str())).arg(QString(idStr.c_str())));
220           pqTreeWidgetItemObject *item1(new pqTreeWidgetItemObject(item00,strs0));
221           item1->setData(0,Qt::UserRole,name0);
222           item1->setData(0,Qt::ToolTipRole,toolTipName0);
223           item1->setData(0,Qt::CheckStateRole,0);
224           if(idInt<0)
225             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(0));
226           if(idInt>0)
227             item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(1));
228           this->propertyManager()->registerLink(item1,"checked",SIGNAL(checkedStateChanged(bool)),this->proxy(),SMProperty,ll++);
229         }
230       itFams->Delete();
231     }
232   it0->Delete(); 
233   this->UI->Fields->header()->setStretchLastSection(true);
234   this->UI->Fields->expandAll();
235   info->Delete();
236   ////////////////////
237   vtkSMProperty *SMPropertyExtractComp(this->proxy()->GetProperty("InsideOut"));
238   this->propertyManager()->registerLink(this->UI->ExtractComplementary,"checked",SIGNAL(stateChanged(int)),this->proxy(),SMPropertyExtractComp);
239   ////////////////////
240   this->UI->VTKConnect->Connect(this->proxy(),vtkCommand::UpdateInformationEvent, this, SLOT(updateSIL()));
241 }
242
243 pqExtractGroupPanel::~pqExtractGroupPanel()
244 {
245 }
246
247 void pqExtractGroupPanel::linkServerManagerProperties()
248 {
249   this->Superclass::linkServerManagerProperties();
250 }
251
252 void pqExtractGroupPanel::updateSIL()
253 {
254   vtkSMProxy* reader = this->referenceProxy()->getProxy();
255   reader->UpdatePropertyInformation(reader->GetProperty("SILUpdateStamp"));
256   int stamp = vtkSMPropertyHelper(reader, "SILUpdateStamp").GetAsInt();
257   if (stamp != this->UI->SILUpdateStamp)
258     {
259     this->UI->SILUpdateStamp = stamp;
260     vtkPVSILInformation* info = vtkPVSILInformation::New();
261     reader->GatherInformation(info);
262     this->UI->SILModel.update(info->GetSIL());
263     this->UI->Fields->expandAll();
264     info->Delete();
265     }
266 }
267
268 void pqExtractGroupPanel::aLev4HasBeenFired()
269 {
270   pqTreeWidgetItemObject *zeItem(qobject_cast<pqTreeWidgetItemObject *>(sender()));
271   if(!zeItem)
272     return;
273   pqTreeWidgetItemObject *father(dynamic_cast<pqTreeWidgetItemObject *>(zeItem->QTreeWidgetItem::parent()));
274   if(!father)
275     return ;
276   if(zeItem->isChecked())
277     {
278       // This part garantees that all leaves having not the same father than zeItem are desactivated
279       foreach(pqTreeWidgetItemObject* elt,this->_all_lev4)
280         {
281           QTreeWidgetItem *testFath(elt->QTreeWidgetItem::parent());
282           if(testFath!=father)
283             if(elt->isChecked())
284               elt->setChecked(false);
285         }
286       //If all leaves are checked the father is check too
287       bool allItemsAreChked(true);
288       for(int i=0;i<father->childCount() && allItemsAreChked;i++)
289         {
290           pqTreeWidgetItemObject *elt(dynamic_cast<pqTreeWidgetItemObject *>(father->child(i)));
291           if(elt && !elt->isChecked())
292             allItemsAreChked=false;
293         }
294       if(allItemsAreChked && !father->isChecked())
295         father->setChecked(true);
296     }
297   else
298     {
299       // if all are unchecked - check it again
300       bool allItemsAreUnChked(true);
301       foreach(pqTreeWidgetItemObject* elt,this->_all_lev4)
302         {
303           if(elt && elt->isChecked())
304             allItemsAreUnChked=false;
305         }
306       if(allItemsAreUnChked)
307         zeItem->setChecked(true);// OK zeItem was required to be unchecked but as it is the last one. Recheck it !
308       else
309         {// if all items are uncheked inside a same parent - uncheck the parent
310           allItemsAreUnChked=true;
311           for(int i=0;i<father->childCount() && allItemsAreUnChked;i++)
312             {
313               pqTreeWidgetItemObject *elt(dynamic_cast<pqTreeWidgetItemObject *>(father->child(i)));
314               if(elt && elt->isChecked())
315             allItemsAreUnChked=false;
316             }
317           if(allItemsAreUnChked && father->isChecked())
318             father->setChecked(false);
319         } 
320     }
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 }