Salome HOME
Merge from V6_main 13/12/2012
[modules/paravis.git] / src / Plugins / MedReader / ParaViewPlugin / pqMedReaderPanel.cxx
1 // Copyright (C) 2010-2012  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
20 #include "pqMedReaderPanel.h"
21 #include "ui_MedReaderPanel.h"
22
23 #include "vtkProcessModule.h"
24 #include "vtkMultiBlockDataSet.h"
25 #include "vtkInformation.h"
26 #include "vtkIntArray.h"
27 #include "vtkSMDoubleVectorProperty.h"
28 #include "vtkSMIntVectorProperty.h"
29 #include "vtkSMStringVectorProperty.h"
30 #include "vtkSMProxy.h"
31 #include "vtkEventQtSlotConnect.h"
32 #include "vtkPVSILInformation.h"
33 #include "vtkGraph.h"
34 #include "vtkSMPropertyHelper.h"
35 #include "vtkStringArray.h"
36 #include "vtkDataSetAttributes.h"
37 #include "vtkMedReader.h"
38
39 #include "vtkMedUtilities.h"
40
41 #include "pqTreeWidgetItemObject.h"
42 #include "pqSMAdaptor.h"
43 #include "pqProxy.h"
44 #include "pqPropertyManager.h"
45 #include "pqSILModel.h"
46 #include "pqProxySILModel.h"
47 #include "pqTreeViewSelectionHelper.h"
48 #include "pqTreeWidgetSelectionHelper.h"
49 #include "pqPropertyLinks.h"
50
51 #include <QHeaderView>
52
53 class pqMedReaderPanel::pqUI: public QObject, public Ui::MedReaderPanel
54 {
55 public:
56   pqUI(pqMedReaderPanel* p) :
57     QObject(p)
58   {
59     this->VTKConnect = vtkSmartPointer<vtkEventQtSlotConnect>::New();
60     this->SILUpdateStamp = -1;
61   }
62
63   ~pqUI()
64   {
65   }
66
67   pqSILModel SILModel;
68   vtkSmartPointer<vtkEventQtSlotConnect> VTKConnect;
69   pqPropertyLinks Links;
70   QMap<QTreeWidgetItem*, QString> TreeItemToPropMap;
71   pqProxySILModel* entityModel;
72   pqProxySILModel* groupModel;
73   int SILUpdateStamp;
74 };
75
76 pqMedReaderPanel::pqMedReaderPanel(pqProxy* object_proxy, QWidget* p) :
77   Superclass(object_proxy, p)
78 {
79   this->UI = new pqUI(this);
80   this->UI->setupUi(this);
81
82   pqProxySILModel* proxyModel;
83
84   // connect groups to groupsRoot
85   proxyModel = new pqProxySILModel("GroupTree", &this->UI->SILModel);
86   proxyModel->setSourceModel(&this->UI->SILModel);
87   this->UI->Groups->setModel(proxyModel);
88   this->UI->Groups->setHeaderHidden(true);
89
90   this->UI->groupModel = new pqProxySILModel("Groups", &this->UI->SILModel);
91   this->UI->groupModel->setSourceModel(&this->UI->SILModel);
92
93   // connect cell types to "EntityRoot"
94   proxyModel = new pqProxySILModel("EntityTree", &this->UI->SILModel);
95   proxyModel->setSourceModel(&this->UI->SILModel);
96   this->UI->Entity->setModel(proxyModel);
97   this->UI->Entity->setHeaderHidden(true);
98
99   this->UI->entityModel = new pqProxySILModel("Entity", &this->UI->SILModel);
100   this->UI->entityModel->setSourceModel(&this->UI->SILModel);
101
102   this->updateSIL();
103
104   this->UI->Groups->header()->setStretchLastSection(true);
105   this->UI->Entity->header()->setStretchLastSection(true);
106
107   this->linkServerManagerProperties();
108
109   QList<pqTreeWidget*> treeWidgets = this->findChildren<pqTreeWidget*> ();
110   foreach (pqTreeWidget* tree, treeWidgets)
111       {
112       new pqTreeWidgetSelectionHelper(tree);
113       }
114
115   QList<pqTreeView*> treeViews = this->findChildren<pqTreeView*> ();
116   foreach (pqTreeView* tree, treeViews)
117       {
118       new pqTreeViewSelectionHelper(tree);
119       }
120
121   this->connect(this->UI->groupModel, SIGNAL(valuesChanged()),
122                 this, SLOT(setModified()));
123   this->connect(this->UI->entityModel, SIGNAL(valuesChanged()),
124                 this, SLOT(setModified()));
125   this->connect(this->UI->TimeCombo, SIGNAL(currentIndexChanged(int)),
126                 this, SLOT(setModified()));
127   this->connect(this->UI->GenerateVectors, SIGNAL(stateChanged(int)),
128                 this, SLOT(setModified()));
129
130   this->UI->tabWidget->setCurrentIndex(0);
131
132   this->UI->VTKConnect->Connect(this->proxy(),
133       vtkCommand::UpdateInformationEvent, this, SLOT(updateSIL()));
134 }
135
136 pqMedReaderPanel::~pqMedReaderPanel()
137 {
138 }
139
140 void pqMedReaderPanel::addSelectionsToTreeWidget(const QString& prop,
141     QTreeWidget* tree, PixmapType pix)
142 {
143   vtkSMProperty* SMProperty = this->proxy()->GetProperty(prop.toAscii().data());
144   QList<QVariant> SMPropertyDomain;
145   SMPropertyDomain = pqSMAdaptor::getSelectionPropertyDomain(SMProperty);
146   int j;
147   for(j = 0; j < SMPropertyDomain.size(); j++)
148     {
149     QString varName = SMPropertyDomain[j].toString();
150     this->addSelectionToTreeWidget(varName, varName, tree, pix, prop, j);
151     }
152 }
153
154 void pqMedReaderPanel::addSelectionToTreeWidget(const QString& name,
155     const QString& realName, QTreeWidget* tree, PixmapType pix,
156     const QString& prop, int propIdx)
157 {
158   static QPixmap pixmaps[] = {
159     QPixmap(":/ParaViewResources/Icons/pqPointData16.png"),
160     QPixmap(":/ParaViewResources/Icons/pqCellData16.png"),
161     QPixmap(":/ParaViewResources/Icons/pqQuadratureData16.png"),
162     QPixmap(":/ParaViewResources/Icons/pqElnoData16.png") };
163
164   vtkSMProperty* SMProperty = this->proxy()->GetProperty(prop.toAscii().data());
165
166   if(!SMProperty || !tree)
167     {
168     return;
169     }
170
171   QList<QString> strs;
172   strs.append(name);
173   pqTreeWidgetItemObject* item;
174   item = new pqTreeWidgetItemObject(tree, strs);
175   item->setData(0, Qt::ToolTipRole, name);
176   if(pix >= 0)
177     {
178     item->setData(0, Qt::DecorationRole, pixmaps[pix]);
179     }
180   item->setData(0, Qt::UserRole, QString("%1 %2").arg((int) pix).arg(realName));
181   this->propertyManager()->registerLink(item, "checked",
182       SIGNAL(checkedStateChanged(bool)), this->proxy(), SMProperty, propIdx);
183
184   this->UI->TreeItemToPropMap[item] = prop;
185 }
186
187 void pqMedReaderPanel::linkServerManagerProperties()
188 {
189   this->UI->Links.addPropertyLink(this->UI->groupModel, "values",
190       SIGNAL(valuesChanged()), this->proxy(), this->proxy()->GetProperty(
191           "Groups"));
192
193   this->UI->Links.addPropertyLink(this->UI->entityModel, "values",
194       SIGNAL(valuesChanged()), this->proxy(), this->proxy()->GetProperty(
195           "Entity"));
196
197   this->UI->Links.addPropertyLink(this->UI->GenerateVectors, "checked",
198       SIGNAL(toggled(bool)), this->proxy(), this->proxy()->GetProperty(
199           "GenerateVectors"));
200
201   this->Superclass::linkServerManagerProperties();
202
203   // do the point variables
204   this->addSelectionsToTreeWidget("PointFieldsArrayStatus",
205       this->UI->Variables, PM_POINT);
206   // do the cell variables
207   this->addSelectionsToTreeWidget("CellFieldsArrayStatus", this->UI->Variables,
208       PM_CELL);
209   // do the quadrature variables
210   this->addSelectionsToTreeWidget("QuadratureFieldsArrayStatus",
211       this->UI->Variables, PM_QUADRATURE);
212
213   // do the Elno variables
214   this->addSelectionsToTreeWidget("ElnoFieldsArrayStatus",
215       this->UI->Variables, PM_ELNO);
216   this->setupAnimationModeWidget();
217 }
218
219 void pqMedReaderPanel::setupAnimationModeWidget()
220 {
221   this->UI->AnimationModeCombo->clear();
222   QList<QVariant> modes = pqSMAdaptor::getEnumerationPropertyDomain(
223       this->proxy()->GetProperty("AnimationMode"));
224   for(int mode = 0; mode < modes.size(); mode++)
225     {
226     QString modeName = modes[mode].toString();
227     this->UI->AnimationModeCombo->addItem(modeName);
228     }
229
230   this->UI->Links.addPropertyLink(this->UI->AnimationModeCombo, "currentIndex",
231       SIGNAL(currentIndexChanged(int)), this->proxy(),
232       this->proxy()->GetProperty("AnimationMode"));
233
234   this->connect(this->UI->AnimationModeCombo,
235       SIGNAL(currentIndexChanged(int)), this, SLOT(animationModeChanged(int)));
236
237   this->UI->Links.addPropertyLink(this->UI->TimeCombo, "currentIndex",
238       SIGNAL(currentIndexChanged(int)), this->proxy(),
239       this->proxy()->GetProperty("TimeIndexForIterations"));
240
241   this->addSelectionsToTreeWidget("FrequencyArrayStatus",
242       this->UI->Modes, PM_NONE);
243
244   vtkSMPropertyHelper helper(this->proxy(), "AnimationMode");
245   int mode = helper.GetAsInt(0);
246   this->animationModeChanged(mode);
247   this->updateAvailableTimes();
248 }
249
250 void pqMedReaderPanel::animationModeChanged(int mode)
251 {
252   if(mode == vtkMedReader::Default || mode == vtkMedReader::PhysicalTime)
253     {
254     this->UI->TimeCombo->hide();
255     this->UI->TimeLabel->hide();
256     this->UI->Modes->hide();
257     }
258   else if(mode == vtkMedReader::Iteration)
259     {
260     this->UI->TimeCombo->show();
261     this->UI->TimeLabel->show();
262     this->UI->Modes->hide();
263     }
264   else // vtkMedReader::Modes
265     {
266     this->UI->TimeCombo->hide();
267     this->UI->TimeLabel->hide();
268     this->UI->Modes->show();
269     }
270   vtkSMPropertyHelper(this->proxy(), "AnimationMode").Set(mode);
271   this->proxy()->UpdateVTKObjects();
272   this->setModified();
273 }
274
275 void pqMedReaderPanel::updateAvailableTimes()
276 {
277   vtkSMDoubleVectorProperty* prop = vtkSMDoubleVectorProperty::SafeDownCast(
278       this->proxy()->GetProperty("AvailableTimes"));
279
280   this->proxy()->UpdatePropertyInformation(prop);
281
282   //prop->GetInformationHelper()->UpdateProperty(
283   //    vtkProcessModuleConnectionManager::GetRootServerConnectionID(),
284   //    vtkProcessModule::DATA_SERVER,
285   //    this->proxy()->GetID(),
286   //    prop);
287
288   this->UI->TimeCombo->clear();
289   double *aux = prop->GetElements();
290
291   for(int tid = 0; tid < prop->GetNumberOfElements(); tid++)
292     {
293     this->UI->TimeCombo->addItem( QString::number(aux[tid]) );
294     }
295
296 }
297
298 void pqMedReaderPanel::updateSIL()
299 {
300   vtkSMProxy* reader = this->referenceProxy()->getProxy();
301   reader->UpdatePropertyInformation(reader->GetProperty("SILUpdateStamp"));
302
303   int stamp = vtkSMPropertyHelper(reader, "SILUpdateStamp").GetAsInt();
304   if (stamp != this->UI->SILUpdateStamp)
305     {
306     this->UI->SILUpdateStamp = stamp;
307     vtkPVSILInformation* info = vtkPVSILInformation::New();
308     reader->GatherInformation(info);
309     this->UI->SILModel.update(info->GetSIL());
310
311     this->UI->Groups->expandAll();
312     this->UI->Entity->expandAll();
313
314     info->Delete();
315     }
316
317   /*
318   this->proxy()->UpdatePropertyInformation(
319       this->proxy()->GetProperty("SILUpdateStamp"));
320
321   int stamp = vtkSMPropertyHelper(this->proxy(), "SILUpdateStamp").GetAsInt();
322   if(stamp != this->UI->SILUpdateStamp)
323     {
324     this->UI->SILUpdateStamp = stamp;
325
326     vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
327     vtkPVSILInformation* info = vtkPVSILInformation::New();
328     pm->GatherInformation(this->proxy()->GetConnectionID(),
329         vtkProcessModule::DATA_SERVER, info, this->proxy()->GetID());
330
331     this->UI->SILModel.update(info->GetSIL());
332
333     this->UI->Groups->expandAll();
334     this->UI->Entity->expandAll();
335
336     info->Delete();
337     }*/
338 }