Salome HOME
Correct bug of SIGSEGV when attempting to filter on an empty group/family.
[modules/paravis.git] / src / Plugins / MEDReader / ParaViewPlugin / pqExtractCellTypePanel.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 "pqExtractCellTypePanel.h"
22 #include "ui_ExtractCellTypePanel.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 "vtkExtractCellType.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 pqExtractCellTypePanel::pqUI: public QObject, public Ui::ExtractCellTypePanel
57 {
58 public:
59   pqUI(pqExtractCellTypePanel* p):QObject(p)
60   {
61     this->VTKConnect = vtkSmartPointer<vtkEventQtSlotConnect>::New();
62     this->SILUpdateStamp = -1;
63   }
64
65   ~pqUI() { }
66
67   pqSILModel SILModel;
68   vtkSmartPointer<vtkEventQtSlotConnect> VTKConnect;
69   pqPropertyLinks Links;
70   QMap<QTreeWidgetItem*, QString> TreeItemToPropMap;
71   int SILUpdateStamp;
72 };
73
74 pqExtractCellTypePanel::pqExtractCellTypePanel(pqProxy* object_proxy, QWidget* p):Superclass(object_proxy, p)
75 {
76   this->UI=new pqUI(this);
77   this->UI->setupUi(this);
78   pqProxySILModel*proxyModel2 = new pqProxySILModel("GeoTypesStatusTree", &this->UI->SILModel);
79   proxyModel2->setSourceModel(&this->UI->SILModel);
80   this->UI->Fields->setHeaderHidden(true);
81   this->updateSIL();
82   this->linkServerManagerProperties();
83   ////////////////////
84   vtkSMProperty *SMProperty(this->proxy()->GetProperty("GeoTypesStatus"));
85   ////////////////////
86   vtkSMProxy* reader = this->referenceProxy()->getProxy();
87   vtkPVSILInformation* info=vtkPVSILInformation::New();
88   reader->GatherInformation(info);
89   vtkGraph *g(info->GetSIL());
90   vtkMutableDirectedGraph *g2(vtkMutableDirectedGraph::SafeDownCast(g));
91   int idNames(0);
92   vtkAbstractArray *verticesNames(g2->GetVertexData()->GetAbstractArray("Names",idNames));
93   vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
94   vtkIdType id0;
95   bool found(false);
96   for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
97     {
98       vtkStdString &st(verticesNames2->GetValue(i));
99       if(st=="CellTypesTree")
100         {
101           id0=i;
102           found=true;
103         }
104     }
105   if(!found)
106     std::cerr << "There is an internal error ! The tree on server side has not the expected look !" << std::endl;
107   vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
108   g2->GetAdjacentVertices(id0,it0);
109   int kk(0),ll(0);
110   while(it0->HasNext())
111     {
112       vtkIdType id1(it0->Next());
113       QString geoTypeName(QString::fromStdString((const char *)verticesNames2->GetValue(id1)));
114       vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());
115       g2->GetAdjacentVertices(id1,it1);
116       QList<QString> strs0; strs0.append(QString("%1").arg(geoTypeName));
117       pqTreeWidgetItemObject *item0(new pqTreeWidgetItemObject(this->UI->Fields,strs0));
118       vtkAdjacentVertexIterator *itGrps(vtkAdjacentVertexIterator::New());
119       g2->GetAdjacentVertices(id1,itGrps);
120       vtkIdType idg(itGrps->Next());
121       QString name0(QString::fromStdString((const char *)verticesNames2->GetValue(idg)));
122       QString toolTipName0(QString("%1 (vtkId=%2)").arg(geoTypeName).arg(name0));
123       item0->setData(0,Qt::UserRole,geoTypeName);
124       item0->setData(0,Qt::ToolTipRole,toolTipName0);
125       item0->setData(0,Qt::CheckStateRole,0);
126       this->propertyManager()->registerLink(item0,"checked",SIGNAL(checkedStateChanged(bool)),this->proxy(),SMProperty,ll++);
127     }
128   it0->Delete(); 
129   this->UI->Fields->header()->setStretchLastSection(true);
130   this->UI->Fields->expandAll();
131   info->Delete();
132   ////////////////////
133   vtkSMProperty *SMPropertyExtractComp(this->proxy()->GetProperty("InsideOut"));
134   this->propertyManager()->registerLink(this->UI->ExtractComplementary,"checked",SIGNAL(stateChanged(int)),this->proxy(),SMPropertyExtractComp);
135   ////////////////////
136   this->UI->VTKConnect->Connect(this->proxy(),vtkCommand::UpdateInformationEvent,this,SLOT(updateSIL()));
137 }
138
139 pqExtractCellTypePanel::~pqExtractCellTypePanel()
140 {
141 }
142
143 void pqExtractCellTypePanel::linkServerManagerProperties()
144 {
145   this->Superclass::linkServerManagerProperties();
146 }
147
148 void pqExtractCellTypePanel::updateSIL()
149 {
150   vtkSMProxy* reader = this->referenceProxy()->getProxy();
151   reader->UpdatePropertyInformation(reader->GetProperty("SILUpdateStamp"));
152   int stamp = vtkSMPropertyHelper(reader, "SILUpdateStamp").GetAsInt();
153   if (stamp != this->UI->SILUpdateStamp)
154     {
155       this->UI->SILUpdateStamp = stamp;
156       vtkPVSILInformation* info = vtkPVSILInformation::New();
157       reader->GatherInformation(info);
158       this->UI->SILModel.update(info->GetSIL());
159       this->UI->Fields->expandAll();
160       info->Delete();
161     }
162 }