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, or (at your option) any later version.
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 "pqExtractGroupPanel.h"
22 #include "ui_ExtractGroupPanel.h"
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"
36 #include "vtkMutableDirectedGraph.h"
37 #include "vtkAdjacentVertexIterator.h"
38 #include "vtkSMPropertyHelper.h"
39 #include "vtkStringArray.h"
40 #include "vtkDataSetAttributes.h"
41 #include "vtkExtractGroup.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"
51 #include "pqPropertyLinks.h"
53 #include <QHeaderView>
57 static const char ZE_SEP[]="@@][@@";
59 class PixSingleExtractPanel
62 static const PixSingleExtractPanel &GetInstance();
63 QPixmap getPixFromStr(int pos) const;
64 PixSingleExtractPanel();
66 static const int NB_OF_DISCR=4;
67 static PixSingleExtractPanel *UNIQUE_INSTANCE;
68 QPixmap _pixmaps[NB_OF_DISCR];
71 PixSingleExtractPanel *PixSingleExtractPanel::UNIQUE_INSTANCE=0;
73 const PixSingleExtractPanel &PixSingleExtractPanel::GetInstance()
76 UNIQUE_INSTANCE=new PixSingleExtractPanel;
77 return *UNIQUE_INSTANCE;
80 PixSingleExtractPanel::PixSingleExtractPanel()
82 _pixmaps[0]=QPixmap(":/ParaViewResources/Icons/pqCellData16.png");
83 _pixmaps[1]=QPixmap(":/ParaViewResources/Icons/pqPointData16.png");
86 QPixmap PixSingleExtractPanel::getPixFromStr(int pos) const
94 class pqExtractGroupPanel::pqUI: public QObject, public Ui::ExtractGroupPanel
97 pqUI(pqExtractGroupPanel* p):QObject(p)
99 this->VTKConnect = vtkSmartPointer<vtkEventQtSlotConnect>::New();
100 this->SILUpdateStamp = -1;
106 vtkSmartPointer<vtkEventQtSlotConnect> VTKConnect;
107 pqPropertyLinks Links;
108 QMap<QTreeWidgetItem*, QString> TreeItemToPropMap;
112 pqExtractGroupPanel::pqExtractGroupPanel(pqProxy* object_proxy, QWidget* p):Superclass(object_proxy, p)
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);
120 this->linkServerManagerProperties();
122 vtkSMProperty *SMProperty(this->proxy()->GetProperty("GroupsFlagsStatus"));
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));
133 vtkAbstractArray *verticesNames(g2->GetVertexData()->GetAbstractArray("Names",idNames));
134 vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
137 for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
139 vtkStdString &st(verticesNames2->GetValue(i));
140 if(st=="MeshesFamsGrps")
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);
151 while(it0->HasNext())
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())
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));
171 vtkAdjacentVertexIterator *itFamsOnGrp(vtkAdjacentVertexIterator::New());
172 g2->GetAdjacentVertices(idg,itFamsOnGrp);
173 bool isOnCell(true),isOnPoint(true);
174 while(itFamsOnGrp->HasNext())
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);
185 { isOnCell=false; isOnPoint=false; break; }
192 { isOnCell=false; isOnPoint=false; break; }
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()));
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())
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);
231 item1->setData(0,Qt::DecorationRole,PixSingleExtractPanel::GetInstance().getPixFromStr(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()));
240 this->UI->Fields->header()->setStretchLastSection(true);
241 this->UI->Fields->expandAll();
244 vtkSMProperty *SMPropertyExtractComp(this->proxy()->GetProperty("InsideOut"));
245 this->propertyManager()->registerLink(this->UI->ExtractComplementary,"checked",SIGNAL(stateChanged(int)),this->proxy(),SMPropertyExtractComp);
247 this->UI->VTKConnect->Connect(this->proxy(),vtkCommand::UpdateInformationEvent, this, SLOT(updateSIL()));
250 pqExtractGroupPanel::~pqExtractGroupPanel()
254 void pqExtractGroupPanel::linkServerManagerProperties()
256 this->Superclass::linkServerManagerProperties();
259 void pqExtractGroupPanel::updateSIL()
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)
266 this->UI->SILUpdateStamp = stamp;
267 vtkPVSILInformation* info = vtkPVSILInformation::New();
268 reader->GatherInformation(info);
269 vtkGraph *sil(info->GetSIL());
272 this->UI->SILModel.update(sil);
273 this->UI->Fields->expandAll();
279 void pqExtractGroupPanel::anItemAsBeenFired()
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);
290 pqTreeWidget *sc(this->UI->Fields);
291 for(int i0=0;i0<sc->topLevelItemCount();i0++)
293 QTreeWidgetItem *lev0(sc->topLevelItem(i0));//Group and Fam
294 for(int i1=0;i1<lev0->childCount();i1++)
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();
305 const char **args=new const char *[nb];
306 for(unsigned int i=0;i<nb;i++)
308 args[i]=sts[i].c_str();
311 int iup(sm->GetImmediateUpdate());
312 //sm->SetNumberOfElements(0);
313 sm->SetElements(args,nb);
314 proxy->UpdateVTKObjects();
315 sm->SetImmediateUpdate(iup);
319 ((vtkSMSourceProxy *)proxy)->UpdatePipelineInformation();
323 std::map<std::string,int> pqExtractGroupPanel::DeduceMapOfFamilyFromSIL(vtkMutableDirectedGraph *graph)
325 std::map<std::string,int> ret;
327 vtkAbstractArray *verticesNames(graph->GetVertexData()->GetAbstractArray("Names",idNames));
328 vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
329 vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
332 for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
334 vtkStdString &st(verticesNames2->GetValue(i));
335 if(st=="MeshesFamsGrps")
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())
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())
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());
368 void pqExtractGroupPanel::updateInformationAndDomains()
370 pqNamedObjectPanel::updateInformationAndDomains();
371 vtkSMProxy *proxy(this->proxy());
372 vtkSMProperty *SMProperty(proxy->GetProperty("GroupsFlagsStatus"));
373 SMProperty->Modified();// agy : THE LINE FOR 7.5.1 !