Salome HOME
Fix for the "0051899: curves are not shown in opened study" issue.
[modules/visu.git] / src / VISUGUI / VisuGUI_OffsetDlg.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "VisuGUI_OffsetDlg.h"
24
25 #include "VisuGUI.h"
26 #include "VisuGUI_Tools.h"
27
28 #include "VISU_ViewManager_i.hh"
29 #include "VISU_Actor.h"
30
31 #include "LightApp_Application.h"
32 #include "LightApp_SelectionMgr.h"
33 #include "SALOME_ListIteratorOfListIO.hxx"
34 #include "SalomeApp_Application.h"
35 #include <SalomeApp_DoubleSpinBox.h>
36 #include "SVTK_ViewWindow.h"
37 #include "SVTK_ViewModel.h"
38 #include "SUIT_ViewManager.h"
39 #include "SUIT_Desktop.h"
40 #include "SUIT_Session.h"
41 #include "SUIT_MessageBox.h"
42 #include "SUIT_ResourceMgr.h"
43
44 // VTK Includes
45 #include "vtkRenderer.h"
46
47 // QT Includes
48 #include <QLabel>
49 #include <QLayout>
50 #include <QCheckBox>
51 #include <QGroupBox>
52 #include <QPushButton>
53 #include <QKeyEvent>
54
55
56 #define  MAXVAL 1e10
57
58
59 VisuGUI_OffsetDlg::VisuGUI_OffsetDlg (VisuGUI* theModule)
60 : QDialog(VISU::GetDesktop(theModule), Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
61   myModule(theModule),
62   mySelectionMgr(VISU::GetSelectionMgr(theModule))
63 {
64   //myPrsList.setAutoDelete(false);
65   //myPointMapList.setAutoDelete(false);
66
67   setWindowTitle(tr("TIT_OFFSETDLG"));
68   setSizeGripEnabled(TRUE);
69
70   QVBoxLayout* TopLayout = new QVBoxLayout (this);
71   TopLayout->setSpacing(6);
72   TopLayout->setMargin(11);
73
74   QWidget* anOffsetsPane = new QWidget (this);
75   QHBoxLayout* aHBLay = new QHBoxLayout( anOffsetsPane );
76   aHBLay->setSpacing(6);
77
78   aHBLay->addWidget( new QLabel ("dX:", anOffsetsPane) );
79   aHBLay->addWidget( myDxEdt = new SalomeApp_DoubleSpinBox (anOffsetsPane) );
80   VISU::initSpinBox( myDxEdt, -MAXVAL, MAXVAL, 1., "length_precision" );
81
82   aHBLay->addWidget( new QLabel("dY:", anOffsetsPane) );;
83   aHBLay->addWidget( myDyEdt = new SalomeApp_DoubleSpinBox (anOffsetsPane) );
84   VISU::initSpinBox( myDyEdt, -MAXVAL, MAXVAL, 1., "length_precision" );
85
86   aHBLay->addWidget( new QLabel("dZ:", anOffsetsPane) );
87   aHBLay->addWidget( myDzEdt = new SalomeApp_DoubleSpinBox (anOffsetsPane) );
88   VISU::initSpinBox( myDzEdt, -MAXVAL, MAXVAL, 1., "length_precision" );
89
90   QPushButton* aResetBtn = new QPushButton(tr("BTN_RESET"), anOffsetsPane);
91   aHBLay->addWidget( aResetBtn );
92   connect(aResetBtn, SIGNAL(clicked()), this, SLOT(onReset()));
93
94   TopLayout->addWidget(anOffsetsPane);
95
96   if (!VISU::GetCStudy(VISU::GetAppStudy(theModule))->GetProperties()->IsLocked()) {
97     mySaveChk = new QCheckBox ("Save to presentation", this);
98     TopLayout->addWidget(mySaveChk);
99     mySaveChk->setChecked(true);
100   } else {
101     mySaveChk = 0;
102   }
103
104   // Common buttons ===========================================================
105   QGroupBox* GroupButtons = new QGroupBox(this);
106   //GroupButtons->setColumnLayout(0, Qt::Vertical);
107   //GroupButtons->layout()->setSpacing(0);
108   //GroupButtons->layout()->setMargin(0);
109   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons);
110   GroupButtonsLayout->setAlignment(Qt::AlignTop);
111   GroupButtonsLayout->setSpacing(6);
112   GroupButtonsLayout->setMargin(11);
113
114   QPushButton* buttonOk = new QPushButton(tr("&OK"), GroupButtons);
115   buttonOk->setAutoDefault(TRUE);
116   buttonOk->setDefault(TRUE);
117   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
118   GroupButtonsLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1);
119
120   QPushButton* buttonApply = new QPushButton(tr("&Apply"), GroupButtons);
121   buttonOk->setAutoDefault(TRUE);
122   GroupButtonsLayout->addWidget(buttonApply, 0, 2);
123   GroupButtonsLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 3);
124
125   QPushButton* buttonCancel = new QPushButton(tr("&Cancel") , GroupButtons);
126   buttonCancel->setAutoDefault(TRUE);
127   GroupButtonsLayout->addWidget(buttonCancel, 0, 4);
128   GroupButtonsLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 5);  
129
130   QPushButton* buttonHelp = new QPushButton(tr("&Help") , GroupButtons );
131   buttonHelp->setAutoDefault(TRUE);
132   GroupButtonsLayout->addWidget(buttonHelp, 0, 6);
133
134   TopLayout->addWidget(GroupButtons);
135
136   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(accept()));
137   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(onApply()));
138   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
139   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(onHelp()));
140   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionChanged()));
141   
142   onSelectionChanged();
143   show();
144 }
145
146 void VisuGUI_OffsetDlg::setVisible(bool show){
147   if ( show  && getPrsCount() > 0 )
148     QDialog::setVisible( show );
149   else
150     QDialog::setVisible( show );
151 }
152
153 void VisuGUI_OffsetDlg::onSelectionChanged(){
154   if(!mySelectionMgr)
155     return;
156   
157   //Clear old selection
158   clearPresentations();
159
160   SALOME_ListIO aListIO;
161   mySelectionMgr->selectedObjects(aListIO);
162
163   SalomeApp_Study* aStudy = VISU::GetAppStudy(myModule);
164   SALOME_ListIteratorOfListIO anIter(aListIO);
165   for (; anIter.More(); anIter.Next()) {
166     Handle(SALOME_InteractiveObject) anIO = anIter.Value();
167     if (anIO->hasEntry()) {
168       QString anEntry(anIO->getEntry());
169       VISU::TObjectInfo anObjectInfo = VISU::GetObjectByEntry(aStudy, anEntry.toLatin1().constData());
170       if(VISU::Prs3d_i* aPrsObject = VISU::GetPrs3dFromBase(anObjectInfo.myBase))
171         addPresentation(aPrsObject);
172       else if (VISU::PointMap3d_i* aPrs = dynamic_cast<VISU::PointMap3d_i*>(anObjectInfo.myBase)) {
173         addPointMapPresentation(aPrs);
174       }
175     }
176   }
177 }
178
179 void VisuGUI_OffsetDlg::clearPresentations(){
180   myPrsList.clear();
181   myPointMapList.clear();
182   myOldOffsets.clear();
183   myOldPointMapOffsets.clear();
184 }
185
186 void VisuGUI_OffsetDlg::addPresentation (VISU::Prs3d_i* thePrs)
187 {
188   myPrsList.append(thePrs);
189   CORBA::Float anOffset[3];
190   thePrs->GetOffset(anOffset[0],anOffset[1],anOffset[2]);
191   OffsetStruct anOffs(anOffset[0],anOffset[1],anOffset[2]);
192   myOldOffsets.append(anOffs);
193   if (myPrsList.count() == 1) {
194     setOffset(anOffs.myOffset);
195   } else if (myPrsList.count() == 2) {
196     OffsetStruct anOffs;
197     setOffset(anOffs.myOffset);
198   }
199 }
200
201 void VisuGUI_OffsetDlg::addPointMapPresentation (VISU::PointMap3d_i* thePrs)
202 {
203   myPointMapList.append(thePrs);
204
205   CORBA::Float anOffset[3];
206   thePrs->GetOffset(anOffset[0],anOffset[1],anOffset[2]);
207   OffsetStruct anOffs(anOffset[0],anOffset[1],anOffset[2]);
208   myOldPointMapOffsets.append(anOffs);
209   if (myPointMapList.count() == 1) {
210     setOffset(anOffs.myOffset);
211   } else if (myPointMapList.count() == 2) {
212     OffsetStruct anOffs;
213     setOffset(anOffs.myOffset);
214   }
215 }
216
217 void VisuGUI_OffsetDlg::setOffset (const double* theOffset)
218 {
219   myDxEdt->setValue(theOffset[0]);
220   myDyEdt->setValue(theOffset[1]);
221   myDzEdt->setValue(theOffset[2]);
222 }
223
224 void VisuGUI_OffsetDlg::getOffset (double* theOffset) const
225 {
226   theOffset[0] = myDxEdt->value();
227   theOffset[1] = myDyEdt->value();
228   theOffset[2] = myDzEdt->value();
229 }
230
231 void VisuGUI_OffsetDlg::onReset()
232 {
233   myDxEdt->setValue(0);
234   myDyEdt->setValue(0);
235   myDzEdt->setValue(0);
236 }
237
238 bool VisuGUI_OffsetDlg::isToSave() const
239 {
240   if (mySaveChk)
241     return mySaveChk->isChecked();
242   else
243     return false;
244 }
245
246 void VisuGUI_OffsetDlg::updateOffset (VISU::Prs3d_i* thePrs, double* theOffset)
247 {
248   if (myPrsList.count() == 0) 
249     return;
250
251   if (isToSave()) {
252     thePrs->SetOffset(theOffset[0],theOffset[1],theOffset[2]);
253     thePrs->UpdateActors();
254     return;
255   }
256
257   ViewManagerList aViewManagerList;
258   SalomeApp_Application* anApp = myModule->getApp();
259   anApp->viewManagers(aViewManagerList);
260   QList<SUIT_ViewManager*>::Iterator anVMIter = aViewManagerList.begin();
261   for (; anVMIter !=  aViewManagerList.end(); anVMIter++ ) {
262     QVector<SUIT_ViewWindow*> aViews = (*anVMIter)->getViews();
263     for (int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
264       if (SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
265         if (SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
266           if (VISU_Actor* anActor = VISU::FindActor(vw, thePrs)) {
267                 anActor->SetPosition(theOffset);
268             vw->onAdjustTrihedron();
269             vw->getRenderer()->ResetCameraClippingRange();
270             vw->Repaint();
271           }
272         }
273       }
274     }
275   }
276 }
277
278 void VisuGUI_OffsetDlg::updatePointMapOffset (VISU::PointMap3d_i* thePrs, double* theOffset)
279 {
280   if (myPointMapList.count() == 0) 
281     return;
282
283   if (isToSave())
284     thePrs->SetOffset(theOffset[0],theOffset[1],theOffset[2]);
285
286   ViewManagerList aViewManagerList;
287   SalomeApp_Application* anApp = myModule->getApp();
288   anApp->viewManagers(aViewManagerList);
289   SUIT_ViewManager* aViewManager;
290   foreach( aViewManager, aViewManagerList ) {
291     QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
292     for (int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
293       if (SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
294         if (SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
295           vw->onAdjustTrihedron();
296
297           if (VISU_ActorBase* anActor = VISU::FindActorBase(vw, thePrs)) {
298             anActor->SetPosition(theOffset);
299             vw->highlight(thePrs->GetIO(), 1);
300             vw->getRenderer()->ResetCameraClippingRange();
301             vw->Repaint();
302           }
303         }
304       }
305     }
306   }
307 }
308
309 void VisuGUI_OffsetDlg::accept()
310 {
311   double anOffset[3];
312   getOffset(anOffset);
313   for (int i = 0; i < myPrsList.count(); i++) {
314     updateOffset(myPrsList.at(i), anOffset);
315   }
316   for (int i = 0; i < myPointMapList.count(); i++) {
317     updatePointMapOffset(myPointMapList.at(i), anOffset);
318   }
319   QDialog::accept();
320 }
321
322 void VisuGUI_OffsetDlg::reject()
323 {
324   for (int i = 0; i < myPrsList.count(); i++) {
325     updateOffset(myPrsList.at(i), myOldOffsets[i].myOffset);
326   }
327   for (int i = 0; i < myPointMapList.count(); i++) {
328     updatePointMapOffset(myPointMapList.at(i), myOldPointMapOffsets[i].myOffset);
329   }
330   QDialog::reject();
331 }
332
333 void VisuGUI_OffsetDlg::onApply()
334 {
335   double anOffset[3];
336   getOffset(anOffset);
337
338   for (int i = 0; i < myPrsList.count(); i++) {
339     updateOffset(myPrsList.at(i), anOffset);
340   }
341   for (int i = 0; i < myPointMapList.count(); i++) {
342     updatePointMapOffset(myPointMapList.at(i), anOffset);
343   }
344 }
345
346 void VisuGUI_OffsetDlg::onHelp()
347 {
348   QString aHelpFileName = "translate_presentation_page.html";
349   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
350   if (app)
351     app->onHelpContextModule(myModule ? app->moduleName(myModule->moduleName()) : QString(""), aHelpFileName);
352   else {
353     QString platform;
354 #ifdef WIN32
355     platform = "winapplication";
356 #else
357     platform = "application";
358 #endif
359     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
360                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
361                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
362                              QObject::tr("BUT_OK"));
363   }
364 }
365
366 void VisuGUI_OffsetDlg::keyPressEvent( QKeyEvent* e )
367 {
368   QDialog::keyPressEvent( e );
369   if ( e->isAccepted() )
370     return;
371
372   if ( e->key() == Qt::Key_F1 )
373     {
374       e->accept();
375       onHelp();
376     }
377 }