Salome HOME
0022921: [CEA 1438] Remove the checkbox in the "time steps"
[modules/paravis.git] / src / Plugins / MEDReader / ParaViewPlugin / pqExtractCellTypePanel.cxx
1 // Copyright (C) 2010-2015  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 "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 "vtkSMSourceProxy.h"
33 #include "vtkEventQtSlotConnect.h"
34 #include "vtkPVSILInformation.h"
35 #include "vtkGraph.h"
36 #include "vtkMutableDirectedGraph.h"
37 #include "vtkAdjacentVertexIterator.h"
38 #include "vtkSMPropertyHelper.h"
39 #include "vtkStringArray.h"
40 #include "vtkDataSetAttributes.h"
41 #include "vtkExtractCellType.h"
42
43 #include "pqTreeWidgetItemObject.h"
44 #include "pqSMAdaptor.h"
45 #include "pqProxy.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"
52
53 #include <QHeaderView>
54
55 #include <sstream>
56
57 static const char ZE_SEP[]="@@][@@";
58
59 class pqExtractCellTypePanel::pqUI: public QObject, public Ui::ExtractCellTypePanel
60 {
61 public:
62   pqUI(pqExtractCellTypePanel* p):QObject(p)
63   {
64     this->VTKConnect = vtkSmartPointer<vtkEventQtSlotConnect>::New();
65     this->SILUpdateStamp = -1;
66   }
67
68   ~pqUI() { }
69
70   pqSILModel SILModel;
71   vtkSmartPointer<vtkEventQtSlotConnect> VTKConnect;
72   pqPropertyLinks Links;
73   QMap<QTreeWidgetItem*, QString> TreeItemToPropMap;
74   int SILUpdateStamp;
75 };
76
77 pqExtractCellTypePanel::pqExtractCellTypePanel(pqProxy* object_proxy, QWidget* p):Superclass(object_proxy, p)
78 {
79   this->UI=new pqUI(this);
80   this->UI->setupUi(this);
81   pqProxySILModel*proxyModel2 = new pqProxySILModel("GeoTypesStatusTree", &this->UI->SILModel);
82   proxyModel2->setSourceModel(&this->UI->SILModel);
83   this->UI->Fields->setHeaderHidden(true);
84   this->updateSIL();
85   this->linkServerManagerProperties();
86   ////////////////////
87   vtkSMProperty *SMProperty(this->proxy()->GetProperty("GeoTypesStatus"));
88   ////////////////////
89   vtkSMProxy* reader = this->referenceProxy()->getProxy();
90   vtkPVSILInformation* info=vtkPVSILInformation::New();
91   reader->GatherInformation(info);
92   vtkGraph *g(info->GetSIL());
93   //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 ?
94   vtkMutableDirectedGraph *g2(static_cast<vtkMutableDirectedGraph *>(g));
95   if(!g2)
96     return ;
97   int idNames(0);
98   vtkAbstractArray *verticesNames(g2->GetVertexData()->GetAbstractArray("Names",idNames));
99   vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
100   vtkIdType id0;
101   bool found(false);
102   for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
103     {
104       vtkStdString &st(verticesNames2->GetValue(i));
105       if(st=="CellTypesTree")
106         {
107           id0=i;
108           found=true;
109         }
110     }
111   if(!found)
112     std::cerr << "There is an internal error ! The tree on server side has not the expected look !" << std::endl;
113   vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
114   g2->GetAdjacentVertices(id0,it0);
115   int kk(0),ll(0);
116   while(it0->HasNext())
117     {
118       vtkIdType id1(it0->Next());
119       QString geoTypeName(QString::fromStdString((const char *)verticesNames2->GetValue(id1)));
120       vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());
121       g2->GetAdjacentVertices(id1,it1);
122       QList<QString> strs0; strs0.append(QString("%1").arg(geoTypeName));
123       pqTreeWidgetItemObject *item0(new pqTreeWidgetItemObject(this->UI->Fields,strs0));
124       vtkAdjacentVertexIterator *itGrps(vtkAdjacentVertexIterator::New());
125       g2->GetAdjacentVertices(id1,itGrps);
126       vtkIdType idg(itGrps->Next());
127       QString name0(QString::fromStdString((const char *)verticesNames2->GetValue(idg)));
128       QString toolTipName0(QString("%1 (vtkId=%2)").arg(geoTypeName).arg(name0));
129       item0->setData(0,Qt::UserRole,geoTypeName);
130       item0->setData(0,Qt::ToolTipRole,toolTipName0);
131       item0->setData(0,Qt::CheckStateRole,0);
132       item0->setProperty("PosInStringVector",QVariant(ll++));
133       connect(item0,SIGNAL(checkedStateChanged(bool)),this,SLOT(anItemAsBeenFired()));
134     }
135   it0->Delete(); 
136   this->UI->Fields->header()->setStretchLastSection(true);
137   this->UI->Fields->expandAll();
138   info->Delete();
139   ////////////////////
140   vtkSMProperty *SMPropertyExtractComp(this->proxy()->GetProperty("InsideOut"));
141   this->propertyManager()->registerLink(this->UI->ExtractComplementary,"checked",SIGNAL(stateChanged(int)),this->proxy(),SMPropertyExtractComp);
142   ////////////////////
143   this->UI->VTKConnect->Connect(this->proxy(),vtkCommand::UpdateInformationEvent,this,SLOT(updateSIL()));
144 }
145
146 pqExtractCellTypePanel::~pqExtractCellTypePanel()
147 {
148 }
149
150 void pqExtractCellTypePanel::linkServerManagerProperties()
151 {
152   this->Superclass::linkServerManagerProperties();
153 }
154
155 void pqExtractCellTypePanel::updateSIL()
156 {
157   vtkSMProxy* reader = this->referenceProxy()->getProxy();
158   reader->UpdatePropertyInformation(reader->GetProperty("SILUpdateStamp"));
159   int stamp = vtkSMPropertyHelper(reader, "SILUpdateStamp").GetAsInt();
160   if (stamp != this->UI->SILUpdateStamp)
161     {
162       this->UI->SILUpdateStamp = stamp;
163       vtkPVSILInformation* info = vtkPVSILInformation::New();
164       reader->GatherInformation(info);
165       vtkGraph *sil(info->GetSIL());
166       if(sil)
167         {
168           this->UI->SILModel.update(sil);
169           this->UI->Fields->expandAll();
170         }
171       info->Delete();
172     }
173 }
174
175 void pqExtractCellTypePanel::anItemAsBeenFired()
176 {
177   ///
178   vtkSMProxy *proxy(this->proxy());
179   vtkSMProperty *SMProperty(proxy->GetProperty("GeoTypesStatus"));
180   vtkSMStringVectorProperty *sm(dynamic_cast<vtkSMStringVectorProperty *>(SMProperty));
181   unsigned int nb(sm->GetNumberOfElements());
182   std::vector<std::string> sts(nb);
183   for(unsigned int i=0;i<nb;i++)
184     sts[i]=sm->GetElement(i);
185   ///
186   pqTreeWidget *sc(this->UI->Fields);
187   for(int i0=0;i0<sc->topLevelItemCount();i0++)
188     {
189       QTreeWidgetItem *lev0(sc->topLevelItem(i0));
190       pqTreeWidgetItemObject *scc(dynamic_cast<pqTreeWidgetItemObject *>(lev0));
191       int ll(scc->property("PosInStringVector").toInt());
192       int v(scc->isChecked());
193       std::ostringstream oss; oss << v;
194       sts[2*ll+1]=oss.str();
195     }
196   ///
197   const char **args=new const char *[nb];
198   for(unsigned int i=0;i<nb;i++)
199     {
200       args[i]=sts[i].c_str();
201     }
202   {
203     int iup(sm->GetImmediateUpdate());
204     //sm->SetNumberOfElements(0);
205     sm->SetElements(args,nb);
206     proxy->UpdateVTKObjects();
207     sm->SetImmediateUpdate(iup);
208   }
209   delete [] args;
210   //
211   ((vtkSMSourceProxy *)proxy)->UpdatePipelineInformation();
212   setModified();
213 }
214
215 void pqExtractCellTypePanel::updateInformationAndDomains()
216 {
217   pqNamedObjectPanel::updateInformationAndDomains();
218   vtkSMProxy *proxy(this->proxy());
219   vtkSMProperty *SMProperty(proxy->GetProperty("GeoTypesStatus"));
220   SMProperty->Modified();// agy : THE LINE FOR 7.5.1 !
221 }