Salome HOME
3fca41a1d23db6973f7ec8d02551a5601f94ac12
[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/
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 "SalomeApp_Application.h"
30 #include "SVTK_ViewWindow.h"
31 #include "SVTK_ViewModel.h"
32 #include "SUIT_Desktop.h"
33
34 #include "QtxDblSpinBox.h"
35
36 // VTK Includes
37 #include "vtkRenderer.h"
38
39 // QT Includes
40 #include <qhbox.h>
41 #include <qlabel.h>
42 #include <qlayout.h>
43 #include <qcheckbox.h>
44 #include <qgroupbox.h>
45 #include <qpushbutton.h>
46
47
48 #define  MAXVAL 1e10
49
50
51 VisuGUI_OffsetDlg::VisuGUI_OffsetDlg (VisuGUI* theModule)
52 : QDialog(VISU::GetDesktop(theModule), 0, false, WStyle_Customize |
53           WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
54 myModule(theModule)
55 {
56   myPrsList.setAutoDelete(false);
57
58   setName("VisuGUI_OffsetDlg");
59   setCaption(tr("TIT_OFFSETDLG"));
60   setSizeGripEnabled(TRUE);
61
62   QVBoxLayout* TopLayout = new QVBoxLayout (this);
63   TopLayout->setSpacing(6);
64   TopLayout->setMargin(11);
65
66   QHBox* aOffsetsPane = new QHBox (this);
67   aOffsetsPane->setSpacing(6);
68
69   new QLabel ("dX:", aOffsetsPane);
70   myDxEdt = new QtxDblSpinBox (aOffsetsPane, "myDxEdt");
71   myDxEdt->setRange(-MAXVAL, MAXVAL);
72
73   new QLabel("dY:", aOffsetsPane);
74   myDyEdt = new QtxDblSpinBox (aOffsetsPane, "myDyEdt");
75   myDyEdt->setRange(-MAXVAL, MAXVAL);
76
77   new QLabel("dZ:", aOffsetsPane);
78   myDzEdt = new QtxDblSpinBox (aOffsetsPane, "myDzEdt");
79   myDzEdt->setRange(-MAXVAL, MAXVAL);
80
81   QPushButton* aResetBtn = new QPushButton(tr("BTN_RESET"), aOffsetsPane);
82   connect(aResetBtn, SIGNAL(clicked()), this, SLOT(onReset()));
83
84   TopLayout->addWidget(aOffsetsPane);
85
86   if (!VISU::GetCStudy(VISU::GetAppStudy(theModule))->GetProperties()->IsLocked()) {
87     mySaveChk = new QCheckBox ("Save to presentation", this);
88     TopLayout->addWidget(mySaveChk);
89     mySaveChk->setChecked(true);
90   } else {
91     mySaveChk = 0;
92   }
93
94   // Common buttons ===========================================================
95   QGroupBox* GroupButtons = new QGroupBox(this, "GroupButtons");
96   GroupButtons->setColumnLayout(0, Qt::Vertical);
97   GroupButtons->layout()->setSpacing(0);
98   GroupButtons->layout()->setMargin(0);
99   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
100   GroupButtonsLayout->setAlignment(Qt::AlignTop);
101   GroupButtonsLayout->setSpacing(6);
102   GroupButtonsLayout->setMargin(11);
103
104   QPushButton* buttonOk = new QPushButton(tr("&OK"), GroupButtons, "buttonOk");
105   buttonOk->setAutoDefault(TRUE);
106   buttonOk->setDefault(TRUE);
107   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
108   GroupButtonsLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1);
109
110   QPushButton* buttonApply = new QPushButton(tr("&Apply"), GroupButtons, "buttonApply");
111   buttonOk->setAutoDefault(TRUE);
112   GroupButtonsLayout->addWidget(buttonApply, 0, 2);
113   GroupButtonsLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 3);
114
115   QPushButton* buttonCancel = new QPushButton(tr("&Cancel") , GroupButtons, "buttonCancel");
116   buttonCancel->setAutoDefault(TRUE);
117   GroupButtonsLayout->addWidget(buttonCancel, 0, 4);
118
119   TopLayout->addWidget(GroupButtons);
120
121   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(accept()));
122   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(onApply()));
123   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
124 }
125
126 void VisuGUI_OffsetDlg::addPresentation (VISU::Prs3d_i* thePrs)
127 {
128   myPrsList.append(thePrs);
129   OffsetStruct aOffs;
130   thePrs->GetOffset(aOffs.myOffset);
131   myOldOffsets.append(aOffs);
132   if (myPrsList.count() == 1) {
133     setOffset(aOffs.myOffset);
134   } else if (myPrsList.count() == 2) {
135     float aOffset[3];
136     aOffset[0] = aOffset[1] = aOffset[2] = 0;
137     setOffset(aOffset);
138   }
139 }
140
141 void VisuGUI_OffsetDlg::setOffset (const float* theOffset)
142 {
143   myDxEdt->setValue(theOffset[0]);
144   myDyEdt->setValue(theOffset[1]);
145   myDzEdt->setValue(theOffset[2]);
146 }
147
148 void VisuGUI_OffsetDlg::getOffset (float* theOffset) const
149 {
150   theOffset[0] = myDxEdt->value();
151   theOffset[1] = myDyEdt->value();
152   theOffset[2] = myDzEdt->value();
153 }
154
155 void VisuGUI_OffsetDlg::onReset()
156 {
157   myDxEdt->setValue(0);
158   myDyEdt->setValue(0);
159   myDzEdt->setValue(0);
160 }
161
162 bool VisuGUI_OffsetDlg::isToSave() const
163 {
164   if (mySaveChk)
165     return mySaveChk->isChecked();
166   else
167     return false;
168 }
169
170 void VisuGUI_OffsetDlg::updateOffset (VISU::Prs3d_i* thePrs, float* theOffset)
171 {
172   if (myPrsList.count() == 0) return;
173
174   if (isToSave()) thePrs->SetOffset(theOffset);
175
176   ViewManagerList aViewManagerList;
177   SalomeApp_Application* anApp = myModule->getApp();
178   anApp->viewManagers(aViewManagerList);
179   QPtrListIterator<SUIT_ViewManager> anVMIter (aViewManagerList);
180   for (; anVMIter.current(); ++anVMIter) {
181     SUIT_ViewManager* aViewManager = anVMIter.current();
182     QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
183     for (int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
184       if (SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
185         if (SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
186           vw->onAdjustTrihedron();
187
188           if (VISU_Actor* anActor = VISU::GetActor(thePrs, vw)) {
189             anActor->SetPosition(theOffset);
190             vw->getRenderer()->ResetCameraClippingRange();
191             vw->Repaint();
192
193             Handle(SALOME_InteractiveObject) anIO;
194             CORBA::Object_var anObject = VISU::GetSelectedObj(myModule, &anIO);
195             if (!CORBA::is_nil(anObject))
196               vw->highlight(anIO, 1);
197           }
198         }
199       }
200     }
201   }
202 }
203
204 void VisuGUI_OffsetDlg::accept()
205 {
206   float aOffset[3];
207   getOffset(aOffset);
208   for (int i = 0; i < myPrsList.count(); i++) {
209     updateOffset(myPrsList.at(i), aOffset);
210   }
211   QDialog::accept();
212 }
213
214 void VisuGUI_OffsetDlg::reject()
215 {
216   for (int i = 0; i < myPrsList.count(); i++) {
217     updateOffset(myPrsList.at(i), myOldOffsets[i].myOffset);
218   }
219   QDialog::reject();
220 }
221
222 void VisuGUI_OffsetDlg::onApply()
223 {
224   float aOffset[3];
225   getOffset(aOffset);
226   for (int i = 0; i < myPrsList.count(); i++) {
227     updateOffset(myPrsList.at(i), aOffset);
228   }
229 }