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