Salome HOME
fa66a70546057c25d47c9d3377c55816ff85f990
[modules/gui.git] / src / OCCViewer / OCCViewer_CubeAxesDlg.cxx
1 // Copyright (C) 2007-2015  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 <Basics_OCCTVersion.hxx>
33
34 #if OCC_VERSION_LARGE > 0x06080000
35 #include <Graphic3d_GraduatedTrihedron.hxx>
36 #endif
37
38 #include <QCheckBox>
39 #include <QGroupBox>
40 #include <QLineEdit>
41
42 /*!
43   \class OCCViewer_CubeAxesDlg::AxisWidget
44   \brief Axis tab widget of the "Graduated axis" dialog box
45   \internal
46 */
47
48 /*!
49   Constructor
50 */
51 OCCViewer_AxisWidget::OCCViewer_AxisWidget (QWidget* theParent)
52 : ViewerTools_AxisWidgetBase(theParent)
53 {
54 }
55
56 /*!
57   Destructor
58 */
59 OCCViewer_AxisWidget::~OCCViewer_AxisWidget()
60 {
61 }
62
63 /*!
64   Create font widget
65 */
66 ViewerTools_FontWidgetBase* OCCViewer_AxisWidget::createFontWidget( QWidget* theParent )
67 {
68   OCCViewer_FontWidget* aFontWidget = new OCCViewer_FontWidget( theParent );
69   aFontWidget->Initialize();
70   return aFontWidget;
71 }
72
73 /*!
74   Set axis data
75 */
76 void OCCViewer_AxisWidget::SetData( const AxisData& theAxisData )
77 {
78   myNameGrp->setChecked( theAxisData.DrawName );
79   myAxisName->setText( theAxisData.Name );
80   myNameFont->SetData( theAxisData.NameColor, 0, false, false, false );
81
82   myLabelsGrp->setChecked( theAxisData.DrawValues );
83   myLabelNumber->setValue( theAxisData.NbValues );
84   myLabelOffset->setValue( theAxisData.Offset );
85   myLabelsFont->SetData( theAxisData.Color, 0, false, false, false );
86
87   myTicksGrp->setChecked( theAxisData.DrawTickmarks );
88   myTickLength->setValue( theAxisData.TickmarkLength );
89 }
90
91 /*!
92   Get axis data
93 */
94 void OCCViewer_AxisWidget::GetData( AxisData& theAxisData )
95 {
96   theAxisData.DrawName = myNameGrp->isChecked();
97   theAxisData.Name = myAxisName->text();
98
99   int aNameFamily;
100   bool aNameBold, aNameItalic, aNameShadow;
101   myNameFont->GetData( theAxisData.NameColor, aNameFamily, aNameBold, aNameItalic, aNameShadow );
102
103   theAxisData.DrawValues = myLabelsGrp->isChecked();
104   theAxisData.NbValues = myLabelNumber->value();
105   theAxisData.Offset = myLabelOffset->value();
106
107   int aLabelFamily;
108   bool aLabelBold, aLabelItalic, aLabelShadow;
109   myLabelsFont->GetData( theAxisData.Color, aLabelFamily, aLabelBold, aLabelItalic, aLabelShadow );
110
111   theAxisData.DrawTickmarks = myTicksGrp->isChecked();
112   theAxisData.TickmarkLength = myTickLength->value();
113 }
114
115 /*
116   Class       : OCCViewer_CubeAxesDlg
117   Description : Dialog for specifying cube axes properties
118 */
119
120 /*!
121   Constructor
122 */
123 OCCViewer_CubeAxesDlg::OCCViewer_CubeAxesDlg(QtxAction* theAction,
124                                              OCCViewer_ViewWindow* theParent,
125                                              const char* theName):
126   ViewerTools_CubeAxesDlgBase(theAction,
127                               theParent, 
128                               theName),
129   myMainWindow(theParent)
130 {
131 }
132
133 /*!
134   Destructor
135 */
136 OCCViewer_CubeAxesDlg::~OCCViewer_CubeAxesDlg()
137 {
138 }
139
140 /*!
141   Create axis widget
142 */
143 ViewerTools_AxisWidgetBase* OCCViewer_CubeAxesDlg::createAxisWidget( QWidget* theParent )
144 {
145   OCCViewer_AxisWidget* anAxisWidget = new OCCViewer_AxisWidget( theParent );
146   anAxisWidget->initialize();
147   return anAxisWidget;
148 }
149
150 /*!
151   Update dialog fields, connect signals and slots, show dialog
152 */
153 void OCCViewer_CubeAxesDlg::initialize()
154 {
155   ViewerTools_CubeAxesDlgBase::initialize();
156
157   for( int i = 0; i < 3; i++ )
158   {
159     if( OCCViewer_AxisWidget* anAxisWidget = dynamic_cast<OCCViewer_AxisWidget*>( myAxes[ i ] ) )
160     {
161       OCCViewer_AxisWidget::AxisData anAxisData;
162       switch( i )
163       {
164         case 0:
165           anAxisData.Name = "X";
166           anAxisData.NameColor = anAxisData.Color = Qt::red;
167           break;
168         case 1:
169           anAxisData.Name = "Y";
170           anAxisData.NameColor = anAxisData.Color = Qt::green;
171           break;
172         case 2:
173           anAxisData.Name = "Z";
174           anAxisData.NameColor = anAxisData.Color = Qt::blue;
175           break;
176       }
177       anAxisData.DrawName       = true;
178       anAxisData.DrawValues     = true;
179       anAxisData.NbValues       = 3;
180       anAxisData.Offset         = 2;
181       anAxisData.DrawTickmarks  = true;
182       anAxisData.TickmarkLength = 5;
183       anAxisWidget->SetData( anAxisData );
184     }
185   }
186 }
187
188 /*!
189   Update dialog fields, connect signals and slots, show dialog
190 */
191 void OCCViewer_CubeAxesDlg::Update()
192 {
193   ViewerTools_CubeAxesDlgBase::Update();
194 }
195
196 /*!
197   Verify validity of entry data
198 */
199 bool OCCViewer_CubeAxesDlg::onApply()
200 {
201   bool isOk = ViewerTools_CubeAxesDlgBase::onApply();
202
203   try
204   {
205     QWidget *aCurrWid = this->focusWidget();
206     aCurrWid->clearFocus();
207     aCurrWid->setFocus();
208
209     if( OCCViewer_ViewPort3d* aViewPort = myMainWindow->getViewPort() )
210     {
211       Handle(V3d_View) aView = aViewPort->getView();
212       if( !aView.IsNull() )
213         ApplyData( aView );
214     }
215   }
216   catch(...)
217   {
218     isOk = false;
219   }
220
221   return isOk;
222 }
223
224 /*!
225   Get graduated trihedron parameters
226 */
227 void OCCViewer_CubeAxesDlg::GetData( bool& theIsVisible, OCCViewer_AxisWidget::AxisData theAxisData[3] )
228 {
229   theIsVisible = myIsVisible->isChecked();
230   for( int i = 0; i < 3; i++ )
231     if( OCCViewer_AxisWidget* anAxisWidget = dynamic_cast<OCCViewer_AxisWidget*>( myAxes[ i ] ) )
232       anAxisWidget->GetData( theAxisData[i] );
233 }
234
235 /*!
236   Set graduated trihedron parameters
237 */
238 void OCCViewer_CubeAxesDlg::SetData( bool theIsVisible, OCCViewer_AxisWidget::AxisData theAxisData[3] )
239 {
240   myIsVisible->setChecked( theIsVisible );
241   for( int i = 0; i < 3; i++ )
242     if( OCCViewer_AxisWidget* anAxisWidget = dynamic_cast<OCCViewer_AxisWidget*>( myAxes[ i ] ) )
243       anAxisWidget->SetData( theAxisData[i] );
244 }
245
246 /*!
247   Apply graduated trihedron parameters
248 */
249 void OCCViewer_CubeAxesDlg::ApplyData( const Handle(V3d_View)& theView )
250 {
251   if( theView.IsNull() )
252     return;
253
254   if( myIsVisible->isChecked() )
255   {
256     OCCViewer_AxisWidget::AxisData anAxisData[3];
257     for( int i = 0; i < 3; i++ ) {
258       if( OCCViewer_AxisWidget* anAxisWidget = dynamic_cast<OCCViewer_AxisWidget*>( myAxes[ i ] ) )
259         anAxisWidget->GetData( anAxisData[i] );
260     }
261
262     // A gap used offset of axis names' offset
263     // (this hard-coded value will be removed when the
264     // font support will be introduced in OCC-6.4)
265     int aGap = 20;
266
267 #if OCC_VERSION_LARGE > 0x06080000
268     Graphic3d_GraduatedTrihedron gt;
269     // main params
270     gt.SetToDrawTickmarks(Standard_True);  // to draw grid
271     gt.SetToDrawAxes(Standard_True);       // to draw axes
272     gt.SetGridColor(Quantity_NOC_WHITE);   // grid color
273     // X axis aspect
274     gt.ChangeXAxisAspect().SetName(anAxisData[0].Name.toLatin1().constData());
275     gt.ChangeXAxisAspect().SetToDrawName(anAxisData[0].DrawName);
276     gt.ChangeXAxisAspect().SetToDrawValues(anAxisData[0].DrawValues);
277     gt.ChangeXAxisAspect().SetToDrawTickmarks(anAxisData[0].DrawTickmarks);
278     gt.ChangeXAxisAspect().SetNameColor(Quantity_Color(anAxisData[0].NameColor.redF(),
279                                                        anAxisData[0].NameColor.greenF(),
280                                                        anAxisData[0].NameColor.blueF(),
281                                                        Quantity_TOC_RGB));
282     gt.ChangeXAxisAspect().SetColor(Quantity_Color(anAxisData[0].Color.redF(),
283                                                    anAxisData[0].Color.greenF(),
284                                                    anAxisData[0].Color.blueF(),
285                                                    Quantity_TOC_RGB));
286     gt.ChangeXAxisAspect().SetTickmarkNumber(anAxisData[0].NbValues-1);
287     gt.ChangeXAxisAspect().SetTickmarkLength(anAxisData[0].TickmarkLength);
288     gt.ChangeXAxisAspect().SetValuesOffset(anAxisData[0].Offset);
289     gt.ChangeXAxisAspect().SetNameOffset(anAxisData[0].Offset + aGap); // see above
290     // Y axis aspect
291     gt.ChangeYAxisAspect().SetName(anAxisData[1].Name.toLatin1().constData());
292     gt.ChangeYAxisAspect().SetToDrawName(anAxisData[1].DrawName);
293     gt.ChangeYAxisAspect().SetToDrawValues(anAxisData[1].DrawValues);
294     gt.ChangeYAxisAspect().SetToDrawTickmarks(anAxisData[1].DrawTickmarks);
295     gt.ChangeYAxisAspect().SetNameColor(Quantity_Color(anAxisData[1].NameColor.redF(),
296                                                        anAxisData[1].NameColor.greenF(),
297                                                        anAxisData[1].NameColor.blueF(),
298                                                        Quantity_TOC_RGB));
299     gt.ChangeYAxisAspect().SetColor(Quantity_Color(anAxisData[1].Color.redF(),
300                                                    anAxisData[1].Color.greenF(),
301                                                    anAxisData[1].Color.blueF(),
302                                                    Quantity_TOC_RGB));
303     gt.ChangeYAxisAspect().SetTickmarkNumber(anAxisData[1].NbValues-1);
304     gt.ChangeYAxisAspect().SetTickmarkLength(anAxisData[1].TickmarkLength);
305     gt.ChangeYAxisAspect().SetValuesOffset(anAxisData[1].Offset);
306     gt.ChangeYAxisAspect().SetNameOffset(anAxisData[1].Offset + aGap); // see above
307     // Z axis aspect
308     gt.ChangeZAxisAspect().SetName(anAxisData[2].Name.toLatin1().constData());
309     gt.ChangeZAxisAspect().SetToDrawName(anAxisData[2].DrawName);
310     gt.ChangeZAxisAspect().SetToDrawValues(anAxisData[2].DrawValues);
311     gt.ChangeZAxisAspect().SetToDrawTickmarks(anAxisData[2].DrawTickmarks);
312     gt.ChangeZAxisAspect().SetNameColor(Quantity_Color(anAxisData[2].NameColor.redF(),
313                                                        anAxisData[2].NameColor.greenF(),
314                                                        anAxisData[2].NameColor.blueF(),
315                                                        Quantity_TOC_RGB));
316     gt.ChangeZAxisAspect().SetColor(Quantity_Color(anAxisData[2].Color.redF(),
317                                                    anAxisData[2].Color.greenF(),
318                                                    anAxisData[2].Color.blueF(),
319                                                    Quantity_TOC_RGB));
320     gt.ChangeZAxisAspect().SetTickmarkNumber(anAxisData[2].NbValues-1);
321     gt.ChangeZAxisAspect().SetTickmarkLength(anAxisData[2].TickmarkLength);
322     gt.ChangeZAxisAspect().SetValuesOffset(anAxisData[2].Offset);
323     gt.ChangeZAxisAspect().SetNameOffset(anAxisData[2].Offset + aGap); // see above
324     // draw trihedron
325     theView->GraduatedTrihedronDisplay(gt);
326 #else
327     theView->GraduatedTrihedronDisplay(
328       anAxisData[0].Name.toLatin1().constData(),
329       anAxisData[1].Name.toLatin1().constData(),
330       anAxisData[2].Name.toLatin1().constData(),
331       anAxisData[0].DrawName,
332       anAxisData[1].DrawName,
333       anAxisData[2].DrawName,
334       anAxisData[0].DrawValues,
335       anAxisData[1].DrawValues,
336       anAxisData[2].DrawValues,
337       Standard_True, // draw grid
338       Standard_False, // draw axes
339       anAxisData[0].NbValues - 1,
340       anAxisData[1].NbValues - 1,
341       anAxisData[2].NbValues - 1,
342       anAxisData[0].Offset,
343       anAxisData[1].Offset,
344       anAxisData[2].Offset,
345       anAxisData[0].Offset + aGap, // see above
346       anAxisData[1].Offset + aGap, // see above
347       anAxisData[2].Offset + aGap, // see above
348       anAxisData[0].DrawTickmarks,
349       anAxisData[1].DrawTickmarks,
350       anAxisData[2].DrawTickmarks,
351       anAxisData[0].TickmarkLength,
352       anAxisData[1].TickmarkLength,
353       anAxisData[2].TickmarkLength,
354       Quantity_NOC_WHITE, // grid color
355       Quantity_Color( anAxisData[0].NameColor.redF(),
356                       anAxisData[0].NameColor.greenF(),
357                       anAxisData[0].NameColor.blueF(),
358                       Quantity_TOC_RGB ),
359       Quantity_Color( anAxisData[1].NameColor.redF(),
360                       anAxisData[1].NameColor.greenF(),
361                       anAxisData[1].NameColor.blueF(),
362                       Quantity_TOC_RGB ),
363       Quantity_Color( anAxisData[2].NameColor.redF(),
364                       anAxisData[2].NameColor.greenF(),
365                       anAxisData[2].NameColor.blueF(),
366                       Quantity_TOC_RGB ),
367       Quantity_Color( anAxisData[0].Color.redF(),
368                       anAxisData[0].Color.greenF(),
369                       anAxisData[0].Color.blueF(),
370                       Quantity_TOC_RGB ),
371       Quantity_Color( anAxisData[1].Color.redF(),
372                       anAxisData[1].Color.greenF(),
373                       anAxisData[1].Color.blueF(),
374                       Quantity_TOC_RGB ),
375       Quantity_Color( anAxisData[2].Color.redF(),
376                       anAxisData[2].Color.greenF(),
377                       anAxisData[2].Color.blueF(),
378                       Quantity_TOC_RGB ) );
379 #endif // OCC_VERSION_LARGE > 0x06080000
380   }
381   else
382     theView->GraduatedTrihedronErase();
383
384   theView->Redraw();
385 }