Salome HOME
Porting Salome to OCCT 7.7.0
[modules/gui.git] / src / OCCViewer / OCCViewer_CubeAxesDlg.cxx
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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 "OCCViewer_CubeAxesDlg.h"
24
25 #include "OCCViewer_FontWidget.h"
26 #include "OCCViewer_ViewPort3d.h"
27 #include "OCCViewer_ViewWindow.h"
28
29 #include "QtxAction.h"
30 #include "QtxIntSpinBox.h"
31
32 #include <Graphic3d_GraduatedTrihedron.hxx>
33
34 #include <QCheckBox>
35 #include <QGroupBox>
36 #include <QLineEdit>
37
38 #include <Basics_OCCTVersion.hxx>
39
40 /*!
41   \class OCCViewer_CubeAxesDlg::AxisWidget
42   \brief Axis tab widget of the "Graduated axis" dialog box
43   \internal
44 */
45
46 /*!
47   Constructor
48 */
49 OCCViewer_AxisWidget::OCCViewer_AxisWidget (QWidget* theParent)
50 : ViewerTools_AxisWidgetBase(theParent)
51 {
52 }
53
54 /*!
55   Destructor
56 */
57 OCCViewer_AxisWidget::~OCCViewer_AxisWidget()
58 {
59 }
60
61 /*!
62   Create font widget
63 */
64 ViewerTools_FontWidgetBase* OCCViewer_AxisWidget::createFontWidget( QWidget* theParent )
65 {
66   OCCViewer_FontWidget* aFontWidget = new OCCViewer_FontWidget( theParent );
67   aFontWidget->Initialize();
68   return aFontWidget;
69 }
70
71 /*!
72   Set axis data
73 */
74 void OCCViewer_AxisWidget::SetData( const AxisData& theAxisData )
75 {
76   myNameGrp->setChecked( theAxisData.DrawName );
77   myAxisName->setText( theAxisData.Name );
78   myNameFont->SetData( theAxisData.NameColor, 0, false, false, false );
79
80   myLabelsGrp->setChecked( theAxisData.DrawValues );
81   myLabelNumber->setValue( theAxisData.NbValues );
82   myLabelOffset->setValue( theAxisData.Offset );
83   myLabelsFont->SetData( theAxisData.Color, 0, false, false, false );
84
85   myTicksGrp->setChecked( theAxisData.DrawTickmarks );
86   myTickLength->setValue( theAxisData.TickmarkLength );
87 }
88
89 /*!
90   Get axis data
91 */
92 void OCCViewer_AxisWidget::GetData( AxisData& theAxisData )
93 {
94   theAxisData.DrawName = myNameGrp->isChecked();
95   theAxisData.Name = myAxisName->text();
96
97   int aNameFamily;
98   bool aNameBold, aNameItalic, aNameShadow;
99   myNameFont->GetData( theAxisData.NameColor, aNameFamily, aNameBold, aNameItalic, aNameShadow );
100
101   theAxisData.DrawValues = myLabelsGrp->isChecked();
102   theAxisData.NbValues = myLabelNumber->value();
103   theAxisData.Offset = myLabelOffset->value();
104
105   int aLabelFamily;
106   bool aLabelBold, aLabelItalic, aLabelShadow;
107   myLabelsFont->GetData( theAxisData.Color, aLabelFamily, aLabelBold, aLabelItalic, aLabelShadow );
108
109   theAxisData.DrawTickmarks = myTicksGrp->isChecked();
110   theAxisData.TickmarkLength = myTickLength->value();
111 }
112
113 /*
114   Class       : OCCViewer_CubeAxesDlg
115   Description : Dialog for specifying cube axes properties
116 */
117
118 /*!
119   Constructor
120 */
121 OCCViewer_CubeAxesDlg::OCCViewer_CubeAxesDlg(QtxAction* theAction,
122                                              OCCViewer_ViewWindow* theParent,
123                                              const char* theName):
124   ViewerTools_CubeAxesDlgBase(theAction,
125                               theParent, 
126                               theName),
127   myMainWindow(theParent)
128 {
129 }
130
131 /*!
132   Destructor
133 */
134 OCCViewer_CubeAxesDlg::~OCCViewer_CubeAxesDlg()
135 {
136 }
137
138 /*!
139   Create axis widget
140 */
141 ViewerTools_AxisWidgetBase* OCCViewer_CubeAxesDlg::createAxisWidget( QWidget* theParent )
142 {
143   OCCViewer_AxisWidget* anAxisWidget = new OCCViewer_AxisWidget( theParent );
144   anAxisWidget->initialize();
145   return anAxisWidget;
146 }
147
148 /*!
149   Update dialog fields, connect signals and slots, show dialog
150 */
151 void OCCViewer_CubeAxesDlg::initialize()
152 {
153   ViewerTools_CubeAxesDlgBase::initialize();
154
155   for( int i = 0; i < 3; i++ )
156   {
157     if( OCCViewer_AxisWidget* anAxisWidget = dynamic_cast<OCCViewer_AxisWidget*>( myAxes[ i ] ) )
158     {
159       OCCViewer_AxisWidget::AxisData anAxisData;
160       switch( i )
161       {
162         case 0:
163           anAxisData.Name = "X";
164           anAxisData.NameColor = anAxisData.Color = Qt::red;
165           break;
166         case 1:
167           anAxisData.Name = "Y";
168           anAxisData.NameColor = anAxisData.Color = Qt::green;
169           break;
170         case 2:
171           anAxisData.Name = "Z";
172           anAxisData.NameColor = anAxisData.Color = Qt::blue;
173           break;
174       }
175       anAxisData.DrawName       = true;
176       anAxisData.DrawValues     = true;
177       anAxisData.NbValues       = 3;
178       anAxisData.Offset         = 2;
179       anAxisData.DrawTickmarks  = true;
180       anAxisData.TickmarkLength = 5;
181       anAxisWidget->SetData( anAxisData );
182     }
183   }
184 }
185
186 /*!
187   Update dialog fields, connect signals and slots, show dialog
188 */
189 void OCCViewer_CubeAxesDlg::Update()
190 {
191   ViewerTools_CubeAxesDlgBase::Update();
192 }
193
194 /*!
195   Verify validity of entry data
196 */
197 bool OCCViewer_CubeAxesDlg::onApply()
198 {
199   bool isOk = ViewerTools_CubeAxesDlgBase::onApply();
200
201   try
202   {
203     QWidget *aCurrWid = this->focusWidget();
204     aCurrWid->clearFocus();
205     aCurrWid->setFocus();
206
207     if( OCCViewer_ViewPort3d* aViewPort = myMainWindow->getViewPort() )
208     {
209       Handle(V3d_View) aView = aViewPort->getView();
210       if( !aView.IsNull() )
211         ApplyData( aView );
212     }
213   }
214   catch(...)
215   {
216     isOk = false;
217   }
218
219   return isOk;
220 }
221
222 /*!
223   Get graduated trihedron parameters
224 */
225 void OCCViewer_CubeAxesDlg::GetData( bool& theIsVisible, OCCViewer_AxisWidget::AxisData theAxisData[3] )
226 {
227   theIsVisible = myIsVisible->isChecked();
228   for( int i = 0; i < 3; i++ )
229     if( OCCViewer_AxisWidget* anAxisWidget = dynamic_cast<OCCViewer_AxisWidget*>( myAxes[ i ] ) )
230       anAxisWidget->GetData( theAxisData[i] );
231 }
232
233 /*!
234   Set graduated trihedron parameters
235 */
236 void OCCViewer_CubeAxesDlg::SetData( bool theIsVisible, OCCViewer_AxisWidget::AxisData theAxisData[3] )
237 {
238   myIsVisible->setChecked( theIsVisible );
239   for( int i = 0; i < 3; i++ )
240     if( OCCViewer_AxisWidget* anAxisWidget = dynamic_cast<OCCViewer_AxisWidget*>( myAxes[ i ] ) )
241       anAxisWidget->SetData( theAxisData[i] );
242 }
243
244 /*!
245   Apply graduated trihedron parameters
246 */
247 void OCCViewer_CubeAxesDlg::ApplyData( const Handle(V3d_View)& theView )
248 {
249   if( theView.IsNull() )
250     return;
251
252   if( myIsVisible->isChecked() )
253   {
254     OCCViewer_AxisWidget::AxisData anAxisData[3];
255     for( int i = 0; i < 3; i++ ) {
256       if( OCCViewer_AxisWidget* anAxisWidget = dynamic_cast<OCCViewer_AxisWidget*>( myAxes[ i ] ) )
257         anAxisWidget->GetData( anAxisData[i] );
258     }
259
260     // A gap used offset of axis names' offset
261     // (this hard-coded value will be removed when the
262     // font support will be introduced in OCC-6.4)
263     int aGap = 20;
264
265     Graphic3d_GraduatedTrihedron gt;
266     // main params
267     gt.SetDrawGrid(Standard_True);       // to draw grid
268     gt.SetDrawAxes(Standard_True);       // to draw axes
269     gt.SetGridColor(Quantity_NOC_WHITE); // grid color
270     // axes params
271     for ( int i = 0; i < 3; i++ ) {
272 #if OCC_VERSION_LARGE < 0x07070000
273       Graphic3d_AxisAspect& aspect = gt.ChangeAxisAspect(i);
274 #else
275       Graphic3d_GraduatedTrihedron::AxisAspect& aspect = gt.ChangeAxisAspect(i);
276 #endif
277       aspect.SetName(anAxisData[i].Name.toUtf8().constData());
278       aspect.SetDrawName(anAxisData[i].DrawName);
279       aspect.SetDrawValues(anAxisData[i].DrawValues);
280       aspect.SetDrawTickmarks(anAxisData[i].DrawTickmarks);
281       aspect.SetNameColor(Quantity_Color(anAxisData[i].NameColor.redF(),
282                                          anAxisData[i].NameColor.greenF(),
283                                          anAxisData[i].NameColor.blueF(),
284                                          Quantity_TOC_RGB));
285       aspect.SetColor(Quantity_Color(anAxisData[i].Color.redF(),
286                                      anAxisData[i].Color.greenF(),
287                                      anAxisData[i].Color.blueF(),
288                                      Quantity_TOC_RGB));
289       aspect.SetTickmarksNumber(anAxisData[i].NbValues-1);
290       aspect.SetTickmarksLength(anAxisData[i].TickmarkLength);
291       aspect.SetValuesOffset(anAxisData[i].Offset);
292       aspect.SetNameOffset(anAxisData[i].Offset + aGap); // see above
293     }
294     // draw trihedron
295     theView->GraduatedTrihedronDisplay(gt);
296   }
297   else
298     theView->GraduatedTrihedronErase();
299
300   theView->Redraw();
301 }