Salome HOME
759c9948cda2971b8044c6e16174dd6883dc46e3
[modules/visu.git] / src / VISUGUI / VisuGUI_OffsetDlg.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include "VisuGUI_OffsetDlg.h"
22
23 #include "VisuGUI.h"
24 #include "VisuGUI_Tools.h"
25
26 #include "VISU_ViewManager_i.hh"
27 #include "VISU_Actor.h"
28
29 #include "LightApp_Application.h"
30 #include "SalomeApp_Application.h"
31 #include "SVTK_ViewWindow.h"
32 #include "SVTK_ViewModel.h"
33 #include "SUIT_Desktop.h"
34 #include "SUIT_Session.h"
35 #include "SUIT_MessageBox.h"
36 #include "SUIT_ResourceMgr.h"
37
38 #include "QtxDblSpinBox.h"
39
40 // VTK Includes
41 #include "vtkRenderer.h"
42
43 // QT Includes
44 #include <qhbox.h>
45 #include <qlabel.h>
46 #include <qlayout.h>
47 #include <qcheckbox.h>
48 #include <qgroupbox.h>
49 #include <qpushbutton.h>
50
51
52 #define  MAXVAL 1e10
53
54
55 VisuGUI_OffsetDlg::VisuGUI_OffsetDlg (VisuGUI* theModule)
56 : QDialog(VISU::GetDesktop(theModule), 0, false, WStyle_Customize |
57           WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
58 myModule(theModule)
59 {
60   myPrsList.setAutoDelete(false);
61
62   setName("VisuGUI_OffsetDlg");
63   setCaption(tr("TIT_OFFSETDLG"));
64   setSizeGripEnabled(TRUE);
65
66   QVBoxLayout* TopLayout = new QVBoxLayout (this);
67   TopLayout->setSpacing(6);
68   TopLayout->setMargin(11);
69
70   QHBox* anOffsetsPane = new QHBox (this);
71   anOffsetsPane->setSpacing(6);
72
73   new QLabel ("dX:", anOffsetsPane);
74   myDxEdt = new QtxDblSpinBox (anOffsetsPane, "myDxEdt");
75   myDxEdt->setRange(-MAXVAL, MAXVAL);
76
77   new QLabel("dY:", anOffsetsPane);
78   myDyEdt = new QtxDblSpinBox (anOffsetsPane, "myDyEdt");
79   myDyEdt->setRange(-MAXVAL, MAXVAL);
80
81   new QLabel("dZ:", anOffsetsPane);
82   myDzEdt = new QtxDblSpinBox (anOffsetsPane, "myDzEdt");
83   myDzEdt->setRange(-MAXVAL, MAXVAL);
84
85   QPushButton* aResetBtn = new QPushButton(tr("BTN_RESET"), anOffsetsPane);
86   connect(aResetBtn, SIGNAL(clicked()), this, SLOT(onReset()));
87
88   TopLayout->addWidget(anOffsetsPane);
89
90   if (!VISU::GetCStudy(VISU::GetAppStudy(theModule))->GetProperties()->IsLocked()) {
91     mySaveChk = new QCheckBox ("Save to presentation", this);
92     TopLayout->addWidget(mySaveChk);
93     mySaveChk->setChecked(true);
94   } else {
95     mySaveChk = 0;
96   }
97
98   // Common buttons ===========================================================
99   QGroupBox* GroupButtons = new QGroupBox(this, "GroupButtons");
100   GroupButtons->setColumnLayout(0, Qt::Vertical);
101   GroupButtons->layout()->setSpacing(0);
102   GroupButtons->layout()->setMargin(0);
103   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
104   GroupButtonsLayout->setAlignment(Qt::AlignTop);
105   GroupButtonsLayout->setSpacing(6);
106   GroupButtonsLayout->setMargin(11);
107
108   QPushButton* buttonOk = new QPushButton(tr("&OK"), GroupButtons, "buttonOk");
109   buttonOk->setAutoDefault(TRUE);
110   buttonOk->setDefault(TRUE);
111   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
112   GroupButtonsLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1);
113
114   QPushButton* buttonApply = new QPushButton(tr("&Apply"), GroupButtons, "buttonApply");
115   buttonOk->setAutoDefault(TRUE);
116   GroupButtonsLayout->addWidget(buttonApply, 0, 2);
117   GroupButtonsLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 3);
118
119   QPushButton* buttonCancel = new QPushButton(tr("&Cancel") , GroupButtons, "buttonCancel");
120   buttonCancel->setAutoDefault(TRUE);
121   GroupButtonsLayout->addWidget(buttonCancel, 0, 4);
122   GroupButtonsLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 5);  
123
124   QPushButton* buttonHelp = new QPushButton(tr("&Help") , GroupButtons, "buttonHelp");
125   buttonHelp->setAutoDefault(TRUE);
126   GroupButtonsLayout->addWidget(buttonHelp, 0, 6);
127
128   TopLayout->addWidget(GroupButtons);
129
130   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(accept()));
131   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(onApply()));
132   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
133   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(onHelp()));
134 }
135
136 void VisuGUI_OffsetDlg::addPresentation (VISU::Prs3d_i* thePrs)
137 {
138   myPrsList.append(thePrs);
139   CORBA::Float anOffset[3];
140   thePrs->GetOffset(anOffset[0],anOffset[1],anOffset[2]);
141   OffsetStruct anOffs(anOffset[0],anOffset[1],anOffset[2]);
142   myOldOffsets.append(anOffs);
143   if (myPrsList.count() == 1) {
144     setOffset(anOffs.myOffset);
145   } else if (myPrsList.count() == 2) {
146     OffsetStruct anOffs;
147     setOffset(anOffs.myOffset);
148   }
149 }
150
151 void VisuGUI_OffsetDlg::setOffset (const vtkFloatingPointType* theOffset)
152 {
153   myDxEdt->setValue(theOffset[0]);
154   myDyEdt->setValue(theOffset[1]);
155   myDzEdt->setValue(theOffset[2]);
156 }
157
158 void VisuGUI_OffsetDlg::getOffset (vtkFloatingPointType* theOffset) const
159 {
160   theOffset[0] = myDxEdt->value();
161   theOffset[1] = myDyEdt->value();
162   theOffset[2] = myDzEdt->value();
163 }
164
165 void VisuGUI_OffsetDlg::onReset()
166 {
167   myDxEdt->setValue(0);
168   myDyEdt->setValue(0);
169   myDzEdt->setValue(0);
170 }
171
172 bool VisuGUI_OffsetDlg::isToSave() const
173 {
174   if (mySaveChk)
175     return mySaveChk->isChecked();
176   else
177     return false;
178 }
179
180 void VisuGUI_OffsetDlg::updateOffset (VISU::Prs3d_i* thePrs, vtkFloatingPointType* theOffset)
181 {
182   if (myPrsList.count() == 0) return;
183
184   if (isToSave()) thePrs->SetOffset(theOffset[0],theOffset[1],theOffset[2]);
185
186   ViewManagerList aViewManagerList;
187   SalomeApp_Application* anApp = myModule->getApp();
188   anApp->viewManagers(aViewManagerList);
189   QPtrListIterator<SUIT_ViewManager> anVMIter (aViewManagerList);
190   for (; anVMIter.current(); ++anVMIter) {
191     SUIT_ViewManager* aViewManager = anVMIter.current();
192     QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
193     for (int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
194       if (SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
195         if (SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
196           vw->onAdjustTrihedron();
197
198           if (VISU_Actor* anActor = VISU::GetActor(thePrs, vw)) {
199             anActor->SetPosition(theOffset);
200             vw->getRenderer()->ResetCameraClippingRange();
201             vw->Repaint();
202
203             Handle(SALOME_InteractiveObject) anIO;
204             CORBA::Object_var anObject = VISU::GetSelectedObj(myModule, &anIO);
205             if (!CORBA::is_nil(anObject))
206               vw->highlight(anIO, 1);
207           }
208         }
209       }
210     }
211   }
212 }
213
214 void VisuGUI_OffsetDlg::accept()
215 {
216   vtkFloatingPointType anOffset[3];
217   getOffset(anOffset);
218   for (int i = 0; i < myPrsList.count(); i++) {
219     updateOffset(myPrsList.at(i), anOffset);
220   }
221   QDialog::accept();
222 }
223
224 void VisuGUI_OffsetDlg::reject()
225 {
226   for (int i = 0; i < myPrsList.count(); i++) {
227     updateOffset(myPrsList.at(i), myOldOffsets[i].myOffset);
228   }
229   QDialog::reject();
230 }
231
232 void VisuGUI_OffsetDlg::onApply()
233 {
234   vtkFloatingPointType anOffset[3];
235   getOffset(anOffset);
236   for (int i = 0; i < myPrsList.count(); i++) {
237     updateOffset(myPrsList.at(i), anOffset);
238   }
239 }
240
241 void VisuGUI_OffsetDlg::onHelp()
242 {
243   QString aHelpFileName = "/files/scaling_presentations.htm";
244   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
245   if (app)
246     app->onHelpContextModule(myModule ? app->moduleName(myModule->moduleName()) : QString(""), aHelpFileName);
247   else {
248     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
249                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
250                            arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(aHelpFileName),
251                            QObject::tr("BUT_OK"));
252   }
253 }